Skip to content

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AugustinMauroy
Copy link
Member

@AugustinMauroy AugustinMauroy commented Jun 1, 2025

Description

It's add some logic to merge or not two code snippets

adding concurency take 110ms to 90ms

Validation

test should pass

Related Issues

close #7803

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run pnpm format to ensure the code follows the style guide.
  • I have run pnpm test to check if all tests are passing.
  • I have run pnpm build to check if the website builds without errors.
  • I've covered new added functionality with unit tests if necessary.

@Copilot Copilot AI review requested due to automatic review settings June 1, 2025 14:31
@AugustinMauroy AugustinMauroy requested a review from a team as a code owner June 1, 2025 14:31
Copy link

vercel bot commented Jun 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nodejs-org ✅ Ready (Inspect) Visit Preview Jun 1, 2025 9:11pm

Copy link
Contributor

@Copilot Copilot AI left a 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.

languages: actualLanguagesArray.join('|'),
},
children: codeBlocksToGroup.map(block =>
JSON.parse(JSON.stringify(block))
Copy link
Preview

Copilot AI Jun 1, 2025

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.

Copy link

codecov bot commented Jun 1, 2025

❌ 5 Tests Failed:

Tests completed Failed Passed Skipped
12 5 7 0
View the top 3 failed test(s) by shortest run time
test::should create CodeTabs for a sequence n snippet of text code blocks
Stack Traces | 0.00356s run time
Error [ERR_TEST_FAILURE]: Should create a single CodeTabs with all text code blocks
+ actual - expected

  {
    children: [
      {
+       data: {
+         meta: 'displayName="Text 1"'
+       },
+       properties: {
+         className: [
+           'language-text'
+         ]
+       },
+       tagName: 'code'
-       children: [
-         {
-           data: {
-             meta: 'displayName="Text 1"'
-           },
-           properties: {
-             className: [
-               'language-text'
-             ]
-           },
-           tagName: 'code'
-         }
-       ],
-       tagName: 'pre'
      },
-     {
-       children: [
-         {
-           data: {
-             meta: 'displayName="Text 2"'
-           },
-           properties: {
-             className: [
-               'language-text'
-             ]
-           },
-           tagName: 'code'
-         }
-       ],
-       tagName: 'pre'
-     },
-     {
-       children: [
-         {
-           data: {
-             meta: 'displayName="Text 3"'
-           },
-           properties: {
-             className: [
-               'language-text'
-             ]
-           },
-           tagName: 'code'
-         }
-       ],
-       tagName: 'pre'
-     }
    ],
+   tagName: 'pre'
-   properties: {
-     displayNames: 'Text 1|Text 2|Text 3',
-     languages: 'text|text|text'
-   },
-   tagName: 'CodeTabs',
-   type: 'element'
  }

    at new Promise (<anonymous>)
    at Array.map (<anonymous>) {
  code: 'ERR_TEST_FAILURE',
  failureType: 'testCodeFailure',
  cause: AssertionError [ERR_ASSERTION]: Should create a single CodeTabs with all text code blocks
  + actual - expected
  
    {
      children: [
        {
  +       data: {
  +         meta: 'displayName="Text 1"'
  +       },
  +       properties: {
  +         className: [
  +           'language-text'
  +         ]
  +       },
  +       tagName: 'code'
  -       children: [
  -         {
  -           data: {
  -             meta: 'displayName="Text 1"'
  -           },
  -           properties: {
  -             className: [
  -               'language-text'
  -             ]
  -           },
  -           tagName: 'code'
  -         }
  -       ],
  -       tagName: 'pre'
        },
  -     {
  -       children: [
  -         {
  -           data: {
  -             meta: 'displayName="Text 2"'
  -           },
  -           properties: {
  -             className: [
  -               'language-text'
  -             ]
  -           },
  -           tagName: 'code'
  -         }
  -       ],
  -       tagName: 'pre'
  -     },
  -     {
  -       children: [
  -         {
  -           data: {
  -             meta: 'displayName="Text 3"'
  -           },
  -           properties: {
  -             className: [
  -               'language-text'
  -             ]
  -           },
  -           tagName: 'code'
  -         }
  -       ],
  -       tagName: 'pre'
  -     }
      ],
  +   tagName: 'pre'
  -   properties: {
  -     displayNames: 'Text 1|Text 2|Text 3',
  -     languages: 'text|text|text'
  -   },
  -   tagName: 'CodeTabs',
  -   type: 'element'
    }
  
      at TestContext.<anonymous> (file:.../runner/work/nodejs.org/nodejs..../src/__tests__/plugin.test.mjs:767:12)
      at Test.runInAsyncScope (node:async_hooks:214:14)
      at Test.run (node:internal/test_runner/test:1047:25)
      at Test.start (node:internal/test_runner/test:944:17)
      at node:internal/test_runner/test:1440:71
      at node:internal/per_context/primordials:483:82
      at new Promise (<anonymous>)
      at new SafePromise (node:internal/per_context/primordials:451:29)
      at node:internal/per_context/primordials:483:9
      at Array.map (<anonymous>) {
    generatedMessage: false,
    code: 'ERR_ASSERTION',
    actual: { tagName: 'pre', children: [Array] },
    expected: { tagName: 'CodeTabs', type: 'element', properties: [Object], children: [Array] },
    operator: 'deepStrictEqual'
  }
}
test::should create CodeTabs for a sequence n snippet of ESM/CJS code blocks
Stack Traces | 0.00619s run time
Error [ERR_TEST_FAILURE]: Should create a single CodeTabs with all ESM code blocks
+ actual - expected

  {
    children: [
      {
+       data: {
+         meta: 'displayName="ESM"'
+       },
+       properties: {
+         className: [
+           'language-esm'
+         ]
+       },
+       tagName: 'code'
-       children: [
-         {
-           data: {
-             meta: 'displayName="ESM"'
-           },
-           properties: {
-             className: [
-               'language-esm'
-             ]
-           },
-           tagName: 'code'
-         }
-       ],
-       tagName: 'pre'
      },
-     {
-       children: [
-         {
-           data: {
-             meta: 'displayName="ESM"'
-           },
-           properties: {
-             className: [
-               'language-esm'
-             ]
-           },
-           tagName: 'code'
-         }
-       ],
-       tagName: 'pre'
-     },
-     {
-       children: [
-         {
-           data: {
-             meta: 'displayName="ESM"'
-           },
-           properties: {
-             className: [
-               'language-esm'
-             ]
-           },
-           tagName: 'code'
-         }
-       ],
-       tagName: 'pre'
-     },
-     {
-       children: [
-         {
-           data: {
-             meta: 'displayName="ESM"'
-           },
-           properties: {
-             className: [
-               'language-esm'
-             ]
-           },
-           tagName: 'code'
-         }
-       ],
-       tagName: 'pre'
-     }
    ],
+   tagName: 'pre'
-   properties: {
-     displayNames: 'ESM|ESM|ESM|ESM',
-     languages: 'esm|esm|esm|esm'
-   },
-   tagName: 'CodeTabs',
-   type: 'element'
  }

    at new Promise (<anonymous>)
    at Array.map (<anonymous>) {
  code: 'ERR_TEST_FAILURE',
  failureType: 'testCodeFailure',
  cause: AssertionError [ERR_ASSERTION]: Should create a single CodeTabs with all ESM code blocks
  + actual - expected
  
    {
      children: [
        {
  +       data: {
  +         meta: 'displayName="ESM"'
  +       },
  +       properties: {
  +         className: [
  +           'language-esm'
  +         ]
  +       },
  +       tagName: 'code'
  -       children: [
  -         {
  -           data: {
  -             meta: 'displayName="ESM"'
  -           },
  -           properties: {
  -             className: [
  -               'language-esm'
  -             ]
  -           },
  -           tagName: 'code'
  -         }
  -       ],
  -       tagName: 'pre'
        },
  -     {
  -       children: [
  -         {
  -           data: {
  -             meta: 'displayName="ESM"'
  -           },
  -           properties: {
  -             className: [
  -               'language-esm'
  -             ]
  -           },
  -           tagName: 'code'
  -         }
  -       ],
  -       tagName: 'pre'
  -     },
  -     {
  -       children: [
  -         {
  -           data: {
  -             meta: 'displayName="ESM"'
  -           },
  -           properties: {
  -             className: [
  -               'language-esm'
  -             ]
  -           },
  -           tagName: 'code'
  -         }
  -       ],
  -       tagName: 'pre'
  -     },
  -     {
  -       children: [
  -         {
  -           data: {
  -             meta: 'displayName="ESM"'
  -           },
  -           properties: {
  -             className: [
  -               'language-esm'
  -             ]
  -           },
  -           tagName: 'code'
  -         }
  -       ],
  -       tagName: 'pre'
  -     }
      ],
  +   tagName: 'pre'
  -   properties: {
  -     displayNames: 'ESM|ESM|ESM|ESM',
  -     languages: 'esm|esm|esm|esm'
  -   },
  -   tagName: 'CodeTabs',
  -   type: 'element'
    }
  
      at TestContext.<anonymous> (file:.../runner/work/nodejs.org/nodejs..../src/__tests__/plugin.test.mjs:667:12)
      at Test.runInAsyncScope (node:async_hooks:214:14)
      at Test.run (node:internal/test_runner/test:1047:25)
      at Test.start (node:internal/test_runner/test:944:17)
      at node:internal/test_runner/test:1440:71
      at node:internal/per_context/primordials:483:82
      at new Promise (<anonymous>)
      at new SafePromise (node:internal/per_context/primordials:451:29)
      at node:internal/per_context/primordials:483:9
      at Array.map (<anonymous>) {
    generatedMessage: false,
    code: 'ERR_ASSERTION',
    actual: { tagName: 'pre', children: [Array] },
    expected: { tagName: 'CodeTabs', type: 'element', properties: [Object], children: [Array] },
    operator: 'deepStrictEqual'
  }
}
test::If there are a sequence of codeblock of CJS/ESM, it should create pairs of CodeTabs
Stack Traces | 0.00868s run time
Error [ERR_TEST_FAILURE]: Should create two CodeTabs groups

4 !== 2

    at new Promise (<anonymous>)
    at Array.map (<anonymous>) {
  code: 'ERR_TEST_FAILURE',
  failureType: 'testCodeFailure',
  cause: AssertionError [ERR_ASSERTION]: Should create two CodeTabs groups
  
  4 !== 2
  
      at TestContext.<anonymous> (file:.../runner/work/nodejs.org/nodejs..../src/__tests__/plugin.test.mjs:290:12)
      at Test.runInAsyncScope (node:async_hooks:214:14)
      at Test.run (node:internal/test_runner/test:1047:25)
      at Test.start (node:internal/test_runner/test:944:17)
      at node:internal/test_runner/test:1440:71
      at node:internal/per_context/primordials:483:82
      at new Promise (<anonymous>)
      at new SafePromise (node:internal/per_context/primordials:451:29)
      at node:internal/per_context/primordials:483:9
      at Array.map (<anonymous>) {
    generatedMessage: false,
    code: 'ERR_ASSERTION',
    actual: 4,
    expected: 2,
    operator: 'strictEqual'
  }
}

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Co-authored-by: Copilot <[email protected]>
Signed-off-by: Augustin Mauroy <[email protected]>
Comment on lines 135 to 164
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'
);
Copy link
Member

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() },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
namedExports: { visit: mockVisit, SKIP: Symbol() },
namedExports: { visit: mockVisit },

If you aren't relying on SKIP anymore, no need to mock it

// First pass: Group adjacent code blocks into <CodeTabs>
const newChildren = [];
let i = 0;
while (i < tree.children.length) {
Copy link
Member

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?

Comment on lines 97 to 98
(lang2 === 'cjs' && lang3 === 'esm') ||
(lang2 === 'esm' && lang3 === 'cjs')
Copy link
Member

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?

Comment on lines 168 to 170
const lang = preElement.properties.className
?.find(cn => cn.startsWith(languagePrefix))
?.substring(languagePrefix.length);
Copy link
Member

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
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(rehype-shiki): sequential codeboxes are merged together
3 participants