-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Injected custom elements and layouts result in crash #2123
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Codecov Report
@@ Coverage Diff @@
## main #2123 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 22 22
Lines 2044 2046 +2
=========================================
+ Hits 2044 2046 +2
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, thanks!
So, I can’t reproduce your problem with your test. Can you think of a reproduction that does show your problem?
Additionally, this might just be due to remark-shiki-twoslash
doing weird things:
a) remark is for markdown, not for HTML. rehype is for HTML.
b) Injecting strings of HTML into an AST is an bad idea, yes it can be fixed with rehype-raw
but that essentially means parsing a file twice?
c) it has several long open issues
If possible, I’d suggest a different syntax highlighter or sending PRs to shiki twoslash to improve the situation
packages/mdx/test/compile.js
Outdated
@@ -1173,6 +1174,29 @@ test('remark-rehype options', async () => { | |||
) | |||
}) | |||
|
|||
test('remark-shiki-twoslash with layout function', async () => { | |||
// Just test that the output can be generated correctly without JS syntax errors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test passes locally for me, without your change to recma-jsx-rewrite
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that. When I was cleaning up the PR I changed the import to get TS to work but that made the test no longer reproduce the problem. Should be fixed now (should pass when my code change is included, should fail with unexpected token ;
when my code change is not included).
I definitely agree with some of your concerns here, the double-parsing is definitely less-than-ideal. I'd like to improve the situation with twoslash, but that is a big project and for now I just wanted to get this bugfix through. I would like to hear more about a and c if you are willing to clarify. Which long-open issues are more concerning to you? And I think it makes sense that |
Also, I am not familiar with the type-coverage check. Is it related to my casting to |
🤔🤔🤔 It still all just works for me with your test case without your code change, nothing broken 🤷♂️ Here’s my patch (excluding package lock changes): +++ b/package.json
@@ -244,5 +244,8 @@
false
]
]
+ },
+ "dependencies": {
+ "remark-shiki-twoslash": "^3.1.0"
}
}
diff --git a/packages/mdx/test/compile.js b/packages/mdx/test/compile.js
index d5a10ee0..aa8a5366 100644
--- a/packages/mdx/test/compile.js
+++ b/packages/mdx/test/compile.js
@@ -21,6 +21,7 @@ import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'
import {VFile} from 'vfile'
import {SourceMapGenerator} from 'source-map'
+import * as remarkShikiTwoslash from 'remark-shiki-twoslash'
import {compile, compileSync, createProcessor, nodeTypes} from '../index.js'
// @ts-expect-error: make sure a single react is used.
import {renderToStaticMarkup as renderToStaticMarkup_} from '../../react/node_modules/react-dom/server.js'
@@ -1173,6 +1174,28 @@ test('remark-rehype options', async () => {
)
})
+test('remark-shiki-twoslash with layout function', async () => {
+ // Just test that the output can be generated correctly without JS syntax errors
+ renderToStaticMarkup(
+ React.createElement(
+ await run(
+ await compile(
+ `
+export default function ({ children }) { return <div>{children}</div>; }
+
+\`\`\`js twoslash
+console.log('hi')
+\`\`\`
+`,
+ {
+ remarkPlugins: [/** @type any */ (remarkShikiTwoslash).default],
+ rehypePlugins: [[rehypeRaw, {passThrough: nodeTypes}]]
+ }
+ )
+ )
+ )
+ )
+})
test('MDX (JSX)', async () => {
assert.equal(
renderToStaticMarkup(
|
The main offenders are:
These are injecting raw strings of HTML into an AST.
For a), see:
For c), (your more specific questions I asked in the previous quote answer), for specific issues, quickly looking through them these two come up:
Correct, |
I commented out the code fix and now it is failing as expected in CI. If you pull the latest commit it should fail locally in the same way hopefully? https://github.com/mdx-js/mdx/runs/8217507997?check_suite_focus=true#step:5:40 |
@wooorm can you help me understand why there is a distinction between a If I was making a rehype plugin that for example did something to I'm asking this because I'm wondering whether rehype-raw is maybe not intended for use with MDX. Are nodes with |
I think I have a reproduction that doesn't rely on shiki-twoslash! If I put If I inject So, depending on the answer to the question "are If "no, If "yes, Then I can push a test like this that reproduces the bug without relying on remark-shiki-twoslash: {
remarkPlugins: [
() => {
const transform = async (markdownAST) => {
markdownAST.children.push({
type: 'html',
value: `<custom-element />`,
position: [],
children: []
})
}
return transform
}
],
rehypePlugins: [
[rehypeRaw, {passThrough: nodeTypes}],
]
} (or something similar that directly injects the Does that sound right to you? Thanks for your patience with me! |
Sweet! The answer is: Hmm, I still have can’t reproduce an error with a test like that: renderToStaticMarkup(
React.createElement(
await run(
await compile('', {
remarkPlugins: [
() => {
const transform = async (markdownAST) => {
markdownAST.children.push({
type: 'html',
value: '<custom-element />',
position: [],
children: []
})
}
return transform
}
],
rehypePlugins: [[rehypeRaw, {passThrough: nodeTypes}]]
})
)
)
) Note: the Do you have a more complete example that reproduces your problem? |
used with remark-shiki-twoslash. @calebeby, who provided the workaround, is the king. - mdx-js/mdx#2123 - mdx-js/mdx#2112
Friendly ping! :) |
Hey @wooorm, thanks for the ping. I'd really like to get back to this, it's on my list but school comes first right now :) |
30844ea
to
5304861
Compare
|
packages/mdx/test/compile.js
Outdated
@@ -1173,6 +1173,41 @@ test('remark-rehype options', async () => { | |||
) | |||
}) | |||
|
|||
/** @see https://github.com/mdx-js/mdx/issues/2112 */ | |||
test.only('layout function with passthrough HTML', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bholmesdev ^-- this! You have an only
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! Didn't realize. Will remove 👏
Alright @wooorm, should be ready to review 👍 |
Thanks everyone, particularly @calebeby and @bholmesdev, the reduced case helped a lot. See the linked commit message for much more information on what was going on and how it was solved. |
Released in |
Closes #2112
Previously, mdx generated
const ;
, which is not valid syntax. If you comment out the fix, the test fails.In the test I didn't add an assertion for the HTML output since that is more tied to remark-shiki-twoslash, but if you would prefer me to add that I can.
@bholmesdev helped me narrow this down, thanks!