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

Convert MD to MDX? #9

Closed
Closed

Description

@borekb

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...)

Activity

  1. wooorm commented on Jul 29, 2020

    @wooorm
    Member

    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!

  2. borekb commented on Jul 29, 2020

    @borekb
    Author

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

  3. wooorm commented on Jul 30, 2020

    @wooorm
    Member

    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!

  4. wooorm commented on Dec 27, 2020

    @wooorm
    Member

    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.

  5. borekb commented on Dec 28, 2020

    @borekb
    Author

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

    Yes, meta/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.

  6. ChristianMurphy commented on Feb 15, 2021

    @ChristianMurphy
    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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions