You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 95b8cdf
Browse filesBrowse the repository at this point in the historyBrowse files
The Shiki plugin registered every bundled language (~250 grammars) in
each thread that highlighted code, which cost ~2s and ~100MB per thread
and made every highlight several times slower, as each code block was
matched against grammars it never uses. Importing it also imported all
of them, through `@node-core/rehype-shiki`'s `LANGS` and its plugin,
on every thread loading `jsx-ast`, the main thread included.
The highlighter now registers a bundled language the first time code in
it is highlighted, along with the bundled languages a configured one
embeds, and lists the bundled ones from their metadata alone, without
importing `LANGS`. The themes are given to Shiki by name, which it keeps
parsed instead of parsing them for every highlight. Importing
`@node-core/rehype-shiki`'s plugin still imports every grammar until
nodejs/nodejs.org#9212 is released.
The grammars now come from doc-kit's own `shiki` dependency (4.4.3)
rather than the copy `@node-core/rehype-shiki` pins (4.3.1). Its C++
grammar highlights types and template arguments differently, which
shows on the Node.js docs' C++ examples.
Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
* @typedef {Pick<import('shiki').LanguageRegistration, 'name' | 'displayName' | 'aliases'>} Language
20
+
*/
21
+
12
22
/**
13
23
* A syntax highlighter, creating its Shiki instance on first use.
14
24
*
@@ -17,7 +27,7 @@ import { getMarkdownPlugins } from '#utils/markdown/plugins.mjs';
17
27
* @property {(languageId?: string) => string} resolveLanguage - Resolves a language, falling back to plain text for unknown ones
18
28
* @property {(code: string, lang: string, meta?: Record<string, unknown>) => string} highlightToHtml - Highlights code, returning the inner HTML of its `<code>` element
19
29
* @property {(code: string, lang: string, meta?: Record<string, unknown>) => ReturnType<import('shiki').HighlighterCore['codeToHast']>} highlightToHast - Highlights code, returning a HAST tree
20
-
* @property {Array<import('shiki').LanguageRegistration>} langs - The languages it highlights
30
+
* @property {Array<Language>} langs - The languages it highlights
21
31
*/
22
32
23
33
/**
@@ -53,6 +63,54 @@ let engine;
53
63
// The highlighters of the options given, by their JSON
54
64
consthighlighters=newMap();
55
65
66
+
// What code in an unknown language is highlighted as
67
+
constFALLBACK_LANGUAGE='text';
68
+
69
+
// The languages Shiki bundles. Registering a grammar takes time and memory,
70
+
// and slows down every highlight after it, so each one is only imported and
71
+
// registered once code in it is highlighted (see `resolveLanguage`).
0 commit comments