Repository navigation
[lexical-code-highlights][lexical-code-core][lexical-code-prism][lexical-code-shiki][lexical-playground] Feature: Add @lexical/code-highlights - #76
Draft
etrepum wants to merge 8 commits into
Conversation
…cal-code-shiki][lexical-playground] Feature: Add @lexical/code-highlights, a code highlighter using Highlights ## Description Lexical has code highlighter extensions for Prism and Shiki. This adds `@lexical/code-highlights`, which highlights code blocks with [Highlights](https://www.npmjs.com/package/@pierre/highlights) (`@pierre/highlights`), a synchronous WebAssembly highlighter with built-in lexers and Zed-compatible themes. Its API mirrors `@lexical/code-shiki` (`CodeHighlightsExtension`, `HighlightsTokenizer`, `registerCodeHighlighting`, language/theme option and normalization helpers), and its language ids follow Shiki's so code blocks keep their language when an editor switches engines. Every language is built in, so only the theme loads lazily (from `@pierre/highlights/themes/loader`). The transform, selection-retention and diffing code that Prism and Shiki each carried a copy of moves to `registerCodeHighlighter` (`@internal`) in `@lexical/code-core`, which all three packages now use; each keeps only its engine-specific `$prepare`/`$tokenize`. Prism now also compares the style of highlight nodes, as Shiki already did, so inline colors left by another highlighter are replaced. The playground gets a "Use Highlights for Code Highlighting" setting, and its toolbar picks language/theme options from a per-engine table. ## Test plan ### Before No Highlights integration. ### After `vitest run --project unit packages/lexical-code-highlights packages/lexical-code-prism packages/lexical-code-shiki packages/lexical-code-core packages/lexical-code` passes; new tests in `packages/lexical-code-highlights/src/__tests__/unit/CodeHighlights.test.ts` cover rendered colors, history merging of the theme load, `diff-xxx`, unsupported languages and themes, `defaultLanguage: null`, the `disabled` signal and the language/theme tables. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Deploying lexical with
|
| Latest commit: |
273e84a
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://baef8c7e.lexical-a1o.pages.dev |
| Branch Preview URL: | https://claude-project-thread-vsfuj3.lexical-a1o.pages.dev |
…cal-code-shiki][lexical-playground] Feature: Address self-review of the Highlights highlighter ## Description - Highlights dedupes theme loads and treats a failed theme import as loaded without colors, so a code block is split uncolored instead of waiting forever. - Code in a language Highlights has no lexer for is colored as plain text with the theme instead of left uncolored. - The token-to-node mapping with `diff-xxx` prefixes that Shiki and Highlights shared by copy moves to `$createCodeHighlightNodesFromTokens` (`@internal`) in `@lexical/code-core`. - Prism passes `keepStyle: true` to `registerCodeHighlighter`, keeping its previous behavior of leaving the style of highlight nodes alone. - The playground's Shiki and Highlights switches turn each other off, and the toolbar normalizes the language once. ## Test plan ### After `vitest run --project unit packages/lexical-code-highlights packages/lexical-code-prism packages/lexical-code-shiki packages/lexical-code-core`, with a new test for a failing theme load. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…HighlightNodesFromTokens declaration ## Description Flow rejects `T: Bound`, the `+` variance sigil and `$ReadOnlyArray` as deprecated; use `T extends`, `readonly` and `ReadonlyArray`. ## Test plan ### After `node scripts/check-flow-types.mjs`: Found 0 errors Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…t use and fall back when it can't compile ## Description `@pierre/highlights` compiles its WebAssembly when its module is evaluated, and `@lexical/code-highlights` imported it statically. Any app that bundled the package, including the playground with Highlights disabled and Prism selected, compiled the engine at startup, and where compiling fails (a CSP without `'wasm-unsafe-eval'`, or no WebAssembly SIMD before Chrome 91, Firefox 89 and Safari 16.4) the whole app failed to load. The engine is now imported on first use, like the themes, through `loadCodeLanguage` (which mirrors `@lexical/code-shiki`'s). If importing it fails, code blocks are split but left uncolored and `isSyntaxHighlightSupported` stays false. The language table is now typed with Highlights' `Lang`, and the README states the browser and CSP requirements. ## Test plan ### Before With `script-src 'self' 'unsafe-inline'`, the playground rendered no editor and threw an uncaught WebAssembly compile error, even in Prism mode. ### After Same CSP in Chromium: Prism mode has one editor and no page errors; Highlights mode has one editor, uncolored code and no page errors. Without the CSP, Highlights colors code as before. New `CodeHighlightsEngineFailure.test.ts` covers a failing engine import. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
🚀 Deploying Preview to Cloudflare 🚀Preview URL: https://claude-project-thread-vsfuj3-lexical-playground.mochimedia.workers.dev (commit 273e84a)This URL reflects your latest Preview deploymentPreview Deployments by commit
|
… and theme option helpers in the README ## Description The `@lexical/code-highlights` README didn't mention `getCodeLanguageOptions()` or `getCodeThemeOptions()`, so readers building a language or theme picker had to find them in the API docs. The README now names both, and the intro paragraph is rewrapped. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…et in place
## Description
The Highlights tests didn't cover caret retention, which `registerCodeHighlighter` handles for every engine. This adds a test that puts the caret mid-token in a JavaScript code block, lets highlighting split the text into several nodes, and checks that typing lands at the same text offset.
## Test plan
### After
`pnpm vitest run --project unit packages/lexical-code-highlights`
```
Test Files 3 passed (3)
Tests 13 passed (13)
```
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…ecolors it
## Description
No Highlights test changed the theme of a code block that was already highlighted. This one switches a block from `one-light` to `github-dark`, waits for the new theme to load, and checks that the block is recolored and its text is unchanged.
## Test plan
### After
`pnpm vitest run --project unit packages/lexical-code-highlights`
```
Test Files 3 passed (3)
Tests 14 passed (14)
```
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
## Description
`loadCodeTheme` keeps a theme's pending load in its map so concurrent callers wait on it. No test exercised that path. This adds one: it starts two loads of `nord` before either settles and checks that both resolve with the theme loaded, and that a later call is a no-op.
## Test plan
### After
`pnpm vitest run --project unit packages/lexical-code-highlights`
```
Test Files 3 passed (3)
Tests 15 passed (15)
```
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Requested by Bob · project thread
Draft on the fork to run CI before the upstream PR.
Description
Adds
@lexical/code-highlights, a code highlighter extension built on Highlights (@pierre/highlights), with the same API shape as@lexical/code-shiki. The transform/selection/diff machinery that Prism and Shiki each copied moves toregisterCodeHighlighter(@internal) in@lexical/code-core, and all three packages use it. The playground gets a "Use Highlights for Code Highlighting" setting.Generated by Claude Code