From f026a9da153589011b669806aa8f132e4e80d267 Mon Sep 17 00:00:00 2001 From: Kotataro taniguchi <119092698+kotarotaniguchi0523@users.noreply.github.com> Date: Mon, 16 Nov 2026 03:44:05 +0000 Subject: [PATCH] perf(md): replace Shiki with @pierre/highlights Highlighting was 91% of the time spent compiling Markdown. Code blocks are now highlighted by @pierre/highlights, a WebAssembly lexer, which makes the Markdown compile step about 15 times faster. The parser, the plugins, the rendered markup and the default themes stay as they are. - `src/highlight.ts` builds the lines from per-line tokens and applies the `{1,3-5}` line range and the `[!code ...]` comment notations with the same classes as before. - Token boundaries and classification differ from Shiki, so some colors change within the same themes. - `themes` replaces `shiki.themes`, which still works and is deprecated. - Focus dimming now applies: the CSS selected `has-focus`, but the class on the block is `has-focused`. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011f44HGUmdFJgasJzEr7dQk --- docs/guide/pages-routing/markdown.md | 20 +- packages/md/package.json | 4 +- packages/md/src/compile.ts | 67 ++---- packages/md/src/highlight.ts | 218 ++++++++++++++++++ packages/md/src/plugin.ts | 3 + packages/md/src/plugins/pre-wrapper.ts | 2 +- packages/md/src/theme/code.css | 8 +- .../md/test/integration/markdown-blog.test.ts | 2 +- packages/md/test/unit/compile-themes.test.ts | 36 +++ packages/md/test/unit/compile.test.ts | 9 +- packages/md/test/unit/highlight.test.ts | 168 ++++++++++++++ packages/md/test/unit/plugins.test.ts | 2 +- .../pages/posts/code-examples.md | 2 +- pnpm-lock.yaml | 23 +- 14 files changed, 476 insertions(+), 88 deletions(-) create mode 100644 packages/md/src/highlight.ts create mode 100644 packages/md/test/unit/compile-themes.test.ts create mode 100644 packages/md/test/unit/highlight.test.ts diff --git a/docs/guide/pages-routing/markdown.md b/docs/guide/pages-routing/markdown.md index de78078bc..4957130da 100644 --- a/docs/guide/pages-routing/markdown.md +++ b/docs/guide/pages-routing/markdown.md @@ -289,7 +289,7 @@ Wrap your markdown content in a `.void-md` element to scope the styles: The theme covers: - **Prose:** headings, paragraphs, lists, blockquotes, tables, inline code, links, horizontal rules, task lists, ``, ``, definition lists, and footnotes. Dark mode works through `prefers-color-scheme` and the `data-theme` attribute. -- **Code blocks:** Shiki dual-theme highlighting with CSS-driven switching and zero JS. +- **Code blocks:** dual-theme syntax highlighting with CSS-driven switching and zero JS. - **Containers:** styles for `:::tip`, `:::warning`, `:::danger`, `:::info`, and `:::details`. - **GitHub alerts:** support for `> [!NOTE]`, `> [!TIP]`, `> [!WARNING]`, and similar syntax. @@ -371,7 +371,7 @@ Custom titles work too: `::: tip Pro Tip`. ### Syntax Highlighting -Code blocks use [Shiki](https://shiki.style) with `github-light` and `github-dark` themes by default. +Code blocks are highlighted with the `github-light` and `github-dark` themes by default. A code block in a language that is not supported renders as plain text. ### Line Highlighting @@ -452,19 +452,19 @@ Headings include permalink anchors. Use `[[toc]]` for a table of contents. Image import { voidMarkdown } from '@void/md/plugin'; voidMarkdown({ - shiki: { - themes: { light: 'github-light', dark: 'github-dark' }, - }, + themes: { light: 'github-light', dark: 'github-dark' }, copyLabel: 'Copy Code', }); ``` -| Option | Type | Default | Description | -| -------------- | -------------------------------- | ------------------------------------------------ | ----------------------------------------------------- | -| `shiki.themes` | `{ light: string; dark: string}` | `{ light: "github-light", dark: "github-dark" }` | Shiki color themes | -| `copyLabel` | `string` | `"Copy Code"` | Tooltip and accessible label for the code copy button | +| Option | Type | Default | Description | +| ----------- | -------------------------------- | ------------------------------------------------ | ----------------------------------------------------------------- | +| `themes` | `{ light: string; dark: string}` | `{ light: "github-light", dark: "github-dark" }` | Code block color themes, such as `pierre-light` and `pierre-dark` | +| `copyLabel` | `string` | `"Copy Code"` | Tooltip and accessible label for the code copy button | -Code block languages load on demand from Shiki's bundled languages. +Code block languages need no configuration. An unknown theme name stops the build with an error. + +The `shiki.themes` option is deprecated. Use `themes`. For a translated site, set `copyLabel` to the label in your site's language, such as `'Copier le code'`. diff --git a/packages/md/package.json b/packages/md/package.json index 332c028d4..7ea849354 100644 --- a/packages/md/package.json +++ b/packages/md/package.json @@ -41,8 +41,7 @@ "typecheck": "tsc --noEmit --rootDir ../.." }, "dependencies": { - "@shikijs/engine-javascript": "catalog:", - "@shikijs/transformers": "catalog:", + "@pierre/highlights": "^0.5.1", "gray-matter": "^4.0.3", "markdown-exit": "catalog:", "markdown-it": "^15.0.3", @@ -52,7 +51,6 @@ "markdown-it-emoji": "^3.1.0", "markdown-it-footnote": "^4.0.0", "pathslash": "catalog:", - "shiki": "catalog:", "tinyglobby": "^0.2.17" }, "devDependencies": { diff --git a/packages/md/src/compile.ts b/packages/md/src/compile.ts index 48653d0c7..f0718f315 100644 --- a/packages/md/src/compile.ts +++ b/packages/md/src/compile.ts @@ -1,17 +1,9 @@ -import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript'; -import { - transformerMetaHighlight, - transformerNotationDiff, - transformerNotationHighlight, - transformerNotationFocus, - transformerNotationErrorLevel, -} from '@shikijs/transformers'; import MarkdownExit from 'markdown-exit'; import type { RuleCore, Token } from 'markdown-exit'; import attrs from 'markdown-it-attrs'; import { full as emoji } from 'markdown-it-emoji'; import footnote from 'markdown-it-footnote'; -import { createHighlighter, bundledLanguages, isSpecialLang, type BundledLanguage } from 'shiki'; +import { createCodeHighlighter } from './highlight.ts'; import { extractHeadings, parseFrontmatter } from './metadata.ts'; import { containerPlugin } from './plugins/containers.ts'; import { gitHubAlertsPlugin } from './plugins/github-alerts.ts'; @@ -39,12 +31,15 @@ export type CompileResult = Readonly<{ export type CompileOptions = Readonly<{ /** Title of the code block copy button. Defaults to `Copy Code`. */ copyLabel?: string; + /** Code block color themes. Defaults to `github-light` and `github-dark`. */ + themes?: { light: string; dark: string }; + /** @deprecated Use `themes`. Removed in the next major version. */ shiki?: { + /** @deprecated Use the top-level `themes` option. Removed in the next major version. */ themes?: { light: string; dark: string }; /** - * @deprecated Has no effect. Code block languages load on demand from - * Shiki's bundled languages. Remove this option. Removed in the next major - * version. + * @deprecated Has no effect. Every supported code block language is + * built in. Remove this option. Removed in the next major version. */ langs?: ReadonlyArray; }; @@ -63,48 +58,20 @@ async function getCompiler(options?: CompileOptions): Promise { async function createCompiler(options?: CompileOptions): Promise { const markdown = new MarkdownExit({ html: true, linkify: true }); - // Shiki highlighting with JS RegExp engine (no WASM) + lazy language loading - const themes = options?.shiki?.themes ?? { - light: 'github-light', - dark: 'github-dark', - }; - const highlighter = await createHighlighter({ - engine: createJavaScriptRegexEngine(), - themes: [themes.light, themes.dark], - langs: [], - }); - const shikiTransformers = [ - transformerMetaHighlight(), - transformerNotationDiff(), - transformerNotationHighlight(), - transformerNotationFocus(), - transformerNotationErrorLevel(), - ]; - markdown.renderer.rules.fence = async (tokens: ReadonlyArray, tokenIndex: number) => { + const highlight = await createCodeHighlighter( + options?.themes ?? options?.shiki?.themes ?? { light: 'github-light', dark: 'github-dark' }, + ); + markdown.renderer.rules.fence = (tokens: ReadonlyArray, tokenIndex: number) => { const token = tokens[tokenIndex]; - const code = token.content; - let lang = token.info ? token.info.split(/\s+/g)[0] : 'text'; - if (!isSpecialLang(lang) && !highlighter.getLoadedLanguages().includes(lang)) { - if (lang in bundledLanguages) { - await highlighter.loadLanguage(lang as BundledLanguage); - } else { - lang = 'text'; - } - } + const lang = token.info ? token.info.split(/\s+/g)[0] : 'text'; // `markdown-it-attrs` strips `{1,3-5}` line-highlight ranges from - // `token.info`; `stash_fence_info` keeps the original so - // `transformerMetaHighlight` still sees them. + // `token.info`; `stash_fence_info` keeps the original so the highlighter + // still sees them. const rawInfo = (token.meta as { rawInfo?: string } | null)?.rawInfo ?? token.info; - return highlighter.codeToHtml(code, { - lang, - themes, - defaultColor: 'light', - transformers: shikiTransformers, - meta: { __raw: rawInfo }, - }); + return highlight(token.content, lang, rawInfo); }; - // Code block wrapping (must come after shiki, before line-numbers) + // Code block wrapping (must come after the fence rule, before line-numbers) markdown.use(preWrapperPlugin, { copyLabel: options?.copyLabel }); markdown.use(containerPlugin); @@ -114,7 +81,7 @@ async function createCompiler(options?: CompileOptions): Promise { markdown.use(gitHubAlertsPlugin); // Save the raw fence info (e.g. `ts {1,3-5}`) before `markdown-it-attrs` strips the - // `{…}`, so `transformerMetaHighlight` still gets the line ranges via `meta.__raw`. + // `{…}`, so the highlighter still gets the line ranges. const stashFenceInfo: RuleCore = (state) => { for (const token of state.tokens) { if (token.type === 'fence') { diff --git a/packages/md/src/highlight.ts b/packages/md/src/highlight.ts new file mode 100644 index 000000000..7f7325cfb --- /dev/null +++ b/packages/md/src/highlight.ts @@ -0,0 +1,218 @@ +import { codeToTokens, isSupportedLanguage } from '@pierre/highlights'; +import type { Lang, Theme, ThemeFamily, ThemedToken } from '@pierre/highlights'; + +export type CodeThemeNames = Readonly<{ light: string; dark: string }>; + +/** Renders a code block. `meta` is the fence info, which may hold a `{1,3-5}` line range. */ +export type CodeHighlighter = (code: string, lang: string, meta: string) => string; + +// The theme CSS and the code-copy script select on these names. +const CSS_VARIABLE_PREFIX = '--shiki-'; + +const NOTATION_RE = /\s*\[!code (\+\+|--|highlight|hl|focus|error|warning)(?::(\d+))?\]/g; +// What is left of a comment once its notations are removed: only delimiters. +const EMPTY_COMMENT_RE = /^\s*(?:\/\/|\/\*|\*\/||--|[#;%"'*]|\s)*\s*$/; +const TOKEN_COMMENT = 1; + +// The classes a notation gives its line. The block gets `has-` and the first one. +const NOTATION_CLASSES: Record> = { + '++': ['diff', 'add'], + '--': ['diff', 'remove'], + highlight: ['highlighted'], + hl: ['highlighted'], + focus: ['focused'], + error: ['highlighted', 'error'], + warning: ['highlighted', 'warning'], +}; + +// Line and block classes come out in this order whatever order the notations appear in. +const LINE_CLASS_ORDER: ReadonlyArray = [ + 'highlighted', + 'diff', + 'add', + 'remove', + 'focused', + 'error', + 'warning', +]; +const PRE_CLASS_ORDER: ReadonlyArray = ['has-diff', 'has-highlighted', 'has-focused']; + +async function loadTheme(name: string): Promise { + if (!/^[a-z0-9-]+$/.test(name)) { + throw new Error(`Invalid code theme name '${name}'.`); + } + try { + const module = (await import(`@pierre/highlights/themes/${name}`)) as { + default: Theme | ThemeFamily; + }; + return module.default; + } catch (error) { + throw new Error(`Unknown code theme '${name}'.`, { cause: error }); + } +} + +function escapeHtml(text: string): string { + return /[&<]/.test(text) ? text.replace(/&/g, '&').replace(/ | null { + const match = /\{([\d,-]+)\}/.exec(meta); + if (!match) { + return null; + } + const lines = new Set(); + for (const part of match[1].split(',')) { + const [from, to] = part.split('-').map((value) => parseInt(value, 10)); + if (Number.isNaN(from)) { + continue; + } + const end = to === undefined || Number.isNaN(to) ? from : to; + for (let line = from; line <= end; line++) { + lines.add(line); + } + } + return lines; +} + +// The highlighter gives every token of one kind the same style object. +const spanOpeners = new WeakMap(); + +function spanOpener(token: ThemedToken): string { + const style = token.htmlStyle; + if (!style) { + return ''; + } + let opener = spanOpeners.get(style); + if (opener === undefined) { + const css = Object.entries(style) + .map(([property, value]) => `${property}:${value}`) + .join(';'); + opener = css ? `` : ''; + spanOpeners.set(style, opener); + } + return opener; +} + +function renderLine(tokens: ReadonlyArray, classes: string): string { + let html = ``; + for (const token of tokens) { + html += `${spanOpener(token)}${escapeHtml(token.content)}`; + } + return `${html}`; +} + +type Notation = Readonly<{ classes: ReadonlyArray; count: number }>; + +/** + * Splits the `[!code ...]` notations from the comments of a line. + * A comment that held nothing else is removed with them. + */ +function takeNotations(tokens: ReadonlyArray): Readonly<{ + tokens: ReadonlyArray; + notations: ReadonlyArray; +}> { + const notations: Array = []; + const kept: Array = []; + for (const token of tokens) { + if (token.type !== TOKEN_COMMENT || !token.content.includes('[!code ')) { + kept.push(token); + continue; + } + const content = token.content.replace( + NOTATION_RE, + (_match, name: string, count: string | undefined) => { + notations.push({ + classes: NOTATION_CLASSES[name], + count: count ? Math.max(1, parseInt(count, 10)) : 1, + }); + return ''; + }, + ); + if (!EMPTY_COMMENT_RE.test(content)) { + kept.push(content === token.content ? token : { ...token, content }); + } + } + return { tokens: kept, notations }; +} + +function isBlank(tokens: ReadonlyArray): boolean { + return tokens.every((token) => !/\S/.test(token.content)); +} + +/** + * Creates the code block highlighter. It renders the block the theme CSS expects: + * one `span.line` per line, light colors inline and dark colors in CSS variables. + */ +export async function createCodeHighlighter(themeNames: CodeThemeNames): Promise { + const [light, dark] = await Promise.all([ + loadTheme(themeNames.light), + loadTheme(themeNames.dark), + ]); + const plainText = 'text' as Lang; + const options = { + themes: { light, dark }, + defaultColor: 'light', + cssVariablePrefix: CSS_VARIABLE_PREFIX, + } as const; + + // The colors of the block depend on the themes alone. + const { bg, fg } = codeToTokens('', { lang: plainText, ...options }); + const preClass = `shiki shiki-themes ${themeNames.light} ${themeNames.dark}`; + const preStyle = escapeAttribute(`background-color:${bg};color:${fg}`); + + return (code, lang, meta) => { + const result = codeToTokens(code, { + lang: isSupportedLanguage(lang) ? lang : plainText, + ...options, + }); + const highlighted = metaHighlightedLines(meta); + + // Most blocks have neither notations nor a line range. + if (!highlighted && !code.includes('[!code ')) { + const body = result.tokens.map((tokens) => renderLine(tokens, 'line')).join('\n'); + return `
${body}
`; + } + + const preClasses = new Set(); + const lines: Array = []; + // Notations still to apply. One with a `:N` count stays for the lines that follow. + let running: Array<{ classes: ReadonlyArray; remaining: number }> = []; + + result.tokens.forEach((lineTokens, index) => { + const { tokens, notations } = takeNotations(lineTokens); + for (const notation of notations) { + preClasses.add(`has-${notation.classes[0]}`); + running.push({ classes: notation.classes, remaining: notation.count }); + } + // A line that held only the comment is dropped, and its notations apply from the next line. + if (notations.length > 0 && isBlank(tokens)) { + return; + } + + const classes = new Set(); + if (highlighted?.has(index + 1)) { + classes.add('highlighted'); + } + for (const entry of running) { + for (const name of entry.classes) { + classes.add(name); + } + entry.remaining--; + } + running = running.filter((entry) => entry.remaining > 0); + + const ordered = LINE_CLASS_ORDER.filter((name) => classes.has(name)); + lines.push(renderLine(tokens, ['line', ...ordered].join(' '))); + }); + + const blockClass = [preClass, ...PRE_CLASS_ORDER.filter((name) => preClasses.has(name))].join( + ' ', + ); + return `
${lines.join('\n')}
`; + }; +} diff --git a/packages/md/src/plugin.ts b/packages/md/src/plugin.ts index dce7d6fbe..7fd0ed741 100644 --- a/packages/md/src/plugin.ts +++ b/packages/md/src/plugin.ts @@ -41,6 +41,8 @@ export type MdPage = Readonly<{ export type MarkdownOptions = Readonly<{ copyLabel?: CompileOptions['copyLabel']; + themes?: CompileOptions['themes']; + /** @deprecated Use `themes`. Removed in the next major version. */ shiki?: CompileOptions['shiki']; }>; @@ -387,6 +389,7 @@ export function useFrontmatter() { return useContext(FrontmatterContext) ?? {}; const { compile } = await import('./compile.ts'); const result = await compile(body, { + themes: options?.themes, shiki: options?.shiki, copyLabel: options?.copyLabel, }).catch((error: unknown) => { diff --git a/packages/md/src/plugins/pre-wrapper.ts b/packages/md/src/plugins/pre-wrapper.ts index 2d51aacdb..15fc4ad99 100644 --- a/packages/md/src/plugins/pre-wrapper.ts +++ b/packages/md/src/plugins/pre-wrapper.ts @@ -22,7 +22,7 @@ function extractLang(info: string): string { /** * Copies the code next to a clicked copy button, without removed diff lines - * and without the empty last line that Shiki adds. + * and without the empty last line of the code block. * Markdown pages are island pages and always load as full documents, so this * classic inline script runs once per page load and adds one delegated * listener. The body must not contain `<`, so neither island tag matching nor diff --git a/packages/md/src/theme/code.css b/packages/md/src/theme/code.css index 4b511f668..b97d96584 100644 --- a/packages/md/src/theme/code.css +++ b/packages/md/src/theme/code.css @@ -83,7 +83,7 @@ mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E"); } -/* Shiki dual theme — keep inline light colors, only swap in dark mode */ +/* Dual theme — keep inline light colors, only swap in dark mode */ .void-md pre.shiki.shiki-themes { background-color: var(--shiki-light-bg, #fff) !important; } @@ -108,7 +108,7 @@ color: var(--shiki-dark) !important; } -/* Shiki notation transformers */ +/* Line notations */ .void-md pre .diff.add { background: rgba(16, 185, 129, 0.14); } @@ -155,12 +155,12 @@ background: rgba(239, 68, 68, 0.14); } -.void-md pre .has-focus .line:not(.focused) { +.void-md pre.has-focused .line:not(.focused) { opacity: 0.4; transition: opacity 0.3s; } -.void-md pre:hover .has-focus .line:not(.focused) { +.void-md pre.has-focused:hover .line:not(.focused) { opacity: 1; } diff --git a/packages/md/test/integration/markdown-blog.test.ts b/packages/md/test/integration/markdown-blog.test.ts index 59ae5c2c1..ccea9b16a 100644 --- a/packages/md/test/integration/markdown-blog.test.ts +++ b/packages/md/test/integration/markdown-blog.test.ts @@ -195,7 +195,7 @@ function defineSidebarTests(getBase: () => string) { expect(body).toContain('Getting Started'); expect(body).toContain('Code Examples'); expect(body).toContain('Learn how to use Void'); - expect(body).toContain('Shiki syntax highlighting demos'); + expect(body).toContain('Syntax highlighting demos'); } finally { await page.context().close(); } diff --git a/packages/md/test/unit/compile-themes.test.ts b/packages/md/test/unit/compile-themes.test.ts new file mode 100644 index 000000000..54e7dd6a3 --- /dev/null +++ b/packages/md/test/unit/compile-themes.test.ts @@ -0,0 +1,36 @@ +import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'; +import type { CompileOptions } from '../../src/compile.ts'; + +const source = '```ts\nconst a = 1;\n```'; + +// The compiler is created once per module, from the options of the first call. +async function compileFresh(options: CompileOptions): Promise { + const { compile } = await import('../../src/compile.ts'); + return (await compile(source, options)).html; +} + +describe('code block themes', () => { + beforeEach(() => { + vi.resetModules(); + }); + + it('uses the themes option', async () => { + const html = await compileFresh({ themes: { light: 'github-light', dark: 'github-dark' } }); + expect(html).toContain('class="shiki shiki-themes github-light github-dark"'); + }); + + it('still reads the deprecated shiki.themes option', async () => { + const html = await compileFresh({ + shiki: { themes: { light: 'vitesse-light', dark: 'vitesse-dark' } }, + }); + expect(html).toContain('class="shiki shiki-themes vitesse-light vitesse-dark"'); + }); + + it('prefers themes over shiki.themes', async () => { + const html = await compileFresh({ + themes: { light: 'github-light', dark: 'github-dark' }, + shiki: { themes: { light: 'vitesse-light', dark: 'vitesse-dark' } }, + }); + expect(html).toContain('class="shiki shiki-themes github-light github-dark"'); + }); +}); diff --git a/packages/md/test/unit/compile.test.ts b/packages/md/test/unit/compile.test.ts index 9292056a8..fa69df907 100644 --- a/packages/md/test/unit/compile.test.ts +++ b/packages/md/test/unit/compile.test.ts @@ -30,8 +30,8 @@ describe('compile', () => { expect(result.headings[3]).toMatchObject({ depth: 2, text: 'Section 2' }); }); - // The 0-based indices of code lines that carry the shiki `highlighted` class. - // (Shiki emits one `` per line plus a trailing empty line, + // The 0-based indices of code lines that carry the `highlighted` class. + // (The highlighter emits one `` per line plus a trailing empty line, // so we compare highlighted positions rather than a fixed line count.) const highlightedLineIndices = (html: string): ReadonlyArray => [...html.matchAll(/ { expect(result.html).not.toContain('highlighted'); }); + it('highlights code blocks with the default light and dark themes', async () => { + const result = await compile('```ts\nconst a = 1;\n```'); + expect(result.html).toContain('class="shiki shiki-themes github-light github-dark"'); + }); + it('still applies markdown-it-attrs to non-fence content', async () => { const result = await compile('Hello {.greeting}\n'); expect(result.html).toContain('greeting'); diff --git a/packages/md/test/unit/highlight.test.ts b/packages/md/test/unit/highlight.test.ts new file mode 100644 index 000000000..ce74c8427 --- /dev/null +++ b/packages/md/test/unit/highlight.test.ts @@ -0,0 +1,168 @@ +import { describe, expect, it } from 'vite-plus/test'; +import { createCodeHighlighter } from '../../src/highlight.ts'; + +const highlight = await createCodeHighlighter({ light: 'github-light', dark: 'github-dark' }); + +function render(code: string, lang = 'ts', meta = lang): string { + return highlight(code, lang, meta); +} + +/** The class and text of each rendered line. */ +function renderedLines(html: string): ReadonlyArray> { + const code = html.slice(html.indexOf('') + ''.length, html.indexOf('')); + return code.split('\n').map((line) => ({ + classes: /^]+>/g, '') + .replace(/</g, '<') + .replace(/&/g, '&'), + })); +} + +function lineClasses(html: string): ReadonlyArray { + return renderedLines(html).map((line) => line.classes); +} + +function preClasses(html: string): string { + return /^
 {
+  it('renders one line span per line with light and dark colors', () => {
+    const html = render('const a = 1;\nconst b = 2;\n');
+
+    expect(preClasses(html)).toBe('shiki shiki-themes github-light github-dark');
+    expect(html).toContain('--shiki-dark-bg:');
+    expect(html).toMatch(/const /);
+    expect(renderedLines(html)).toEqual([
+      { classes: 'line', text: 'const a = 1;' },
+      { classes: 'line', text: 'const b = 2;' },
+      { classes: 'line', text: '' },
+    ]);
+  });
+
+  it('escapes the code', () => {
+    const html = render(' && x\n', 'text');
+    expect(html).not.toContain(' && x');
+  });
+
+  it('renders an unknown language as plain text', () => {
+    const html = render('alpha\n', 'no-such-language');
+    expect(renderedLines(html)[0]).toEqual({ classes: 'line', text: 'alpha' });
+  });
+
+  it('marks diff lines and removes the notation comment', () => {
+    const html = render('a(); // [!code ++]\nb(); // [!code --]\nc();\n');
+
+    expect(preClasses(html)).toContain('has-diff');
+    expect(renderedLines(html).slice(0, 3)).toEqual([
+      { classes: 'line diff add', text: 'a(); ' },
+      { classes: 'line diff remove', text: 'b(); ' },
+      { classes: 'line', text: 'c();' },
+    ]);
+  });
+
+  it('marks highlight, focus, error and warning lines', () => {
+    const html = render(
+      [
+        'a(); // [!code highlight]',
+        'b(); // [!code hl]',
+        'c(); // [!code focus]',
+        'd(); // [!code error]',
+        'e(); // [!code warning]',
+        '',
+      ].join('\n'),
+    );
+
+    expect(preClasses(html)).toBe(
+      'shiki shiki-themes github-light github-dark has-highlighted has-focused',
+    );
+    expect(lineClasses(html)).toEqual([
+      'line highlighted',
+      'line highlighted',
+      'line focused',
+      'line highlighted error',
+      'line highlighted warning',
+      'line',
+    ]);
+  });
+
+  it('keeps the rest of a comment that carries a notation', () => {
+    const html = render('a(); // keep this [!code focus]\n');
+    expect(renderedLines(html)[0]).toEqual({ classes: 'line focused', text: 'a(); // keep this' });
+  });
+
+  it('reads notations from the comment syntax of other languages', () => {
+    const shell = render('npm install # [!code ++]\n', 'bash');
+    expect(renderedLines(shell)[0]).toEqual({ classes: 'line diff add', text: 'npm install ' });
+
+    const markup = render('

x

\n', 'html'); + expect(renderedLines(markup)[0]).toEqual({ classes: 'line diff add', text: '

x

' }); + + const block = render('a { } /* [!code focus] */\n', 'css'); + expect(renderedLines(block)[0]).toEqual({ classes: 'line focused', text: 'a { } ' }); + }); + + it('removes a comment that held only a notation, whatever its delimiter', () => { + const cases: ReadonlyArray<[string, string, string]> = [ + ['lisp', '(a) ; [!code ++]\n', '(a) '], + ['latex', 'x % [!code ++]\n', 'x '], + ['sql', 'select 1 -- [!code ++]\n', 'select 1 '], + ['vim', 'let a=1 " [!code ++]\n', 'let a=1 '], + ]; + for (const [lang, code, text] of cases) { + expect(renderedLines(render(code, lang))[0]).toEqual({ classes: 'line diff add', text }); + } + }); + + it('applies a notation with a count to the following lines', () => { + const html = render('a(); // [!code ++:2]\nb();\nc();\n'); + expect(lineClasses(html)).toEqual(['line diff add', 'line diff add', 'line', 'line']); + }); + + it('applies a notation on its own line to the next line', () => { + const html = render('a();\n// [!code focus]\nb();\n'); + expect(renderedLines(html)).toEqual([ + { classes: 'line', text: 'a();' }, + { classes: 'line focused', text: 'b();' }, + { classes: 'line', text: '' }, + ]); + }); + + it('applies a counted notation on its own line to the lines after it', () => { + const html = render('// [!code ++:2]\na();\nb();\nc();\n'); + expect(lineClasses(html)).toEqual(['line diff add', 'line diff add', 'line', 'line']); + }); + + it('leaves a notation outside a comment alone', () => { + const html = render('const s = "// [!code ++]";\n'); + expect(preClasses(html)).not.toContain('has-diff'); + expect(renderedLines(html)[0]).toEqual({ + classes: 'line', + text: 'const s = "// [!code ++]";', + }); + }); + + it('combines a line range from the fence info with a notation', () => { + const html = render('a(); // [!code ++]\nb();\nc();\n', 'ts', 'ts {1,3}'); + expect(lineClasses(html)).toEqual([ + 'line highlighted diff add', + 'line', + 'line highlighted', + 'line', + ]); + }); + + it('uses the named themes', async () => { + const other = await createCodeHighlighter({ light: 'vitesse-light', dark: 'vitesse-dark' }); + const html = other('const a = 1;\n', 'ts', 'ts'); + expect(preClasses(html)).toBe('shiki shiki-themes vitesse-light vitesse-dark'); + }); + + it('rejects a theme that does not exist', async () => { + await expect( + createCodeHighlighter({ light: 'no-such-theme', dark: 'github-dark' }), + ).rejects.toThrow("Unknown code theme 'no-such-theme'."); + }); +}); diff --git a/packages/md/test/unit/plugins.test.ts b/packages/md/test/unit/plugins.test.ts index 3acaad78a..8be7c0d04 100644 --- a/packages/md/test/unit/plugins.test.ts +++ b/packages/md/test/unit/plugins.test.ts @@ -61,7 +61,7 @@ describe('plugins', () => { expect(prose.html).not.toContain(' { + it('copy script drops the empty last line of the code block', () => { const copied: Array = []; clickCopy(['const x = 1;', '', 'const y = 2;', ''], (text) => { copied.push(text); diff --git a/playground/markdown-blog/pages/posts/code-examples.md b/playground/markdown-blog/pages/posts/code-examples.md index e32f7521d..3261bf067 100644 --- a/playground/markdown-blog/pages/posts/code-examples.md +++ b/playground/markdown-blog/pages/posts/code-examples.md @@ -1,6 +1,6 @@ --- title: Code Examples -description: Shiki syntax highlighting demos +description: Syntax highlighting demos --- Void supports syntax highlighting with dual themes (light/dark). diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cc9c2ab6a..4ff299691 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -255,12 +255,6 @@ catalogs: '@react-router/dev': specifier: ^8.5.1 version: 8.5.1 - '@shikijs/engine-javascript': - specifier: ^4.6.0 - version: 4.6.0 - '@shikijs/transformers': - specifier: ^4.6.0 - version: 4.6.0 '@stylexjs/stylex': specifier: 0.19.1 version: 0.19.1 @@ -592,12 +586,9 @@ importers: packages/md: dependencies: - '@shikijs/engine-javascript': - specifier: 'catalog:' - version: 4.6.0 - '@shikijs/transformers': - specifier: 'catalog:' - version: 4.6.0 + '@pierre/highlights': + specifier: ^0.5.1 + version: 0.5.1 gray-matter: specifier: ^4.0.3 version: 4.0.3 @@ -625,9 +616,6 @@ importers: pathslash: specifier: 'catalog:' version: 0.1.0 - shiki: - specifier: 'catalog:' - version: 4.6.0 tinyglobby: specifier: ^0.2.17 version: 0.2.17 @@ -4689,6 +4677,9 @@ packages: resolution: {integrity: sha512-7FNeNl8NCE7aINx7WXiKQrPYZWC/hvrTsmk6zmxbI7LTXE7hVek/n8AfVgpe2y82zl3w0HvCHN0bVKMBoJcC0w==} engines: {node: '>= 10.0.0'} + '@pierre/highlights@0.5.1': + resolution: {integrity: sha512-c2WF6sgwgKdxXU6FpWDLWUYHRca/cJy+3QJkWAqYqEAFxqlPf8eSH0ehHcFFjKBmT+9KBl9H7q6z7HMRU7+m6g==} + '@polka/url@1.0.0-next.29': resolution: {integrity: sha512-wwQAWhWSuHaag8c4q/KN/vCoeOJYshAIvMQwD4GpSb3OiZklFfvAgmj0VCBBImRpuF/aFgIRzllXlVX93Jevww==} @@ -13048,6 +13039,8 @@ snapshots: '@parcel/watcher-win32-arm64': 2.6.0 '@parcel/watcher-win32-x64': 2.6.0 + '@pierre/highlights@0.5.1': {} + '@polka/url@1.0.0-next.29': {} '@poppinss/colors@4.1.6':