docs/mdx/rehype.mjs

154 lines
4.3 KiB
JavaScript
Raw Normal View History

import {
transformerMetaHighlight,
transformerNotationFocus,
transformerNotationHighlight,
} from "@shikijs/transformers";
2024-07-22 11:49:47 +02:00
import { slugifyWithCounter } from "@sindresorhus/slugify";
import * as acorn from "acorn";
import { toString as mdastToString } from "mdast-util-to-string";
import { mdxAnnotations } from "mdx-annotations";
import { createHighlighter } from "shiki";
2024-07-22 11:49:47 +02:00
import { visit } from "unist-util-visit";
function rehypeParseCodeBlocks() {
return (tree) => {
// biome-ignore lint/style/useNamingConvention: <explanation>
visit(tree, "element", (node, _nodeIndex, parentNode) => {
2024-07-22 11:49:47 +02:00
if (node.tagName === "code" && node.properties.className) {
parentNode.properties.language =
node.properties.className[0]?.replace(/^language-/, "");
}
});
};
}
const highlighter = await createHighlighter({
langs: [
"typescript",
"javascript",
"json",
"css",
"html",
"json5",
"jsonc",
"markdown",
"bash",
"php",
"python",
2024-07-27 15:37:58 +02:00
"http",
],
themes: [],
});
2024-07-22 11:49:47 +02:00
highlighter.loadTheme("dark-plus");
2024-07-22 11:49:47 +02:00
function rehypeShiki() {
return async (tree) => {
visit(tree, "element", (node) => {
if (
node.tagName === "pre" &&
node.children[0]?.tagName === "code"
) {
const codeNode = node.children[0];
const textNode = codeNode.children[0];
node.properties.code = textNode.value;
if (node.properties.language) {
const html = highlighter.codeToHtml(textNode.value, {
lang: node.properties.language,
theme: "dark-plus",
transformers: [
2025-02-15 15:43:23 +01:00
transformerNotationFocus({
matchAlgorithm: "v3",
}),
transformerNotationHighlight({
matchAlgorithm: "v3",
}),
transformerMetaHighlight({
matchAlgorithm: "v3",
}),
],
2024-07-22 11:49:47 +02:00
});
textNode.value = html;
2024-07-22 11:49:47 +02:00
}
}
});
};
}
function rehypeSlugify() {
return (tree) => {
const slugify = slugifyWithCounter();
visit(tree, "element", (node) => {
if (node.tagName === "h2" && !node.properties.id) {
node.properties.id = slugify(mdastToString(node));
}
});
};
}
function rehypeAddMDXExports(getExports) {
return (tree) => {
const exports = Object.entries(getExports(tree));
for (const [name, value] of exports) {
for (const node of tree.children) {
if (
node.type === "mdxjsEsm" &&
new RegExp(`export\\s+const\\s+${name}\\s*=`).test(
node.value,
)
) {
return;
}
}
const exportStr = `export const ${name} = ${value}`;
tree.children.push({
type: "mdxjsEsm",
value: exportStr,
data: {
estree: acorn.parse(exportStr, {
sourceType: "module",
ecmaVersion: "latest",
}),
},
});
}
};
}
function getSections(node) {
const sections = [];
for (const child of node.children ?? []) {
if (child.type === "element" && child.tagName === "h2") {
sections.push(`{
title: ${JSON.stringify(mdastToString(child))},
id: ${JSON.stringify(child.properties.id)},
...${child.properties.annotation}
}`);
} else if (child.children) {
sections.push(...getSections(child));
}
}
return sections;
}
export const rehypePlugins = [
mdxAnnotations.rehype,
rehypeParseCodeBlocks,
rehypeShiki,
rehypeSlugify,
[
rehypeAddMDXExports,
(tree) => ({
sections: `[${getSections(tree).join()}]`,
}),
],
];