From: Adam Dullage Date: Thu, 27 Jun 2024 18:41:23 +0000 (+0100) Subject: #202 - Convert relative hash links to absolute links in customHTMLRenderer X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=3d40a4685f1b1efcec10ddae2af66e38e8c1e840;p=flatnotes.git #202 - Convert relative hash links to absolute links in customHTMLRenderer --- diff --git a/client/components/toastui/baseOptions.js b/client/components/toastui/baseOptions.js index 60ff3ca..f156115 100644 --- a/client/components/toastui/baseOptions.js +++ b/client/components/toastui/baseOptions.js @@ -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; }, };