wooorm / mdxjs-rs

Compile MDX to JavaScript in Rust
https://docs.rs/mdxjs/
MIT License
429 stars 14 forks source link

Exposing the interface to return the transformed swc AST? #30

Closed XiNiHa closed 1 year ago

XiNiHa commented 1 year ago

I'd like to transform MDX to swc AST to make the package I'm working on (tagged-md) to support transforming the content to JSX. Are you open for exposing the interface?

wooorm commented 1 year ago

This project generates entire files: a whole program. How do you plan on injecting that AST into another AST? Please tell me more about your use case.

wooorm commented 1 year ago

Your project seems to be about md`` -> a string of HTML, isn’t markdown-rs what you’re looking for? This project allows JavaScript inside those strings, with imports and such, and it exports several things, how do you want to deal with those?

XiNiHa commented 1 year ago

While it currently transforms the markdown as a string of HTML (and already using markdown-rs for it) some of the users requested to support transforming the markdown into JSX, maybe like this:

const Component = () => (
  <main>
    {md`
      # Hello
    `}
  </main>
)

I see that mdx-rs is focused on transforming MDX into JavaScript, including imports and inline JS. Actually that sounds a bit mismatched with my use case 🤔 Maybe I should just build my own mdast => swc AST transformer ;(

wooorm commented 1 year ago

MDX is supposed to transform into whole files, it also includes an import to a particular JSX factory (e.g., react/jsx-runtime) for example. This would be a bit different.

MDX also has a bunch of things to improve the situation. Which requires somewhat complex code. Such as that you can pass a p component and it will be used for paragraphs in markdown. That can’t work in your example.

My suggestion is to start with markdown-rs using the to_mdast functionality. Then use and copy/paste https://github.com/wooorm/mdxjs-rs/blob/main/src/mdast_util_to_hast.rs. Then you can probably use and copy/paste https://github.com/wooorm/mdxjs-rs/blob/main/src/hast_util_to_swc.rs, but you might need more of a fork.

That AST should be possible then to inject into SWC? But there might be more things you run into. I’d appreciate hearing about those!

XiNiHa commented 1 year ago

Sounds pretty good, thanks for the advice! I'll close the issue then 👍