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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve MDX optimize with sibling nodes #10887
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/mdx": patch | ||
--- | ||
|
||
Updates the `optimize` option to group static sibling nodes as a `<Fragment />`. This reduces the number of AST nodes and simplifies runtime rendering of MDX pages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/mdx": patch | ||
--- | ||
|
||
Fixes `export const components` keys detection for the `optimize` option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/integrations/mdx/test/units/rehype-optimize-static.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import assert from 'node:assert/strict'; | ||
import { describe, it } from 'node:test'; | ||
import { compile as _compile } from '@mdx-js/mdx'; | ||
import { rehypeOptimizeStatic } from '../../dist/rehype-optimize-static.js'; | ||
|
||
/** | ||
* @param {string} mdxCode | ||
* @param {Readonly<import('@mdx-js/mdx').CompileOptions>} options | ||
*/ | ||
async function compile(mdxCode, options) { | ||
const result = await _compile(mdxCode, { | ||
jsx: true, | ||
rehypePlugins: [rehypeOptimizeStatic], | ||
...options, | ||
}); | ||
const code = result.toString(); | ||
// Capture the returned JSX code for testing | ||
const jsx = code.match(/return (.+);\n\}\nexport default function MDXContent/s)?.[1]; | ||
if (jsx == null) throw new Error('Could not find JSX code in compiled MDX'); | ||
return dedent(jsx); | ||
} | ||
|
||
function dedent(str) { | ||
const lines = str.split('\n'); | ||
if (lines.length <= 1) return str; | ||
// Get last line indent, and dedent this amount for the other lines | ||
const lastLineIndent = lines[lines.length - 1].match(/^\s*/)[0].length; | ||
return lines.map((line, i) => (i === 0 ? line : line.slice(lastLineIndent))).join('\n'); | ||
} | ||
|
||
describe('rehype-optimize-static', () => { | ||
it('works', async () => { | ||
const jsx = await compile(`# hello`); | ||
assert.equal( | ||
jsx, | ||
`\ | ||
<_components.h1 {...{ | ||
"set:html": "hello" | ||
}} />` | ||
); | ||
}); | ||
|
||
it('groups sibling nodes as a single Fragment', async () => { | ||
const jsx = await compile(`\ | ||
# hello | ||
|
||
foo bar | ||
`); | ||
assert.equal( | ||
jsx, | ||
`\ | ||
<Fragment set:html="<h1>hello</h1> | ||
<p>foo bar</p>" />` | ||
); | ||
}); | ||
|
||
it('skips optimization of components', async () => { | ||
const jsx = await compile(`\ | ||
import Comp from './Comp.jsx'; | ||
|
||
# hello | ||
|
||
This is a <Comp /> | ||
`); | ||
assert.equal( | ||
jsx, | ||
`\ | ||
<><Fragment set:html="<h1>hello</h1> | ||
" /><_components.p>{"This is a "}<Comp /></_components.p></>` | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The previous access was incorrect and never actually worked 馃槄 I should look into properly typing this in the future.