|
| 1 | +// TODO: Should we use `micromark-extension-mdx` instead? It's "non-JS aware," |
| 2 | +// which is what we want, but it doesn't seem to recognize non-expression |
| 3 | +// syntax (such as import statements). |
| 4 | +import { mdxjs } from "micromark-extension-mdxjs"; |
| 5 | +import { fromMarkdown } from "mdast-util-from-markdown"; |
| 6 | +import { mathFromMarkdown } from "mdast-util-math"; |
| 7 | +import { math } from "micromark-extension-math"; |
| 8 | +import { mdxFromMarkdown } from "mdast-util-mdx"; |
| 9 | +import { directive } from "micromark-extension-directive"; |
| 10 | +import { directiveFromMarkdown } from "mdast-util-directive"; |
| 11 | +import { toHast } from "mdast-util-to-hast"; |
| 12 | +import { h } from "hastscript"; |
| 13 | +import { toHtml } from "hast-util-to-html"; |
| 14 | + |
| 15 | +const mdxNodes = [ |
| 16 | + "mdxjsEsm", |
| 17 | + "mdxFlowExpression", |
| 18 | + "mdxJsxFlowElement", |
| 19 | + "mdxJsxTextElement", |
| 20 | + "mdxTextExpression", |
| 21 | +]; |
| 22 | + |
| 23 | +const directiveNodes = [ |
| 24 | + "containerDirective", |
| 25 | + "leafDirective", |
| 26 | + "textDirective", |
| 27 | +]; |
| 28 | + |
| 29 | +function isComment(source) { |
| 30 | + return source.startsWith("{/*") && source.endsWith("*/}"); |
| 31 | +} |
| 32 | + |
| 33 | +function createCustomHandler(doc) { |
| 34 | + return function customHandler(state, node, parent) { |
| 35 | + const start = node.position.start.offset; |
| 36 | + const end = node.position.end.offset; |
| 37 | + |
| 38 | + const source = doc.slice(start, end); |
| 39 | + if (node.type === "mdxFlowExpression" && isComment(source)) { |
| 40 | + return { type: "comment", value: source.slice(3, -3) }; |
| 41 | + } else if (mdxNodes.includes(node.type)) { |
| 42 | + const className = `mdxNode ${node.type}`; |
| 43 | + if (source.includes("\n")) { |
| 44 | + return h("pre", h("code", { className }, source)); |
| 45 | + } else { |
| 46 | + return h("code", { className }, source); |
| 47 | + } |
| 48 | + } else if (directiveNodes.includes(node.type)) { |
| 49 | + // Handle directives like :::tip{title="Examples" collapse} |
| 50 | + // Convert them to div elements with appropriate classes and content |
| 51 | + const directiveType = node.name || 'directive'; |
| 52 | + const attributes = node.attributes || {}; |
| 53 | + |
| 54 | + // Convert directive to a div with class |
| 55 | + const divProps = { class: `directive directive-${directiveType}` }; |
| 56 | + |
| 57 | + // Add data attributes for directive attributes |
| 58 | + for (const [key, value] of Object.entries(attributes)) { |
| 59 | + divProps[`data-${key}`] = value; |
| 60 | + } |
| 61 | + |
| 62 | + // Process children if they exist |
| 63 | + const children = node.children ? |
| 64 | + node.children.map(child => state.one(child, node)) : |
| 65 | + []; |
| 66 | + |
| 67 | + return h('div', divProps, children); |
| 68 | + } |
| 69 | + |
| 70 | + return null; |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +function toValeAST(doc) { |
| 75 | + const mdast = fromMarkdown(doc, { |
| 76 | + extensions: [mdxjs(), math(), directive()], |
| 77 | + mdastExtensions: [mdxFromMarkdown(), mathFromMarkdown(), directiveFromMarkdown()], |
| 78 | + }); |
| 79 | + |
| 80 | + const customHandler = createCustomHandler(doc); |
| 81 | + |
| 82 | + const hast = toHast(mdast, { |
| 83 | + allowDangerousHtml: true, |
| 84 | + passThrough: [...mdxNodes, ...directiveNodes], |
| 85 | + handlers: { |
| 86 | + mdxjsEsm: customHandler, |
| 87 | + mdxFlowExpression: customHandler, |
| 88 | + mdxJsxFlowElement: customHandler, |
| 89 | + mdxJsxTextElement: customHandler, |
| 90 | + mdxTextExpression: customHandler, |
| 91 | + containerDirective: customHandler, |
| 92 | + leafDirective: customHandler, |
| 93 | + textDirective: customHandler, |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + return toHtml(hast); |
| 98 | +} |
| 99 | + |
| 100 | +export { toValeAST }; |
0 commit comments