Broke the header link wrapping out into smaller functions
This commit is contained in:
parent
ac16d7aef1
commit
fa95546988
|
@ -399,9 +399,41 @@ fn build_header_links(html: String, filename: &str) -> String {
|
||||||
|
|
||||||
regex
|
regex
|
||||||
.replace_all(&html, |caps: &Captures| {
|
.replace_all(&html, |caps: &Captures| {
|
||||||
let level = &caps[1];
|
let level = caps[1].parse().expect(
|
||||||
let text = &caps[2];
|
"Regex should ensure we only ever get numbers here",
|
||||||
let mut id = text.to_string();
|
);
|
||||||
|
|
||||||
|
wrap_header_with_link(level, &caps[2], &mut id_counter, filename)
|
||||||
|
})
|
||||||
|
.into_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wrap_header_with_link(level: usize, content: &str, id_counter: &mut HashMap<String, usize>, filename: &str)
|
||||||
|
-> String {
|
||||||
|
let id = id_from_content(content);
|
||||||
|
|
||||||
|
let id_count = *id_counter.get(&id).unwrap_or(&0);
|
||||||
|
id_counter.insert(id.clone(), id_count + 1);
|
||||||
|
|
||||||
|
let id = if id_count > 0 {
|
||||||
|
format!("{}-{}", id, id_count)
|
||||||
|
} else {
|
||||||
|
id
|
||||||
|
};
|
||||||
|
|
||||||
|
format!(
|
||||||
|
r#"<a class="header" href="{filename}#{id}" id="{id}"><h{level}>{text}</h{level}></a>"#,
|
||||||
|
level = level,
|
||||||
|
id = id,
|
||||||
|
text = content,
|
||||||
|
filename = filename
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn id_from_content(content: &str) -> String {
|
||||||
|
let mut content = content.to_string();
|
||||||
|
|
||||||
|
// Skip any tags or html-encoded stuff
|
||||||
let repl_sub = vec![
|
let repl_sub = vec![
|
||||||
"<em>",
|
"<em>",
|
||||||
"</em>",
|
"</em>",
|
||||||
|
@ -416,9 +448,10 @@ fn build_header_links(html: String, filename: &str) -> String {
|
||||||
""",
|
""",
|
||||||
];
|
];
|
||||||
for sub in repl_sub {
|
for sub in repl_sub {
|
||||||
id = id.replace(sub, "");
|
content = content.replace(sub, "");
|
||||||
}
|
}
|
||||||
let id = id.chars()
|
|
||||||
|
content.chars()
|
||||||
.filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' {
|
.filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' {
|
||||||
if c.is_ascii() {
|
if c.is_ascii() {
|
||||||
Some(c.to_ascii_lowercase())
|
Some(c.to_ascii_lowercase())
|
||||||
|
@ -430,26 +463,7 @@ fn build_header_links(html: String, filename: &str) -> String {
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
.collect::<String>();
|
.collect()
|
||||||
|
|
||||||
let id_count = *id_counter.get(&id).unwrap_or(&0);
|
|
||||||
id_counter.insert(id.clone(), id_count + 1);
|
|
||||||
|
|
||||||
let id = if id_count > 0 {
|
|
||||||
format!("{}-{}", id, id_count)
|
|
||||||
} else {
|
|
||||||
id
|
|
||||||
};
|
|
||||||
|
|
||||||
format!(
|
|
||||||
"<a class=\"header\" href=\"{filename}#{id}\" id=\"{id}\"><h{level}>{text}</h{level}></a>",
|
|
||||||
level = level,
|
|
||||||
id = id,
|
|
||||||
text = text,
|
|
||||||
filename = filename
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.into_owned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// anchors to the same page (href="#anchor") do not work because of
|
// anchors to the same page (href="#anchor") do not work because of
|
||||||
|
|
Loading…
Reference in New Issue