Skip to content

Commit ee37d6d

Browse files
committed
Highlight text - fix #5
1 parent 7d65b36 commit ee37d6d

13 files changed

+507
-346
lines changed

.changeset/giant-hounds-taste.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@code-hike/lighter": patch
3+
---
4+
5+
Add text grammar

lib/dist/browser.esm.mjs

Lines changed: 89 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,37 @@ const defaults = {
225225
"tab.hoverForeground": "tab.inactiveForeground",
226226
};
227227

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();
232229
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);
236244
}
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;
239257
}
240-
return themeCache.get(name);
258+
return toFinalTheme(rawTheme);
241259
}
242260
async function reallyLoadThemeByName(name) {
243261
try {
@@ -309,7 +327,13 @@ const THEME_NAMES = [
309327
"slack-ochin",
310328
"solarized-dark",
311329
"solarized-light",
312-
];
330+
];
331+
class UnknownThemeError$1 extends Error {
332+
constructor(theme) {
333+
super(`Unknown theme: ${theme}`);
334+
this.theme = theme;
335+
}
336+
}
313337

314338
// generated by lib/utils/3.update-languages-data.mjs
315339
const LANG_NAMES = [
@@ -2465,13 +2489,7 @@ function getStyle(metadata, colors) {
24652489
}
24662490

24672491
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) {
24752493
// initialize the registry the first time
24762494
if (!registry) {
24772495
const onigLibPromise = main.exports.loadWASM(onig).then(() => ({
@@ -2483,8 +2501,40 @@ function loadGrammars(alias) {
24832501
loadGrammar: (scopeName) => loadGrammarByScope(scopeName),
24842502
});
24852503
}
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];
24882538
}
24892539
class UnknownLanguageError extends Error {
24902540
constructor(alias) {
@@ -2501,6 +2551,10 @@ function highlightTokensWithScopes(code, grammar, theme) {
25012551
registry.setTheme(theme);
25022552
const colorMap = registry.getColorMap();
25032553
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: {} }]);
25042558
}
25052559

25062560
function parseRelativeRanges(relativeRange, lineNumber) {
@@ -2971,22 +3025,21 @@ function isAnnotatedConfig(config) {
29713025
}
29723026
async function highlight(code, lang, themeOrThemeName = "dark-plus", config = {}) {
29733027
const theCode = code || "";
2974-
const theLang = lang || "js"; // TODO default to text
3028+
const theLang = lang || "text";
29753029
if (typeof theCode !== "string") {
29763030
throw new Error("Syntax highlighter error: code must be a string");
29773031
}
29783032
if (typeof theLang !== "string") {
29793033
throw new Error("Syntax highlighter error: lang must be a string");
29803034
}
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);
29903043
if (isAnnotatedConfig(config)) {
29913044
const annotations = (config === null || config === void 0 ? void 0 : config.annotations) || [];
29923045
return {
@@ -3003,6 +3056,9 @@ async function highlight(code, lang, themeOrThemeName = "dark-plus", config = {}
30033056
};
30043057
}
30053058
}
3059+
async function preload(langs, theme) {
3060+
await Promise.all([preloadGrammars(langs), preloadTheme(theme)]);
3061+
}
30063062
/** @deprecated use highlight instead */
30073063
async function highlightWithScopes(code, alias, themeOrThemeName = "dark-plus") {
30083064
return highlight(code, alias, themeOrThemeName, { scopes: true });
@@ -3015,10 +3071,10 @@ async function extractAnnotations(code, lang, annotationNames = []) {
30153071
if (annotationNames.length === 0) {
30163072
return { code, annotations: [] };
30173073
}
3018-
const { grammarsPromise } = loadGrammars(lang);
3019-
const grammar = await grammarsPromise;
3074+
await preloadGrammars([lang]);
3075+
const { grammar } = getGrammar(lang);
30203076
const { newCode, annotations } = extractCommentsFromCode(code, grammar, annotationNames);
30213077
return { code: newCode, annotations };
30223078
}
30233079

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 };

lib/dist/index.cjs.js

Lines changed: 89 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)