|
1 | 1 | import { Expression } from "estree"
|
2 | 2 | import isPlainObject from "is-plain-obj"
|
3 | 3 | import { annotationsMap } from "./annotations"
|
4 |
| -// import unified from "unified" |
5 |
| -// import remarkRehype from "remark-rehype" |
6 |
| -// import { toEstree } from "hast-util-to-estree" |
| 4 | +import unified from "unified" |
| 5 | +import remarkRehype from "remark-rehype" |
| 6 | +import toEstree from "hast-util-to-estree" |
| 7 | +import { Node, Parent } from "unist" |
7 | 8 |
|
8 | 9 | // forked from https://github.com/remcohaszing/estree-util-value-to-estree/blob/main/src/index.ts
|
9 | 10 |
|
@@ -180,6 +181,12 @@ export function valueToEstree(
|
180 | 181 | }
|
181 | 182 | }
|
182 | 183 | if (options.instanceAsObject || isPlainObject(value)) {
|
| 184 | + if ((value as any)?.name === MDX_CHILDREN) { |
| 185 | + const tree = { ...(value as any) } |
| 186 | + tree.name = null |
| 187 | + return (mdastToEstree(tree) as any).body[0].expression |
| 188 | + } |
| 189 | + |
183 | 190 | return {
|
184 | 191 | type: "ObjectExpression",
|
185 | 192 | // @ts-expect-error: looks like an object.
|
@@ -233,29 +240,36 @@ export function valueToEstree(
|
233 | 240 | throw new TypeError(`Unsupported value: ${String(value)}`)
|
234 | 241 | }
|
235 | 242 |
|
236 |
| -// export async function mdastToEstree(node: Node) { |
237 |
| -// const nodeTypes = [ |
238 |
| -// "mdxFlowExpression", |
239 |
| -// "mdxJsxFlowElement", |
240 |
| -// "mdxJsxTextElement", |
241 |
| -// "mdxTextExpression", |
242 |
| -// "mdxjsEsm", |
243 |
| -// ] |
244 |
| -// const changedTree = unified() |
245 |
| -// .use(remarkRehype, { |
246 |
| -// allowDangerousHtml: true, |
247 |
| -// passThrough: nodeTypes, |
248 |
| -// }) |
249 |
| -// .use(rehypeRecma as any) |
250 |
| -// .runSync(node as any) |
| 243 | +export function mdastToEstree(node: Node) { |
| 244 | + const nodeTypes = [ |
| 245 | + "mdxFlowExpression", |
| 246 | + "mdxJsxFlowElement", |
| 247 | + "mdxJsxTextElement", |
| 248 | + "mdxTextExpression", |
| 249 | + "mdxjsEsm", |
| 250 | + ] |
| 251 | + const changedTree = unified() |
| 252 | + .use(remarkRehype, { |
| 253 | + allowDangerousHtml: true, |
| 254 | + passThrough: nodeTypes, |
| 255 | + }) |
| 256 | + .use(rehypeRecma as any) |
| 257 | + .runSync(node) |
251 | 258 |
|
252 |
| -// return changedTree |
253 |
| -// } |
| 259 | + return changedTree |
| 260 | +} |
254 | 261 |
|
255 |
| -// function rehypeRecma() { |
256 |
| -// return (tree: any) => toEstree(tree) |
257 |
| -// } |
| 262 | +function rehypeRecma() { |
| 263 | + return (tree: any) => (toEstree as any)(tree) |
| 264 | +} |
258 | 265 |
|
259 |
| -// export function wrapChildren() { |
260 |
| -// return {} |
261 |
| -// } |
| 266 | +const MDX_CHILDREN = "MDX_CHILDREN" |
| 267 | + |
| 268 | +export function wrapChildren(children: Node[]) { |
| 269 | + const tree = { |
| 270 | + type: "mdxJsxFlowElement", |
| 271 | + children, |
| 272 | + name: MDX_CHILDREN, |
| 273 | + } |
| 274 | + return tree |
| 275 | +} |
0 commit comments