Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning on broken internal links #1621

Open
smani opened this issue Sep 10, 2014 · 7 comments
Open

Warning on broken internal links #1621

smani opened this issue Sep 10, 2014 · 7 comments

Comments

@smani
Copy link

smani commented Sep 10, 2014

Do we have an option to warn if there are broken internal links in the document?

Example:

# FooBar Details

blah..blah

## Some other section

Refer [FooBar Details](#foobar-detls) section for more information.

The internal link #foobar-detls is misspelt and hence there won't be any hyperlink in the output (or it would be broken). It would be helpful if pandoc can print warnings in these cases.

@neongreen
Copy link

neongreen commented Sep 21, 2014

Currently Pandoc's document representation has no notion for anchors. Therefore, all links which point to -anchors which were inserted by hand- would be considered “broken”, even though they aren't. Making the warning optional isn't a very good solution, as people who use anchors often would either have to turn it off (and lose the privilege of being warned about broken links, which seems rather useful), or put up with as many warnings as they have anchor links.

Until there is a proper solution (anchors-in-document-model), it might be feasible to simply keep a list of anchors (HTML's <a name=...></a>, LaTeX's \hypertarget, don't know about the rest) found while parsing, and then check internal links against those anchors as well as header identifiers.

@mpickering
Copy link
Collaborator

By convention anchors are any identifier (currently id on code blocks, spans, divs and headers). I would like to investigate if this suggestion was possible.

@jgm
Copy link
Owner

jgm commented Mar 5, 2017

This should be possible, but it's somewhat complex.

We'd need to (a) keep a running list of identifiers used, and (b) have explicit links return "futures" that check this list in state and emit a warning if necessary.

@jgm jgm mentioned this issue Jun 14, 2019
9 tasks
@sebunger
Copy link

If it is significantly easier to implement, then implementing this as part of, say, the markdown writer (rather than the reader) would meet my needs at least. I run all documents through markdown to markdown step first anyway auto-fix all the indentations, tables, etc.

I think most other users could also easily add a MD to MD conversion step in order to get the error messages for broken links even if they throw the result of that conversion away.

@tarleb
Copy link
Collaborator

tarleb commented Apr 14, 2020

I recently used a Lua filter for this purpose. It worked quite well:

local identifiers = {}

function Block (b)
  if b.identifier then
    identifiers[b.identifier] = true
  end
end

function Inline (i)
  if i.identifier then
    identifiers[i.identifier] = true
  end
end

function Link (l)
  local anchor = l.target:match('#(.*)')
  if anchor and not identifiers[anchor] then
    io.stderr:write("broken link: " .. anchor .. "\n")
  end
end

return {
  {Block = Block, Inline = Inline},
  {Link = Link}
}

@jgm
Copy link
Owner

jgm commented Apr 14, 2020

It would not be that hard to build this into the markdown reader, but it may make more sense to use a filter anyway; after all, it's a check on the AST that makes sense no matter what the input format was.

@sebunger
Copy link

sebunger commented Apr 15, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants