fix header ids not in sync with the link

This commit is contained in:
Mylloon 2024-12-13 18:48:31 +01:00
parent 41311c05ed
commit f72a341787
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -59,8 +59,10 @@ pub fn get_options(path: Option<FilePath>, metadata_type: MType) -> ComrakOption
.autolink(true)
.tasklist(true)
.superscript(true)
.header_ids(match path.clone() {
Some(fp) => format!("{}-", fp.path.get(..fp.path.len() - 3).unwrap_or_default()),
.header_ids(match path {
Some(ref fp) => {
format!("{}-", fp.path.get(..fp.path.len() - 3).unwrap_or_default())
}
None => String::new(),
})
.footnotes(true)
@ -190,6 +192,21 @@ fn custom_img_size(html: &str) -> String {
.unwrap()
}
fn fix_headers_ids(html: &str) -> String {
rewrite_str(
html,
RewriteStrSettings {
element_content_handlers: vec![element!("a[href^='#']", |el| {
el.set_attribute("href", &format!("#{}", el.get_attribute("id").unwrap()))
.unwrap();
Ok(())
})],
..RewriteStrSettings::default()
},
)
.unwrap()
}
/// Fix local images to base64 and integration of markdown files
fn fix_images_and_integration(
path: &FilePath,
@ -308,6 +325,7 @@ pub fn read_md(
(html_content, children_metadata) =
fix_images_and_integration(path, &html_content, metadata_type, recursive);
html_content = custom_img_size(&html_content);
html_content = fix_headers_ids(&html_content);
(html_content, mail_obfsucated) = mail_obfuscation(&html_content);
let mut final_metadata = Metadata {