Skip to content

Commit f2fa543

Browse files
committed
fix(ids): start with 2?!
1 parent 46abf2b commit f2fa543

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Diff for: crates/rari-doc/src/html/rewriter.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::collections::HashSet;
23

34
use lol_html::html_content::ContentType;
45
use lol_html::{element, text, HtmlRewriter, Settings};
@@ -18,8 +19,33 @@ pub fn post_process_html<T: PageLike>(
1819
sidebar: bool,
1920
) -> Result<String, DocError> {
2021
let mut output = vec![];
22+
let mut ids = HashSet::new();
2123

2224
let mut element_content_handlers = vec![
25+
element!("*[id]", |el| {
26+
if let Some(id) = el.get_attribute("id") {
27+
if !ids.contains(id.as_str()) {
28+
let (prefix, mut count) = if let Some((prefix, counter)) = id.rsplit_once('_') {
29+
if counter.chars().all(|c| c.is_ascii_digit()) {
30+
let count = counter.parse::<i64>().unwrap_or_default() + 1;
31+
(prefix, count)
32+
} else {
33+
(id.as_str(), 2)
34+
}
35+
} else {
36+
(id.as_str(), 2)
37+
};
38+
let mut id = format!("{prefix}_{count}");
39+
while !ids.insert(id) && count < 666 {
40+
count += 1;
41+
id = format!("{prefix}_{count}");
42+
}
43+
} else {
44+
ids.insert(id);
45+
}
46+
}
47+
Ok(())
48+
}),
2349
element!("img:not([loading])", |el| {
2450
el.set_attribute("loading", "lazy")?;
2551
Ok(())

Diff for: crates/rari-md/src/html.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Anchorizer {
123123
let anchor = if uniq == 0 {
124124
Cow::from(&id)
125125
} else {
126-
Cow::from(format!("{}_{}", id, uniq))
126+
Cow::from(format!("{}_{}", id, uniq + 1))
127127
};
128128

129129
if !self.0.contains(&*anchor) {

0 commit comments

Comments
 (0)