-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Unable to parse inline JSX element with MDX expression child #136
Comments
Would love to have you help! The commit you link to could definitely be it. In the commit message, I linked to the commits I tried to port. Perhaps there‘s a difference? |
Sounds good! I'll take closer look and familiarize myself with the internals a bit. I've started by adding a test case to assert_eq!(
to_mdast("<a>{1}</a>", &mdx.parse)?,
Node::Root(Root {
children: vec![Node::Paragraph(Paragraph {
children: vec![Node::MdxJsxTextElement(MdxJsxTextElement {
name: Some("a".into()),
attributes: vec![],
children: vec![Node::MdxTextExpression(MdxTextExpression {
value: "1".into(),
position: Some(Position::new(1, 1, 0, 1, 3, 2)),
stops: vec![(0, 2)]
}),],
position: Some(Position::new(1, 1, 0, 1, 3, 2))
}),],
position: Some(Position::new(1, 1, 0, 1, 11, 10))
})],
position: Some(Position::new(1, 1, 0, 1, 11, 10))
}),
"should support mdx jsx (text) with expression child"
); I'll see how far I get! |
I think I can see the issue, but I'm struggling to figure out the right fix. The issue is indeed in pub fn end(tokenizer: &mut Tokenizer) -> State {
// ...comments removed for brevity...
match tokenizer.current {
None | Some(b'\n') => {
reset(tokenizer);
State::Ok
}
// -------- Attempting to parse a JSX tag after the expr ---------
Some(b'<') if tokenizer.parse_state.options.constructs.mdx_jsx_flow => {
tokenizer.attempt(
State::Next(StateName::MdxExpressionFlowAfter),
State::Next(StateName::MdxExpressionFlowNok),
);
State::Retry(StateName::MdxJsxStart)
}
// ----------------------------------------------------------------
_ => {
reset(tokenizer);
State::Nok
}
}
} If you take out those lines handling the JSX tag case, what seems to happen is that we try to parse the closing tag With this line, what instead happens is we never backtrack, and we get the mismatch error because it seems there's a leftover event after we run So unless I'm way off here, it seems like we'd like to be able to do the same thing and backtrack once we've parsed the full line? e.g. if you change the highlighted code above to this: Some(b'<') if tokenizer.parse_state.options.constructs.mdx_jsx_flow => {
tokenizer.tokenize_state.token_1 = Name::MdxJsxFlowTag;
tokenizer.attempt(
State::Next(StateName::MdxJsxFlowAfter),
State::Next(StateName::MdxJsxFlowNok),
);
State::Retry(StateName::MdxJsxStart)
} This parses, but we don't get an inline JSX flow inside a Paragraph, but a block-level JSX tag instead. Any pointers here would be really appreciated! |
I actually just noticed that calling
This was the config I used: let mut opts = markdown::Options::default();
let parse_opts = markdown::ParseOptions::mdx();
opts.parse = parse_opts;
// Parse the markdown file into an AST
let mdast = markdown::to_mdast(&content, &opts.parse); Update Assuming that should be valid syntax, what would the AST be? The test claims it should be a block-level construct, but what is the container? // test in question:
assert_eq!(
to_html_with_options("{1}<x/>", &swc)?,
"",
"should support an expression and then a tag (flow)"
); (Sorry this is getting a bit rambly... I'm going to take another look later with some fresh eyes!) |
thanks! |
It seems that since
1.0.0-alpha.17
this syntax doesn't work any more:You get the following error:
1.0.0-alpha.16
parses it correctly:I used the following config:
Perhaps this is the commit that caused it? 71361e4
Happy to take a stab at fixing this. Any pointers about what might be going on and where to start would be appreciated!
The text was updated successfully, but these errors were encountered: