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

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

Open
max-heller opened this issue Jul 10, 2024 · 0 comments
Labels
C-enhancement Category: Enhancement or feature request

Comments

@max-heller
Copy link
Contributor

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:

mdBook/src/book/summary.rs

Lines 595 to 606 in ec996d3

/// Removes the styling from a list of Markdown events and returns just the
/// plain text.
fn stringify_events(events: Vec<Event<'_>>) -> String {
events
.into_iter()
.filter_map(|t| match t {
Event::Text(text) | Event::Code(text) => Some(text.into_string()),
Event::SoftBreak => Some(String::from(" ")),
_ => None,
})
.collect()
}

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.

#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.

mdBook/src/book/summary.rs

Lines 164 to 165 in ec996d3

src: &'a str,
stream: pulldown_cmark::OffsetIter<'a, DefaultBrokenLinkCallback>,

Notes

This would have to be a breaking change

@max-heller max-heller added the C-enhancement Category: Enhancement or feature request label Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement or feature request
Projects
None yet
Development

No branches or pull requests

1 participant