Skip to content

Commit 1e96be7

Browse files
authored
chore: increase performance of legacy generators (#505)
1 parent c77ec50 commit 1e96be7

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

src/generators/legacy-html-all/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { join, resolve } from 'node:path';
55

66
import HTMLMinifier from '@minify-html/node';
77

8-
import { getRemarkRehype } from '../../utils/remark.mjs';
8+
import { getRemarkRehypeWithShiki } from '../../utils/remark.mjs';
99
import dropdowns from '../legacy-html/utils/buildDropdowns.mjs';
1010
import tableOfContents from '../legacy-html/utils/tableOfContents.mjs';
1111

@@ -49,7 +49,7 @@ export default {
4949
const inputWithoutIndex = input.filter(entry => entry.api !== 'index');
5050

5151
// Gets a Remark Processor that parses Markdown to minified HTML
52-
const remarkWithRehype = getRemarkRehype();
52+
const remarkWithRehype = getRemarkRehypeWithShiki();
5353

5454
// Current directory path relative to the `index.mjs` file
5555
// from the `legacy-html` generator, as all the assets are there

src/generators/legacy-html/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import dropdowns from './utils/buildDropdowns.mjs';
1010
import { safeCopy } from './utils/safeCopy.mjs';
1111
import tableOfContents from './utils/tableOfContents.mjs';
1212
import { groupNodesByModule } from '../../utils/generators.mjs';
13-
import { getRemarkRehype } from '../../utils/remark.mjs';
13+
import { getRemarkRehypeWithShiki } from '../../utils/remark.mjs';
1414

1515
/**
1616
* @typedef {{
@@ -53,7 +53,7 @@ export default {
5353
const generatedValues = [];
5454

5555
// Gets a Remark Processor that parses Markdown to minified HTML
56-
const remarkRehypeProcessor = getRemarkRehype();
56+
const remarkRehypeProcessor = getRemarkRehypeWithShiki();
5757

5858
const groupedModules = groupNodesByModule(input);
5959

src/generators/legacy-json/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default {
5353
const nodes = groupedModules.get(head.api);
5454

5555
const section = buildSection(head, nodes);
56+
5657
generatedValues.push(section);
5758

5859
return section;

src/utils/highlighter.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ function isCodeBlock(node) {
3232
);
3333
}
3434

35-
export const highlighter = await createHighlighter();
35+
export const highlighter = await createHighlighter({ wasm: true });
3636

3737
/**
3838
* Creates a HAST transformer for Shiki which is used for transforming our codeboxes
3939
*
40+
* @deprecated This is used only for the legacy-html generator, please use `@node-core/rehype-shiki` directly instead.
41+
*
4042
* @type {import('unified').Plugin}
4143
*/
4244
export default function rehypeShikiji() {

src/utils/remark.mjs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,24 @@ export const getRemark = () =>
2626

2727
/**
2828
* Retrieves an instance of Remark configured to output stringified HTML code
29-
* including parsing Code Boxes with syntax highlighting
3029
*/
3130
export const getRemarkRehype = () =>
31+
unified()
32+
.use(remarkParse)
33+
// We make Rehype ignore existing HTML nodes (just the node itself, not its children)
34+
// as these are nodes we manually created during the rehype process
35+
// We also allow dangerous HTML to be passed through, since we have HTML within our Markdown
36+
// and we trust the sources of the Markdown files
37+
.use(remarkRehype, { allowDangerousHtml: true, passThrough })
38+
// We allow dangerous HTML to be passed through, since we have HTML within our Markdown
39+
// and we trust the sources of the Markdown files
40+
.use(rehypeStringify, { allowDangerousHtml: true });
41+
42+
/**
43+
* Retrieves an instance of Remark configured to output stringified HTML code
44+
* including parsing Code Boxes with syntax highlighting
45+
*/
46+
export const getRemarkRehypeWithShiki = () =>
3247
unified()
3348
.use(remarkParse)
3449
// We make Rehype ignore existing HTML nodes (just the node itself, not its children)
@@ -38,8 +53,6 @@ export const getRemarkRehype = () =>
3853
.use(remarkRehype, { allowDangerousHtml: true, passThrough })
3954
// This is a custom ad-hoc within the Shiki Rehype plugin, used to highlight code
4055
// and transform them into HAST nodes
41-
// @TODO: Get rid of @shikijis/rehype and use our own Rehype plugin for Shiki
42-
// since we have CJS/ESM nodes. (Base off from the nodejs/nodejs.org repository)
4356
.use(syntaxHighlighter)
4457
// We allow dangerous HTML to be passed through, since we have HTML within our Markdown
4558
// and we trust the sources of the Markdown files
@@ -58,10 +71,7 @@ export const getRemarkRecma = () =>
5871
// as these are nodes we manually created during the generation process
5972
// We also allow dangerous HTML to be passed through, since we have HTML within our Markdown
6073
// and we trust the sources of the Markdown files
61-
.use(remarkRehype, {
62-
allowDangerousHtml: true,
63-
passThrough,
64-
})
74+
.use(remarkRehype, { allowDangerousHtml: true, passThrough })
6575
// Any `raw` HTML in the markdown must be converted to AST in order for Recma to understand it
6676
.use(rehypeRaw, { passThrough })
6777
.use(() => singletonShiki)

0 commit comments

Comments
 (0)