lychee icon indicating copy to clipboard operation
lychee copied to clipboard

Check for broken full reference links in Markdown

Open norswap opened this issue 3 years ago • 7 comments

Currently lychee does not check for bad "link references" (not sure if this is the proper name), e.g.

This is a [link text][link-ref].

[link-ref-with-typo]: https://nyan.cat

It would be great if that was caught!

norswap avatar Jan 11 '22 16:01 norswap

First, this is not the correct syntax for link reference. See https://github.github.com/gfm/#link-reference-definitions

It should be

[foo]: /url "title"

[foo]

Second, we relies on pulldown-cmark to parse hyperlinks in markdown documents. Thankfully, it's able to find broken links via https://docs.rs/pulldown-cmark/latest/pulldown_cmark/struct.Parser.html#method.new_with_broken_link_callback

But if you want to identify possible 'typo's, the job would be harder as we may need to calculate some text distances.

lebensterben avatar Jan 11 '22 16:01 lebensterben

Oh, I did not know about new_with_broken_link_callback! Thanks for mentioning it. We could start by printing a warning for broken links? That would already be a step into the right direction.

mre avatar Jan 11 '22 16:01 mre

@lebensterben These types of links have been in Markdown since the very start (https://daringfireball.net/projects/markdown/syntax#link) and have always been supported by Github. They are in fact specified in the document you linked (https://github.github.com/gfm/#example-535). What you linked is the reference for the link definition (the [ref]: link part).

@mre A warning meaning it does not cause the program to return a non-zero code?

norswap avatar Jan 11 '22 16:01 norswap

@norswap Thanks for pointing that out. It's a valid full reference link. https://github.github.com/gfm/#full-reference-link

I suggest to change the title of this issue accordingly.

lebensterben avatar Jan 11 '22 16:01 lebensterben

@mre A warning meaning it does not cause the program to return a non-zero code?

Good point. We can actually treat it as an error. After all, the link is broken.

mre avatar Jan 12 '22 08:01 mre