rust-lang / mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
https://rust-lang.github.io/mdBook/
Mozilla Public License 2.0
17.71k stars 1.61k forks source link

Headers containing links result in invalid HTML; nested `a` tags #221

Open ollie27 opened 7 years ago

ollie27 commented 7 years ago

If a header contains a link like the following it will result in nested a tags which is invalid.

## Header with [link](https://example.com).

This results in:

<a class="header" href="foo.html#header-with-a-hrefhttpsexamplecomlinka" name="header-with-a-hrefhttpsexamplecomlinka"><h2>Header with <a href="https://example.com/">link</a>.</h2></a>

A possible solution is to put the anchor at the start or end of the header rather than around the whole thing.

azerupi commented 7 years ago

I might be wrong but is this not a bug with Pulldown-cmark?

ollie27 commented 7 years ago

No. pulldown-cmark creates the inner link but it's mdBook which naively surrounds the entire header in another <a> tag: https://github.com/azerupi/mdBook/blob/74e75d2cfbba79bfde31fbb43cc86df0671e4606/src/renderer/html_handlebars/hbs_renderer.rs#L273

azerupi commented 7 years ago

Ah right! Sorry, my bad. 😄

So the thing is that we would like the header to be a link to itself so that user can click on the anchor and share the link directly. This can't work if the header is already a link obviously but I have no idea how we should handle that. Maybe add a special case for headers containing a link? I'm not sure.

ollie27 commented 7 years ago

You can see what GitHub does for example: https://github.com/azerupi/mdBook/blob/master/README.md