Repository navigation
perf: highlight with fewer grammars by default #1235
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@doc-kit/core': minor | ||
| --- | ||
|
|
||
| Code is highlighted with fewer grammars by default, as each one takes its own memory on every thread: JavaScript (`js`, `mjs`, `cjs`, `javascript`) with TypeScript's grammar, which already highlights types, and C++ (`cpp`, `c++`) with C's. The Shiki plugin's `langAlias` option takes precedence over these defaults, so `{ cpp: 'cpp', 'c++': 'cpp' }` brings back C++'s own grammar, and aliases now keep working once a grammar with the same aliases is registered. A highlighter's `resolveLanguage` now returns the language code is highlighted as, as its Shiki instance names it (`typescript` for `mjs`): highlight with what it returns. Transformers see that language too. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 'use strict'; | ||
|
|
||
| // What code in an unknown language is highlighted as | ||
| export const FALLBACK_LANGUAGE = 'text'; | ||
|
|
||
| // What code in these languages is highlighted as, unless configured otherwise. | ||
| // Every grammar takes its own memory on each thread, and time to register, and | ||
| // TypeScript's highlights JavaScript (types are highlighted as TypeScript | ||
| // anyway), as C's does most of C++ | ||
| export const DEFAULT_ALIASES = new Map([ | ||
| ['javascript', 'typescript'], | ||
| ['js', 'typescript'], | ||
| ['mjs', 'typescript'], | ||
| ['cjs', 'typescript'], | ||
|
Comment on lines
+11
to
+14
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these being aliased, what's wrong with the JS type?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing wrong with it, it's just heavy. JavaScript's grammar is ~60MB on every thread, and TypeScript's highlights JS just fine (we already use it for types and signatures). Together with C++ going to C, that's ~190MB less peak memory on the Node.js docs and a 2-7% faster build. In the light theme JS looks the same, except for one console output block tagged as |
||
| ['cpp', 'c'], | ||
| ['c++', 'c'], | ||
| ]); | ||
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.
why is rust here? do we have this in the codebase? its not in the release note or PR
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.
It's just a stand-in tbh. The test checks that a bundled language gets registered the first time it's highlighted, through one of its own aliases. It used
mjsbefore, but that goes to TypeScript now, so I picked something nothing aliases (rs). Added a comment so the next person doesn't wonder too :)