Skip to content

Commit 988ed9b

Browse files
authored
Merge pull request #2807 from ehuss/footnotes-in-a-row
Test and add a fix for multiple footnotes in a row
2 parents 54a5d74 + 4a47b3d commit 988ed9b

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

crates/mdbook-markdown/src/lib.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ pub fn render_markdown_with_path(
8080
let mut in_footnote_name = String::new();
8181
// This is the list of events to build the footnote definition.
8282
let mut in_footnote = Vec::new();
83+
// This is used to add space between consecutive footnotes. I was unable
84+
// to figure out a way to do this just with pure CSS.
85+
let mut prev_was_footnote = false;
8386

8487
let events = new_cmark_parser(text, smart_punctuation)
8588
.map(clean_codeblock_headers)
@@ -93,6 +96,7 @@ pub fn render_markdown_with_path(
9396
.filter_map(|event| {
9497
match event {
9598
Event::Start(Tag::FootnoteDefinition(name)) => {
99+
prev_was_footnote = false;
96100
if !in_footnote.is_empty() {
97101
log::warn!("internal bug: nested footnote not expected in {path:?}");
98102
}
@@ -119,14 +123,19 @@ pub fn render_markdown_with_path(
119123
let len = footnote_numbers.len() + 1;
120124
let (n, count) = footnote_numbers.entry(name.clone()).or_insert((len, 0));
121125
*count += 1;
122-
let html = Event::Html(
123-
format!(
124-
"<sup class=\"footnote-reference\" id=\"fr-{name}-{count}\">\
125-
<a href=\"#footnote-{name}\">{n}</a>\
126-
</sup>"
127-
)
128-
.into(),
129-
);
126+
let mut html = String::new();
127+
if prev_was_footnote {
128+
write!(html, " ").unwrap();
129+
}
130+
prev_was_footnote = true;
131+
write!(
132+
html,
133+
"<sup class=\"footnote-reference\" id=\"fr-{name}-{count}\">\
134+
<a href=\"#footnote-{name}\">{n}</a>\
135+
</sup>"
136+
)
137+
.unwrap();
138+
let html = Event::Html(html.into());
130139
if in_footnote_name.is_empty() {
131140
Some(html)
132141
} else {
@@ -138,9 +147,13 @@ pub fn render_markdown_with_path(
138147
// While inside a footnote, accumulate all events into a local.
139148
_ if !in_footnote_name.is_empty() => {
140149
in_footnote.push(event);
150+
prev_was_footnote = false;
141151
None
142152
}
143-
_ => Some(event),
153+
_ => {
154+
prev_was_footnote = false;
155+
Some(event)
156+
}
144157
}
145158
});
146159

tests/testsuite/markdown/footnotes/expected/footnotes.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ <h1 id="footnote-tests"><a class="header" href="#footnote-tests">Footnote tests<
77
<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>
88
<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>
99
<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>
10+
<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>
1011
<hr>
1112
<ol class="footnote-definition"><li id="footnote-1">
1213
<p>This is a footnote. <a href="#fr-1-1"></a> <a href="#fr-1-2">↩2</a></p>
@@ -43,4 +44,13 @@ <h1 id="footnote-tests"><a class="header" href="#footnote-tests">Footnote tests<
4344
<li id="footnote-in-between">
4445
<p>Footnote between duplicates. <a href="#fr-in-between-1"></a></p>
4546
</li>
47+
<li id="footnote-a">
48+
<p>Footnote 1 <a href="#fr-a-1"></a></p>
49+
</li>
50+
<li id="footnote-b">
51+
<p>Footnote 2 <a href="#fr-b-1"></a></p>
52+
</li>
53+
<li id="footnote-c">
54+
<p>Footnote 3 <a href="#fr-c-1"></a></p>
55+
</li>
4656
</ol>

tests/testsuite/markdown/footnotes/src/footnotes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ And another[^in-between] that references the duplicate again.[^multiple-definiti
4545
[^in-between]: Footnote between duplicates.
4646

4747
[^multiple-definitions]: This is the second definition of the footnote with tag multiple-definitions
48+
49+
Multiple footnotes in a row.[^a][^b][^c]
50+
51+
[^a]: Footnote 1
52+
[^b]: Footnote 2
53+
[^c]: Footnote 3

0 commit comments

Comments
 (0)