diff --git a/integrations/my_autolink_headings.js b/integrations/my_autolink_headings.js index c0355d1..de0db8a 100644 --- a/integrations/my_autolink_headings.js +++ b/integrations/my_autolink_headings.js @@ -7,10 +7,8 @@ import { matches } from 'hast-util-select'; // Add IDs and SVG permalinks to headings // (rehype-autolink-headings is good, but can't be configured to ignore some headings) -const anchor = fromHtmlIsomorphic( - '', - { fragment: true }, -).children[0]; +const anchorHtml = + ''; // Should match the method in bin/build/checks/links.pl function slugIt(heading) { @@ -28,16 +26,16 @@ function autoLinkHeadings(options) { if (!isElement(node, headings) || (exclude && matches(exclude, node))) { return CONTINUE; } - const newAnchor = structuredClone(anchor); - if (node.properties.id) { - const id = node.properties.id; - newAnchor.properties = { ...newAnchor.properties, href: '#' + id }; - } else { - const id = slugIt(node); - newAnchor.properties = { ...newAnchor.properties, href: '#' + id }; - node.properties.id = id; + + if (!node.properties.id) { + node.properties.id = slugIt(node); } - node.children.unshift(newAnchor); + const id = node.properties.id; + const anchor = fromHtmlIsomorphic(anchorHtml, { fragment: true }) + .children[0]; + anchor.properties = { ...anchor.properties, href: '#' + id }; + + node.children.unshift(anchor); return SKIP; }); };