Skip to content

feat: support MDX v2#166

Merged
kentcdodds merged 3 commits into
masterfrom
pr/asts
Dec 15, 2020
Merged

feat: support MDX v2#166
kentcdodds merged 3 commits into
masterfrom
pr/asts

Conversation

@kentcdodds
Copy link
Copy Markdown
Collaborator

What: convert the HTML to an AST using parse5 and hast-util-from-parse5

Why: Support MDX v2

How: add a simple utility function to convert a string of HTML to a hast and replace the link node with the element node.

Checklist:

  • Documentation: N/A
  • Tests - The existing tests should "just pass" as soon as remark supports parsing these
  • Ready to be merged NO

cc @wooorm please confirm that currently remark does not support element nodes. When I tested this (with remark v13), I got the error: Cannot handle unknown node 'element'.

Once remark supports this, then we should only need minor changes to this PR for things to work I think.

BREAKING CHANGE: You need to use MDX v2 (or remark v??) for this to work.

Copy link
Copy Markdown
Contributor

@wooorm wooorm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for taking this on @kentcdodds!

There was a bug in your code, but with my suggestion, this change should work in all Gatsby, MDX, and remark versions (because the feature it depends on has worked from the start, 5 years ago!): not a breaking change (although, of course: trust but verify)

Comment thread src/index.js Outdated
Comment thread src/index.js Outdated
BREAKING CHANGE: You need to use MDX v2 for this to work
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 15, 2020

Codecov Report

Merging #166 (c325dfc) into master (8b57979) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #166   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           17        17           
  Lines          300       299    -1     
  Branches        94        94           
=========================================
- Hits           300       299    -1     
Impacted Files Coverage Δ
src/index.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8b57979...c325dfc. Read the comment docs.

Copy link
Copy Markdown
Collaborator Author

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready to go 👍

Comment thread src/index.js
// convert the HTML string into an AST
const htmlElement = htmlToHast(html);

// set the paragraphNode.data with the necessary properties
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wooorm, thanks so much for your help on this one. I'm not sure I understand how this works (I'm pretty sure the paragaphNode's type is still paragraph, but somehow it changes to the data.hName property when the AST is compiled to HTML 🤷‍♂️

In any case, this is great! And it's nice to know that we're future-proof and completely backward compatible :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem!

The paragraph is till a paragraph. But it defined (though .data) that it should turn into something else when going to HTML. It defines an element name, props, and children for that!

So, when going from markdown to HTML, the process sees that there is something else defined rather than the normal markdown children, and it’ll use that instead

@kentcdodds kentcdodds marked this pull request as ready for review December 15, 2020 21:03
@kentcdodds kentcdodds merged commit 5e2f2ce into master Dec 15, 2020
@kentcdodds kentcdodds deleted the pr/asts branch December 15, 2020 21:03
@kentcdodds
Copy link
Copy Markdown
Collaborator Author

@all-contributors please add @wooorm for review and code

@allcontributors
Copy link
Copy Markdown
Contributor

@kentcdodds

I've put up a pull request to add @wooorm! 🎉

Comment thread src/__tests__/plugin.js
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 4.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants