2024-01-05 23:46:54 +08:00
|
|
|
import * as mdx from "@mdx-js/mdx";
|
2022-03-21 00:53:16 +08:00
|
|
|
import * as swc from "@swc/core";
|
2024-06-18 20:52:41 +08:00
|
|
|
import remarkMath from 'remark-math';
|
|
|
|
import rehypeKatex from 'rehype-katex';
|
2022-03-21 00:53:16 +08:00
|
|
|
|
|
|
|
export function renderMdx(markdown: string): string {
|
|
|
|
const esmCode = mdx.compileSync(markdown, {
|
|
|
|
jsxRuntime: "classic",
|
|
|
|
pragma: "Vue.h",
|
|
|
|
pragmaFrag: "Vue.Fragment",
|
2024-06-18 20:52:41 +08:00
|
|
|
pragmaImportSource: "vue",
|
|
|
|
remarkPlugins: [remarkMath],
|
|
|
|
rehypePlugins: [rehypeKatex]
|
2022-03-21 00:53:16 +08:00
|
|
|
}).value.toString("utf-8");
|
|
|
|
|
|
|
|
const codeRemovedImport = esmCode.replace(/^import .*$/m, "");
|
|
|
|
|
|
|
|
return swc.transformSync(codeRemovedImport, {
|
|
|
|
jsc: {
|
|
|
|
parser: {
|
|
|
|
syntax: "ecmascript"
|
|
|
|
},
|
|
|
|
loose: true,
|
|
|
|
},
|
|
|
|
minify: true,
|
|
|
|
module: { type: "commonjs" },
|
|
|
|
}).code;
|
|
|
|
}
|