Converting / Escaping Markdown into MDX #123
-
This might seem like a simple question, but there's some nuance to it. I want to take valid markdown and transform it into valid MDX. The problem is that certain strings in markdown are not valid if interpreted as MDX. For example: What I'm trying to do is use But so far I haven't cracked it. Here's my latest attempt: import {fromMarkdown} from 'mdast-util-from-markdown';
import {toMarkdown} from 'mdast-util-to-markdown';
import {mdxFromMarkdown, mdxToMarkdown} from 'mdast-util-mdx';
import {mdx} from 'micromark-extension-mdx';
const markdownContent = `
# Hello {not MDX}
## Properties
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [color?](./package-a.colorpicker.color.md) | | string | _(Optional)_ |
| [ee](./package-a.colorpicker.ee.md) | | [IEE](./package-a.iee.md) | |
| [id](./package-a.colorpicker.id.md) | <code>readonly</code> | string | |
| [onColorChange?](./package-a.colorpicker.oncolorchange.md) | | (color: string) => void | _(Optional)_ |
| [onColorChangeWithId?](./package-a.colorpicker.oncolorchangewithid.md) | | (p0: { id: string; color: string; customCheckboxVal: boolean; }) => void | _(Optional)_ |
`;
// Parse markdown into mdast
const tree = fromMarkdown(markdownContent, {
extensions: [mdx()],
mdastExtensions: [mdxFromMarkdown()],
})
console.log(JSON.stringify(tree, undefined, 2));
// Write mdast as MDX
const out = toMarkdown(tree, {
extensions: [mdxToMarkdown()],
});
console.log(out); However, this is broken in many ways:
So the output is the same as the input. It's functionally the same if I omit all extension options and simply use Is there a way to do this? Maybe I haven't found the right libs yet. Is there a "mdast to MDX" function that you know of? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I may have cracked the code: import {fromMarkdown} from 'mdast-util-from-markdown';
import {toMarkdown} from 'mdast-util-to-markdown';
import {mdxExpressionToMarkdown} from 'mdast-util-mdx-expression'
const markdownContent = `
# Hello {not MDX}
## Properties
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [color?](./package-a.colorpicker.color.md) | | string | _(Optional)_ |
| [ee](./package-a.colorpicker.ee.md) | | [IEE](./package-a.iee.md) | |
| [id](./package-a.colorpicker.id.md) | <code>readonly</code> | string | |
| [onColorChange?](./package-a.colorpicker.oncolorchange.md) | | (color: string) => void | _(Optional)_ |
| [onColorChangeWithId?](./package-a.colorpicker.oncolorchangewithid.md) | | (p0: { id: string; color: string; customCheckboxVal: boolean; }) => void | _(Optional)_ |
`;
// Parse as markdown, into mdast
const tree = fromMarkdown(markdownContent);
// Write mdast as MDX
const out = toMarkdown(tree, {
extensions: [mdxExpressionToMarkdown()]
});
console.log(out);
// >
// # Hello \{not MDX}
//
// ## Properties
//
// | Property | Modifiers | Type | Description |
// | --- | --- | --- | --- |
// | [color?](./package-a.colorpicker.color.md) | | string | *(Optional)* |
// | [ee](./package-a.colorpicker.ee.md) | | [IEE](./package-a.iee.md) | |
// | [id](./package-a.colorpicker.id.md) | <code>readonly</code> | string | |
// | [onColorChange?](./package-a.colorpicker.oncolorchange.md) | | (color: string) => void | *(Optional)* |
// | [onColorChangeWithId?](./package-a.colorpicker.oncolorchangewithid.md) | | (p0: \{ id: string; color: string; customCheckboxVal: boolean; }) => void | *(Optional)* | Notably, it escapes |
Beta Was this translation helpful? Give feedback.
I may have cracked the code: