forked from vercel/streamdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
252 lines (218 loc) · 6.57 KB
/
index.ts
File metadata and controls
252 lines (218 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"use client";
import {
type BundledLanguage,
type BundledTheme,
type ThemeRegistrationAny,
bundledLanguages,
bundledLanguagesInfo,
createHighlighter,
type HighlighterGeneric,
type SpecialLanguage,
type TokensResult,
} from "shiki";
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
const jsEngine = createJavaScriptRegexEngine({ forgiving: true });
export type ThemeInput = BundledTheme | ThemeRegistrationAny;
/**
* Result from code highlighting
*/
export type HighlightResult = TokensResult;
/**
* Options for highlighting code
*/
export interface HighlightOptions {
code: string;
language: BundledLanguage;
themes: [ThemeInput, ThemeInput];
}
/**
* Plugin for code syntax highlighting (Shiki)
*/
export interface CodeHighlighterPlugin {
/**
* Get list of supported languages
*/
getSupportedLanguages: () => BundledLanguage[];
/**
* Get the configured themes
*/
getThemes: () => [ThemeInput, ThemeInput];
/**
* Highlight code and return tokens
* Returns null if highlighting not ready yet (async loading)
* Use callback for async result
*/
highlight: (
options: HighlightOptions,
callback?: (result: HighlightResult) => void
) => HighlightResult | null;
name: "shiki";
/**
* Check if language is supported
*/
supportsLanguage: (language: BundledLanguage) => boolean;
type: "code-highlighter";
}
/**
* Options for creating a code plugin
*/
export interface CodePluginOptions {
/**
* Default themes for syntax highlighting [light, dark]
* @default ["github-light", "github-dark"]
*/
themes?: [ThemeInput, ThemeInput];
}
const languageAliases = Object.fromEntries(
bundledLanguagesInfo.flatMap((info) =>
(info.aliases ?? []).map((alias) => [alias, info.id as BundledLanguage])
)
) as Record<string, BundledLanguage>;
// Build language name set for quick lookup
const languageNames = new Set<BundledLanguage>(
Object.keys(bundledLanguages) as BundledLanguage[]
);
const normalizeLanguage = (language: string): string => {
const trimmed = language.trim();
const lower = trimmed.toLowerCase();
const alias = languageAliases[lower];
if (alias) {
return alias;
}
if (languageNames.has(lower as BundledLanguage)) {
return lower;
}
return lower;
};
// Singleton highlighter cache
const highlighterCache = new Map<
string,
Promise<HighlighterGeneric<BundledLanguage, BundledTheme>>
>();
// Token cache
const tokensCache = new Map<string, TokensResult>();
// Subscribers for async token updates
const subscribers = new Map<string, Set<(result: TokensResult) => void>>();
const getThemeName = (theme: ThemeInput): string =>
typeof theme === "string" ? theme : (theme.name ?? "custom");
const getHighlighterCacheKey = (
language: BundledLanguage,
themes: [ThemeInput, ThemeInput]
) => `${language}-${getThemeName(themes[0])}-${getThemeName(themes[1])}`;
const getTokensCacheKey = (
code: string,
language: string,
themeNames: [string, string]
) => {
const start = code.slice(0, 100);
const end = code.length > 100 ? code.slice(-100) : "";
return `${language}:${themeNames[0]}:${themeNames[1]}:${code.length}:${start}:${end}`;
};
const getHighlighter = (
language: BundledLanguage,
themes: [ThemeInput, ThemeInput]
): Promise<HighlighterGeneric<BundledLanguage, BundledTheme>> => {
const cacheKey = getHighlighterCacheKey(language, themes);
if (highlighterCache.has(cacheKey)) {
return highlighterCache.get(cacheKey) as Promise<
HighlighterGeneric<BundledLanguage, BundledTheme>
>;
}
const highlighterPromise = createHighlighter({
themes,
langs: [language],
engine: jsEngine,
});
highlighterCache.set(cacheKey, highlighterPromise);
return highlighterPromise;
};
/**
* Create a code plugin with optional configuration
*/
export function createCodePlugin(
options: CodePluginOptions = {}
): CodeHighlighterPlugin {
const defaultThemes: [ThemeInput, ThemeInput] = options.themes ?? [
"github-light",
"github-dark",
];
return {
name: "shiki",
type: "code-highlighter",
supportsLanguage(language: BundledLanguage): boolean {
const resolvedLanguage = normalizeLanguage(language);
return languageNames.has(resolvedLanguage as BundledLanguage);
},
getSupportedLanguages(): BundledLanguage[] {
return Array.from(languageNames);
},
getThemes(): [ThemeInput, ThemeInput] {
return defaultThemes;
},
highlight(
{ code, language, themes }: HighlightOptions,
callback?: (result: HighlightResult) => void
): HighlightResult | null {
const resolvedLanguage = normalizeLanguage(language);
const themeNames: [string, string] = [
getThemeName(themes[0]),
getThemeName(themes[1]),
];
const tokensCacheKey = getTokensCacheKey(
code,
resolvedLanguage,
themeNames
);
// Return cached result if available
if (tokensCache.has(tokensCacheKey)) {
return tokensCache.get(tokensCacheKey) as TokensResult;
}
// Subscribe callback if provided
if (callback) {
if (!subscribers.has(tokensCacheKey)) {
subscribers.set(tokensCacheKey, new Set());
}
const subs = subscribers.get(tokensCacheKey) as Set<
(result: TokensResult) => void
>;
subs.add(callback);
}
// Start highlighting in background
getHighlighter(resolvedLanguage as BundledLanguage, themes)
.then((highlighter) => {
const availableLangs = highlighter.getLoadedLanguages();
const langToUse = (
availableLangs.includes(resolvedLanguage as BundledLanguage)
? (resolvedLanguage as BundledLanguage)
: "text"
) as BundledLanguage | SpecialLanguage;
const result = highlighter.codeToTokens(code, {
lang: langToUse,
themes: {
light: themeNames[0],
dark: themeNames[1],
},
});
// Cache the result
tokensCache.set(tokensCacheKey, result);
// Notify all subscribers
const subs = subscribers.get(tokensCacheKey);
if (subs) {
for (const sub of subs) {
sub(result);
}
subscribers.delete(tokensCacheKey);
}
})
.catch((error) => {
console.error("[Streamdown Code] Failed to highlight code:", error);
subscribers.delete(tokensCacheKey);
});
return null;
},
};
}
/**
* Pre-configured code plugin with default settings
*/
export const code = createCodePlugin();