@@ -225,19 +225,37 @@ const defaults = {
225
225
"tab.hoverForeground": "tab.inactiveForeground",
226
226
};
227
227
228
- async function loadTheme(theme) {
229
- let rawTheme = typeof theme === "string" ? await loadThemeByName(theme) : theme;
230
- return toFinalTheme(rawTheme);
231
- }
228
+ const promiseCache = new Map();
232
229
const themeCache = new Map();
233
- function loadThemeByName(name) {
234
- if (!THEME_NAMES.includes(name)) {
235
- return Promise.resolve(undefined);
230
+ async function preloadTheme(theme) {
231
+ if (typeof theme === "string") {
232
+ const name = theme;
233
+ if (!THEME_NAMES.includes(name)) {
234
+ throw new UnknownThemeError$1(name);
235
+ }
236
+ if (!promiseCache.has(name)) {
237
+ const promise = reallyLoadThemeByName(name).then((theme) => {
238
+ themeCache.set(name, theme);
239
+ return theme;
240
+ });
241
+ promiseCache.set(name, promise);
242
+ }
243
+ return promiseCache.get(name);
236
244
}
237
- if (!themeCache.has(name)) {
238
- themeCache.set(name, reallyLoadThemeByName(name));
245
+ return theme;
246
+ }
247
+ function getTheme(theme) {
248
+ let rawTheme = null;
249
+ if (typeof theme === "string") {
250
+ rawTheme = themeCache.get(theme);
251
+ if (!rawTheme) {
252
+ throw new Error("Syntax highlighting error: theme not loaded");
253
+ }
254
+ }
255
+ else {
256
+ rawTheme = theme;
239
257
}
240
- return themeCache.get(name );
258
+ return toFinalTheme(rawTheme );
241
259
}
242
260
async function reallyLoadThemeByName(name) {
243
261
try {
@@ -309,7 +327,13 @@ const THEME_NAMES = [
309
327
"slack-ochin",
310
328
"solarized-dark",
311
329
"solarized-light",
312
- ];
330
+ ];
331
+ class UnknownThemeError$1 extends Error {
332
+ constructor(theme) {
333
+ super(`Unknown theme: ${theme}`);
334
+ this.theme = theme;
335
+ }
336
+ }
313
337
314
338
// generated by lib/utils/3.update-languages-data.mjs
315
339
const LANG_NAMES = [
@@ -2465,13 +2489,7 @@ function getStyle(metadata, colors) {
2465
2489
}
2466
2490
2467
2491
let registry = null;
2468
- function loadGrammars(alias) {
2469
- // get the language object from the alias
2470
- const langData = aliasToLangData(alias);
2471
- // if the language object is not found, throw
2472
- if (!langData) {
2473
- throw new UnknownLanguageError(alias);
2474
- }
2492
+ function preloadGrammars(languages) {
2475
2493
// initialize the registry the first time
2476
2494
if (!registry) {
2477
2495
const onigLibPromise = main.exports.loadWASM(onig).then(() => ({
@@ -2483,8 +2501,40 @@ function loadGrammars(alias) {
2483
2501
loadGrammar: (scopeName) => loadGrammarByScope(scopeName),
2484
2502
});
2485
2503
}
2486
- const grammarsPromise = registry.loadGrammar(langData.scopeName);
2487
- return { langId: langData.id, grammarsPromise };
2504
+ const promises = languages
2505
+ .filter((alias) => alias != "text")
2506
+ .map((alias) => {
2507
+ const langData = aliasToLangData(alias);
2508
+ if (!langData) {
2509
+ throw new UnknownLanguageError(alias);
2510
+ }
2511
+ return registry.loadGrammar(langData.scopeName);
2512
+ });
2513
+ return Promise.all(promises);
2514
+ }
2515
+ function getGrammar(alias) {
2516
+ if (alias == "text") {
2517
+ return {
2518
+ langId: "text",
2519
+ grammar: null,
2520
+ };
2521
+ }
2522
+ const langData = aliasToLangData(alias);
2523
+ if (!langData) {
2524
+ throw new UnknownLanguageError(alias);
2525
+ }
2526
+ const grammar = getGrammarFromRegistry(langData.scopeName);
2527
+ if (!grammar) {
2528
+ throw new Error(`Syntax highlighting error: grammar for ${alias} not loaded`);
2529
+ }
2530
+ return {
2531
+ langId: langData.id,
2532
+ grammar,
2533
+ };
2534
+ }
2535
+ function getGrammarFromRegistry(scopeName) {
2536
+ const { _syncRegistry } = registry;
2537
+ return _syncRegistry === null || _syncRegistry === void 0 ? void 0 : _syncRegistry._grammars[scopeName];
2488
2538
}
2489
2539
class UnknownLanguageError extends Error {
2490
2540
constructor(alias) {
@@ -2501,6 +2551,10 @@ function highlightTokensWithScopes(code, grammar, theme) {
2501
2551
registry.setTheme(theme);
2502
2552
const colorMap = registry.getColorMap();
2503
2553
return tokenizeWithScopes(code, grammar, colorMap);
2554
+ }
2555
+ function highlightText(code) {
2556
+ const lines = code.split(/\r?\n|\r/g);
2557
+ return lines.map((line) => [{ content: line, style: {} }]);
2504
2558
}
2505
2559
2506
2560
function parseRelativeRanges(relativeRange, lineNumber) {
@@ -2971,22 +3025,21 @@ function isAnnotatedConfig(config) {
2971
3025
}
2972
3026
async function highlight(code, lang, themeOrThemeName = "dark-plus", config = {}) {
2973
3027
const theCode = code || "";
2974
- const theLang = lang || "js"; // TODO default to text
3028
+ const theLang = lang || "text";
2975
3029
if (typeof theCode !== "string") {
2976
3030
throw new Error("Syntax highlighter error: code must be a string");
2977
3031
}
2978
3032
if (typeof theLang !== "string") {
2979
3033
throw new Error("Syntax highlighter error: lang must be a string");
2980
3034
}
2981
- const { langId, grammarsPromise } = loadGrammars(theLang);
2982
- const theme = await loadTheme(themeOrThemeName);
2983
- if (!theme) {
2984
- throw new UnknownThemeError(themeOrThemeName);
2985
- }
2986
- const grammar = await grammarsPromise;
2987
- const lines = (config === null || config === void 0 ? void 0 : config.scopes)
2988
- ? highlightTokensWithScopes(theCode, grammar, theme)
2989
- : highlightTokens(theCode, grammar, theme);
3035
+ await preload([theLang], themeOrThemeName);
3036
+ const { langId, grammar } = getGrammar(theLang);
3037
+ const theme = getTheme(themeOrThemeName);
3038
+ const lines = langId == "text"
3039
+ ? highlightText(theCode)
3040
+ : (config === null || config === void 0 ? void 0 : config.scopes)
3041
+ ? highlightTokensWithScopes(theCode, grammar, theme)
3042
+ : highlightTokens(theCode, grammar, theme);
2990
3043
if (isAnnotatedConfig(config)) {
2991
3044
const annotations = (config === null || config === void 0 ? void 0 : config.annotations) || [];
2992
3045
return {
@@ -3003,6 +3056,9 @@ async function highlight(code, lang, themeOrThemeName = "dark-plus", config = {}
3003
3056
};
3004
3057
}
3005
3058
}
3059
+ async function preload(langs, theme) {
3060
+ await Promise.all([preloadGrammars(langs), preloadTheme(theme)]);
3061
+ }
3006
3062
/** @deprecated use highlight instead */
3007
3063
async function highlightWithScopes(code, alias, themeOrThemeName = "dark-plus") {
3008
3064
return highlight(code, alias, themeOrThemeName, { scopes: true });
@@ -3015,10 +3071,10 @@ async function extractAnnotations(code, lang, annotationNames = []) {
3015
3071
if (annotationNames.length === 0) {
3016
3072
return { code, annotations: [] };
3017
3073
}
3018
- const { grammarsPromise } = loadGrammars( lang);
3019
- const grammar = await grammarsPromise ;
3074
+ await preloadGrammars([ lang] );
3075
+ const { grammar } = getGrammar(lang) ;
3020
3076
const { newCode, annotations } = extractCommentsFromCode(code, grammar, annotationNames);
3021
3077
return { code: newCode, annotations };
3022
3078
}
3023
3079
3024
- export { LANG_NAMES, THEME_NAMES, UnknownLanguageError, UnknownThemeError, annotatedHighlight, extractAnnotations, highlight, highlightWithScopes };
3080
+ export { LANG_NAMES, THEME_NAMES, UnknownLanguageError, UnknownThemeError, annotatedHighlight, extractAnnotations, highlight, highlightWithScopes, preload };
0 commit comments