@cerebriline/ui 0.0.0-stage → 4.100.313
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/ADOPTION.md +833 -0
- package/LICENSE +201 -0
- package/RADIX-COLORS-LICENSE +21 -0
- package/README.md +427 -2
- package/components/agent-approval-card.css +70 -0
- package/components/agent-approval-card.tsx +109 -0
- package/components/agent-ask-question.css +103 -0
- package/components/agent-ask-question.tsx +276 -0
- package/components/agent-aurora.css +228 -0
- package/components/agent-aurora.tsx +96 -0
- package/components/agent-changes.css +13 -0
- package/components/agent-changes.tsx +167 -0
- package/components/agent-chat/agent-chat.css +556 -0
- package/components/agent-chat/index.tsx +1037 -0
- package/components/agent-chat/tool-diff.tsx +165 -0
- package/components/agent-chat/tool-summary/agents.ts +114 -0
- package/components/agent-chat/tool-summary/diff.ts +152 -0
- package/components/agent-chat/tool-summary/index.ts +928 -0
- package/components/agent-chat/tool-summary/parsers.ts +611 -0
- package/components/agent-chat/tool-summary/paths.ts +84 -0
- package/components/agent-chat/tool-summary/team.ts +401 -0
- package/components/agent-command-output.tsx +63 -0
- package/components/agent-composer.tsx +263 -0
- package/components/agent-context-usage.tsx +181 -0
- package/components/agent-conversation-layout.tsx +139 -0
- package/components/agent-hero-heading.css +53 -0
- package/components/agent-hero-heading.tsx +45 -0
- package/components/agent-image-lightbox.tsx +48 -0
- package/components/agent-prompt-queue.css +20 -0
- package/components/agent-prompt-queue.tsx +350 -0
- package/components/agent-pull-request-bar.css +13 -0
- package/components/agent-pull-request-bar.tsx +346 -0
- package/components/agent-quick-actions.css +19 -0
- package/components/agent-quick-actions.tsx +70 -0
- package/components/agent-session-row.tsx +183 -0
- package/components/agent-welcome-hero-pointer.ts +199 -0
- package/components/agent-welcome-hero.css +205 -0
- package/components/agent-welcome-hero.tsx +68 -0
- package/components/attachment-drop-zone.tsx +154 -0
- package/components/badge.tsx +21 -0
- package/components/button.tsx +324 -0
- package/components/generated-media.tsx +173 -0
- package/components/github-icon.tsx +19 -0
- package/components/index.ts +133 -0
- package/components/markdown.css +490 -0
- package/components/markdown.ts +365 -0
- package/components/mermaid-block.tsx +722 -0
- package/components/mermaid-diagram.ts +1260 -0
- package/components/mermaid-dom.ts +301 -0
- package/components/message-scroller.css +118 -0
- package/components/message-scroller.tsx +114 -0
- package/components/search-combobox.css +67 -0
- package/components/search-combobox.tsx +433 -0
- package/components/session-status.css +19 -0
- package/components/session-status.tsx +39 -0
- package/components/switch.css +221 -0
- package/components/switch.tsx +48 -0
- package/components.css +16 -0
- package/dist/components/agent-approval-card.d.ts +14 -0
- package/dist/components/agent-approval-card.d.ts.map +1 -0
- package/dist/components/agent-approval-card.js +12 -0
- package/dist/components/agent-approval-card.js.map +1 -0
- package/dist/components/agent-ask-question.d.ts +18 -0
- package/dist/components/agent-ask-question.d.ts.map +1 -0
- package/dist/components/agent-ask-question.js +101 -0
- package/dist/components/agent-ask-question.js.map +1 -0
- package/dist/components/agent-aurora.d.ts +3 -0
- package/dist/components/agent-aurora.d.ts.map +1 -0
- package/dist/components/agent-aurora.js +60 -0
- package/dist/components/agent-aurora.js.map +1 -0
- package/dist/components/agent-changes.d.ts +26 -0
- package/dist/components/agent-changes.d.ts.map +1 -0
- package/dist/components/agent-changes.js +19 -0
- package/dist/components/agent-changes.js.map +1 -0
- package/dist/components/agent-chat/disclosure.d.ts +25 -0
- package/dist/components/agent-chat/disclosure.d.ts.map +1 -0
- package/dist/components/agent-chat/disclosure.js +39 -0
- package/dist/components/agent-chat/disclosure.js.map +1 -0
- package/dist/components/agent-chat/index.d.ts +140 -0
- package/dist/components/agent-chat/index.d.ts.map +1 -0
- package/dist/components/agent-chat/index.js +379 -0
- package/dist/components/agent-chat/index.js.map +1 -0
- package/dist/components/agent-chat/tool-diff.d.ts +32 -0
- package/dist/components/agent-chat/tool-diff.d.ts.map +1 -0
- package/dist/components/agent-chat/tool-diff.js +97 -0
- package/dist/components/agent-chat/tool-diff.js.map +1 -0
- package/dist/components/agent-chat/tool-summary/agents.d.ts +10 -0
- package/dist/components/agent-chat/tool-summary/agents.d.ts.map +1 -0
- package/dist/components/agent-chat/tool-summary/agents.js +100 -0
- package/dist/components/agent-chat/tool-summary/agents.js.map +1 -0
- package/dist/components/agent-chat/tool-summary/diff.d.ts +22 -0
- package/dist/components/agent-chat/tool-summary/diff.d.ts.map +1 -0
- package/dist/components/agent-chat/tool-summary/diff.js +127 -0
- package/dist/components/agent-chat/tool-summary/diff.js.map +1 -0
- package/dist/components/agent-chat/tool-summary/index.d.ts +145 -0
- package/dist/components/agent-chat/tool-summary/index.d.ts.map +1 -0
- package/dist/components/agent-chat/tool-summary/index.js +663 -0
- package/dist/components/agent-chat/tool-summary/index.js.map +1 -0
- package/dist/components/agent-chat/tool-summary/parsers.d.ts +114 -0
- package/dist/components/agent-chat/tool-summary/parsers.d.ts.map +1 -0
- package/dist/components/agent-chat/tool-summary/parsers.js +502 -0
- package/dist/components/agent-chat/tool-summary/parsers.js.map +1 -0
- package/dist/components/agent-chat/tool-summary/paths.d.ts +10 -0
- package/dist/components/agent-chat/tool-summary/paths.d.ts.map +1 -0
- package/dist/components/agent-chat/tool-summary/paths.js +88 -0
- package/dist/components/agent-chat/tool-summary/paths.js.map +1 -0
- package/dist/components/agent-chat/tool-summary/team.d.ts +16 -0
- package/dist/components/agent-chat/tool-summary/team.d.ts.map +1 -0
- package/dist/components/agent-chat/tool-summary/team.js +283 -0
- package/dist/components/agent-chat/tool-summary/team.js.map +1 -0
- package/dist/components/agent-command-output.d.ts +14 -0
- package/dist/components/agent-command-output.d.ts.map +1 -0
- package/dist/components/agent-command-output.js +22 -0
- package/dist/components/agent-command-output.js.map +1 -0
- package/dist/components/agent-composer.d.ts +34 -0
- package/dist/components/agent-composer.d.ts.map +1 -0
- package/dist/components/agent-composer.js +65 -0
- package/dist/components/agent-composer.js.map +1 -0
- package/dist/components/agent-context-usage.d.ts +28 -0
- package/dist/components/agent-context-usage.d.ts.map +1 -0
- package/dist/components/agent-context-usage.js +51 -0
- package/dist/components/agent-context-usage.js.map +1 -0
- package/dist/components/agent-conversation-layout.d.ts +30 -0
- package/dist/components/agent-conversation-layout.d.ts.map +1 -0
- package/dist/components/agent-conversation-layout.js +24 -0
- package/dist/components/agent-conversation-layout.js.map +1 -0
- package/dist/components/agent-hero-heading.d.ts +2 -0
- package/dist/components/agent-hero-heading.d.ts.map +1 -0
- package/dist/components/agent-hero-heading.js +20 -0
- package/dist/components/agent-hero-heading.js.map +1 -0
- package/dist/components/agent-image-lightbox.d.ts +11 -0
- package/dist/components/agent-image-lightbox.d.ts.map +1 -0
- package/dist/components/agent-image-lightbox.js +8 -0
- package/dist/components/agent-image-lightbox.js.map +1 -0
- package/dist/components/agent-prompt-queue.d.ts +14 -0
- package/dist/components/agent-prompt-queue.d.ts.map +1 -0
- package/dist/components/agent-prompt-queue.js +114 -0
- package/dist/components/agent-prompt-queue.js.map +1 -0
- package/dist/components/agent-pull-request-bar.d.ts +72 -0
- package/dist/components/agent-pull-request-bar.d.ts.map +1 -0
- package/dist/components/agent-pull-request-bar.js +90 -0
- package/dist/components/agent-pull-request-bar.js.map +1 -0
- package/dist/components/agent-quick-actions.d.ts +14 -0
- package/dist/components/agent-quick-actions.d.ts.map +1 -0
- package/dist/components/agent-quick-actions.js +12 -0
- package/dist/components/agent-quick-actions.js.map +1 -0
- package/dist/components/agent-session-row.d.ts +45 -0
- package/dist/components/agent-session-row.d.ts.map +1 -0
- package/dist/components/agent-session-row.js +34 -0
- package/dist/components/agent-session-row.js.map +1 -0
- package/dist/components/agent-welcome-hero-pointer.d.ts +3 -0
- package/dist/components/agent-welcome-hero-pointer.d.ts.map +1 -0
- package/dist/components/agent-welcome-hero-pointer.js +169 -0
- package/dist/components/agent-welcome-hero-pointer.js.map +1 -0
- package/dist/components/agent-welcome-hero.d.ts +9 -0
- package/dist/components/agent-welcome-hero.d.ts.map +1 -0
- package/dist/components/agent-welcome-hero.js +14 -0
- package/dist/components/agent-welcome-hero.js.map +1 -0
- package/dist/components/attachment-drop-zone.d.ts +15 -0
- package/dist/components/attachment-drop-zone.d.ts.map +1 -0
- package/dist/components/attachment-drop-zone.js +64 -0
- package/dist/components/attachment-drop-zone.js.map +1 -0
- package/dist/components/badge.d.ts +5 -0
- package/dist/components/badge.d.ts.map +1 -0
- package/dist/components/badge.js +7 -0
- package/dist/components/badge.js.map +1 -0
- package/dist/components/button.d.ts +27 -0
- package/dist/components/button.d.ts.map +1 -0
- package/dist/components/button.js +209 -0
- package/dist/components/button.js.map +1 -0
- package/dist/components/generated-media.d.ts +12 -0
- package/dist/components/generated-media.d.ts.map +1 -0
- package/dist/components/generated-media.js +105 -0
- package/dist/components/generated-media.js.map +1 -0
- package/dist/components/github-icon.d.ts +3 -0
- package/dist/components/github-icon.d.ts.map +1 -0
- package/dist/components/github-icon.js +6 -0
- package/dist/components/github-icon.js.map +1 -0
- package/dist/components/index.d.ts +24 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +25 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/markdown.d.ts +48 -0
- package/dist/components/markdown.d.ts.map +1 -0
- package/dist/components/markdown.js +264 -0
- package/dist/components/markdown.js.map +1 -0
- package/dist/components/mermaid-block.d.ts +14 -0
- package/dist/components/mermaid-block.d.ts.map +1 -0
- package/dist/components/mermaid-block.js +339 -0
- package/dist/components/mermaid-block.js.map +1 -0
- package/dist/components/mermaid-diagram.d.ts +264 -0
- package/dist/components/mermaid-diagram.d.ts.map +1 -0
- package/dist/components/mermaid-diagram.js +999 -0
- package/dist/components/mermaid-diagram.js.map +1 -0
- package/dist/components/mermaid-dom.d.ts +51 -0
- package/dist/components/mermaid-dom.d.ts.map +1 -0
- package/dist/components/mermaid-dom.js +246 -0
- package/dist/components/mermaid-dom.js.map +1 -0
- package/dist/components/message-scroller.d.ts +23 -0
- package/dist/components/message-scroller.d.ts.map +1 -0
- package/dist/components/message-scroller.js +31 -0
- package/dist/components/message-scroller.js.map +1 -0
- package/dist/components/search-combobox.d.ts +44 -0
- package/dist/components/search-combobox.d.ts.map +1 -0
- package/dist/components/search-combobox.js +191 -0
- package/dist/components/search-combobox.js.map +1 -0
- package/dist/components/session-status.d.ts +9 -0
- package/dist/components/session-status.d.ts.map +1 -0
- package/dist/components/session-status.js +11 -0
- package/dist/components/session-status.js.map +1 -0
- package/dist/components/switch.d.ts +7 -0
- package/dist/components/switch.d.ts.map +1 -0
- package/dist/components/switch.js +14 -0
- package/dist/components/switch.js.map +1 -0
- package/package.json +177 -5
- package/theme/base.css +83 -0
- package/theme/component-theme.css +126 -0
- package/theme/index.css +3 -0
- package/theme/palette.css +347 -0
- package/theme/scoped-tokens.css +503 -0
- package/theme/theme.css +90 -0
- package/theme/tokens.css +165 -0
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import type { MermaidConfig } from "mermaid";
|
|
2
|
+
import type {
|
|
3
|
+
HighlighterCore,
|
|
4
|
+
LanguageRegistration,
|
|
5
|
+
ThemeRegistration,
|
|
6
|
+
} from "shiki/core";
|
|
7
|
+
import type {
|
|
8
|
+
CodeHighlighterPlugin,
|
|
9
|
+
ControlsConfig,
|
|
10
|
+
DiagramPlugin,
|
|
11
|
+
} from "streamdown";
|
|
12
|
+
import {
|
|
13
|
+
buildMermaidConfig,
|
|
14
|
+
createDefaultMermaidConfig,
|
|
15
|
+
createMermaidService,
|
|
16
|
+
defaultMermaidLoader,
|
|
17
|
+
type LazyMermaidInstance,
|
|
18
|
+
type MermaidModuleLoader,
|
|
19
|
+
} from "./mermaid-diagram.js";
|
|
20
|
+
import { readMermaidTheme } from "./mermaid-dom.js";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Shared Streamdown configuration for agent chat Markdown, so every product
|
|
24
|
+
* renders assistant prose the same way. Products keep their own Streamdown
|
|
25
|
+
* wrapper (link policy, image policy, extra plugins) and pass these in:
|
|
26
|
+
*
|
|
27
|
+
* <Streamdown
|
|
28
|
+
* className="cline-markdown"
|
|
29
|
+
* controls={agentMarkdownControls}
|
|
30
|
+
* plugins={{ code: markdownCodeHighlighter }}
|
|
31
|
+
* >
|
|
32
|
+
*
|
|
33
|
+
* Pair with `@cerebriline/ui/components/markdown.css` for the matching visual
|
|
34
|
+
* treatment (single quiet code blocks with a hover copy control, chat-scale
|
|
35
|
+
* headings, table cards).
|
|
36
|
+
*
|
|
37
|
+
* Runtime requirements (optional peer dependencies): `streamdown`, `shiki`,
|
|
38
|
+
* `@shikijs/langs`, and `@shikijs/themes`.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
type HighlightResult = NonNullable<
|
|
42
|
+
ReturnType<CodeHighlighterPlugin["highlight"]>
|
|
43
|
+
>;
|
|
44
|
+
|
|
45
|
+
/** Copy on code blocks; no downloads; no mermaid/table chrome. The matching
|
|
46
|
+
* CSS hides the code-block header and reveals the copy control on hover. */
|
|
47
|
+
export const agentMarkdownControls = {
|
|
48
|
+
code: { copy: true, download: false },
|
|
49
|
+
mermaid: false,
|
|
50
|
+
table: false,
|
|
51
|
+
} satisfies ControlsConfig;
|
|
52
|
+
|
|
53
|
+
/** Desktop/host opt-in controls for interactive Mermaid diagrams. The default
|
|
54
|
+
* controls above stay diagram-free so existing consumers don't change unless
|
|
55
|
+
* they also register a Mermaid DiagramPlugin. */
|
|
56
|
+
export const agentMarkdownControlsWithMermaid = {
|
|
57
|
+
...agentMarkdownControls,
|
|
58
|
+
mermaid: {
|
|
59
|
+
copy: true,
|
|
60
|
+
download: true,
|
|
61
|
+
fullscreen: true,
|
|
62
|
+
panZoom: true,
|
|
63
|
+
},
|
|
64
|
+
} satisfies ControlsConfig;
|
|
65
|
+
|
|
66
|
+
export {
|
|
67
|
+
buildMermaidConfig,
|
|
68
|
+
buildMermaidThemeVariables,
|
|
69
|
+
createDefaultMermaidConfig,
|
|
70
|
+
DIAGRAM_LINK_HREF_ATTRIBUTE,
|
|
71
|
+
type MermaidColorMode,
|
|
72
|
+
type MermaidColorResolver,
|
|
73
|
+
type MermaidModuleLoader,
|
|
74
|
+
type MermaidThemeOptions,
|
|
75
|
+
type MermaidThemeTokens,
|
|
76
|
+
neutralizeDiagramLinks,
|
|
77
|
+
} from "./mermaid-diagram.js";
|
|
78
|
+
|
|
79
|
+
/** Base-theme defaults from the palette (light); hosts with a DOM resolve the
|
|
80
|
+
* live design tokens instead, see `resolveThemedMermaidConfig`. */
|
|
81
|
+
export const DEFAULT_MERMAID_CONFIG: MermaidConfig =
|
|
82
|
+
createDefaultMermaidConfig("light");
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Creates a Streamdown Mermaid *diagram plugin* (Streamdown's built-in block
|
|
86
|
+
* chrome). Hosts that want the owned block (filename header, PNG/MMD
|
|
87
|
+
* downloads, themed PNGs) register `createMermaidRenderer` from
|
|
88
|
+
* `@cerebriline/ui/components/mermaid-block` instead.
|
|
89
|
+
*
|
|
90
|
+
* The default config is resolved from the live Cline design tokens at render
|
|
91
|
+
* time (falling back to the palette without a DOM) and re-applied when the
|
|
92
|
+
* resolved theme changes; configs supplied through `getMermaid(config)` layer
|
|
93
|
+
* on top, with `securityLevel` always forced to `strict`.
|
|
94
|
+
*/
|
|
95
|
+
export function createLazyMermaidPlugin(
|
|
96
|
+
loader: MermaidModuleLoader = defaultMermaidLoader,
|
|
97
|
+
): DiagramPlugin {
|
|
98
|
+
const service = createMermaidService(loader);
|
|
99
|
+
let overrides: MermaidConfig = {};
|
|
100
|
+
let cached: { config: MermaidConfig; key: string } | undefined;
|
|
101
|
+
|
|
102
|
+
const instance: LazyMermaidInstance = {
|
|
103
|
+
initialize(nextConfig: MermaidConfig) {
|
|
104
|
+
overrides = { ...overrides, ...nextConfig };
|
|
105
|
+
cached = undefined;
|
|
106
|
+
},
|
|
107
|
+
render(id: string, source: string) {
|
|
108
|
+
const theme = readMermaidTheme();
|
|
109
|
+
// One config object per theme key so the shared service only calls
|
|
110
|
+
// `initialize` when the theme or the overrides change.
|
|
111
|
+
if (cached?.key !== theme.key) {
|
|
112
|
+
cached = {
|
|
113
|
+
config: {
|
|
114
|
+
...buildMermaidConfig(theme.tokens, theme.mode, {
|
|
115
|
+
fontFamily: theme.fontFamily,
|
|
116
|
+
}),
|
|
117
|
+
...overrides,
|
|
118
|
+
securityLevel: "strict",
|
|
119
|
+
},
|
|
120
|
+
key: theme.key,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return service.render(id, source, cached.config);
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
getMermaid(nextConfig?: MermaidConfig) {
|
|
129
|
+
if (nextConfig) instance.initialize(nextConfig);
|
|
130
|
+
return instance;
|
|
131
|
+
},
|
|
132
|
+
language: "mermaid",
|
|
133
|
+
name: "mermaid",
|
|
134
|
+
type: "diagram",
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export const SUPPORTED_MARKDOWN_LANGUAGES = [
|
|
139
|
+
"bash",
|
|
140
|
+
"css",
|
|
141
|
+
"diff",
|
|
142
|
+
"html",
|
|
143
|
+
"javascript",
|
|
144
|
+
"json",
|
|
145
|
+
"jsonc",
|
|
146
|
+
"jsx",
|
|
147
|
+
"markdown",
|
|
148
|
+
"python",
|
|
149
|
+
"shellscript",
|
|
150
|
+
"tsx",
|
|
151
|
+
"typescript",
|
|
152
|
+
"yaml",
|
|
153
|
+
] as const;
|
|
154
|
+
|
|
155
|
+
type SupportedMarkdownLanguage = (typeof SUPPORTED_MARKDOWN_LANGUAGES)[number];
|
|
156
|
+
|
|
157
|
+
const SUPPORTED_LANGUAGE_SET = new Set<string>(SUPPORTED_MARKDOWN_LANGUAGES);
|
|
158
|
+
|
|
159
|
+
const LANGUAGE_ALIASES: Record<string, SupportedMarkdownLanguage> = {
|
|
160
|
+
cjs: "javascript",
|
|
161
|
+
console: "shellscript",
|
|
162
|
+
htm: "html",
|
|
163
|
+
js: "javascript",
|
|
164
|
+
json5: "jsonc",
|
|
165
|
+
md: "markdown",
|
|
166
|
+
mjs: "javascript",
|
|
167
|
+
py: "python",
|
|
168
|
+
sh: "shellscript",
|
|
169
|
+
shell: "shellscript",
|
|
170
|
+
ts: "typescript",
|
|
171
|
+
yml: "yaml",
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const LANGUAGE_LOADERS: Record<
|
|
175
|
+
SupportedMarkdownLanguage,
|
|
176
|
+
() => Promise<LanguageRegistration[]>
|
|
177
|
+
> = {
|
|
178
|
+
bash: () => import("@shikijs/langs/bash").then((module) => module.default),
|
|
179
|
+
css: () => import("@shikijs/langs/css").then((module) => module.default),
|
|
180
|
+
diff: () => import("@shikijs/langs/diff").then((module) => module.default),
|
|
181
|
+
html: () => import("@shikijs/langs/html").then((module) => module.default),
|
|
182
|
+
javascript: () =>
|
|
183
|
+
import("@shikijs/langs/javascript").then((module) => module.default),
|
|
184
|
+
json: () => import("@shikijs/langs/json").then((module) => module.default),
|
|
185
|
+
jsonc: () => import("@shikijs/langs/jsonc").then((module) => module.default),
|
|
186
|
+
jsx: () => import("@shikijs/langs/jsx").then((module) => module.default),
|
|
187
|
+
markdown: () =>
|
|
188
|
+
import("@shikijs/langs/markdown").then((module) => module.default),
|
|
189
|
+
python: () =>
|
|
190
|
+
import("@shikijs/langs/python").then((module) => module.default),
|
|
191
|
+
shellscript: () =>
|
|
192
|
+
import("@shikijs/langs/shellscript").then((module) => module.default),
|
|
193
|
+
tsx: () => import("@shikijs/langs/tsx").then((module) => module.default),
|
|
194
|
+
typescript: () =>
|
|
195
|
+
import("@shikijs/langs/typescript").then((module) => module.default),
|
|
196
|
+
yaml: () => import("@shikijs/langs/yaml").then((module) => module.default),
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const LIGHT_THEME = "github-light";
|
|
200
|
+
const DARK_THEME = "github-dark";
|
|
201
|
+
const MAX_CACHED_RESULTS = 256;
|
|
202
|
+
|
|
203
|
+
let highlighterPromise: Promise<HighlighterCore> | undefined;
|
|
204
|
+
let themesPromise: Promise<void> | undefined;
|
|
205
|
+
const languagePromises = new Map<SupportedMarkdownLanguage, Promise<void>>();
|
|
206
|
+
const resultCache = new Map<string, HighlightResult>();
|
|
207
|
+
const pendingHighlights = new Map<string, Promise<HighlightResult>>();
|
|
208
|
+
const loggedFailures = new Set<string>();
|
|
209
|
+
|
|
210
|
+
function normalizeLanguage(language: string): SupportedMarkdownLanguage | null {
|
|
211
|
+
const normalized = language.trim().toLowerCase();
|
|
212
|
+
if (!normalized) return null;
|
|
213
|
+
const aliased = LANGUAGE_ALIASES[normalized] ?? normalized;
|
|
214
|
+
return SUPPORTED_LANGUAGE_SET.has(aliased)
|
|
215
|
+
? (aliased as SupportedMarkdownLanguage)
|
|
216
|
+
: null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function getHighlighter(): Promise<HighlighterCore> {
|
|
220
|
+
if (!highlighterPromise) {
|
|
221
|
+
highlighterPromise = Promise.all([
|
|
222
|
+
import("shiki/core"),
|
|
223
|
+
import("shiki/engine/javascript"),
|
|
224
|
+
]).then(([core, engine]) =>
|
|
225
|
+
core.createHighlighterCore({
|
|
226
|
+
engine: engine.createJavaScriptRegexEngine({ forgiving: true }),
|
|
227
|
+
}),
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
return highlighterPromise;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function ensureThemes(highlighter: HighlighterCore): Promise<void> {
|
|
234
|
+
if (!themesPromise) {
|
|
235
|
+
themesPromise = Promise.all([
|
|
236
|
+
import("@shikijs/themes/github-light").then((module) => module.default),
|
|
237
|
+
import("@shikijs/themes/github-dark").then((module) => module.default),
|
|
238
|
+
]).then((themes: ThemeRegistration[]) => highlighter.loadTheme(...themes));
|
|
239
|
+
}
|
|
240
|
+
return themesPromise;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function ensureLanguage(
|
|
244
|
+
highlighter: HighlighterCore,
|
|
245
|
+
language: SupportedMarkdownLanguage,
|
|
246
|
+
): Promise<void> {
|
|
247
|
+
const existing = languagePromises.get(language);
|
|
248
|
+
if (existing) return existing;
|
|
249
|
+
|
|
250
|
+
const loading = LANGUAGE_LOADERS[language]().then((registrations) =>
|
|
251
|
+
highlighter.loadLanguage(...registrations),
|
|
252
|
+
);
|
|
253
|
+
languagePromises.set(language, loading);
|
|
254
|
+
return loading;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function rawHighlight(code: string): HighlightResult {
|
|
258
|
+
return {
|
|
259
|
+
bg: "transparent",
|
|
260
|
+
fg: "inherit",
|
|
261
|
+
tokens: code.split("\n").map((line) =>
|
|
262
|
+
line
|
|
263
|
+
? [
|
|
264
|
+
{
|
|
265
|
+
bgColor: "transparent",
|
|
266
|
+
color: "inherit",
|
|
267
|
+
content: line,
|
|
268
|
+
htmlStyle: {},
|
|
269
|
+
offset: 0,
|
|
270
|
+
},
|
|
271
|
+
]
|
|
272
|
+
: [],
|
|
273
|
+
),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function cacheResult(key: string, result: HighlightResult): void {
|
|
278
|
+
resultCache.delete(key);
|
|
279
|
+
resultCache.set(key, result);
|
|
280
|
+
if (resultCache.size <= MAX_CACHED_RESULTS) return;
|
|
281
|
+
|
|
282
|
+
const oldestKey = resultCache.keys().next().value;
|
|
283
|
+
if (oldestKey !== undefined) resultCache.delete(oldestKey);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function reportHighlightFailure(
|
|
287
|
+
language: SupportedMarkdownLanguage,
|
|
288
|
+
error: unknown,
|
|
289
|
+
): void {
|
|
290
|
+
if (loggedFailures.has(language)) return;
|
|
291
|
+
loggedFailures.add(language);
|
|
292
|
+
console.warn(
|
|
293
|
+
`Syntax highlighting unavailable for ${language}; rendering plain code.`,
|
|
294
|
+
error,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function loadHighlight(
|
|
299
|
+
code: string,
|
|
300
|
+
language: SupportedMarkdownLanguage,
|
|
301
|
+
): Promise<HighlightResult> {
|
|
302
|
+
const cacheKey = `${language}\0${code}`;
|
|
303
|
+
const cached = resultCache.get(cacheKey);
|
|
304
|
+
if (cached) return Promise.resolve(cached);
|
|
305
|
+
|
|
306
|
+
const pending = pendingHighlights.get(cacheKey);
|
|
307
|
+
if (pending) return pending;
|
|
308
|
+
|
|
309
|
+
const loading = getHighlighter()
|
|
310
|
+
.then(async (highlighter) => {
|
|
311
|
+
await ensureThemes(highlighter);
|
|
312
|
+
await ensureLanguage(highlighter, language);
|
|
313
|
+
const result = highlighter.codeToTokens(code, {
|
|
314
|
+
lang: language,
|
|
315
|
+
themes: {
|
|
316
|
+
dark: DARK_THEME,
|
|
317
|
+
light: LIGHT_THEME,
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
return {
|
|
321
|
+
bg: result.bg,
|
|
322
|
+
fg: result.fg,
|
|
323
|
+
rootStyle: result.rootStyle,
|
|
324
|
+
tokens: result.tokens,
|
|
325
|
+
} satisfies HighlightResult;
|
|
326
|
+
})
|
|
327
|
+
.catch((error: unknown) => {
|
|
328
|
+
reportHighlightFailure(language, error);
|
|
329
|
+
return rawHighlight(code);
|
|
330
|
+
})
|
|
331
|
+
.then((result) => {
|
|
332
|
+
cacheResult(cacheKey, result);
|
|
333
|
+
return result;
|
|
334
|
+
})
|
|
335
|
+
.finally(() => {
|
|
336
|
+
pendingHighlights.delete(cacheKey);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
pendingHighlights.set(cacheKey, loading);
|
|
340
|
+
return loading;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Annotated (not `satisfies`) so the emitted declaration names the public
|
|
344
|
+
// CodeHighlighterPlugin type instead of streamdown's unexported internals.
|
|
345
|
+
export const markdownCodeHighlighter: CodeHighlighterPlugin = {
|
|
346
|
+
getSupportedLanguages: () => [...SUPPORTED_MARKDOWN_LANGUAGES],
|
|
347
|
+
getThemes: () => [LIGHT_THEME, DARK_THEME],
|
|
348
|
+
highlight: ({ code, language }, callback) => {
|
|
349
|
+
const normalizedLanguage = normalizeLanguage(language);
|
|
350
|
+
if (!normalizedLanguage) return rawHighlight(code);
|
|
351
|
+
|
|
352
|
+
const cacheKey = `${normalizedLanguage}\0${code}`;
|
|
353
|
+
const cached = resultCache.get(cacheKey);
|
|
354
|
+
if (cached) return cached;
|
|
355
|
+
|
|
356
|
+
const loading = loadHighlight(code, normalizedLanguage);
|
|
357
|
+
if (callback) {
|
|
358
|
+
void loading.then((result) => callback(result));
|
|
359
|
+
}
|
|
360
|
+
return null;
|
|
361
|
+
},
|
|
362
|
+
name: "shiki",
|
|
363
|
+
supportsLanguage: (language) => normalizeLanguage(language) !== null,
|
|
364
|
+
type: "code-highlighter",
|
|
365
|
+
};
|