in kit/preprocessors/mdsvex/index.js [137:180]
function transform(tree) {
let headings = [];
visit(tree, "heading", (node, index, parent) => {
const depth = node.depth;
let title = getTitleText(node);
let local = "";
const match = title.match(/\[\s(.*?)\s\]$/);
if (match && match[1]) {
local = match[1];
title = title.replace(match[0], "").trim();
} else {
local = title
.trim()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with hyphens
.replace(/[^\p{L}\p{N}-]+/gu, ""); // Keep letters, numbers, and hyphens only
}
headings = addToTree(headings, { title, local, sections: [], depth });
// Create a svelte node (in remark grammar, the type is "html")
const svelteNode = {
type: "html",
value: `<Heading title="${title.replaceAll(
"{",
"{"
)}" local="${local}" headingTag="h${depth}"/>`,
};
// Replace the old node with the new Svelte node
parent.children[index] = svelteNode;
});
visit(tree, "text", onText);
visit(tree, "html", onHtml);
visit(tree, "blockquote", onBlockquote);
let jsonString = JSON.stringify(headings[0]);
if (jsonString) {
jsonString = jsonString.replaceAll("'", "\\'");
}
tree.children.unshift({
type: "html",
value: `<script context="module">export const metadata = '${jsonString}';</script>`,
});
}