Merge pull request #2807 from ehuss/footnotes-in-a-row

Test and add a fix for multiple footnotes in a row
This commit is contained in:
Eric Huss 2025-08-19 00:18:40 +00:00 committed by GitHub
commit 988ed9b5bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 9 deletions

View file

@ -80,6 +80,9 @@ pub fn render_markdown_with_path(
let mut in_footnote_name = String::new(); let mut in_footnote_name = String::new();
// This is the list of events to build the footnote definition. // This is the list of events to build the footnote definition.
let mut in_footnote = Vec::new(); 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) let events = new_cmark_parser(text, smart_punctuation)
.map(clean_codeblock_headers) .map(clean_codeblock_headers)
@ -93,6 +96,7 @@ pub fn render_markdown_with_path(
.filter_map(|event| { .filter_map(|event| {
match event { match event {
Event::Start(Tag::FootnoteDefinition(name)) => { Event::Start(Tag::FootnoteDefinition(name)) => {
prev_was_footnote = false;
if !in_footnote.is_empty() { if !in_footnote.is_empty() {
log::warn!("internal bug: nested footnote not expected in {path:?}"); 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 len = footnote_numbers.len() + 1;
let (n, count) = footnote_numbers.entry(name.clone()).or_insert((len, 0)); let (n, count) = footnote_numbers.entry(name.clone()).or_insert((len, 0));
*count += 1; *count += 1;
let html = Event::Html( let mut html = String::new();
format!( if prev_was_footnote {
"<sup class=\"footnote-reference\" id=\"fr-{name}-{count}\">\ write!(html, " ").unwrap();
<a href=\"#footnote-{name}\">{n}</a>\ }
</sup>" prev_was_footnote = true;
) write!(
.into(), html,
); "<sup class=\"footnote-reference\" id=\"fr-{name}-{count}\">\
<a href=\"#footnote-{name}\">{n}</a>\
</sup>"
)
.unwrap();
let html = Event::Html(html.into());
if in_footnote_name.is_empty() { if in_footnote_name.is_empty() {
Some(html) Some(html)
} else { } else {
@ -138,9 +147,13 @@ pub fn render_markdown_with_path(
// While inside a footnote, accumulate all events into a local. // While inside a footnote, accumulate all events into a local.
_ if !in_footnote_name.is_empty() => { _ if !in_footnote_name.is_empty() => {
in_footnote.push(event); in_footnote.push(event);
prev_was_footnote = false;
None None
} }
_ => Some(event), _ => {
prev_was_footnote = false;
Some(event)
}
} }
}); });

View file

@ -7,6 +7,7 @@
<p>Testing when referring to something earlier.<sup class="footnote-reference" id="fr-define-before-use-1"><a href="#footnote-define-before-use">6</a></sup></p> <p>Testing when referring to something earlier.<sup class="footnote-reference" id="fr-define-before-use-1"><a href="#footnote-define-before-use">6</a></sup></p>
<p>Footnote that is defined multiple times.<sup class="footnote-reference" id="fr-multiple-definitions-1"><a href="#footnote-multiple-definitions">7</a></sup></p> <p>Footnote that is defined multiple times.<sup class="footnote-reference" id="fr-multiple-definitions-1"><a href="#footnote-multiple-definitions">7</a></sup></p>
<p>And another<sup class="footnote-reference" id="fr-in-between-1"><a href="#footnote-in-between">8</a></sup> that references the duplicate again.<sup class="footnote-reference" id="fr-multiple-definitions-2"><a href="#footnote-multiple-definitions">7</a></sup></p> <p>And another<sup class="footnote-reference" id="fr-in-between-1"><a href="#footnote-in-between">8</a></sup> that references the duplicate again.<sup class="footnote-reference" id="fr-multiple-definitions-2"><a href="#footnote-multiple-definitions">7</a></sup></p>
<p>Multiple footnotes in a row.<sup class="footnote-reference" id="fr-a-1"><a href="#footnote-a">9</a></sup> <sup class="footnote-reference" id="fr-b-1"><a href="#footnote-b">10</a></sup> <sup class="footnote-reference" id="fr-c-1"><a href="#footnote-c">11</a></sup></p>
<hr> <hr>
<ol class="footnote-definition"><li id="footnote-1"> <ol class="footnote-definition"><li id="footnote-1">
<p>This is a footnote. <a href="#fr-1-1"></a> <a href="#fr-1-2">↩2</a></p> <p>This is a footnote. <a href="#fr-1-1"></a> <a href="#fr-1-2">↩2</a></p>
@ -43,4 +44,13 @@ With a reference inside.<sup class="footnote-reference" id="fr-1-2"><a href="#fo
<li id="footnote-in-between"> <li id="footnote-in-between">
<p>Footnote between duplicates. <a href="#fr-in-between-1"></a></p> <p>Footnote between duplicates. <a href="#fr-in-between-1"></a></p>
</li> </li>
<li id="footnote-a">
<p>Footnote 1 <a href="#fr-a-1"></a></p>
</li>
<li id="footnote-b">
<p>Footnote 2 <a href="#fr-b-1"></a></p>
</li>
<li id="footnote-c">
<p>Footnote 3 <a href="#fr-c-1"></a></p>
</li>
</ol> </ol>

View file

@ -45,3 +45,9 @@ And another[^in-between] that references the duplicate again.[^multiple-definiti
[^in-between]: Footnote between duplicates. [^in-between]: Footnote between duplicates.
[^multiple-definitions]: This is the second definition of the footnote with tag multiple-definitions [^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