@beoe/rehype-d2
Version:
rehype d2 plugin
25 lines (24 loc) • 841 B
JavaScript
import { rehypeCodeHookImg } from "@beoe/rehype-code-hook-img";
import { d2 } from "./d2.js";
export const rehypeD2 = rehypeCodeHookImg({
language: "d2",
render: async (code, opts) => {
const { darkMode, d2Options, ...rest } = opts;
const newD2Options = { ...d2Options, ...rest };
if (newD2Options.theme !== undefined) {
// @ts-ignore
newD2Options.themeID = parseFloat(newD2Options.theme);
}
const { svg, data } = await d2(code, newD2Options);
let darkSvg;
if (darkMode) {
darkSvg = (await d2(code, {
...newD2Options,
// @ts-ignore
themeID: parseFloat(newD2Options?.darkTheme ?? "200"),
})).svg;
}
return { svg, darkSvg, data };
},
});
export default rehypeD2;