@surflane/ui 0.8.0 → 0.9.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 +1 -1
- package/dist/syntax-context.d.ts +11 -2
- package/dist/syntax-context.js +34 -1
- package/dist/syntax-highlighter.js +34 -23
- package/dist/syntax-highlighting.d.ts +14 -1
- package/dist/syntax-highlighting.js +54 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -55,7 +55,7 @@ Chart, DatePicker, Markdown, Tree and syntax highlighting have their own entry p
|
|
|
55
55
|
| `Tree` from `@surflane/ui/tree` | `@headless-tree/core @headless-tree/react @tanstack/react-virtual` |
|
|
56
56
|
| `SyntaxHighlighting` from `@surflane/ui/syntax-highlighting` | `shiki` |
|
|
57
57
|
|
|
58
|
-
`CodeBlock` and `SyntaxHighlighter` render plain text until a `<SyntaxHighlighting>` provider wraps them.
|
|
58
|
+
`CodeBlock` and `SyntaxHighlighter` render plain text until a `<SyntaxHighlighting>` provider wraps them. With `<SyntaxHighlighting suspense>`, a server render waits for the tokens and writes highlighted HTML.
|
|
59
59
|
|
|
60
60
|
## Frameworks
|
|
61
61
|
|
package/dist/syntax-context.d.ts
CHANGED
|
@@ -3,5 +3,14 @@ import type { SyntaxToken } from './syntax-engine.js';
|
|
|
3
3
|
export type SyntaxEngine = {
|
|
4
4
|
highlightCode(code: string, language: string): Promise<SyntaxToken[][] | null>;
|
|
5
5
|
};
|
|
6
|
-
/**
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Loads the highlighting engine, and says whether code waits for its tokens inside
|
|
8
|
+
* Suspense. Without a provider, code renders as plain text.
|
|
9
|
+
*/
|
|
10
|
+
export declare const SyntaxEngineContext: React.Context<{
|
|
11
|
+
load: () => Promise<SyntaxEngine>;
|
|
12
|
+
suspense: boolean;
|
|
13
|
+
} | null>;
|
|
14
|
+
export declare function requestTokens(load: () => Promise<SyntaxEngine>, code: string, language: string): Promise<SyntaxToken[][] | null>;
|
|
15
|
+
/** Records tokens already known, such as those a server render wrote. */
|
|
16
|
+
export declare function provideTokens(code: string, language: string, tokens: SyntaxToken[][]): void;
|
package/dist/syntax-context.js
CHANGED
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import * as React from 'react';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Loads the highlighting engine, and says whether code waits for its tokens inside
|
|
5
|
+
* Suspense. Without a provider, code renders as plain text.
|
|
6
|
+
*/
|
|
4
7
|
export const SyntaxEngineContext = /* @__PURE__ */ React.createContext(null);
|
|
8
|
+
// One request per language and code, so a render that suspended finds its tokens again.
|
|
9
|
+
// A settled request carries React's status and value, so use() reads it at once.
|
|
10
|
+
const requests = new Map();
|
|
11
|
+
export function requestTokens(load, code, language) {
|
|
12
|
+
const key = `${language}\n${code}`;
|
|
13
|
+
let pending = requests.get(key);
|
|
14
|
+
if (!pending) {
|
|
15
|
+
const request = load()
|
|
16
|
+
.then((engine) => engine.highlightCode(code, language))
|
|
17
|
+
.catch(() => null);
|
|
18
|
+
void request.then((tokens) => Object.assign(request, { status: 'fulfilled', value: tokens }));
|
|
19
|
+
pending = request;
|
|
20
|
+
remember(key, pending);
|
|
21
|
+
}
|
|
22
|
+
return pending;
|
|
23
|
+
}
|
|
24
|
+
/** Records tokens already known, such as those a server render wrote. */
|
|
25
|
+
export function provideTokens(code, language, tokens) {
|
|
26
|
+
const key = `${language}\n${code}`;
|
|
27
|
+
if (!requests.has(key))
|
|
28
|
+
remember(key, Object.assign(Promise.resolve(tokens), {
|
|
29
|
+
status: 'fulfilled',
|
|
30
|
+
value: tokens,
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
function remember(key, request) {
|
|
34
|
+
requests.set(key, request);
|
|
35
|
+
if (requests.size > 500)
|
|
36
|
+
requests.delete(requests.keys().next().value);
|
|
37
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import * as React from 'react';
|
|
4
4
|
import { ScrollArea } from './scroll-area.js';
|
|
5
|
-
import { SyntaxEngineContext } from './syntax-context.js';
|
|
5
|
+
import { SyntaxEngineContext, requestTokens } from './syntax-context.js';
|
|
6
6
|
import { cx } from './parts.js';
|
|
7
7
|
export const syntaxLanguages = [
|
|
8
8
|
'tsx',
|
|
@@ -31,41 +31,52 @@ const aliases = {
|
|
|
31
31
|
plaintext: 'text',
|
|
32
32
|
txt: 'text',
|
|
33
33
|
};
|
|
34
|
+
function Highlighted({ tokens, render, }) {
|
|
35
|
+
return render(React.use(tokens));
|
|
36
|
+
}
|
|
34
37
|
/**
|
|
35
38
|
* Rendering remains selectable React text; it never inserts executable HTML. Tokens are
|
|
36
39
|
* colored inside a SyntaxHighlighting provider; without one, the code renders plain.
|
|
37
40
|
*/
|
|
38
41
|
export const SyntaxHighlighter = /* @__PURE__ */ React.forwardRef(function SyntaxHighlighter({ code, language = 'text', inline = false, label = 'Highlighted source', wrap = false, className, ...props }, ref) {
|
|
39
|
-
const
|
|
42
|
+
const source = React.useContext(SyntaxEngineContext);
|
|
40
43
|
const name = language.trim().toLowerCase();
|
|
41
44
|
const lang = aliases[name] ?? name;
|
|
45
|
+
const highlightable = source !== null &&
|
|
46
|
+
lang !== 'text' &&
|
|
47
|
+
syntaxLanguages.includes(lang) &&
|
|
48
|
+
code.length <= 50000;
|
|
49
|
+
// Without suspense, code shows plain until an effect brings its tokens.
|
|
42
50
|
const [result, setResult] = React.useState();
|
|
51
|
+
// A block waits only for the code it mounted with. Code that changes later is
|
|
52
|
+
// highlighted in place, so a boundary never hides shown code behind its fallback.
|
|
53
|
+
const [first] = React.useState(() => ({ code, lang }));
|
|
54
|
+
const suspense = highlightable &&
|
|
55
|
+
source.suspense &&
|
|
56
|
+
first.code === code &&
|
|
57
|
+
first.lang === lang;
|
|
43
58
|
React.useEffect(() => {
|
|
44
|
-
|
|
45
|
-
if (!load ||
|
|
46
|
-
lang === 'text' ||
|
|
47
|
-
!syntaxLanguages.includes(lang) ||
|
|
48
|
-
code.length > 50000)
|
|
59
|
+
if (!highlightable || suspense)
|
|
49
60
|
return;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (current)
|
|
61
|
+
let live = true;
|
|
62
|
+
void requestTokens(source.load, code, lang).then((tokens) => {
|
|
63
|
+
if (live)
|
|
54
64
|
setResult({ code, language: lang, tokens });
|
|
55
|
-
})
|
|
56
|
-
.catch(() => {
|
|
57
|
-
if (current)
|
|
58
|
-
setResult({ code, language: lang, tokens: null });
|
|
59
65
|
});
|
|
60
66
|
return () => {
|
|
61
|
-
|
|
67
|
+
live = false;
|
|
62
68
|
};
|
|
63
|
-
}, [code, lang,
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
}, [code, lang, source, highlightable, suspense]);
|
|
70
|
+
const render = (tokens) => {
|
|
71
|
+
const eols = code.match(/\r\n|\n|\r/g) ?? [];
|
|
72
|
+
const valid = tokens && tokens.length === eols.length + 1;
|
|
73
|
+
return (_jsx("code", { ...props, ref: ref, className: cx('pui-syntax', className), "data-language": lang, "data-highlighted": valid ? 'true' : 'false', children: valid
|
|
74
|
+
? tokens.map((line, i) => (_jsxs(React.Fragment, { children: [line.map((token, j) => (_jsx("span", { "data-syntax": token.role, children: token.content }, j))), eols[i] ?? ''] }, i)))
|
|
75
|
+
: code }));
|
|
76
|
+
};
|
|
77
|
+
// With suspense, the block waits for its tokens behind its own boundary, plain
|
|
78
|
+
// meanwhile: a server render writes it highlighted and hydration keeps that HTML,
|
|
79
|
+
// while a block that appears later never suspends anything around it.
|
|
80
|
+
const element = suspense ? (_jsx(React.Suspense, { fallback: render(null), children: _jsx(Highlighted, { tokens: requestTokens(source.load, code, lang), render: render }) })) : (render(result?.code === code && result.language === lang ? result.tokens : null));
|
|
70
81
|
return inline ? (element) : (_jsx(ScrollArea, { label: label, orientation: 'horizontal', rootClassName: 'pui-syntax-scroll', children: _jsx("pre", { className: 'pui-syntax-pre', "data-wrap": wrap || undefined, children: element }) }));
|
|
71
82
|
});
|
|
@@ -2,7 +2,20 @@ import * as React from 'react';
|
|
|
2
2
|
/**
|
|
3
3
|
* Turns on syntax highlighting for CodeBlock, SyntaxHighlighter and Markdown inside it.
|
|
4
4
|
* Requires the optional `shiki` package; without this provider, code renders as plain text.
|
|
5
|
+
* Code shows as plain text until its tokens arrive. With `suspense`, each block waits
|
|
6
|
+
* for them behind its own Suspense boundary instead, so a server render writes it
|
|
7
|
+
* highlighted and hydration keeps that HTML. Nothing around a block suspends.
|
|
5
8
|
*/
|
|
6
|
-
export declare function SyntaxHighlighting({ children, }: {
|
|
9
|
+
export declare function SyntaxHighlighting({ children, suspense, }: {
|
|
7
10
|
children?: React.ReactNode;
|
|
11
|
+
suspense?: boolean;
|
|
8
12
|
}): React.JSX.Element;
|
|
13
|
+
/**
|
|
14
|
+
* Reads the tokens of the server-rendered code under `root` from its HTML, so
|
|
15
|
+
* hydration needs neither the engine nor a grammar. With `suspense`, await it before
|
|
16
|
+
* `hydrateRoot`: every block then hydrates at once, so no boundary is left waiting for
|
|
17
|
+
* its tokens, where a context change above it would make React drop the highlighted
|
|
18
|
+
* HTML for the plain fallback. A block whose HTML doesn't read back as its code is
|
|
19
|
+
* highlighted again.
|
|
20
|
+
*/
|
|
21
|
+
export declare function preloadSyntaxHighlighting(root?: ParentNode): Promise<undefined>;
|
|
@@ -1,12 +1,63 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import { SyntaxEngineContext } from './syntax-context.js';
|
|
3
|
+
import { SyntaxEngineContext, provideTokens, requestTokens, } from './syntax-context.js';
|
|
4
4
|
// The engine and each grammar load on first use, in their own chunks.
|
|
5
5
|
const load = () => import('./syntax-engine.js');
|
|
6
|
+
const sources = {
|
|
7
|
+
plain: { load, suspense: false },
|
|
8
|
+
suspense: { load, suspense: true },
|
|
9
|
+
};
|
|
6
10
|
/**
|
|
7
11
|
* Turns on syntax highlighting for CodeBlock, SyntaxHighlighter and Markdown inside it.
|
|
8
12
|
* Requires the optional `shiki` package; without this provider, code renders as plain text.
|
|
13
|
+
* Code shows as plain text until its tokens arrive. With `suspense`, each block waits
|
|
14
|
+
* for them behind its own Suspense boundary instead, so a server render writes it
|
|
15
|
+
* highlighted and hydration keeps that HTML. Nothing around a block suspends.
|
|
9
16
|
*/
|
|
10
|
-
export function SyntaxHighlighting({ children, }) {
|
|
11
|
-
return (_jsx(SyntaxEngineContext.Provider, { value:
|
|
17
|
+
export function SyntaxHighlighting({ children, suspense = false, }) {
|
|
18
|
+
return (_jsx(SyntaxEngineContext.Provider, { value: suspense ? sources.suspense : sources.plain, children: children }));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Reads the tokens of the server-rendered code under `root` from its HTML, so
|
|
22
|
+
* hydration needs neither the engine nor a grammar. With `suspense`, await it before
|
|
23
|
+
* `hydrateRoot`: every block then hydrates at once, so no boundary is left waiting for
|
|
24
|
+
* its tokens, where a context change above it would make React drop the highlighted
|
|
25
|
+
* HTML for the plain fallback. A block whose HTML doesn't read back as its code is
|
|
26
|
+
* highlighted again.
|
|
27
|
+
*/
|
|
28
|
+
export function preloadSyntaxHighlighting(root = document) {
|
|
29
|
+
return Promise.all(Array.from(root.querySelectorAll('code.pui-syntax[data-highlighted="true"]'), (code) => {
|
|
30
|
+
const text = code.textContent ?? '';
|
|
31
|
+
const language = code.dataset.language;
|
|
32
|
+
const tokens = readTokens(code, text);
|
|
33
|
+
if (tokens)
|
|
34
|
+
provideTokens(text, language, tokens);
|
|
35
|
+
else
|
|
36
|
+
return requestTokens(load, text, language);
|
|
37
|
+
})).then(() => undefined);
|
|
38
|
+
}
|
|
39
|
+
// SyntaxHighlighter writes a line's tokens as spans and ends it with its line break.
|
|
40
|
+
function readTokens(code, text) {
|
|
41
|
+
const lines = [[]];
|
|
42
|
+
let read = '';
|
|
43
|
+
for (const node of code.childNodes) {
|
|
44
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
45
|
+
const content = node.textContent ?? '';
|
|
46
|
+
lines[lines.length - 1].push({
|
|
47
|
+
content,
|
|
48
|
+
role: node.dataset.syntax ?? '',
|
|
49
|
+
});
|
|
50
|
+
read += content;
|
|
51
|
+
}
|
|
52
|
+
else if (node.nodeType === Node.TEXT_NODE) {
|
|
53
|
+
const breaks = node.textContent ?? '';
|
|
54
|
+
if (!/^(\r\n|\n|\r)*$/.test(breaks))
|
|
55
|
+
return null;
|
|
56
|
+
const count = breaks.match(/\r\n|\n|\r/g)?.length ?? 0;
|
|
57
|
+
for (let line = 0; line < count; line++)
|
|
58
|
+
lines.push([]);
|
|
59
|
+
read += breaks;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return read === text ? lines : null;
|
|
12
63
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@surflane/ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Surflane React components built on Base UI, with data, chart, code and Markdown components.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"design-system",
|
|
@@ -407,7 +407,7 @@
|
|
|
407
407
|
"peerDependencies": {
|
|
408
408
|
"@headless-tree/core": "^1.8.0",
|
|
409
409
|
"@headless-tree/react": "^1.8.0",
|
|
410
|
-
"@surflane/design-tokens": "^0.
|
|
410
|
+
"@surflane/design-tokens": "^0.9.0",
|
|
411
411
|
"@tanstack/react-virtual": "^3.14.21",
|
|
412
412
|
"react": "^19.0.0",
|
|
413
413
|
"react-aria-components": "^1.22.1",
|