mdBook icon indicating copy to clipboard operation
mdBook copied to clipboard

Keep summary links untouched (including Markdown formatting) instead of extracting just plain text

Open max-heller opened this issue 1 year ago • 0 comments

Problem

When a link/part title/etc. in SUMMARY.md contains Markdown fancier than plaintext, mdBook strips out all of the formatting and uses just the plaintext as the name of the item:

https://github.com/rust-lang/mdBook/blob/ec996d350955a8974f8cae5a55c1374ae8da4fac/src/book/summary.rs#L595-L606

I'm running into an issue where:

  • A chapter is named "`Box<T>`"
  • The name of the chapter, as reported by mdBook ("Box<T>" without the backticks), is inserted into another chapter
  • The <T> is interpreted as raw HTML since it is no longer enclosed in inline code
  • The chapter title is rendered as "Box" (with no <T>, since that is interpreted as an empty and meaningless HTML tag)
  • When converting the book to EPUB, which is more strict about closing tags, the <T> being interpreted as HTML instead of inline code breaks rendering altogether

Proposed Solution

I propose that mdBook preserve the original Markdown for links/part titles/etc. in SUMMARY.md.

https://github.com/rust-lang/mdBook/pull/1785, which relates to a similar issue, noted this, stating that:

The only safe thing to do would be to grab the raw text from the original markdown, which is awkward to do with pulldown_cmark.

Is this still an issue? It should be possible to use the ranges provided by pulldown_cmark through its offset iterator (which mdBook already uses to parse the summary) to grab the Markdown source from the raw &str of the summary by keeping track of the start of the range when the name begins and the end of the range when the name ends.

https://github.com/rust-lang/mdBook/blob/ec996d350955a8974f8cae5a55c1374ae8da4fac/src/book/summary.rs#L164-L165

Notes

This would have to be a breaking change

max-heller avatar Jul 10 '24 18:07 max-heller