From f72a341787a85e702fbdcf97b3feeae20db27008 Mon Sep 17 00:00:00 2001
From: Mylloon <kennel.anri@tutanota.com>
Date: Fri, 13 Dec 2024 18:48:31 +0100
Subject: [PATCH] fix header ids not in sync with the link

---
 src/utils/markdown.rs | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/utils/markdown.rs b/src/utils/markdown.rs
index f0cacb9..aa51581 100644
--- a/src/utils/markdown.rs
+++ b/src/utils/markdown.rs
@@ -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 {