@eventuras/ratio-ui-shiki 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -1
- package/dist/CodeBlock/CodeBlock.d.ts.map +1 -1
- package/dist/CodeBlock/CodeBlock.js +3 -6
- package/dist/CodeBlock/CodeBlock.js.map +1 -1
- package/dist/highlighter.d.ts +16 -11
- package/dist/highlighter.d.ts.map +1 -1
- package/dist/highlighter.js +27 -24
- package/dist/highlighter.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/tokens/index.d.ts +46 -0
- package/dist/tokens/index.d.ts.map +1 -0
- package/dist/tokens/index.js +60 -0
- package/dist/tokens/index.js.map +1 -0
- package/package.json +21 -15
package/README.md
CHANGED
|
@@ -63,6 +63,40 @@ const hl = await createRatioHighlighter();
|
|
|
63
63
|
array of React nodes (not an HTML string), this path never uses
|
|
64
64
|
`dangerouslySetInnerHTML` — it is injection-safe by construction.
|
|
65
65
|
|
|
66
|
+
### Precomputed tokens: highlight at build time, render without Shiki
|
|
67
|
+
|
|
68
|
+
When the page re-renders on the client (SPA, or React Router's `ssr: false` +
|
|
69
|
+
prerender), `shikiToDualLines` isn't enough — whatever produces the lines must
|
|
70
|
+
also run in the browser. Split it in two instead: tokenize in a **build
|
|
71
|
+
script** with `codeToDualTokens`, ship the result as JSON, and render it with
|
|
72
|
+
`tokensToLines` from the Shiki-free `tokens` entrypoint. The client then
|
|
73
|
+
bundles ~1 kB — no grammars, no engine — and code is highlighted from the very
|
|
74
|
+
first paint.
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
// build script (Node) — runs once per build
|
|
78
|
+
import { createRatioHighlighter, codeToDualTokens } from '@eventuras/ratio-ui-shiki';
|
|
79
|
+
|
|
80
|
+
const hl = await createRatioHighlighter();
|
|
81
|
+
const tokens = codeToDualTokens(hl, source, 'tsx');
|
|
82
|
+
await fs.writeFile('tokens.json', JSON.stringify(tokens));
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
// client — imports only the tiny tokens module, never Shiki
|
|
87
|
+
import { CodeBlock } from '@eventuras/ratio-ui/core/CodeBlock';
|
|
88
|
+
import { tokensToLines, DualThemeStyles } from '@eventuras/ratio-ui-shiki/tokens';
|
|
89
|
+
import tokens from './tokens.json';
|
|
90
|
+
|
|
91
|
+
<DualThemeStyles />
|
|
92
|
+
<CodeBlock code={source} language="tsx" highlightedLines={tokensToLines(tokens)} />;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`shikiToDualLines(hl, code, lang)` is exactly
|
|
96
|
+
`tokensToLines(codeToDualTokens(hl, code, lang))` — the two paths render
|
|
97
|
+
byte-identical markup (unit-tested), so you can start client-side and move to
|
|
98
|
+
precomputed tokens later without visual change.
|
|
99
|
+
|
|
66
100
|
### Advanced: `useShikiHighlighter`
|
|
67
101
|
|
|
68
102
|
The hook the `<CodeBlock>` wrapper is built on. It loads (and shares) the
|
|
@@ -106,4 +140,7 @@ const hl = await createRatioHighlighter({
|
|
|
106
140
|
hoisting); include it manually only when you render dual-theme lines by hand.
|
|
107
141
|
- Entrypoints are split for RSC: the root (`.`) is server-safe (highlighter
|
|
108
142
|
helpers + `DUAL_THEME_CSS` + types); the client component and hook live at
|
|
109
|
-
`@eventuras/ratio-ui-shiki/CodeBlock` and `.../useShikiHighlighter
|
|
143
|
+
`@eventuras/ratio-ui-shiki/CodeBlock` and `.../useShikiHighlighter`; the
|
|
144
|
+
Shiki-free precomputed-token renderer lives at
|
|
145
|
+
`@eventuras/ratio-ui-shiki/tokens` (`tokensToLines`, `DualThemeStyles`,
|
|
146
|
+
`DUAL_THEME_CSS`, types — imports nothing from Shiki).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeBlock.d.ts","sourceRoot":"","sources":["../../src/CodeBlock/CodeBlock.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAkB,MAAM,OAAO,CAAC;AACvC,OAAO,EAEL,KAAK,cAAc,IAAI,kBAAkB,EAC1C,MAAM,oCAAoC,CAAC;AAC5C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,
|
|
1
|
+
{"version":3,"file":"CodeBlock.d.ts","sourceRoot":"","sources":["../../src/CodeBlock/CodeBlock.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAkB,MAAM,OAAO,CAAC;AACvC,OAAO,EAEL,KAAK,cAAc,IAAI,kBAAkB,EAC1C,MAAM,oCAAoC,CAAC;AAC5C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAoC,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAInF,MAAM,WAAW,cAAe,SAAQ,IAAI,CAAC,kBAAkB,EAAE,kBAAkB,CAAC;IAClF;;;;;;;;OAQG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,eAAe,CAAC;CAC/B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,EACxB,IAAI,EACJ,QAAQ,EACR,MAAuB,EACvB,WAAW,EACX,GAAG,IAAI,EACR,EAAE,QAAQ,CAAC,cAAc,CAAC,qBA6B1B;yBAnCe,SAAS;;;AAsCzB,eAAe,SAAS,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/*! @eventuras/ratio-ui-shiki · Shiki syntax highlighting for ratio-ui · https://github.com/losol/ratio-ui · (c) 2026 Losol AS · MPL-2.0 */
|
|
2
2
|
"use client";
|
|
3
|
-
import {
|
|
3
|
+
import { DualThemeStyles } from "../tokens/index.js";
|
|
4
|
+
import { DEFAULT_THEMES, shikiToDualLines } from "../highlighter.js";
|
|
4
5
|
import { useShikiHighlighter } from "../useShikiHighlighter/index.js";
|
|
5
6
|
import { useMemo } from "react";
|
|
6
7
|
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -40,11 +41,7 @@ function CodeBlock$1({ code, language, themes = DEFAULT_THEMES, highlighter, ...
|
|
|
40
41
|
light,
|
|
41
42
|
dark
|
|
42
43
|
]);
|
|
43
|
-
return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(
|
|
44
|
-
href: "ratio-ui-shiki-dual",
|
|
45
|
-
precedence: "medium",
|
|
46
|
-
children: DUAL_THEME_CSS
|
|
47
|
-
}), /* @__PURE__ */ jsx(CodeBlock, {
|
|
44
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(DualThemeStyles, {}), /* @__PURE__ */ jsx(CodeBlock, {
|
|
48
45
|
code,
|
|
49
46
|
language,
|
|
50
47
|
highlightedLines,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeBlock.js","names":[],"sources":["../../src/CodeBlock/CodeBlock.tsx"],"sourcesContent":["// ratio-ui · design system for knowledge sharing\n// SPDX-FileCopyrightText: 2026 Losol AS\n// SPDX-License-Identifier: MPL-2.0\n\n'use client';\n\nimport React, { useMemo } from 'react';\nimport {\n CodeBlock as CoreCodeBlock,\n type CodeBlockProps as CoreCodeBlockProps,\n} from '@eventuras/ratio-ui/core/CodeBlock';\nimport type { HighlighterCore } from 'shiki/core';\nimport { shikiToDualLines, DEFAULT_THEMES,
|
|
1
|
+
{"version":3,"file":"CodeBlock.js","names":[],"sources":["../../src/CodeBlock/CodeBlock.tsx"],"sourcesContent":["// ratio-ui · design system for knowledge sharing\n// SPDX-FileCopyrightText: 2026 Losol AS\n// SPDX-License-Identifier: MPL-2.0\n\n'use client';\n\nimport React, { useMemo } from 'react';\nimport {\n CodeBlock as CoreCodeBlock,\n type CodeBlockProps as CoreCodeBlockProps,\n} from '@eventuras/ratio-ui/core/CodeBlock';\nimport type { HighlighterCore } from 'shiki/core';\nimport { shikiToDualLines, DEFAULT_THEMES, type DualThemes } from '../highlighter';\nimport { DualThemeStyles } from '../tokens';\nimport { useShikiHighlighter } from '../useShikiHighlighter';\n\nexport interface CodeBlockProps extends Omit<CoreCodeBlockProps, 'highlightedLines'> {\n /**\n * Light + dark Shiki theme pair to render with. The colors follow the app's\n * mode (`data-theme` / `data-color-scheme`), like the rest of ratio-ui.\n *\n * The built-in highlighter only loads `github-light` / `github-dark`; to\n * render a different pair, also pass a `highlighter` that has those themes\n * loaded — otherwise the block falls back to un-highlighted code.\n * @default { light: 'github-light', dark: 'github-dark' }\n */\n themes?: DualThemes;\n /**\n * Reuse an existing highlighter — e.g. one shared singleton from\n * `createRatioHighlighter` preloaded with your languages — instead of letting\n * this component lazily create its own. Ensure it has `language` and both\n * themes loaded; otherwise the block falls back to un-highlighted code.\n */\n highlighter?: HighlighterCore;\n}\n\n/**\n * `CodeBlock`, wired to Shiki — a drop-in for `@eventuras/ratio-ui/core/CodeBlock`\n * that highlights `code` for the given `language`. It loads a highlighter\n * asynchronously (via `useShikiHighlighter`) and renders raw code until it is\n * ready, so there is no flash of missing content (graceful degradation). Token\n * colors follow the app's light/dark mode automatically.\n *\n * For static or server-rendered content, prefer the core `CodeBlock` with\n * pre-computed `highlightedLines` (build them with `shikiToDualLines` +\n * `DUAL_THEME_CSS`) so no highlighter ships to the client.\n */\nexport function CodeBlock({\n code,\n language,\n themes = DEFAULT_THEMES,\n highlighter,\n ...rest\n}: Readonly<CodeBlockProps>) {\n const { light, dark } = themes;\n const activeHighlighter = useShikiHighlighter(highlighter);\n\n const highlightedLines = useMemo(() => {\n const hl = activeHighlighter;\n if (!hl || !language) return undefined;\n const loadedThemes = hl.getLoadedThemes();\n if (\n !hl.getLoadedLanguages().includes(language) ||\n !loadedThemes.includes(light) ||\n !loadedThemes.includes(dark)\n ) {\n return undefined;\n }\n try {\n return shikiToDualLines(hl, code, language, { light, dark });\n } catch {\n return undefined; // unknown grammar/theme → raw code fallback\n }\n }, [activeHighlighter, code, language, light, dark]);\n\n return (\n <>\n {/* Injection-safe theme switch; React hoists + dedupes by href. */}\n <DualThemeStyles />\n <CoreCodeBlock code={code} language={language} highlightedLines={highlightedLines} {...rest} />\n </>\n );\n}\n\nCodeBlock.displayName = 'ShikiCodeBlock';\nexport default CodeBlock;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA+CA,SAAgB,YAAU,EACxB,MACA,UACA,SAAS,gBACT,aACA,GAAG,QACwB;CAC3B,MAAM,EAAE,OAAO,SAAS;CACxB,MAAM,oBAAoB,oBAAoB,WAAW;CAEzD,MAAM,mBAAmB,cAAc;EACrC,MAAM,KAAK;EACX,IAAI,CAAC,MAAM,CAAC,UAAU,OAAO,KAAA;EAC7B,MAAM,eAAe,GAAG,gBAAgB;EACxC,IACE,CAAC,GAAG,mBAAmB,CAAC,CAAC,SAAS,QAAQ,KAC1C,CAAC,aAAa,SAAS,KAAK,KAC5B,CAAC,aAAa,SAAS,IAAI,GAE3B;EAEF,IAAI;GACF,OAAO,iBAAiB,IAAI,MAAM,UAAU;IAAE;IAAO;GAAK,CAAC;EAC7D,QAAQ;GACN;EACF;CACF,GAAG;EAAC;EAAmB;EAAM;EAAU;EAAO;CAAI,CAAC;CAEnD,OACE,qBAAA,YAAA,EAAA,UAAA,CAEE,oBAAC,iBAAD,CAAkB,CAAA,GAClB,oBAAC,WAAD;EAAqB;EAAgB;EAA4B;EAAkB,GAAI;CAAO,CAAA,CAC9F,EAAA,CAAA;AAEN;AAEA,YAAU,cAAc"}
|
package/dist/highlighter.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
2
|
import { HighlighterCore } from 'shiki/core';
|
|
3
3
|
import { BundledLanguage, BundledTheme, LanguageInput, ThemeInput } from 'shiki';
|
|
4
|
+
import { DualTokenLine } from './tokens';
|
|
5
|
+
export { SHIKI_TOKEN_CLASS, DUAL_THEME_CSS } from './tokens';
|
|
4
6
|
/**
|
|
5
7
|
* Languages preloaded when you don't pass your own to {@link createRatioHighlighter}.
|
|
6
8
|
* Kept in sync with the fine-grained `@shikijs/langs/*` imports below.
|
|
@@ -13,17 +15,6 @@ export interface DualThemes {
|
|
|
13
15
|
}
|
|
14
16
|
/** Default light/dark theme pair — the code surface follows the app's mode. */
|
|
15
17
|
export declare const DEFAULT_THEMES: DualThemes;
|
|
16
|
-
/** Class set on every dual-theme token span; the CSS below switches its color. */
|
|
17
|
-
export declare const SHIKI_TOKEN_CLASS = "ratio-shiki-token";
|
|
18
|
-
/**
|
|
19
|
-
* CSS that flips token colors with the app's mode. Shiki writes each token's
|
|
20
|
-
* light/dark colors into the `--shiki-light` / `--shiki-dark` custom properties;
|
|
21
|
-
* this reads the right one per `data-theme` / `data-color-scheme` — the same
|
|
22
|
-
* dark selectors ratio-ui's own tokens use. The `<CodeBlock>` wrapper injects
|
|
23
|
-
* this automatically (React style hoisting); include it yourself only if you
|
|
24
|
-
* render dual-theme lines by hand.
|
|
25
|
-
*/
|
|
26
|
-
export declare const DUAL_THEME_CSS = ".ratio-shiki-token{color:var(--shiki-light)}\n:root[data-theme=\"dark\"] .ratio-shiki-token,\n:root[data-color-scheme=\"dark\"] .ratio-shiki-token{color:var(--shiki-dark)}";
|
|
27
18
|
export interface RatioHighlighterOptions {
|
|
28
19
|
/**
|
|
29
20
|
* Languages to preload, as Shiki language *inputs* (dynamic imports) — e.g.
|
|
@@ -52,12 +43,26 @@ export declare function createRatioHighlighter(options?: RatioHighlighterOptions
|
|
|
52
43
|
* HTML string). `lang`/`theme` must already be loaded in `highlighter`.
|
|
53
44
|
*/
|
|
54
45
|
export declare function shikiToLines(highlighter: HighlighterCore, code: string, lang: BundledLanguage | string, theme?: BundledTheme | string): React.ReactNode[];
|
|
46
|
+
/**
|
|
47
|
+
* Tokenize `code` for a **light + dark** theme pair into plain, serializable
|
|
48
|
+
* data ({@link DualTokenLine}) — each token carries both colors as
|
|
49
|
+
* `--shiki-light` / `--shiki-dark` custom properties. This is the build-time
|
|
50
|
+
* half of the precomputed pipeline: run it in a build script (Node), ship the
|
|
51
|
+
* result as JSON, and render it with `tokensToLines` from
|
|
52
|
+
* `@eventuras/ratio-ui-shiki/tokens` — the client then needs no highlighter,
|
|
53
|
+
* grammars, or engine at all. `lang` and both themes must already be loaded
|
|
54
|
+
* in `highlighter`.
|
|
55
|
+
*/
|
|
56
|
+
export declare function codeToDualTokens(highlighter: HighlighterCore, code: string, lang: BundledLanguage | string, themes?: DualThemes): DualTokenLine[];
|
|
55
57
|
/**
|
|
56
58
|
* Turn `code` into one React node per line for a **light + dark** theme pair.
|
|
57
59
|
* Each token span carries both colors as `--shiki-light` / `--shiki-dark` custom
|
|
58
60
|
* properties and the {@link SHIKI_TOKEN_CLASS} class; pair it with
|
|
59
61
|
* {@link DUAL_THEME_CSS} so the color follows the app's mode. Injection-safe.
|
|
60
62
|
* `lang` and both themes must already be loaded in `highlighter`.
|
|
63
|
+
*
|
|
64
|
+
* Shorthand for `tokensToLines(codeToDualTokens(...))` — split the two halves
|
|
65
|
+
* yourself to highlight at build time and render without Shiki on the client.
|
|
61
66
|
*/
|
|
62
67
|
export declare function shikiToDualLines(highlighter: HighlighterCore, code: string, lang: BundledLanguage | string, themes?: DualThemes): React.ReactNode[];
|
|
63
68
|
//# sourceMappingURL=highlighter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"highlighter.d.ts","sourceRoot":"","sources":["../src/highlighter.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAmB,MAAM,OAAO,CAAC;AACxC,OAAO,EAAyB,KAAK,eAAe,EAAE,MAAM,YAAY,CAAC;AAEzE,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"highlighter.d.ts","sourceRoot":"","sources":["../src/highlighter.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAmB,MAAM,OAAO,CAAC;AACxC,OAAO,EAAyB,KAAK,eAAe,EAAE,MAAM,YAAY,CAAC;AAEzE,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACtF,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAI7D,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,aAAa,EAAE,eAAe,EAc1C,CAAC;AAEF,uCAAuC;AACvC,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,YAAY,CAAC;IACpB,IAAI,EAAE,YAAY,CAAC;CACpB;AAED,+EAA+E;AAC/E,eAAO,MAAM,cAAc,EAAE,UAA2D,CAAC;AAEzF,MAAM,WAAW,uBAAuB;IACtC;;;;OAIG;IACH,KAAK,CAAC,EAAE,aAAa,EAAE,CAAC;IACxB;;;;OAIG;IACH,MAAM,CAAC,EAAE,UAAU,EAAE,CAAC;CACvB;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,GAAE,uBAA4B,GAAG,OAAO,CAAC,eAAe,CAAC,CAuBtG;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,WAAW,EAAE,eAAe,EAC5B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,eAAe,GAAG,MAAM,EAC9B,KAAK,GAAE,YAAY,GAAG,MAA4B,GACjD,KAAK,CAAC,SAAS,EAAE,CA0BnB;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,eAAe,EAC5B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,eAAe,GAAG,MAAM,EAC9B,MAAM,GAAE,UAA2B,GAClC,aAAa,EAAE,CAiBjB;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,eAAe,EAC5B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,eAAe,GAAG,MAAM,EAC9B,MAAM,GAAE,UAA2B,GAClC,KAAK,CAAC,SAAS,EAAE,CAEnB"}
|
package/dist/highlighter.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/*! @eventuras/ratio-ui-shiki · Shiki syntax highlighting for ratio-ui · https://github.com/losol/ratio-ui · (c) 2026 Losol AS · MPL-2.0 */
|
|
2
|
+
import { tokensToLines } from "./tokens/index.js";
|
|
2
3
|
import { Fragment } from "react";
|
|
3
4
|
import { createHighlighterCore } from "shiki/core";
|
|
4
5
|
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
|
|
@@ -28,19 +29,6 @@ var DEFAULT_THEMES = {
|
|
|
28
29
|
light: "github-light",
|
|
29
30
|
dark: "github-dark"
|
|
30
31
|
};
|
|
31
|
-
/** Class set on every dual-theme token span; the CSS below switches its color. */
|
|
32
|
-
var SHIKI_TOKEN_CLASS = "ratio-shiki-token";
|
|
33
|
-
/**
|
|
34
|
-
* CSS that flips token colors with the app's mode. Shiki writes each token's
|
|
35
|
-
* light/dark colors into the `--shiki-light` / `--shiki-dark` custom properties;
|
|
36
|
-
* this reads the right one per `data-theme` / `data-color-scheme` — the same
|
|
37
|
-
* dark selectors ratio-ui's own tokens use. The `<CodeBlock>` wrapper injects
|
|
38
|
-
* this automatically (React style hoisting); include it yourself only if you
|
|
39
|
-
* render dual-theme lines by hand.
|
|
40
|
-
*/
|
|
41
|
-
var DUAL_THEME_CSS = `.${SHIKI_TOKEN_CLASS}{color:var(--shiki-light)}
|
|
42
|
-
:root[data-theme="dark"] .${SHIKI_TOKEN_CLASS},
|
|
43
|
-
:root[data-color-scheme="dark"] .${SHIKI_TOKEN_CLASS}{color:var(--shiki-dark)}`;
|
|
44
32
|
/**
|
|
45
33
|
* Create a Shiki highlighter preconfigured for ratio-ui. Built on `shiki/core`
|
|
46
34
|
* (`createHighlighterCore`) with the WASM-free JavaScript regex engine and a
|
|
@@ -93,25 +81,40 @@ function shikiToLines(highlighter, code, lang, theme = DEFAULT_THEMES.dark) {
|
|
|
93
81
|
}) }, i));
|
|
94
82
|
}
|
|
95
83
|
/**
|
|
84
|
+
* Tokenize `code` for a **light + dark** theme pair into plain, serializable
|
|
85
|
+
* data ({@link DualTokenLine}) — each token carries both colors as
|
|
86
|
+
* `--shiki-light` / `--shiki-dark` custom properties. This is the build-time
|
|
87
|
+
* half of the precomputed pipeline: run it in a build script (Node), ship the
|
|
88
|
+
* result as JSON, and render it with `tokensToLines` from
|
|
89
|
+
* `@eventuras/ratio-ui-shiki/tokens` — the client then needs no highlighter,
|
|
90
|
+
* grammars, or engine at all. `lang` and both themes must already be loaded
|
|
91
|
+
* in `highlighter`.
|
|
92
|
+
*/
|
|
93
|
+
function codeToDualTokens(highlighter, code, lang, themes = DEFAULT_THEMES) {
|
|
94
|
+
const { tokens } = highlighter.codeToTokens(code, {
|
|
95
|
+
lang,
|
|
96
|
+
themes,
|
|
97
|
+
defaultColor: false
|
|
98
|
+
});
|
|
99
|
+
return tokens.map((line) => line.map((token) => ({
|
|
100
|
+
content: token.content,
|
|
101
|
+
...token.htmlStyle ? { htmlStyle: { ...token.htmlStyle } } : {}
|
|
102
|
+
})));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
96
105
|
* Turn `code` into one React node per line for a **light + dark** theme pair.
|
|
97
106
|
* Each token span carries both colors as `--shiki-light` / `--shiki-dark` custom
|
|
98
107
|
* properties and the {@link SHIKI_TOKEN_CLASS} class; pair it with
|
|
99
108
|
* {@link DUAL_THEME_CSS} so the color follows the app's mode. Injection-safe.
|
|
100
109
|
* `lang` and both themes must already be loaded in `highlighter`.
|
|
110
|
+
*
|
|
111
|
+
* Shorthand for `tokensToLines(codeToDualTokens(...))` — split the two halves
|
|
112
|
+
* yourself to highlight at build time and render without Shiki on the client.
|
|
101
113
|
*/
|
|
102
114
|
function shikiToDualLines(highlighter, code, lang, themes = DEFAULT_THEMES) {
|
|
103
|
-
|
|
104
|
-
lang,
|
|
105
|
-
themes,
|
|
106
|
-
defaultColor: false
|
|
107
|
-
});
|
|
108
|
-
return tokens.map((line, i) => /* @__PURE__ */ jsx(Fragment, { children: line.map((token, j) => /* @__PURE__ */ jsx("span", {
|
|
109
|
-
className: SHIKI_TOKEN_CLASS,
|
|
110
|
-
style: token.htmlStyle,
|
|
111
|
-
children: token.content
|
|
112
|
-
}, j)) }, i));
|
|
115
|
+
return tokensToLines(codeToDualTokens(highlighter, code, lang, themes));
|
|
113
116
|
}
|
|
114
117
|
//#endregion
|
|
115
|
-
export { DEFAULT_LANGS, DEFAULT_THEMES,
|
|
118
|
+
export { DEFAULT_LANGS, DEFAULT_THEMES, codeToDualTokens, createRatioHighlighter, shikiToDualLines, shikiToLines };
|
|
116
119
|
|
|
117
120
|
//# sourceMappingURL=highlighter.js.map
|
package/dist/highlighter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"highlighter.js","names":[],"sources":["../src/highlighter.tsx"],"sourcesContent":["// ratio-ui · design system for knowledge sharing\n// SPDX-FileCopyrightText: 2026 Losol AS\n// SPDX-License-Identifier: MPL-2.0\n\nimport React, { Fragment } from 'react';\nimport { createHighlighterCore, type HighlighterCore } from 'shiki/core';\nimport { createJavaScriptRegexEngine } from 'shiki/engine/javascript';\nimport type { BundledLanguage, BundledTheme, LanguageInput, ThemeInput } from 'shiki';\n\n/**\n * Languages preloaded when you don't pass your own to {@link createRatioHighlighter}.\n * Kept in sync with the fine-grained `@shikijs/langs/*` imports below.\n */\nexport const DEFAULT_LANGS: BundledLanguage[] = [\n 'tsx',\n 'ts',\n 'jsx',\n 'js',\n 'json',\n 'bash',\n 'css',\n 'html',\n 'markdown',\n 'yaml',\n 'xml',\n 'python',\n 'csharp',\n];\n\n/** A light + dark Shiki theme pair. */\nexport interface DualThemes {\n light: BundledTheme;\n dark: BundledTheme;\n}\n\n/** Default light/dark theme pair — the code surface follows the app's mode. */\nexport const DEFAULT_THEMES: DualThemes = { light: 'github-light', dark: 'github-dark' };\n\
|
|
1
|
+
{"version":3,"file":"highlighter.js","names":[],"sources":["../src/highlighter.tsx"],"sourcesContent":["// ratio-ui · design system for knowledge sharing\n// SPDX-FileCopyrightText: 2026 Losol AS\n// SPDX-License-Identifier: MPL-2.0\n\nimport React, { Fragment } from 'react';\nimport { createHighlighterCore, type HighlighterCore } from 'shiki/core';\nimport { createJavaScriptRegexEngine } from 'shiki/engine/javascript';\nimport type { BundledLanguage, BundledTheme, LanguageInput, ThemeInput } from 'shiki';\nimport { tokensToLines, type DualTokenLine } from './tokens';\n\n// Re-exported so existing imports keep working; they live in `./tokens` (the\n// Shiki-free module) so precomputed-token consumers don't pull this file in.\nexport { SHIKI_TOKEN_CLASS, DUAL_THEME_CSS } from './tokens';\n\n/**\n * Languages preloaded when you don't pass your own to {@link createRatioHighlighter}.\n * Kept in sync with the fine-grained `@shikijs/langs/*` imports below.\n */\nexport const DEFAULT_LANGS: BundledLanguage[] = [\n 'tsx',\n 'ts',\n 'jsx',\n 'js',\n 'json',\n 'bash',\n 'css',\n 'html',\n 'markdown',\n 'yaml',\n 'xml',\n 'python',\n 'csharp',\n];\n\n/** A light + dark Shiki theme pair. */\nexport interface DualThemes {\n light: BundledTheme;\n dark: BundledTheme;\n}\n\n/** Default light/dark theme pair — the code surface follows the app's mode. */\nexport const DEFAULT_THEMES: DualThemes = { light: 'github-light', dark: 'github-dark' };\n\nexport interface RatioHighlighterOptions {\n /**\n * Languages to preload, as Shiki language *inputs* (dynamic imports) — e.g.\n * `import('@shikijs/langs/python')`, not the string names. Defaults to inputs\n * for the {@link DEFAULT_LANGS} languages. Replaces the default set when set.\n */\n langs?: LanguageInput[];\n /**\n * Themes to preload, as Shiki theme inputs — e.g.\n * `import('@shikijs/themes/nord')`. Defaults to the `github-light` /\n * `github-dark` pair.\n */\n themes?: ThemeInput[];\n}\n\n/**\n * Create a Shiki highlighter preconfigured for ratio-ui. Built on `shiki/core`\n * (`createHighlighterCore`) with the WASM-free JavaScript regex engine and a\n * curated, individually-imported language/theme set — so consumers bundle only\n * these grammars, not Shiki's full bundled registry. The call is async and\n * relatively heavy — create one and share it (the `<CodeBlock>` wrapper does).\n */\nexport function createRatioHighlighter(options: RatioHighlighterOptions = {}): Promise<HighlighterCore> {\n return createHighlighterCore({\n engine: createJavaScriptRegexEngine(),\n langs: options.langs ?? [\n import('@shikijs/langs/tsx'),\n import('@shikijs/langs/ts'),\n import('@shikijs/langs/jsx'),\n import('@shikijs/langs/js'),\n import('@shikijs/langs/json'),\n import('@shikijs/langs/bash'),\n import('@shikijs/langs/css'),\n import('@shikijs/langs/html'),\n import('@shikijs/langs/markdown'),\n import('@shikijs/langs/yaml'),\n import('@shikijs/langs/xml'),\n import('@shikijs/langs/python'),\n import('@shikijs/langs/csharp'),\n ],\n themes: options.themes ?? [\n import('@shikijs/themes/github-light'),\n import('@shikijs/themes/github-dark'),\n ],\n });\n}\n\n/**\n * Turn `code` into one React node per line for a **single** theme — each a run\n * of `<span>`s carrying Shiki's token colors. Injection-safe (real elements, no\n * HTML string). `lang`/`theme` must already be loaded in `highlighter`.\n */\nexport function shikiToLines(\n highlighter: HighlighterCore,\n code: string,\n lang: BundledLanguage | string,\n theme: BundledTheme | string = DEFAULT_THEMES.dark,\n): React.ReactNode[] {\n const { tokens } = highlighter.codeToTokens(code, {\n lang: lang as BundledLanguage,\n theme: theme as BundledTheme,\n });\n\n return tokens.map((line, i) => (\n <Fragment key={i}>\n {line.map((token, j) => {\n const fontStyle = token.fontStyle ?? 0;\n return (\n <span\n key={j}\n style={{\n color: token.color,\n fontStyle: fontStyle & 1 ? 'italic' : undefined,\n fontWeight: fontStyle & 2 ? 'bold' : undefined,\n textDecoration: fontStyle & 4 ? 'underline' : undefined,\n }}\n >\n {token.content}\n </span>\n );\n })}\n </Fragment>\n ));\n}\n\n/**\n * Tokenize `code` for a **light + dark** theme pair into plain, serializable\n * data ({@link DualTokenLine}) — each token carries both colors as\n * `--shiki-light` / `--shiki-dark` custom properties. This is the build-time\n * half of the precomputed pipeline: run it in a build script (Node), ship the\n * result as JSON, and render it with `tokensToLines` from\n * `@eventuras/ratio-ui-shiki/tokens` — the client then needs no highlighter,\n * grammars, or engine at all. `lang` and both themes must already be loaded\n * in `highlighter`.\n */\nexport function codeToDualTokens(\n highlighter: HighlighterCore,\n code: string,\n lang: BundledLanguage | string,\n themes: DualThemes = DEFAULT_THEMES,\n): DualTokenLine[] {\n const { tokens } = highlighter.codeToTokens(code, {\n lang: lang as BundledLanguage,\n themes: themes as unknown as Record<string, BundledTheme>,\n defaultColor: false,\n });\n\n return tokens.map((line) =>\n line.map((token) => ({\n content: token.content,\n // Copied into a fresh plain object so the result is JSON-clean no\n // matter what Shiki hands back.\n ...(token.htmlStyle\n ? { htmlStyle: { ...(token.htmlStyle as Record<string, string>) } }\n : {}),\n })),\n );\n}\n\n/**\n * Turn `code` into one React node per line for a **light + dark** theme pair.\n * Each token span carries both colors as `--shiki-light` / `--shiki-dark` custom\n * properties and the {@link SHIKI_TOKEN_CLASS} class; pair it with\n * {@link DUAL_THEME_CSS} so the color follows the app's mode. Injection-safe.\n * `lang` and both themes must already be loaded in `highlighter`.\n *\n * Shorthand for `tokensToLines(codeToDualTokens(...))` — split the two halves\n * yourself to highlight at build time and render without Shiki on the client.\n */\nexport function shikiToDualLines(\n highlighter: HighlighterCore,\n code: string,\n lang: BundledLanguage | string,\n themes: DualThemes = DEFAULT_THEMES,\n): React.ReactNode[] {\n return tokensToLines(codeToDualTokens(highlighter, code, lang, themes));\n}\n"],"mappings":";;;;;;;;;;;AAkBA,IAAa,gBAAmC;CAC9C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;AASA,IAAa,iBAA6B;CAAE,OAAO;CAAgB,MAAM;AAAc;;;;;;;;AAwBvF,SAAgB,uBAAuB,UAAmC,CAAC,GAA6B;CACtG,OAAO,sBAAsB;EAC3B,QAAQ,4BAA4B;EACpC,OAAO,QAAQ,SAAS;GACtB,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;GACP,OAAO;EACT;EACA,QAAQ,QAAQ,UAAU,CACxB,OAAO,iCACP,OAAO,8BACT;CACF,CAAC;AACH;;;;;;AAOA,SAAgB,aACd,aACA,MACA,MACA,QAA+B,eAAe,MAC3B;CACnB,MAAM,EAAE,WAAW,YAAY,aAAa,MAAM;EAC1C;EACC;CACT,CAAC;CAED,OAAO,OAAO,KAAK,MAAM,MACvB,oBAAC,UAAD,EAAA,UACG,KAAK,KAAK,OAAO,MAAM;EACtB,MAAM,YAAY,MAAM,aAAa;EACrC,OACE,oBAAC,QAAD;GAEE,OAAO;IACL,OAAO,MAAM;IACb,WAAW,YAAY,IAAI,WAAW,KAAA;IACtC,YAAY,YAAY,IAAI,SAAS,KAAA;IACrC,gBAAgB,YAAY,IAAI,cAAc,KAAA;GAChD;GAEC,UAAA,MAAM;EACH,GATC,CASD;CAEV,CAAC,EACO,GAjBK,CAiBL,CACX;AACH;;;;;;;;;;;AAYA,SAAgB,iBACd,aACA,MACA,MACA,SAAqB,gBACJ;CACjB,MAAM,EAAE,WAAW,YAAY,aAAa,MAAM;EAC1C;EACE;EACR,cAAc;CAChB,CAAC;CAED,OAAO,OAAO,KAAK,SACjB,KAAK,KAAK,WAAW;EACnB,SAAS,MAAM;EAGf,GAAI,MAAM,YACN,EAAE,WAAW,EAAE,GAAI,MAAM,UAAqC,EAAE,IAChE,CAAC;CACP,EAAE,CACJ;AACF;;;;;;;;;;;AAYA,SAAgB,iBACd,aACA,MACA,MACA,SAAqB,gBACF;CACnB,OAAO,cAAc,iBAAiB,aAAa,MAAM,MAAM,MAAM,CAAC;AACxE"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { createRatioHighlighter, shikiToLines, shikiToDualLines, DEFAULT_LANGS, DEFAULT_THEMES,
|
|
1
|
+
export { createRatioHighlighter, shikiToLines, shikiToDualLines, codeToDualTokens, DEFAULT_LANGS, DEFAULT_THEMES, type RatioHighlighterOptions, type DualThemes, } from './highlighter';
|
|
2
|
+
export { tokensToLines, DualThemeStyles, SHIKI_TOKEN_CLASS, DUAL_THEME_CSS, type DualToken, type DualTokenLine, } from './tokens';
|
|
2
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,sBAAsB,EACtB,YAAY,EACZ,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,sBAAsB,EACtB,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,KAAK,uBAAuB,EAC5B,KAAK,UAAU,GAChB,MAAM,eAAe,CAAC;AAIvB,OAAO,EACL,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,cAAc,EACd,KAAK,SAAS,EACd,KAAK,aAAa,GACnB,MAAM,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
/*! @eventuras/ratio-ui-shiki · Shiki syntax highlighting for ratio-ui · https://github.com/losol/ratio-ui · (c) 2026 Losol AS · MPL-2.0 */
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { DUAL_THEME_CSS, DualThemeStyles, SHIKI_TOKEN_CLASS, tokensToLines } from "./tokens/index.js";
|
|
3
|
+
import { DEFAULT_LANGS, DEFAULT_THEMES, codeToDualTokens, createRatioHighlighter, shikiToDualLines, shikiToLines } from "./highlighter.js";
|
|
4
|
+
export { DEFAULT_LANGS, DEFAULT_THEMES, DUAL_THEME_CSS, DualThemeStyles, SHIKI_TOKEN_CLASS, codeToDualTokens, createRatioHighlighter, shikiToDualLines, shikiToLines, tokensToLines };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
/** Class set on every dual-theme token span; {@link DUAL_THEME_CSS} switches its color. */
|
|
3
|
+
export declare const SHIKI_TOKEN_CLASS = "ratio-shiki-token";
|
|
4
|
+
/**
|
|
5
|
+
* CSS that flips token colors with the mode of the nearest scope. Shiki writes
|
|
6
|
+
* each token's light/dark colors into `--shiki-light` / `--shiki-dark`; a
|
|
7
|
+
* switch set on the scope — `:root` and its `data-theme` / `data-color-scheme`
|
|
8
|
+
* arms, `.surface-light` and `.surface-dark` — is inherited down to the token,
|
|
9
|
+
* which reads the dark color while the switch is on and falls back to light.
|
|
10
|
+
* The same scopes and the same inheritance as ratio-ui's own code-surface
|
|
11
|
+
* tokens, so a band inside a band resolves to the inner one. Rendered for you
|
|
12
|
+
* by {@link DualThemeStyles} and by the `<CodeBlock>` wrapper; include it
|
|
13
|
+
* yourself only if you render dual-theme lines by hand.
|
|
14
|
+
*
|
|
15
|
+
* The switch: `initial` is a custom property's guaranteed-invalid value, so
|
|
16
|
+
* `var()` of it voids `--shiki-color` and `color` takes its fallback. A lone
|
|
17
|
+
* space is a valid, empty value that leaves `--shiki-color` whole.
|
|
18
|
+
*/
|
|
19
|
+
export declare const DUAL_THEME_CSS = ":root,.surface-light{--shiki-dark-on:initial}\n:root[data-theme=\"dark\"],:root[data-color-scheme=\"dark\"],.surface-dark{--shiki-dark-on: }\n.ratio-shiki-token{--shiki-color:var(--shiki-dark-on) var(--shiki-dark);color:var(--shiki-color,var(--shiki-light))}";
|
|
20
|
+
/**
|
|
21
|
+
* One Shiki token in serializable form: its text plus the inline style
|
|
22
|
+
* carrying the `--shiki-light` / `--shiki-dark` colors. Plain data — it
|
|
23
|
+
* survives `JSON.stringify`/`parse`, so tokens can be computed once at build
|
|
24
|
+
* time and shipped to the client instead of grammars.
|
|
25
|
+
*/
|
|
26
|
+
export interface DualToken {
|
|
27
|
+
content: string;
|
|
28
|
+
htmlStyle?: Record<string, string>;
|
|
29
|
+
}
|
|
30
|
+
/** One line of code as dual-theme tokens. */
|
|
31
|
+
export type DualTokenLine = DualToken[];
|
|
32
|
+
/**
|
|
33
|
+
* Renders {@link DUAL_THEME_CSS} as a hoisted `<style>` tag. React dedupes it
|
|
34
|
+
* by `href`, so rendering this next to Shiki `<CodeBlock>`s (which inject the
|
|
35
|
+
* same tag) adds nothing. Drop it once anywhere precomputed lines render.
|
|
36
|
+
*/
|
|
37
|
+
export declare function DualThemeStyles(): React.JSX.Element;
|
|
38
|
+
/**
|
|
39
|
+
* Turn dual-theme tokens back into one React node per line — the shape core
|
|
40
|
+
* CodeBlock's `highlightedLines` takes. This is the runtime half of the
|
|
41
|
+
* precomputed pipeline: `codeToDualTokens` at build time, this at render.
|
|
42
|
+
* Injection-safe (real elements, no HTML string). Pair with
|
|
43
|
+
* {@link DualThemeStyles} so the colors follow the app's mode.
|
|
44
|
+
*/
|
|
45
|
+
export declare function tokensToLines(lines: DualTokenLine[]): React.ReactNode[];
|
|
46
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tokens/index.tsx"],"names":[],"mappings":"AAIA;;;;;GAKG;AAEH,OAAO,KAAmB,MAAM,OAAO,CAAC;AAExC,2FAA2F;AAC3F,eAAO,MAAM,iBAAiB,sBAAsB,CAAC;AAErD;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,cAAc,uQAE6F,CAAC;AAEzH;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACpC;AAED,6CAA6C;AAC7C,MAAM,MAAM,aAAa,GAAG,SAAS,EAAE,CAAC;AAExC;;;;GAIG;AACH,wBAAgB,eAAe,sBAM9B;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,aAAa,EAAE,GAAG,KAAK,CAAC,SAAS,EAAE,CAcvE"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/*! @eventuras/ratio-ui-shiki · Shiki syntax highlighting for ratio-ui · https://github.com/losol/ratio-ui · (c) 2026 Losol AS · MPL-2.0 */
|
|
2
|
+
import { Fragment } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
//#region src/tokens/index.tsx
|
|
5
|
+
/**
|
|
6
|
+
* The Shiki-free half of the precomputed-highlighting pipeline. Everything in
|
|
7
|
+
* this module is plain data + tiny renderers with **no shiki import**, so a
|
|
8
|
+
* client that renders precomputed tokens (built with `codeToDualTokens` at
|
|
9
|
+
* build time) bundles about a kilobyte from here — no grammars, no engine.
|
|
10
|
+
*/
|
|
11
|
+
/** Class set on every dual-theme token span; {@link DUAL_THEME_CSS} switches its color. */
|
|
12
|
+
var SHIKI_TOKEN_CLASS = "ratio-shiki-token";
|
|
13
|
+
/**
|
|
14
|
+
* CSS that flips token colors with the mode of the nearest scope. Shiki writes
|
|
15
|
+
* each token's light/dark colors into `--shiki-light` / `--shiki-dark`; a
|
|
16
|
+
* switch set on the scope — `:root` and its `data-theme` / `data-color-scheme`
|
|
17
|
+
* arms, `.surface-light` and `.surface-dark` — is inherited down to the token,
|
|
18
|
+
* which reads the dark color while the switch is on and falls back to light.
|
|
19
|
+
* The same scopes and the same inheritance as ratio-ui's own code-surface
|
|
20
|
+
* tokens, so a band inside a band resolves to the inner one. Rendered for you
|
|
21
|
+
* by {@link DualThemeStyles} and by the `<CodeBlock>` wrapper; include it
|
|
22
|
+
* yourself only if you render dual-theme lines by hand.
|
|
23
|
+
*
|
|
24
|
+
* The switch: `initial` is a custom property's guaranteed-invalid value, so
|
|
25
|
+
* `var()` of it voids `--shiki-color` and `color` takes its fallback. A lone
|
|
26
|
+
* space is a valid, empty value that leaves `--shiki-color` whole.
|
|
27
|
+
*/
|
|
28
|
+
var DUAL_THEME_CSS = `:root,.surface-light{--shiki-dark-on:initial}
|
|
29
|
+
:root[data-theme="dark"],:root[data-color-scheme="dark"],.surface-dark{--shiki-dark-on: }
|
|
30
|
+
.${SHIKI_TOKEN_CLASS}{--shiki-color:var(--shiki-dark-on) var(--shiki-dark);color:var(--shiki-color,var(--shiki-light))}`;
|
|
31
|
+
/**
|
|
32
|
+
* Renders {@link DUAL_THEME_CSS} as a hoisted `<style>` tag. React dedupes it
|
|
33
|
+
* by `href`, so rendering this next to Shiki `<CodeBlock>`s (which inject the
|
|
34
|
+
* same tag) adds nothing. Drop it once anywhere precomputed lines render.
|
|
35
|
+
*/
|
|
36
|
+
function DualThemeStyles() {
|
|
37
|
+
return /* @__PURE__ */ jsx("style", {
|
|
38
|
+
href: "ratio-ui-shiki-dual",
|
|
39
|
+
precedence: "medium",
|
|
40
|
+
children: DUAL_THEME_CSS
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Turn dual-theme tokens back into one React node per line — the shape core
|
|
45
|
+
* CodeBlock's `highlightedLines` takes. This is the runtime half of the
|
|
46
|
+
* precomputed pipeline: `codeToDualTokens` at build time, this at render.
|
|
47
|
+
* Injection-safe (real elements, no HTML string). Pair with
|
|
48
|
+
* {@link DualThemeStyles} so the colors follow the app's mode.
|
|
49
|
+
*/
|
|
50
|
+
function tokensToLines(lines) {
|
|
51
|
+
return lines.map((line, i) => /* @__PURE__ */ jsx(Fragment, { children: line.map((token, j) => /* @__PURE__ */ jsx("span", {
|
|
52
|
+
className: SHIKI_TOKEN_CLASS,
|
|
53
|
+
style: token.htmlStyle,
|
|
54
|
+
children: token.content
|
|
55
|
+
}, j)) }, i));
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { DUAL_THEME_CSS, DualThemeStyles, SHIKI_TOKEN_CLASS, tokensToLines };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/tokens/index.tsx"],"sourcesContent":["// ratio-ui · design system for knowledge sharing\n// SPDX-FileCopyrightText: 2026 Losol AS\n// SPDX-License-Identifier: MPL-2.0\n\n/**\n * The Shiki-free half of the precomputed-highlighting pipeline. Everything in\n * this module is plain data + tiny renderers with **no shiki import**, so a\n * client that renders precomputed tokens (built with `codeToDualTokens` at\n * build time) bundles about a kilobyte from here — no grammars, no engine.\n */\n\nimport React, { Fragment } from 'react';\n\n/** Class set on every dual-theme token span; {@link DUAL_THEME_CSS} switches its color. */\nexport const SHIKI_TOKEN_CLASS = 'ratio-shiki-token';\n\n/**\n * CSS that flips token colors with the mode of the nearest scope. Shiki writes\n * each token's light/dark colors into `--shiki-light` / `--shiki-dark`; a\n * switch set on the scope — `:root` and its `data-theme` / `data-color-scheme`\n * arms, `.surface-light` and `.surface-dark` — is inherited down to the token,\n * which reads the dark color while the switch is on and falls back to light.\n * The same scopes and the same inheritance as ratio-ui's own code-surface\n * tokens, so a band inside a band resolves to the inner one. Rendered for you\n * by {@link DualThemeStyles} and by the `<CodeBlock>` wrapper; include it\n * yourself only if you render dual-theme lines by hand.\n *\n * The switch: `initial` is a custom property's guaranteed-invalid value, so\n * `var()` of it voids `--shiki-color` and `color` takes its fallback. A lone\n * space is a valid, empty value that leaves `--shiki-color` whole.\n */\nexport const DUAL_THEME_CSS = `:root,.surface-light{--shiki-dark-on:initial}\n:root[data-theme=\"dark\"],:root[data-color-scheme=\"dark\"],.surface-dark{--shiki-dark-on: }\n.${SHIKI_TOKEN_CLASS}{--shiki-color:var(--shiki-dark-on) var(--shiki-dark);color:var(--shiki-color,var(--shiki-light))}`;\n\n/**\n * One Shiki token in serializable form: its text plus the inline style\n * carrying the `--shiki-light` / `--shiki-dark` colors. Plain data — it\n * survives `JSON.stringify`/`parse`, so tokens can be computed once at build\n * time and shipped to the client instead of grammars.\n */\nexport interface DualToken {\n content: string;\n htmlStyle?: Record<string, string>;\n}\n\n/** One line of code as dual-theme tokens. */\nexport type DualTokenLine = DualToken[];\n\n/**\n * Renders {@link DUAL_THEME_CSS} as a hoisted `<style>` tag. React dedupes it\n * by `href`, so rendering this next to Shiki `<CodeBlock>`s (which inject the\n * same tag) adds nothing. Drop it once anywhere precomputed lines render.\n */\nexport function DualThemeStyles() {\n return (\n <style href=\"ratio-ui-shiki-dual\" precedence=\"medium\">\n {DUAL_THEME_CSS}\n </style>\n );\n}\n\n/**\n * Turn dual-theme tokens back into one React node per line — the shape core\n * CodeBlock's `highlightedLines` takes. This is the runtime half of the\n * precomputed pipeline: `codeToDualTokens` at build time, this at render.\n * Injection-safe (real elements, no HTML string). Pair with\n * {@link DualThemeStyles} so the colors follow the app's mode.\n */\nexport function tokensToLines(lines: DualTokenLine[]): React.ReactNode[] {\n return lines.map((line, i) => (\n <Fragment key={i}>\n {line.map((token, j) => (\n <span\n key={j}\n className={SHIKI_TOKEN_CLASS}\n style={token.htmlStyle as React.CSSProperties}\n >\n {token.content}\n </span>\n ))}\n </Fragment>\n ));\n}\n"],"mappings":";;;;;;;;;;;AAcA,IAAa,oBAAoB;;;;;;;;;;;;;;;;AAiBjC,IAAa,iBAAiB;;GAE3B,kBAAkB;;;;;;AAqBrB,SAAgB,kBAAkB;CAChC,OACE,oBAAC,SAAD;EAAO,MAAK;EAAsB,YAAW;EAC1C,UAAA;CACI,CAAA;AAEX;;;;;;;;AASA,SAAgB,cAAc,OAA2C;CACvE,OAAO,MAAM,KAAK,MAAM,MACtB,oBAAC,UAAD,EAAA,UACG,KAAK,KAAK,OAAO,MAChB,oBAAC,QAAD;EAEE,WAAW;EACX,OAAO,MAAM;EAEZ,UAAA,MAAM;CACH,GALC,CAKD,CACP,EACO,GAVK,CAUL,CACX;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eventuras/ratio-ui-shiki",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Alpha — Shiki syntax highlighting for Ratio UI's CodeBlock",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"homepage": "https://github.com/losol/ratio-ui/tree/main/packages/ratio-ui-shiki#readme",
|
|
@@ -32,6 +32,10 @@
|
|
|
32
32
|
"./useShikiHighlighter": {
|
|
33
33
|
"types": "./dist/useShikiHighlighter/index.d.ts",
|
|
34
34
|
"import": "./dist/useShikiHighlighter/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./tokens": {
|
|
37
|
+
"types": "./dist/tokens/index.d.ts",
|
|
38
|
+
"import": "./dist/tokens/index.js"
|
|
35
39
|
}
|
|
36
40
|
},
|
|
37
41
|
"main": "./dist/index.js",
|
|
@@ -45,26 +49,28 @@
|
|
|
45
49
|
"tag": "alpha"
|
|
46
50
|
},
|
|
47
51
|
"devDependencies": {
|
|
48
|
-
"@
|
|
49
|
-
"@
|
|
50
|
-
"@
|
|
51
|
-
"@
|
|
52
|
-
"
|
|
52
|
+
"@eventuras/eslint-config": "^1.0.4",
|
|
53
|
+
"@eventuras/typescript-config": "^1.0.0",
|
|
54
|
+
"@eventuras/vite-config": "^0.3.1",
|
|
55
|
+
"@shikijs/langs": "^4.4.3",
|
|
56
|
+
"@shikijs/themes": "^4.4.3",
|
|
57
|
+
"@types/react": "^19.3.0",
|
|
58
|
+
"@types/react-dom": "^19.3.0",
|
|
59
|
+
"shiki": "^4.4.3",
|
|
53
60
|
"typescript": "6.0.3",
|
|
54
|
-
"vite": "8.
|
|
55
|
-
"
|
|
56
|
-
"@ratio-ui
|
|
57
|
-
"@ratio-ui/typescript-config": "1.0.0",
|
|
58
|
-
"@ratio-ui/eslint-config": "1.0.3"
|
|
61
|
+
"vite": "8.3.0",
|
|
62
|
+
"vitest": "4.1.11",
|
|
63
|
+
"@eventuras/ratio-ui": "2.31.1"
|
|
59
64
|
},
|
|
60
65
|
"peerDependencies": {
|
|
61
66
|
"@eventuras/ratio-ui": "^2.13.0",
|
|
62
|
-
"@shikijs/langs": "^
|
|
63
|
-
"@shikijs/themes": "^
|
|
64
|
-
"shiki": "^
|
|
67
|
+
"@shikijs/langs": "^4.4.3",
|
|
68
|
+
"@shikijs/themes": "^4.4.3",
|
|
69
|
+
"shiki": "^4.4.3"
|
|
65
70
|
},
|
|
66
71
|
"scripts": {
|
|
67
72
|
"build": "tsc && vite build",
|
|
68
|
-
"lint": "eslint ."
|
|
73
|
+
"lint": "eslint .",
|
|
74
|
+
"test": "vitest run"
|
|
69
75
|
}
|
|
70
76
|
}
|