-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
fix(rehype-shiki): sequential codeboxes are merged together #7805
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
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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.
Pull Request Overview
This PR refactors the rehype-shiki plugin to merge adjacent code blocks (specifically CJS/ESM pairs) into grouped CodeTabs elements while updating the relevant tests.
- Refactored code block grouping logic by replacing the visit traversal with a while loop and newChildren array.
- Updated tests to verify the new grouping behavior and added concurrency options.
- Adjusted the highlighter tests to use the new concurrent test configuration.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
packages/rehype-shiki/src/plugin.mjs | Refactored merging of sequential code blocks into CodeTabs and updated syntax highlighting integration. |
packages/rehype-shiki/src/tests/plugin.test.mjs | Revised tests for the grouping functionality with updated expectations and concurrency settings. |
packages/rehype-shiki/src/tests/highlighter.test.mjs | Minor test update to include concurrency settings. |
packages/rehype-shiki/src/plugin.mjs
Outdated
languages: actualLanguagesArray.join('|'), | ||
}, | ||
children: codeBlocksToGroup.map(block => | ||
JSON.parse(JSON.stringify(block)) |
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.
Consider using a dedicated deep clone utility instead of JSON.parse(JSON.stringify(...)) to improve performance and maintainability when cloning code blocks.
Copilot uses AI. Check for mistakes.
❌ 5 Tests Failed:
View the top 3 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
Co-authored-by: Copilot <[email protected]> Signed-off-by: Augustin Mauroy <[email protected]>
const firstGroup = parent.children[0]; | ||
assert.strictEqual( | ||
firstGroup.tagName, | ||
'CodeTabs', | ||
'Group 1 should be CodeTabs' | ||
); | ||
assert.strictEqual( | ||
firstGroup.properties.languages, | ||
'cjs|esm', | ||
'Group 1 languages should be cjs|esm' | ||
); | ||
assert.strictEqual( | ||
firstGroup.properties.displayNames, | ||
'CJS|ESM', | ||
'Group 1 displayNames should be CJS|ESM' | ||
); | ||
assert.ok( | ||
Array.isArray(firstGroup.children) && firstGroup.children.length === 2, | ||
'Group 1 should contain 2 code blocks' | ||
); | ||
assert.strictEqual( | ||
firstGroup.children[0].children[0].properties.className[0], | ||
'language-cjs', | ||
'Group 1, Block 1 should be CJS' | ||
); | ||
assert.strictEqual( | ||
firstGroup.children[1].children[0].properties.className[0], | ||
'language-esm', | ||
'Group 1, Block 2 should be ESM' | ||
); |
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.
Can we use deepStrictEqual
or partialDeepStrictEqual
instead of comparing each with it's own assert?
@@ -19,18 +19,21 @@ mock.module('unist-util-visit', { | |||
namedExports: { visit: mockVisit, SKIP: Symbol() }, |
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.
namedExports: { visit: mockVisit, SKIP: Symbol() }, | |
namedExports: { visit: mockVisit }, |
If you aren't relying on SKIP
anymore, no need to mock it
packages/rehype-shiki/src/plugin.mjs
Outdated
// First pass: Group adjacent code blocks into <CodeTabs> | ||
const newChildren = []; | ||
let i = 0; | ||
while (i < tree.children.length) { |
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.
I'm not a huge fan that we are using a while
loop and a visit
, is there an alternative?
packages/rehype-shiki/src/plugin.mjs
Outdated
(lang2 === 'cjs' && lang3 === 'esm') || | ||
(lang2 === 'esm' && lang3 === 'cjs') |
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.
Maybe make a groupableLangs
constant?
packages/rehype-shiki/src/plugin.mjs
Outdated
const lang = preElement.properties.className | ||
?.find(cn => cn.startsWith(languagePrefix)) | ||
?.substring(languagePrefix.length); |
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.
getLanguage
?
@@ -44,6 +44,14 @@ server.listen(port, hostname, () => { | |||
}); | |||
``` | |||
|
|||
```cjs |
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.
But that is intentional. Sequential codeboxes are supposed to be merged together afaik.
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.
take a look to the issue. In doc it's supposed to be separated
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.
Maybe we can make this an opt-in thing, so we can opt-in on api-docs-tooling
?
Description
It's add some logic to merge or not two code snippets
adding
concurency
take110ms
to90ms
Validation
test should pass
Related Issues
close #7803
Check List
pnpm format
to ensure the code follows the style guide.pnpm test
to check if all tests are passing.pnpm build
to check if the website builds without errors.