Skip to content

Avoid using the same file twice in SUMMARY.md #2613

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 1 commit into from
Mar 31, 2025
Merged
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
108 changes: 108 additions & 0 deletions src/book/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use log::{debug, trace, warn};
use memchr::Memchr;
use pulldown_cmark::{DefaultBrokenLinkCallback, Event, HeadingLevel, Tag, TagEnd};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fmt::{self, Display, Formatter};
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -245,6 +246,11 @@ impl<'a> SummaryParser<'a> {
.parse_affix(false)
.with_context(|| "There was an error parsing the suffix chapters")?;

let mut files = HashSet::new();
for part in [&prefix_chapters, &numbered_chapters, &suffix_chapters] {
self.check_for_duplicates(&part, &mut files)?;
}

Ok(Summary {
title,
prefix_chapters,
Expand All @@ -253,6 +259,29 @@ impl<'a> SummaryParser<'a> {
})
}

/// Recursively check for duplicate files in the summary items.
fn check_for_duplicates<'b>(
&self,
items: &'b [SummaryItem],
files: &mut HashSet<&'b PathBuf>,
) -> Result<()> {
for item in items {
if let SummaryItem::Link(link) = item {
if let Some(location) = &link.location {
if !files.insert(location) {
bail!(anyhow::anyhow!(
"Duplicate file in SUMMARY.md: {:?}",
location
));
}
}
// Recursively check nested items
self.check_for_duplicates(&link.nested_items, files)?;
}
}
Ok(())
}

/// Parse the affix chapters.
fn parse_affix(&mut self, is_prefix: bool) -> Result<Vec<SummaryItem>> {
let mut items = Vec::new();
Expand Down Expand Up @@ -1127,4 +1156,83 @@ mod tests {
let got = parser.parse_affix(false).unwrap();
assert_eq!(got, should_be);
}

#[test]
fn duplicate_entries_1() {
let src = r#"
# Summary
- [A](./a.md)
- [A](./a.md)
"#;

let res = parse_summary(src);
assert!(res.is_err());
let error_message = res.err().unwrap().to_string();
assert_eq!(error_message, r#"Duplicate file in SUMMARY.md: "./a.md""#);
}

#[test]
fn duplicate_entries_2() {
let src = r#"
# Summary
- [A](./a.md)
- [A](./a.md)
"#;

let res = parse_summary(src);
assert!(res.is_err());
let error_message = res.err().unwrap().to_string();
assert_eq!(error_message, r#"Duplicate file in SUMMARY.md: "./a.md""#);
}
#[test]
fn duplicate_entries_3() {
let src = r#"
# Summary
- [A](./a.md)
- [B](./b.md)
- [A](./a.md)
"#;

let res = parse_summary(src);
assert!(res.is_err());
let error_message = res.err().unwrap().to_string();
assert_eq!(error_message, r#"Duplicate file in SUMMARY.md: "./a.md""#);
}

#[test]
fn duplicate_entries_4() {
let src = r#"
# Summary
[A](./a.md)
- [B](./b.md)
- [A](./a.md)
"#;

let res = parse_summary(src);
assert!(res.is_err());
let error_message = res.err().unwrap().to_string();
assert_eq!(error_message, r#"Duplicate file in SUMMARY.md: "./a.md""#);
}

#[test]
fn duplicate_entries_5() {
let src = r#"
# Summary
[A](./a.md)

# hi
- [B](./b.md)

# bye

---

[A](./a.md)
"#;

let res = parse_summary(src);
assert!(res.is_err());
let error_message = res.err().unwrap().to_string();
assert_eq!(error_message, r#"Duplicate file in SUMMARY.md: "./a.md""#);
}
}