diff --git a/crates/mdbook-markdown/src/lib.rs b/crates/mdbook-markdown/src/lib.rs index a014ca6b3c..b45d06b0a4 100644 --- a/crates/mdbook-markdown/src/lib.rs +++ b/crates/mdbook-markdown/src/lib.rs @@ -80,6 +80,9 @@ pub fn render_markdown_with_path( let mut in_footnote_name = String::new(); // This is the list of events to build the footnote definition. let mut in_footnote = Vec::new(); + // This is used to add space between consecutive footnotes. I was unable + // to figure out a way to do this just with pure CSS. + let mut prev_was_footnote = false; let events = new_cmark_parser(text, smart_punctuation) .map(clean_codeblock_headers) @@ -93,6 +96,7 @@ pub fn render_markdown_with_path( .filter_map(|event| { match event { Event::Start(Tag::FootnoteDefinition(name)) => { + prev_was_footnote = false; if !in_footnote.is_empty() { log::warn!("internal bug: nested footnote not expected in {path:?}"); } @@ -119,14 +123,19 @@ pub fn render_markdown_with_path( let len = footnote_numbers.len() + 1; let (n, count) = footnote_numbers.entry(name.clone()).or_insert((len, 0)); *count += 1; - let html = Event::Html( - format!( - "\ - {n}\ - " - ) - .into(), - ); + let mut html = String::new(); + if prev_was_footnote { + write!(html, " ").unwrap(); + } + prev_was_footnote = true; + write!( + html, + "\ + {n}\ + " + ) + .unwrap(); + let html = Event::Html(html.into()); if in_footnote_name.is_empty() { Some(html) } else { @@ -138,9 +147,13 @@ pub fn render_markdown_with_path( // While inside a footnote, accumulate all events into a local. _ if !in_footnote_name.is_empty() => { in_footnote.push(event); + prev_was_footnote = false; None } - _ => Some(event), + _ => { + prev_was_footnote = false; + Some(event) + } } }); diff --git a/tests/testsuite/markdown/footnotes/expected/footnotes.html b/tests/testsuite/markdown/footnotes/expected/footnotes.html index 8904cb06a2..69faf6e00d 100644 --- a/tests/testsuite/markdown/footnotes/expected/footnotes.html +++ b/tests/testsuite/markdown/footnotes/expected/footnotes.html @@ -7,6 +7,7 @@

Footnote tests<

Testing when referring to something earlier.6

Footnote that is defined multiple times.7

And another8 that references the duplicate again.7

+

Multiple footnotes in a row.9 10 11


  1. This is a footnote. ↩2

    @@ -43,4 +44,13 @@

    Footnote tests<
  2. Footnote between duplicates.

  3. +
  4. +

    Footnote 1

    +
  5. +
  6. +

    Footnote 2

    +
  7. +
  8. +

    Footnote 3

    +
\ No newline at end of file diff --git a/tests/testsuite/markdown/footnotes/src/footnotes.md b/tests/testsuite/markdown/footnotes/src/footnotes.md index d7719a74ea..bdc30f5553 100644 --- a/tests/testsuite/markdown/footnotes/src/footnotes.md +++ b/tests/testsuite/markdown/footnotes/src/footnotes.md @@ -45,3 +45,9 @@ And another[^in-between] that references the duplicate again.[^multiple-definiti [^in-between]: Footnote between duplicates. [^multiple-definitions]: This is the second definition of the footnote with tag multiple-definitions + +Multiple footnotes in a row.[^a][^b][^c] + +[^a]: Footnote 1 +[^b]: Footnote 2 +[^c]: Footnote 3