fix(rehype-shiki): sequential codeboxes are merged together#7805
fix(rehype-shiki): sequential codeboxes are merged together#7805AugustinMauroy wants to merge 4 commits intomainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
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.
Consider using a dedicated deep clone utility instead of JSON.parse(JSON.stringify(...)) to improve performance and maintainability when cloning code blocks.
❌ 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 |
| 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.
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.
| 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.
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.
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); |
| }); | ||
| ``` | ||
|
|
||
| ```cjs |
There was a problem hiding this comment.
But that is intentional. Sequential codeboxes are supposed to be merged together afaik.
There was a problem hiding this comment.
take a look to the issue. In doc it's supposed to be separated
There was a problem hiding this comment.
Then let's follow it the way it is supposed to be.
|
@AugustinMauroy are you going to put some effort here? |
|
@ovflowd I'm waiting for consensus on #7805 (comment) |
Co-authored-by: Manish Kumar ⛄ <manishprivet@protonmail.com> Signed-off-by: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com>
|
okay let's me put some energy on that |
|
@AugustinMauroy Are you still working on this, or has it staled out? |
I tried a few things yesterday but I couldn't get anywhere, just endless loops... |
|
Perhaps open an help-wanted issue with more details on the issue? |
already exist #7803 |
Description
It's add some logic to merge or not two code snippets
adding
concurencytake110msto90msValidation
test should pass
Related Issues
close #7803
Check List
pnpm formatto ensure the code follows the style guide.pnpm testto check if all tests are passing.pnpm buildto check if the website builds without errors.