Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a codemod for syntax changes we can detect #1043

Closed
johno opened this issue Apr 30, 2020 · 5 comments
Closed

Implement a codemod for syntax changes we can detect #1043

johno opened this issue Apr 30, 2020 · 5 comments
Labels
🏗 area/tools This affects tooling 🦋 type/enhancement This is great to have 💎 v2 Issues related to v2 🙆 yes/confirmed This is confirmed and ready to be worked on
Milestone

Comments

@johno
Copy link
Member

johno commented Apr 30, 2020

  • <https://mdxjs.com> => [https://mdxjs.com](https://mdxjs.com)
  • <3 => &lt;3
@johno johno added 🦋 type/enhancement This is great to have 🙆 yes/confirmed This is confirmed and ready to be worked on 💎 v2 Issues related to v2 🏗 area/tools This affects tooling labels Apr 30, 2020
@wooorm
Copy link
Member

wooorm commented May 1, 2020

This could be done by parsing with the old remark-mdx, and compiling with the new one, although the old JSX tags would need to be parsed to the new one (or ignored and injected raw, which will then result in errors that people will manually fix, but incorrect JSX is unlikely as if it doesn’t work now then it probably didn’t work already)

var before = unified()
  .use(remarkParse)
  .use(remarkOldMdx)

var after = unified()
  .use(remarkStringify)
  .use(remarkNewMdx)
  .use(remarkNewMdxJs)

var oldTree = before.runSync(before.parse(doc))

var newTree = changeOldToNewTree(oldTree)

var output = after.stringify(after.runSync(newTree))

Finally, unified will reformat code. So alternatively, you could use the old tree to inspect if those constructs were used, and then use their positional information to replace in the document.

@wooorm
Copy link
Member

wooorm commented May 6, 2020

For my last paragraph, magic-string would help with that! https://github.com/Rich-Harris/magic-string

@johno johno added this to the v2 milestone Jul 22, 2020
@wooorm
Copy link
Member

wooorm commented Oct 19, 2021

I opted not to. I’ve thought about how it’d work a couple times but it’s complex. It’d be a lot of work and wouldn’t work in some cases.
From now on though, as we do have an AST starting last RC, that includes tags and elements and expressions and all that, we can start supporting codemods for future changes.

@wooorm wooorm closed this as completed Oct 19, 2021
@karlhorky
Copy link
Contributor

Is there currently a codemod to convert MDX v1 code to v2? We have a lot of content written in MDX v1 and would like to avoid rewriting manually by hand if possible.

I saw https://github.com/mdx-js/codemods, but it seems a bit old...

@wooorm
Copy link
Member

wooorm commented Aug 8, 2022

Last comment still holds. A proper code mod is really complex.
I’m not sure how much data you have.
I recommend using remark with remark-mdx on your docs folder:

remark -u remark-mdx docs/

It’ll throw errors with pretty descriptive messages. With that, and some regex kung-fu, you should be able to get far.


Hashicorp had something somewhat working for this, but it was still missing things. Let me know if you‘re interested in getting your hands dirty and improving that project and I’ll ping folks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏗 area/tools This affects tooling 🦋 type/enhancement This is great to have 💎 v2 Issues related to v2 🙆 yes/confirmed This is confirmed and ready to be worked on
Development

No branches or pull requests

3 participants