Skip to content

Commit 4a47b3d

Browse files
committed
Add space between consecutive footnotes
This fixes it so that consecutive footnotes have a little space between them so they aren't jammed together.
1 parent 82a457b commit 4a47b3d

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +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>
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>
1111
<hr>
1212
<ol class="footnote-definition"><li id="footnote-1">
1313
<p>This is a footnote. <a href="#fr-1-1"></a> <a href="#fr-1-2">↩2</a></p>

0 commit comments

Comments
 (0)