Skip to content

Commit 7e9be8d

Browse files
krishanjmistryehuss
authored andcommitted
Warn on duplicate footnote definition and ignore subsequent definitions
1 parent 09d22e9 commit 7e9be8d

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/utils/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ pub fn render_markdown_with_path(
235235
// `count` is the number of references to this footnote (used for multiple
236236
// linkbacks, and checking for unused footnotes).
237237
let mut footnote_numbers = HashMap::new();
238-
// This is a list of (name, Vec<Event>)
238+
// This is a map of name -> Vec<Event>
239239
// `name` is the name of the footnote.
240240
// The events list is the list of events needed to build the footnote definition.
241-
let mut footnote_defs = Vec::new();
241+
let mut footnote_defs = HashMap::new();
242242

243243
// The following are used when currently processing a footnote definition.
244244
//
@@ -268,7 +268,16 @@ pub fn render_markdown_with_path(
268268
Event::End(TagEnd::FootnoteDefinition) => {
269269
let def_events = std::mem::take(&mut in_footnote);
270270
let name = std::mem::take(&mut in_footnote_name);
271-
footnote_defs.push((name, def_events));
271+
272+
if footnote_defs.contains_key(&name) {
273+
log::warn!(
274+
"footnote `{name}` in {} defined multiple times - \
275+
not updating to new definition",
276+
path.map_or_else(|| Cow::from("<unknown>"), |p| p.to_string_lossy())
277+
);
278+
} else {
279+
footnote_defs.insert(name, def_events);
280+
}
272281
None
273282
}
274283
Event::FootnoteReference(name) => {
@@ -304,7 +313,12 @@ pub fn render_markdown_with_path(
304313
html::push_html(&mut body, events);
305314

306315
if !footnote_defs.is_empty() {
307-
add_footnote_defs(&mut body, path, footnote_defs, &footnote_numbers);
316+
add_footnote_defs(
317+
&mut body,
318+
path,
319+
footnote_defs.into_iter().collect(),
320+
&footnote_numbers,
321+
);
308322
}
309323

310324
body

tests/testsuite/markdown.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,22 @@ fn custom_header_attributes() {
1717
// Test for a variety of footnote renderings.
1818
#[test]
1919
fn footnotes() {
20-
BookTest::from_dir("markdown/footnotes").check_main_file(
21-
"book/footnotes.html",
22-
file!["markdown/footnotes/expected/footnotes.html"],
23-
);
20+
BookTest::from_dir("markdown/footnotes")
21+
.run("build", |cmd| {
22+
cmd.expect_stderr(str![[r#"
23+
[TIMESTAMP] [INFO] (mdbook::book): Book building has started
24+
[TIMESTAMP] [INFO] (mdbook::book): Running the html backend
25+
[TIMESTAMP] [WARN] (mdbook::utils): footnote `multiple-definitions` in <unknown> defined multiple times - not updating to new definition
26+
[TIMESTAMP] [WARN] (mdbook::utils): footnote `unused` in `<unknown>` is defined but not referenced
27+
[TIMESTAMP] [WARN] (mdbook::utils): footnote `multiple-definitions` in footnotes.md defined multiple times - not updating to new definition
28+
[TIMESTAMP] [WARN] (mdbook::utils): footnote `unused` in `footnotes.md` is defined but not referenced
29+
30+
"#]]);
31+
})
32+
.check_main_file(
33+
"book/footnotes.html",
34+
file!["markdown/footnotes/expected/footnotes.html"],
35+
);
2436
}
2537

2638
// Basic table test.

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ <h1 id="footnote-tests"><a class="header" href="#footnote-tests">Footnote tests<
4040
<li id="footnote-multiple-definitions">
4141
<p>This is the first definition of the footnote with tag multiple-definitions <a href="#fr-multiple-definitions-1"></a> <a href="#fr-multiple-definitions-2">↩2</a></p>
4242
</li>
43-
<li id="footnote-multiple-definitions">
44-
<p>This is the second definition of the footnote with tag multiple-definitions <a href="#fr-multiple-definitions-1"></a> <a href="#fr-multiple-definitions-2">↩2</a></p>
45-
</li>
4643
<li id="footnote-in-between">
4744
<p>Footnote between duplicates. <a href="#fr-in-between-1"></a></p>
4845
</li>

0 commit comments

Comments
 (0)