Skip to content

Commit cc7f8be

Browse files
authored
Merge pull request #2922 from ehuss/header-id-lowercase
Lowercase heading IDs
2 parents 475951c + 051fc9f commit cc7f8be

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ The following is a summary of the changes that may require your attention when u
5858
[#2847](https://github.com/rust-lang/mdBook/pull/2847)
5959
- Added support for admonitions. These are enabled by default, with the option `output.html.admonitions` to disable it.
6060
[#2851](https://github.com/rust-lang/mdBook/pull/2851)
61-
- Headers that start or end with HTML characters like `<`, `&`, or `>` now replace those characters in the link ID with `-` instead of being stripped. This brings the header ID generation closer to other tools and sites.
62-
[#2844](https://github.com/rust-lang/mdBook/pull/2844)
61+
- Header ID generation has some minor changes to bring the ID generation closer to other tools and sites:
62+
- IDs now use Unicode lowercase instead of ASCII lowercase.
63+
[#2922](https://github.com/rust-lang/mdBook/pull/2922)
64+
- Headers that start or end with HTML characters like `<`, `&`, or `>` now replace those characters in the link ID with `-` instead of being stripped.
65+
[#2844](https://github.com/rust-lang/mdBook/pull/2844)
6366

6467
### CLI changes
6568

crates/mdbook-html/src/utils.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,22 @@ pub(crate) fn unique_id(id: &str, used: &mut HashSet<String>) -> String {
7474

7575
/// Generates an HTML id from the given text.
7676
pub(crate) fn id_from_content(content: &str) -> String {
77+
// This is intended to be close to how header ID generation is done in
78+
// other sites and tools, but is not 100% the same. Not all sites and
79+
// tools use the same algorithm. See these for more information:
80+
//
81+
// - https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#section-links
82+
// - https://docs.gitlab.com/user/markdown/#heading-ids-and-links
83+
// - https://pandoc.org/MANUAL.html#extension-auto_identifiers
84+
// - https://kramdown.gettalong.org/converter/html#auto-ids
85+
// - https://docs.rs/comrak/latest/comrak/options/struct.Extension.html#structfield.header_ids
7786
content
7887
.trim()
88+
.to_lowercase()
7989
.chars()
8090
.filter_map(|ch| {
8191
if ch.is_alphanumeric() || ch == '_' || ch == '-' {
82-
Some(ch.to_ascii_lowercase())
92+
Some(ch)
8393
} else if ch.is_whitespace() {
8494
Some('-')
8595
} else {
@@ -120,6 +130,6 @@ mod tests {
120130
assert_eq!(id_from_content("한국어"), "한국어");
121131
assert_eq!(id_from_content(""), "");
122132
assert_eq!(id_from_content("中文標題 CJK title"), "中文標題-cjk-title");
123-
assert_eq!(id_from_content("Über"), "Über");
133+
assert_eq!(id_from_content("Über"), "über");
124134
}
125135
}

0 commit comments

Comments
 (0)