Repository navigation
feat(plugins): add pierre-highlights plugin
#612
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d9fa946
feat(plugins): add `pierre-highlights` plugin
atinux 6bb050f
fix(plugins): harden `pierre-highlights` against review findings
atinux 067d6bc
perf(plugins): reuse `pierre-highlights` output for unchanged streami…
atinux c369ccd
fix(plugins): cache `pierre-highlights` tokens, not output nodes
atinux c3c2670
test(plugins): assert a single tokenizer call across streaming updates
atinux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| --- | ||
| title: Pierre (Highlight) | ||
| description: Fast syntax highlighting for code blocks and language-tagged inline code using @pierre/highlights. | ||
| seo: | ||
| title: Syntax Highlighting using Pierre Highlights | ||
| navigation: | ||
| icon: i-lucide-cpu | ||
| links: | ||
| - label: Shiki | ||
| icon: i-lucide-code | ||
| to: /plugins/built-in/shiki | ||
| color: neutral | ||
| variant: soft | ||
| - label: Rangi | ||
| icon: i-lucide-zap | ||
| to: /plugins/built-in/rangi | ||
| color: neutral | ||
| variant: soft | ||
| - label: '@pierre/highlights' | ||
| icon: i-simple-icons-github | ||
| to: https://github.com/pierrecomputer/pierre/tree/main/packages/highlights | ||
| color: neutral | ||
| variant: soft | ||
| --- | ||
|
|
||
| The `comark/plugins/pierre-highlights` plugin highlights code blocks with [`@pierre/highlights`](https://diffs.com/highlights), a synchronous highlighter written in WebAssembly. It ships 152 language lexers in one wasm module and renders the same tree as the [Shiki plugin](/plugins/built-in/shiki): `<span class="line">` lines, inline colors, and `--shiki-dark` variables, so existing Shiki styles keep working. | ||
|
|
||
| `@pierre/highlights` is a peer dependency: | ||
|
|
||
| ```bash [terminal] | ||
| npm install @pierre/highlights | ||
| ``` | ||
|
|
||
| ::warning | ||
| `@pierre/highlights` is in beta. Its colors follow Shiki's themes but its lexers are coarser than TextMate grammars, so some tokens get a different color than with Shiki. | ||
| :: | ||
|
|
||
| ::tip | ||
| Pick Highlights for server rendering and streaming, where its speed and quick startup matter most. Use [Shiki](/plugins/built-in/shiki) for transformers, custom grammars, or Comark syntax colors in `comark` fences, and [Rangi](/plugins/built-in/rangi) for the smallest browser bundle. | ||
| :: | ||
|
|
||
| ## Usage | ||
|
|
||
| ```typescript | ||
| import { parseMarkdown } from 'comark' | ||
| import pierreHighlights from 'comark/plugins/pierre-highlights' | ||
|
|
||
| const result = await parseMarkdown(content, { | ||
| plugins: [pierreHighlights()] | ||
| }) | ||
| ``` | ||
|
|
||
| With framework components: | ||
|
|
||
| ::code-group | ||
|
|
||
| ```vue [Vue] | ||
| <script setup lang="ts"> | ||
| import { Markdown } from '@comark/vue' | ||
| import pierreHighlights from '@comark/vue/plugins/pierre-highlights' | ||
| </script> | ||
|
|
||
| <template> | ||
| <Suspense> | ||
| <Markdown :plugins="[pierreHighlights()]">{{ content }}</Markdown> | ||
| </Suspense> | ||
| </template> | ||
| ``` | ||
|
|
||
| ```tsx [React] | ||
| import { Markdown } from '@comark/react' | ||
| import pierreHighlights from '@comark/react/plugins/pierre-highlights' | ||
|
|
||
| <Markdown plugins={[pierreHighlights()]}> | ||
| {content} | ||
| </Markdown> | ||
| ``` | ||
|
|
||
| ```svelte [Svelte] | ||
| <script> | ||
| import { Markdown } from '@comark/svelte' | ||
| import pierreHighlights from '@comark/svelte/plugins/pierre-highlights' | ||
| let { content } = $props() | ||
| </script> | ||
|
|
||
| <Markdown value={content} plugins={[pierreHighlights()]} /> | ||
| ``` | ||
|
|
||
| :: | ||
|
|
||
| --- | ||
|
|
||
| ## Performance | ||
|
|
||
| Highlighting time added to a parse, measured with Node 26 on the same documents. The first row is the [`codeMarkdown`](https://github.com/comarkdown/comark/blob/main/benchmarks/fixtures.ts) fixture used by `benchmarks/plugins.bench.ts`. | ||
|
|
||
| | Document | Shiki | Rangi | Highlights | | ||
| |---|---|---|---| | ||
| | 4 short fences | 4.0 ms | 0.30 ms | 0.11 ms | | ||
| | One 564-line TypeScript file | 288 ms | 9.3 ms | 2.0 ms | | ||
| | 16,000 lines of TypeScript (530 KiB) | 7.2 s | 267 ms | 83 ms | | ||
|
|
||
| Startup is close to Rangi's (about 20 ms to the first highlighted parse, against 700 ms for Shiki). The trade-off is size: the wasm module is inlined in the JavaScript, adding about 185 KiB gzipped to a browser bundle, against about 14 KiB for Rangi. | ||
|
|
||
| --- | ||
|
|
||
| ## Features | ||
|
|
||
| ### Themes | ||
|
|
||
| Pass themes from `@pierre/highlights/themes`. It includes Shiki's community themes, converted to the [Zed](https://zed.dev) theme format, plus Pierre's own. The defaults are Material Theme Lighter and Material Theme Palenight, the same as the Shiki plugin: | ||
|
|
||
| ```typescript | ||
| import githubLight from '@pierre/highlights/themes/github-light' | ||
| import githubDark from '@pierre/highlights/themes/github-dark' | ||
|
|
||
| // light/dark pair: light colors inline, dark colors as --shiki-dark variables | ||
| pierreHighlights({ themes: { light: githubLight, dark: githubDark } }) | ||
|
|
||
| // a single theme | ||
| pierreHighlights({ themes: { dark: githubDark } }) | ||
| ``` | ||
|
|
||
| Import each theme from its own path. The `@pierre/highlights/themes` entry bundles every theme. | ||
|
|
||
| ### Languages | ||
|
|
||
| Fence languages and their aliases are passed to Highlights (`ts`, `typescript`, `sh`, `yml`, …). `comark` fences use its `mdc` lexer, which is Markdown without Comark's component syntax, and `json-render` and `yaml-render` fences use JSON and YAML. A fence with an unknown language keeps its text unhighlighted under the `shiki pierre-highlights` class. Custom grammars are not supported. | ||
|
|
||
| ### Inline code | ||
|
|
||
| Inline code is highlighted when it declares a language with the attributes syntax. `lang` wins over `language`: | ||
|
|
||
| ```markdown | ||
| The type is `Ref<HTMLInputElement | null>`{lang="ts-type"} and the component is `<UButton />`{lang="vue-html"}. | ||
| ``` | ||
|
|
||
| `ts-type` is highlighted as a TypeScript type and `vue-html` as Vue template markup. Inline code naming a language Highlights does not know is left as written, so `` `Bonjour`{lang="fr"} `` stays plain text. Set `inlineCode: false` to turn this off. | ||
|
|
||
| ### Line highlighting | ||
|
|
||
| Fence info `{2-3,5}` marks the selected lines with the `.highlight` class, the same as the Shiki plugin. | ||
|
|
||
| --- | ||
|
|
||
| ## Options | ||
|
|
||
| | Option | Type | Default | Description | | ||
| |---|---|---|---| | ||
| | `themes` | `{ light?: Theme, dark?: Theme }` | Material Lighter / Palenight | Themes from `@pierre/highlights/themes` | | ||
| | `preStyles` | `boolean` | `false` | Add the theme's background and foreground colors to `<pre>` | | ||
| | `inlineCode` | `boolean` | `true` | Highlight inline code that declares a language | | ||
|
|
||
| --- | ||
|
|
||
| ## Styling | ||
|
|
||
| Code blocks and inline code get the `shiki pierre-highlights` class, so the dark-mode CSS from the [Shiki plugin](/plugins/built-in/shiki) applies unchanged. It switches to the `--shiki-dark*` variables, which are only emitted with a light and a dark theme; with a single theme the inline colors need no extra CSS: | ||
|
|
||
| ```css | ||
| html.dark .shiki span { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| color: var(--shiki-dark) !important; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| background-color: var(--shiki-dark-bg) !important; | ||
| font-style: var(--shiki-dark-font-style) !important; | ||
| font-weight: var(--shiki-dark-font-weight) !important; | ||
| text-decoration: var(--shiki-dark-text-decoration) !important; | ||
| } | ||
|
|
||
| /* With preStyles: true */ | ||
| html.dark pre.shiki { | ||
| background-color: var(--shiki-dark-bg) !important; | ||
| color: var(--shiki-dark) !important; | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Discard stale preview results when the Pierre import finishes.
If the first Pierre import is slow, a user can disable Pierre or edit the Markdown and complete a newer parse first. When the older parse resumes, it can replace the current preview with output from the old input or plugin selection. The import rejection is caught; the problem is that neither preview writer checks whether its request is still current. Use a shared request ID for preview and generation parses. Invalidate pending requests when the editor is cleared, and allow only the current request to update the document, error, loading state, and statistics.
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.
Written by an AI agent on behalf of @atinux.
Skipped in this PR. The same race already exists when the Shiki plugin loads its highlighter on first use, so a request-ID guard in
updatePreviewwould be a separate Playground change.