Warn on duplicate footnote definition and ignore subsequent definitions

This commit is contained in:
Krishan Mistry 2025-04-29 11:41:53 +01:00 committed by Eric Huss
parent 09d22e926f
commit 7e9be8dee3
3 changed files with 34 additions and 11 deletions

View file

@ -235,10 +235,10 @@ pub fn render_markdown_with_path(
// `count` is the number of references to this footnote (used for multiple
// linkbacks, and checking for unused footnotes).
let mut footnote_numbers = HashMap::new();
// This is a list of (name, Vec<Event>)
// This is a map of name -> Vec<Event>
// `name` is the name of the footnote.
// The events list is the list of events needed to build the footnote definition.
let mut footnote_defs = Vec::new();
let mut footnote_defs = HashMap::new();
// The following are used when currently processing a footnote definition.
//
@ -268,7 +268,16 @@ pub fn render_markdown_with_path(
Event::End(TagEnd::FootnoteDefinition) => {
let def_events = std::mem::take(&mut in_footnote);
let name = std::mem::take(&mut in_footnote_name);
footnote_defs.push((name, def_events));
if footnote_defs.contains_key(&name) {
log::warn!(
"footnote `{name}` in {} defined multiple times - \
not updating to new definition",
path.map_or_else(|| Cow::from("<unknown>"), |p| p.to_string_lossy())
);
} else {
footnote_defs.insert(name, def_events);
}
None
}
Event::FootnoteReference(name) => {
@ -304,7 +313,12 @@ pub fn render_markdown_with_path(
html::push_html(&mut body, events);
if !footnote_defs.is_empty() {
add_footnote_defs(&mut body, path, footnote_defs, &footnote_numbers);
add_footnote_defs(
&mut body,
path,
footnote_defs.into_iter().collect(),
&footnote_numbers,
);
}
body

View file

@ -17,10 +17,22 @@ fn custom_header_attributes() {
// Test for a variety of footnote renderings.
#[test]
fn footnotes() {
BookTest::from_dir("markdown/footnotes").check_main_file(
"book/footnotes.html",
file!["markdown/footnotes/expected/footnotes.html"],
);
BookTest::from_dir("markdown/footnotes")
.run("build", |cmd| {
cmd.expect_stderr(str![[r#"
[TIMESTAMP] [INFO] (mdbook::book): Book building has started
[TIMESTAMP] [INFO] (mdbook::book): Running the html backend
[TIMESTAMP] [WARN] (mdbook::utils): footnote `multiple-definitions` in <unknown> defined multiple times - not updating to new definition
[TIMESTAMP] [WARN] (mdbook::utils): footnote `unused` in `<unknown>` is defined but not referenced
[TIMESTAMP] [WARN] (mdbook::utils): footnote `multiple-definitions` in footnotes.md defined multiple times - not updating to new definition
[TIMESTAMP] [WARN] (mdbook::utils): footnote `unused` in `footnotes.md` is defined but not referenced
"#]]);
})
.check_main_file(
"book/footnotes.html",
file!["markdown/footnotes/expected/footnotes.html"],
);
}
// Basic table test.

View file

@ -40,9 +40,6 @@ With a reference inside.<sup class="footnote-reference" id="fr-1-2"><a href="#fo
<li id="footnote-multiple-definitions">
<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>
</li>
<li id="footnote-multiple-definitions">
<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>
</li>
<li id="footnote-in-between">
<p>Footnote between duplicates. <a href="#fr-in-between-1"></a></p>
</li>