#202 - Convert relative hash links to absolute links in customHTMLRenderer
authorAdam Dullage <redacted>
Thu, 27 Jun 2024 18:41:23 +0000 (19:41 +0100)
committerAdam Dullage <redacted>
Thu, 27 Jun 2024 18:41:23 +0000 (19:41 +0100)
client/components/toastui/baseOptions.js

index 60ff3cab7a31a3f6f9d72cfc37256abf07e0c22b..f156115f456b54987e8260e13a2ebe03daace958 100644 (file)
@@ -99,23 +99,34 @@ function extendedAutolinks(source) {
 }
 
 const customHTMLRenderer = {
-  heading(node, { entering, getChildrenText }) {
-    const tagName = `h${node.level}`;
-
+  // Add id attribute to headings
+  heading(node, { entering, getChildrenText, origin }) {
+    const original = origin();
     if (entering) {
-      return {
-        type: "openTag",
-        tagName,
-        attributes: {
-          id: getChildrenText(node)
-            .toLowerCase()
-            .replace(/[^a-z0-9-\s]*/g, "")
-            .trim()
-            .replace(/\s/g, "-"),
-        },
+      original.attributes = {
+        id: getChildrenText(node)
+          .toLowerCase()
+          .replace(/[^a-z0-9-\s]*/g, "")
+          .trim()
+          .replace(/\s/g, "-"),
       };
     }
-    return { type: "closeTag", tagName };
+    return original;
+  },
+  // Convert relative hash links to absolute links
+  link(_, { entering, origin }) {
+    const original = origin();
+    if (entering) {
+      const href = original.attributes.href;
+      if (href.startsWith("#")) {
+        const targetRoute = {
+          ...router.currentRoute.value,
+          hash: href,
+        };
+        original.attributes.href = router.resolve(targetRoute).href;
+      }
+    }
+    return original;
   },
 };
 
git clone https://git.99rst.org/PROJECT