1
Fork 0

Bugfix: Use slug (normalied header) to decide whether to use a different link anchor

We simply used the wrong thing to check it!

Fixes #15
This commit is contained in:
Jan-Erik Rediger 2021-04-21 10:26:43 +02:00
parent 8165026716
commit cb9413d50f

View file

@ -184,7 +184,7 @@ fn add_toc(content: &str, cfg: &Config) -> Result<String> {
if let Some(level) = current_header_level.take() { if let Some(level) = current_header_level.take() {
let header = current_header.clone(); let header = current_header.clone();
let mut slug = mdbook::utils::normalize_id(&header); let mut slug = mdbook::utils::normalize_id(&header);
let id_count = id_counter.entry(header.clone()).or_insert(0); let id_count = id_counter.entry(slug.clone()).or_insert(0);
// Append unique ID if multiple headers with the same name exist // Append unique ID if multiple headers with the same name exist
// to follow what mdBook does // to follow what mdBook does
@ -757,4 +757,32 @@ First paragraph
assert_eq!(expected, add_toc(content, &with_marker(marker)).unwrap()); assert_eq!(expected, add_toc(content, &with_marker(marker)).unwrap());
} }
#[test]
fn similar_heading_different_casing() {
// Regression test #15
// Previously we didn't use the normalized header ("slug") to decide whether to use
// different link anchors.
//
let content = r#"# Chapter
<!-- toc -->
# Tag
## tag
"#;
let expected = r#"# Chapter
* [Tag](#tag)
* [tag](#tag-1)
# Tag
## tag"#;
assert_eq!(expected, add_toc(content, &default()).unwrap());
}
} }