UNPKG

fumadocs-docgen

Version:

Useful remark utilities and plugins

79 lines (77 loc) 2.04 kB
import { __async } from "./chunk-4AFQP74Z.js"; // src/remark-ts2js.ts import { visit } from "unist-util-visit"; import { generateCodeBlockTabs, parseCodeBlockAttributes } from "fumadocs-core/mdx-plugins"; function remarkTypeScriptToJavaScript({ persist = false, defaultValue = "ts", disableTrigger = false } = {}) { return (tree, file) => __async(null, null, function* () { const oxc = yield import("oxc-transform"); visit(tree, "code", (node) => { var _a, _b, _c; const lang = node.lang; if (lang !== "ts" && lang !== "tsx") return; const meta = parseCodeBlockAttributes((_a = node.meta) != null ? _a : "", ["ts2js"]); if (!disableTrigger && !("ts2js" in meta.attributes)) return; const result = oxc.transform( `${(_b = file.path) != null ? _b : "test"}.${lang}`, node.value, { sourcemap: false, jsx: "preserve" } ); const targetLang = lang === "tsx" ? "jsx" : "js"; const replacement = generateCodeBlockTabs({ persist, defaultValue, triggers: [ { value: "ts", children: [{ type: "text", value: "TypeScript" }] }, { value: "js", children: [{ type: "text", value: "JavaScript" }] } ], tabs: [ { value: "ts", children: [ { type: "code", lang: node.lang, meta: meta.rest, value: node.value } ] }, { value: "js", children: [ { type: "code", lang: targetLang, meta: (_c = meta.attributes.ts2js) != null ? _c : meta.rest, value: result.code } ] } ] }); Object.assign(node, replacement); return "skip"; }); }); } export { remarkTypeScriptToJavaScript };