Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Convert MD to MDX? #9

Closed
borekb opened this issue Jul 29, 2020 · 6 comments
Closed

Convert MD to MDX? #9

borekb opened this issue Jul 29, 2020 · 6 comments

Comments

@borekb
Copy link

borekb commented Jul 29, 2020

Is there a utility in the unified ecosystem to convert Markdown (CommonMark) to MDX?

We're looking for a way to feed our Markdown files that contain things like <img src=""> or <br> or <span style="color: red"> to a tool that only accepts MDX, and those things would currently be parse errors.

(I'm not sure if it's even possible to safely convert CommonMark to MDX, maybe there are edge cases that I don't realize. But someone here will probably know better than me...)

@wooorm
Copy link
Member

wooorm commented Jul 29, 2020

Super interesting idea!

The start of the process would be remark-parse, remark-rehype (w/ allowDangerousHtml), then rehype-raw. You now have an AST that contains parsed element nodes with properties and whatnot. Instead of strings of HTML in Markdown, you have an HTML AST.

The missing part then would be based on hast-util-to-mdast, but include generic mapping of stuff that cannot be normal markdown, like those elements, perhaps even with a handler to mdxBlockElement and mdxSpanElement nodes that remark-mdx@next supports.

The last part then is using remark-stringify with remark-mdx@next, and you’ll have proper MDX!

@borekb
Copy link
Author

borekb commented Jul 29, 2020

That's beyond my ability 😄. But thanks for all the hints!

@wooorm
Copy link
Member

wooorm commented Jul 30, 2020

Oh I forgot about this! https://github.com/mapbox/jsxtreme-markdown/tree/main/packages/hast-util-to-jsx#readme.
That’ll turn it into serialized JSX, but a similar approach can be used for transform to mdxBlockElement!

@wooorm
Copy link
Member

wooorm commented Dec 27, 2020

It’s now possible to turn HTML into JavaScript with https://github.com/syntax-tree/hast-util-to-estree. Markdown to HTML can already be done, including raw embedded HTML.

But still, the MDX source format is different from the MD source format. This idea post is about converting between them, but from the the linked issue, it sounds like an XY problem, and that you’re in fact looking to allow different parsers in docusaurus.

@borekb
Copy link
Author

borekb commented Dec 28, 2020

that you’re in fact looking to allow different parsers in docusaurus.

Yes, facebook/docusaurus#3018 is about Docusaurus supporting multiple parsers. This idea is more low-level, enabling any tool that currently works with MDX only to also work with MD.

@ChristianMurphy
Copy link
Member

Thanks for starting the discussion @borekb!
We're in the process unifying ideas in with discussions unifiedjs/collective#44
If you'd like to continue this thread, or start a new one https://github.com/unifiedjs/unified/discussions will be the home for ideas going forward.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants