Skip to content

Warn on and ignore duplicate footnote definitions #2681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down
56 changes: 16 additions & 40 deletions tests/testsuite/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Tests for special markdown rendering.

use crate::prelude::*;
use snapbox::file;

// Checks custom header id and classes.
#[test]
Expand All @@ -17,46 +18,21 @@ fn custom_header_attributes() {
#[test]
fn footnotes() {
BookTest::from_dir("markdown/footnotes")
.check_main_file("book/footnotes.html", str![[r##"
<h1 id="footnote-tests"><a class="header" href="#footnote-tests">Footnote tests</a></h1>
<p>Footnote example<sup class="footnote-reference" id="fr-1-1"><a href="#footnote-1">1</a></sup>, or with a word<sup class="footnote-reference" id="fr-word-1"><a href="#footnote-word">2</a></sup>.</p>
<p>There are multiple references to word<sup class="footnote-reference" id="fr-word-2"><a href="#footnote-word">2</a></sup>.</p>
<p>Footnote without a paragraph<sup class="footnote-reference" id="fr-para-1"><a href="#footnote-para">3</a></sup></p>
<p>Footnote with multiple paragraphs<sup class="footnote-reference" id="fr-multiple-1"><a href="#footnote-multiple">4</a></sup></p>
<p>Footnote name with wacky characters<sup class="footnote-reference" id="fr-&quot;wacky&quot;-1"><a href="#footnote-&quot;wacky&quot;">5</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>
<hr>
<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>
</li>
<li id="footnote-word">
<p>A longer footnote.
With multiple lines. <a href="other.html">Link to other</a>.
With a reference inside.<sup class="footnote-reference" id="fr-1-2"><a href="#footnote-1">1</a></sup> <a href="#fr-word-1">↩</a> <a href="#fr-word-2">↩2</a></p>
</li>
<li id="footnote-para">
<ol>
<li>Item one
<ol>
<li>Sub-item</li>
</ol>
</li>
<li>Item two</li>
</ol>
<a href="#fr-para-1">↩</a></li>
<li id="footnote-multiple">
<p>One</p>
<p>Two</p>
<p>Three <a href="#fr-multiple-1">↩</a></p>
</li>
<li id="footnote-&quot;wacky&quot;">
<p>Testing footnote id with special characters. <a href="#fr-&quot;wacky&quot;-1">↩</a></p>
</li>
<li id="footnote-define-before-use">
<p>This is defined before it is referred to. <a href="#fr-define-before-use-1">↩</a></p>
</li>
</ol>
"##]]);
.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.
Expand Down
46 changes: 46 additions & 0 deletions tests/testsuite/markdown/footnotes/expected/footnotes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<h1 id="footnote-tests"><a class="header" href="#footnote-tests">Footnote tests</a></h1>
<p>Footnote example<sup class="footnote-reference" id="fr-1-1"><a href="#footnote-1">1</a></sup>, or with a word<sup class="footnote-reference" id="fr-word-1"><a href="#footnote-word">2</a></sup>.</p>
<p>There are multiple references to word<sup class="footnote-reference" id="fr-word-2"><a href="#footnote-word">2</a></sup>.</p>
<p>Footnote without a paragraph<sup class="footnote-reference" id="fr-para-1"><a href="#footnote-para">3</a></sup></p>
<p>Footnote with multiple paragraphs<sup class="footnote-reference" id="fr-multiple-1"><a href="#footnote-multiple">4</a></sup></p>
<p>Footnote name with wacky characters<sup class="footnote-reference" id="fr-&quot;wacky&quot;-1"><a href="#footnote-&quot;wacky&quot;">5</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>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>
<hr>
<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>
</li>
<li id="footnote-word">
<p>A longer footnote.
With multiple lines. <a href="other.html">Link to other</a>.
With a reference inside.<sup class="footnote-reference" id="fr-1-2"><a href="#footnote-1">1</a></sup> <a href="#fr-word-1">↩</a> <a href="#fr-word-2">↩2</a></p>
</li>
<li id="footnote-para">
<ol>
<li>Item one
<ol>
<li>Sub-item</li>
</ol>
</li>
<li>Item two</li>
</ol>
<a href="#fr-para-1">↩</a></li>
<li id="footnote-multiple">
<p>One</p>
<p>Two</p>
<p>Three <a href="#fr-multiple-1">↩</a></p>
</li>
<li id="footnote-&quot;wacky&quot;">
<p>Testing footnote id with special characters. <a href="#fr-&quot;wacky&quot;-1">↩</a></p>
</li>
<li id="footnote-define-before-use">
<p>This is defined before it is referred to. <a href="#fr-define-before-use-1">↩</a></p>
</li>
<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-in-between">
<p>Footnote between duplicates. <a href="#fr-in-between-1">↩</a></p>
</li>
</ol>
10 changes: 10 additions & 0 deletions tests/testsuite/markdown/footnotes/src/footnotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ Footnote name with wacky characters[^"wacky"]
[^"wacky"]: Testing footnote id with special characters.

Testing when referring to something earlier.[^define-before-use]

Footnote that is defined multiple times.[^multiple-definitions]

[^multiple-definitions]: This is the first definition of the footnote with tag multiple-definitions

And another[^in-between] that references the duplicate again.[^multiple-definitions]

[^in-between]: Footnote between duplicates.

[^multiple-definitions]: This is the second definition of the footnote with tag multiple-definitions