[docs-infra] Extend Code Highlighting Syntax#1307
Conversation
Deploy preview
Bundle size
PerformanceTotal duration: 16.10 ms +0.37 ms(+2.3%) | Renders: 4 (+0) | Paint: 70.20 ms +2.59 ms(+3.8%)
Check out the code infra dashboard for more information about this PR. |
There was a problem hiding this comment.
Pull request overview
Extends the docs-infra code highlighting pipeline by post-processing starry-night HAST output to add fine-grained di-* token classes (while preserving existing pl-* classes), enabling more precise styling for numbers, booleans, nullish values, JSX tags/components, and CSS constructs.
Changes:
- Add
extendSyntaxTokens+ sharedlanguageCapabilitiesto enrich highlighted HAST with additivedi-*classes, applied in both server (parseSource) and client (CodeProvider) highlighting paths. - Update inline-code enhancement to wrap HTML/JSX tag brackets with
di-ht/di-jt, and add TS-only built-in type enhancement for inline code. - Fix CSS
@importparsing to treat scoped package imports as externals; centralize demo theme + token styling intosyntax.cssand update docs/demos accordingly.
Reviewed changes
Copilot reviewed 32 out of 32 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/docs-infra/src/pipeline/transformHtmlCodeInline/transformHtmlCodeInline.ts | Runs extendSyntaxTokens for inline highlighted code trees. |
| packages/docs-infra/src/pipeline/parseSource/parseSource.ts | Runs extendSyntaxTokens during block highlighting before gutters. |
| packages/docs-infra/src/pipeline/parseSource/languageCapabilities.ts | New shared capability model derived from grammar scope. |
| packages/docs-infra/src/pipeline/parseSource/extendSyntaxTokens.ts | New HAST post-processor adding additive di-* classes. |
| packages/docs-infra/src/pipeline/parseSource/extendSyntaxTokens.test.ts | Extensive unit coverage for extendSyntaxTokens behaviors. |
| packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.ts | Treats scoped @scope/... CSS imports as externals. |
| packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.test.ts | Adds assertions for scoped CSS import handling. |
| packages/docs-infra/src/pipeline/loadServerTypes/snapshots/highlightTypes.test.ts.snap | Snapshot updates reflecting new di-* classes. |
| packages/docs-infra/src/pipeline/enhanceCodeTypes/getLanguageCapabilities.ts | Reuses shared LanguageCapabilities type. |
| packages/docs-infra/src/pipeline/enhanceCodeInline/enhanceCodeInline.ts | Adds di-ht/di-jt wrappers and TS built-in type enhancement. |
| packages/docs-infra/src/pipeline/enhanceCodeInline/enhanceCodeInline.test.ts | Updates expectations for wrapper spans + built-in type enhancement. |
| packages/docs-infra/src/pipeline/enhanceCodeEmphasis/enhanceCodeEmphasis.test.ts | Snapshot updates reflecting new di-* classes in emphasized output. |
| packages/docs-infra/src/CodeProvider/CodeProvider.tsx | Applies extendSyntaxTokens in client-side highlighting path. |
| docs/components/DemoPerformanceContentLoading/DemoPerformanceContentLoading.tsx | Switches demo styling import to centralized syntax.css. |
| docs/components/DemoPerformanceContent/DemoPerformanceContent.tsx | Switches demo styling import to centralized syntax.css. |
| docs/app/global.css | Removes inline .pl-smi override from global stylesheet. |
| docs/app/docs-infra/pipeline/parse-source/page.mdx | Documents di-* classes and updates sample language block. |
| docs/app/docs-infra/pipeline/page.mdx | Updates pipeline docs to reflect new inline enhancements + token classes. |
| docs/app/docs-infra/pipeline/enhance-code-inline/page.mdx | Updates docs for di-ht/di-jt wrappers + TS built-in type enhancement. |
| docs/app/docs-infra/pipeline/enhance-code-emphasis/demos/IndentContent.tsx | Uses centralized syntax.css for demo styling. |
| docs/app/docs-infra/pipeline/enhance-code-emphasis/demos/CollapsibleContent.tsx | Uses centralized syntax.css for demo styling. |
| docs/app/docs-infra/components/code-highlighter/demos/syntax.css | New centralized theme import + di-* styling variables/rules. |
| docs/app/docs-infra/components/code-highlighter/demos/demo-fallback/DemoContentLoading.tsx | Uses centralized syntax.css. |
| docs/app/docs-infra/components/code-highlighter/demos/demo-fallback-all-variants/DemoContentLoading.tsx | Uses centralized syntax.css. |
| docs/app/docs-infra/components/code-highlighter/demos/demo-fallback-all-files/DemoContentLoading.tsx | Uses centralized syntax.css. |
| docs/app/docs-infra/components/code-highlighter/demos/DemoContent.tsx | Uses centralized syntax.css. |
| docs/app/docs-infra/components/code-highlighter/demos/CodeContent.tsx | Uses centralized syntax.css. |
| docs/app/docs-infra/components/code-controller-context/demos/multi-file/MultiFileContent.tsx | Uses centralized syntax.css. |
| docs/app/docs-infra/components/code-controller-context/demos/demo-live/DemoLiveContent.tsx | Uses centralized syntax.css. |
| docs/app/docs-infra/components/code-controller-context/demos/code-editor/CodeEditorContent.tsx | Uses centralized syntax.css. |
| docs/app/bench/docs-infra/components/code-highlighter/demos/CodeContentLoading.tsx | Uses centralized syntax.css. |
| docs/app/bench/docs-infra/components/code-highlighter/demos/CodeContent.tsx | Uses centralized syntax.css. |
There was a problem hiding this comment.
Is starry-night only used to do the syntax highlighting? With the amount of code/complexity introduced to post-process it, almost feels like we could write our own HAST generator from a token stream. e.g. create a hastHighlighter for lezer or similar.
Co-authored-by: Copilot <copilot@github.com>
Yes, but it uses the same regex that VSCode uses. Our extension leans on existing HAST annotations that it outputs, checks their contents or siblings, and adds more detail. It doesn't need to read the entire source text again and only makes a single pass over the HAST. It's essentially a rehype plugin, the same API users of docs-infra would have to extend highlighting
It seems tempting, but I think starry-night has a solid core, written by the maintainer of HAST and MDX. It would be interesting to contribute upstream to it to give us a more performant entrypoint for our extensions |
Summary
Adds
extendSyntaxTokens, a post-processor that enriches starry-night HAST output with fine-graineddi-*CSS classes for styling individual token categories (numbers, booleans, JSX components, CSS properties, etc.) without fighting starry-night's coarsepl-*classes.All
di-*classes are added additively — existingpl-*classes are never removed — so the base theme still works and consumers can progressively override specific tokens.Preview Deploy
Downstream Base UI PR
New
di-*classesdi-numdi-booldi-nnull,undefined,"")di-thisthis/superdi-btstring,number, etc.)di-jsxdi-htdi-jtdi-akdi-aedi-avdi-dadi-cpdi-cvChanges
extendSyntaxTokens(new): Single-pass HAST walker that addsdi-*classes based on grammar scope. Language capabilities extracted into sharedlanguageCapabilities.ts.enhanceCodeInline: Extended with tag bracket wrapping (di-ht/di-jt), token reclassification (pl-en "function"→pl-k), and built-in type enhancement for inline code (TypeScript only, using sharedgetLanguageCapabilities).parseImportsAndComments: Fixed scoped package parsing (@wooorm/starry-night/style/lightwas not recognized).parse-source/page.mdxandenhance-code-inline/page.mdx.syntax.css.Breaking
.di-htand.di-jtclasses need to be added to properly style<div>and<Box>inline