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

Fix parsing of JSX with expression #138

Merged
merged 1 commit into from
Sep 19, 2024

Conversation

begleynk
Copy link
Contributor

@begleynk begleynk commented Sep 19, 2024

Disclaimer: I am not entirely sure that this is the correct fix, but it does pass the tests. Happy to make changes in the likely case I haven't quite understood this fully.

This fixes the issues mentioned in #136, where this case <x>{1}</x> was no longer parsed, and {1}<x/> caused a panic when converted into an AST (not an issue when converting to HTML).

I was first confused about what the expected output should be here, so I looked at micromark for inspiration. Here's what I found:

  • Micromark parses <x>{1}</x> as a block level Flow element with an expression (unlike this crate as of alpha.16, which produced a paragraph with an inline JSX text node)
  • Micromark parses {1}</x> into an AST with a root element, with two children: the expr, and the JSX tag

This change now makes this crate matches micromark's behavior.

Test program I used
import {mdxjs} from 'micromark-extension-mdxjs'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {mdxFromMarkdown} from 'mdast-util-mdx'


const doc = "<span>{1}</span>";

const tree = fromMarkdown(doc, {
  extensions: [mdxjs()],
  mdastExtensions: [mdxFromMarkdown()]
})


console.log(JSON.stringify(tree, null, 2))

This also removes the MdxExpressionFlowNok part of the state, as it wasn't used any more.

Fixes #136

@wooorm wooorm merged commit 5f781cd into wooorm:main Sep 19, 2024
3 checks passed
@wooorm
Copy link
Owner

wooorm commented Sep 19, 2024

thanks!

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

Successfully merging this pull request may close these issues.

Unable to parse inline JSX element with MDX expression child
2 participants