xonecas.io
70160
Ce

cel-tui / commit a5480a7

refactor(components): use direct lextide class styling

xonecas2026-04-12 a5480a763a530316e72c09ab303b0dee440931cd parent f4339cc

3 files changed

examples/syntax-highlight.ts+7−16
@@ -40,40 +40,31 @@ const CUSTOM_THEME: SyntaxHighlightTheme = {
4040 tokenColors: [
4141 { settings: { foreground: "#f8f8f2" } },
4242 {
43- scope: ["comment", "string.quoted.docstring.multi"],
43+ scope: ["comment", "quote", "doctag"],
4444 settings: { foreground: "#7f849c", fontStyle: "italic" },
4545 },
4646 {
47- scope: ["keyword", "storage", "entity.name.operator"],
47+ scope: ["keyword", "operator"],
4848 settings: { foreground: "#ff79c6" },
4949 },
5050 {
51- scope: ["string", "markup.inline"],
51+ scope: ["string", "code"],
5252 settings: { foreground: "#f1fa8c" },
5353 },
5454 {
55- scope: ["constant.numeric", "constant.language", "constant.character"],
55+ scope: ["number", "literal", "escape", "symbol"],
5656 settings: { foreground: "#ffb86c" },
5757 },
5858 {
59- scope: ["entity.name.function", "support.function", "meta.function-call"],
59+ scope: ["function_", "title"],
6060 settings: { foreground: "#50fa7b" },
6161 },
6262 {
63- scope: [
64- "entity.name.type",
65- "support.type",
66- "support.class",
67- "entity.other.inherited-class",
68- ],
63+ scope: ["class_", "type", "built_in", "inherited__"],
6964 settings: { foreground: "#8be9fd" },
7065 },
7166 {
72- scope: [
73- "variable.parameter",
74- "meta.object-literal.key",
75- "meta.property-name",
76- ],
67+ scope: ["params", "property", "attr", "attribute", "selector-attr"],
7768 settings: { foreground: "#bd93f9" },
7869 },
7970 ],
packages/components/src/syntax-highlight.test.ts+33−6
@@ -139,8 +139,16 @@ describe("SyntaxHighlight", () => {
139139 expect(findText(node, "42").props.fgColor).toBe("color03");
140140 });
141141
142- test("custom theme object overrides default colors", () => {
143- const node = render('const message = "hello"', "javascript", {
142+ test("default ansi16 theme styles markdown code blocks from direct lextide classes", () => {
143+ const node = render("# title\n\n```ts\nconst value = 1\n```", "markdown");
144+
145+ expect(findText(node, "#").props.fgColor).toBeDefined();
146+ expect(findText(node, "```ts").props.fgColor).toBeDefined();
147+ expect(findText(node, "const").props.fgColor).toBeDefined();
148+ });
149+
150+ test("custom theme object matches bare lextide class names directly", () => {
151+ const node = render("class Derived extends Base {}", "typescript", {
144152 theme: {
145153 name: "syntax-highlight-test-theme",
146154 type: "dark",
@@ -149,16 +157,35 @@ describe("SyntaxHighlight", () => {
149157 tokenColors: [
150158 { settings: { foreground: "#e5e5e5" } },
151159 {
152- scope: ["keyword", "storage"],
160+ scope: ["class_", "inherited__"],
153161 settings: { foreground: "#cd3131" },
154162 },
155- { scope: "string", settings: { foreground: "#0dbc79" } },
156163 ],
157164 },
158165 });
159166
160- expect(findText(node, "const").props.fgColor).toBe("color01");
161- expect(findTextContaining(node, "hello").props.fgColor).toBe("color02");
167+ expect(findText(node, "Derived").props.fgColor).toBe("color01");
168+ expect(findText(node, "Base").props.fgColor).toBe("color01");
169+ expect(findText(node, "class").props.fgColor).toBe("color05");
170+ });
171+
172+ test("custom theme object matches prefixed hljs class names directly", () => {
173+ const node = render("# title\n\n```ts\nconst value = 1\n```", "markdown", {
174+ theme: {
175+ name: "syntax-highlight-prefixed-class-theme",
176+ type: "dark",
177+ fg: "#e5e5e5",
178+ bg: "#000000",
179+ tokenColors: [
180+ { settings: { foreground: "#e5e5e5" } },
181+ { scope: "hljs-code", settings: { foreground: "#0dbc79" } },
182+ ],
183+ },
184+ });
185+
186+ expect(findText(node, "```ts").props.fgColor).toBe("color02");
187+ expect(findText(node, "const").props.fgColor).toBe("color02");
188+ expect(findText(node, "#").props.fgColor).toBe("color09");
162189 });
163190
164191 test("named theme presets are accepted", () => {
packages/components/src/syntax-highlight.ts+123−187
@@ -33,29 +33,9 @@ const ANSI_SLOT_HEX: ReadonlyArray<readonly [Color, string]> = [
3333 ["color15", "#ffffff"],
3434 ];
3535
36-type TokenCategory =
37- | "addition"
38- | "comment"
39- | "deletion"
40- | "emphasis"
41- | "function"
42- | "keyword"
43- | "link"
44- | "literal"
45- | "meta"
46- | "number"
47- | "parameter"
48- | "property"
49- | "regexp"
50- | "string"
51- | "strong"
52- | "tag"
53- | "type"
54- | "variable";
55-
5636 /** A best-effort token color override entry. */
5737 export interface SyntaxHighlightThemeTokenColor {
58- /** Target scopes or categories. */
38+ /** Target lextide / highlight.js classes, with or without the `hljs-` prefix. */
5939 scope?: string | readonly string[];
6040 /** Style settings for the matched scopes. */
6141 settings: {
@@ -94,7 +74,7 @@ export interface SyntaxHighlightProps {
9474 *
9575 * Built-in presets currently include `"default"` and `"dark-plus"`.
9676 * Theme registration objects are applied as best-effort overrides onto the
97- * lextide token categories used by this component.
77+ * lextide / highlight.js classes emitted by this component.
9878 */
9979 theme?: SyntaxHighlightTheme;
10080 }
@@ -142,125 +122,68 @@ function requiredAt<T>(
142122 return item;
143123 }
144124
145-const DEFAULT_CATEGORY_STYLES: Readonly<Record<TokenCategory, StyleProps>> = {
146- addition: { fgColor: "color02" },
147- comment: { fgColor: "color08", italic: true },
148- deletion: { fgColor: "color01" },
149- emphasis: { italic: true },
150- function: { fgColor: "color04" },
151- keyword: { fgColor: "color05" },
152- link: { fgColor: "color04", underline: true },
153- literal: { fgColor: "color03" },
154- meta: { fgColor: "color09", bold: true },
155- number: { fgColor: "color03" },
156- parameter: { fgColor: "color11" },
157- property: { fgColor: "color06" },
158- regexp: { fgColor: "color01" },
159- string: { fgColor: "color02" },
160- strong: { bold: true },
161- tag: { fgColor: "color01" },
162- type: { fgColor: "color06" },
163- variable: {},
125+type ClassStyleRecord = Readonly<Record<string, ResolvedClassStyle>>;
126+
127+const DEFAULT_CLASS_STYLES: ClassStyleRecord = {
128+ addition: { style: { fgColor: "color02" } },
129+ attr: { style: { fgColor: "color06" } },
130+ attribute: { style: { fgColor: "color06" } },
131+ bullet: { style: { fgColor: "color03" } },
132+ built_in: { style: { fgColor: "color06" } },
133+ class_: { style: { fgColor: "color06" } },
134+ code: { style: { fgColor: "color02" } },
135+ comment: { style: { fgColor: "color08", italic: true } },
136+ deletion: { style: { fgColor: "color01" } },
137+ doctag: { style: { fgColor: "color08", italic: true } },
138+ emphasis: { style: { italic: true } },
139+ escape: { style: { fgColor: "color03" } },
140+ function_: { style: { fgColor: "color04" } },
141+ inherited__: { style: { fgColor: "color06" } },
142+ keyword: { style: { fgColor: "color05" } },
143+ link: { style: { fgColor: "color04", underline: true } },
144+ literal: { style: { fgColor: "color03" } },
145+ meta: { style: { fgColor: "color09", bold: true } },
146+ name: { style: { fgColor: "color01" } },
147+ number: { style: { fgColor: "color03" } },
148+ operator: { style: { fgColor: "color05" } },
149+ params: { style: { fgColor: "color11" } },
150+ property: { style: { fgColor: "color06" } },
151+ quote: { style: { fgColor: "color08", italic: true } },
152+ regexp: { style: { fgColor: "color01" } },
153+ section: { style: { fgColor: "color09", bold: true } },
154+ "selector-attr": { style: { fgColor: "color06" } },
155+ "selector-class": { style: { fgColor: "color06" } },
156+ "selector-id": { style: { fgColor: "color06" } },
157+ "selector-pseudo": { style: { fgColor: "color05" } },
158+ "selector-tag": { style: { fgColor: "color01" } },
159+ string: { style: { fgColor: "color02" } },
160+ strong: { style: { bold: true } },
161+ subst: { resetToBase: true, style: {} },
162+ symbol: { style: { fgColor: "color03" } },
163+ tag: { style: { fgColor: "color01" } },
164+ title: { style: { fgColor: "color04" } },
165+ type: { style: { fgColor: "color06" } },
166+ variable: { style: {} },
164167 };
165168
166-const DARK_PLUS_CATEGORY_STYLES: Readonly<
167- Partial<Record<TokenCategory, StyleProps>>
168-> = {
169+const DARK_PLUS_CLASS_STYLE_OVERRIDES: Readonly<Record<string, StyleProps>> = {
170+ code: { fgColor: "color09" },
169171 comment: { fgColor: "color08", italic: true },
170- function: { fgColor: "color11" },
172+ function_: { fgColor: "color11" },
171173 keyword: { fgColor: "color12" },
172174 meta: { fgColor: "color08" },
175+ name: { fgColor: "color09" },
173176 number: { fgColor: "color10" },
174177 property: { fgColor: "color07" },
175178 regexp: { fgColor: "color09" },
179+ section: { fgColor: "color08" },
176180 string: { fgColor: "color09" },
177181 tag: { fgColor: "color09" },
182+ title: { fgColor: "color11" },
178183 type: { fgColor: "color06" },
179184 };
180185
181-const CATEGORY_CLASSES: Readonly<Record<TokenCategory, readonly string[]>> = {
182- addition: ["addition"],
183- comment: ["comment", "doctag", "quote"],
184- deletion: ["deletion"],
185- emphasis: ["emphasis"],
186- function: ["function_", "title"],
187- keyword: ["keyword", "operator", "selector-pseudo"],
188- link: ["link"],
189- literal: ["bullet", "literal", "symbol"],
190- meta: ["meta", "section"],
191- number: ["number"],
192- parameter: ["params"],
193- property: [
194- "attr",
195- "attribute",
196- "property",
197- "selector-attr",
198- "selector-class",
199- "selector-id",
200- ],
201- regexp: ["regexp"],
202- string: ["string"],
203- strong: ["strong"],
204- tag: ["name", "selector-tag", "tag"],
205- type: ["built_in", "class_", "type"],
206- variable: ["variable"],
207-};
208-
209-const CATEGORY_SCOPE_HINTS: Readonly<Record<TokenCategory, readonly string[]>> =
210- {
211- addition: ["addition", "markup.inserted"],
212- comment: ["comment", "quote"],
213- deletion: ["deletion", "markup.deleted"],
214- emphasis: ["emphasis", "markup.italic"],
215- function: [
216- "entity.name.function",
217- "function",
218- "meta.function-call",
219- "support.function",
220- "title",
221- ],
222- keyword: ["entity.name.operator", "keyword", "operator", "storage"],
223- link: ["link", "meta.link"],
224- literal: [
225- "bullet",
226- "constant.character",
227- "constant.language",
228- "literal",
229- "symbol",
230- ],
231- meta: ["markup.heading", "meta", "section"],
232- number: ["constant.numeric", "number"],
233- parameter: ["params", "variable.parameter"],
234- property: [
235- "attr",
236- "attribute",
237- "entity.other.attribute-name",
238- "meta.object-literal.key",
239- "meta.property-name",
240- "property",
241- "support.type.property-name",
242- ],
243- regexp: ["regexp", "string.regexp"],
244- string: ["markup.inline", "string"],
245- strong: ["markup.bold", "markup.heading", "strong"],
246- tag: ["entity.name.tag", "name", "selector-tag", "tag"],
247- type: [
248- "built_in",
249- "class",
250- "entity.name.type",
251- "entity.other.inherited-class",
252- "support.class",
253- "support.type",
254- "type",
255- ],
256- variable: ["variable"],
257- };
258-
259-const DEFAULT_RESOLVED_THEME = buildResolvedTheme(
260- DEFAULT_THEME_NAME,
261- {},
262- DEFAULT_CATEGORY_STYLES,
263-);
186+const DEFAULT_RESOLVED_THEME = buildResolvedTheme(DEFAULT_THEME_NAME, {});
264187
265188 function hashString(input: string): string {
266189 let hash = 2166136261;
@@ -371,28 +294,74 @@ function themeSettingsToStyle(settings: {
371294 };
372295 }
373296
297+function normalizeClassName(className: string): string {
298+ return className.startsWith("hljs-") ? className.slice(5) : className;
299+}
300+
301+function cloneClassStyles(
302+ source: ClassStyleRecord,
303+): Map<string, ResolvedClassStyle> {
304+ const classStyles = new Map<string, ResolvedClassStyle>();
305+
306+ for (const [className, resolved] of Object.entries(source)) {
307+ classStyles.set(normalizeClassName(className), {
308+ resetToBase: resolved.resetToBase,
309+ style: { ...resolved.style },
310+ });
311+ }
312+
313+ return classStyles;
314+}
315+
316+function applyClassStyleOverride(
317+ classStyles: Map<string, ResolvedClassStyle>,
318+ className: string,
319+ style: StyleProps,
320+): void {
321+ const normalized = normalizeClassName(className);
322+ const current = classStyles.get(normalized);
323+
324+ classStyles.set(normalized, {
325+ resetToBase: current?.resetToBase,
326+ style: mergeStyles(current?.style ?? {}, style),
327+ });
328+}
329+
330+function applyThemeTokenColors(
331+ classStyles: Map<string, ResolvedClassStyle>,
332+ tokenColors: readonly SyntaxHighlightThemeTokenColor[] | undefined,
333+ baseStyle: StyleProps,
334+): void {
335+ for (const tokenColor of tokenColors ?? []) {
336+ const style = themeSettingsToStyle(tokenColor.settings);
337+ const scopes = tokenColor.scope
338+ ? Array.isArray(tokenColor.scope)
339+ ? tokenColor.scope
340+ : [tokenColor.scope]
341+ : [];
342+
343+ if (scopes.length === 0) {
344+ Object.assign(baseStyle, mergeStyles(baseStyle, style));
345+ continue;
346+ }
347+
348+ for (const scope of scopes) {
349+ applyClassStyleOverride(classStyles, scope, style);
350+ }
351+ }
352+}
353+
374354 function buildResolvedTheme(
375355 cacheKey: string,
376356 baseStyle: StyleProps,
377- categoryStyles: Readonly<Record<TokenCategory, StyleProps>>,
357+ classStyleOverrides?: Readonly<Record<string, StyleProps>>,
378358 ): ResolvedSyntaxHighlightTheme {
379- const classStyles = new Map<string, ResolvedClassStyle>();
359+ const classStyles = cloneClassStyles(DEFAULT_CLASS_STYLES);
380360
381- for (const [category, classes] of Object.entries(CATEGORY_CLASSES) as [
382- TokenCategory,
383- readonly string[],
384- ][]) {
385- const style = categoryStyles[category];
386- for (const className of classes) {
387- classStyles.set(className, { style });
388- }
361+ for (const [className, style] of Object.entries(classStyleOverrides ?? {})) {
362+ applyClassStyleOverride(classStyles, className, style);
389363 }
390364
391- classStyles.set("subst", {
392- resetToBase: true,
393- style: baseStyle,
394- });
395-
396365 return {
397366 baseStyle,
398367 cacheKey,
@@ -400,10 +369,6 @@ function buildResolvedTheme(
400369 };
401370 }
402371
403-function scopeMatchesHint(scope: string, hint: string): boolean {
404- return scope === hint || scope.startsWith(`${hint}.`);
405-}
406-
407372 function resolveTheme(
408373 theme?: SyntaxHighlightTheme,
409374 ): ResolvedSyntaxHighlightTheme {
@@ -419,10 +384,7 @@ function resolveTheme(
419384 return buildResolvedTheme(
420385 "preset:dark-plus",
421386 { fgColor: "color07" },
422- {
423- ...DEFAULT_CATEGORY_STYLES,
424- ...DARK_PLUS_CATEGORY_STYLES,
425- },
387+ DARK_PLUS_CLASS_STYLE_OVERRIDES,
426388 );
427389 }
428390
@@ -432,38 +394,15 @@ function resolveTheme(
432394 background: theme.bg,
433395 foreground: theme.fg,
434396 });
435- const categoryStyles: Record<TokenCategory, StyleProps> = {
436- ...DEFAULT_CATEGORY_STYLES,
437- };
438-
439- for (const tokenColor of theme.tokenColors ?? []) {
440- const style = themeSettingsToStyle(tokenColor.settings);
441- const scopes = tokenColor.scope
442- ? Array.isArray(tokenColor.scope)
443- ? tokenColor.scope
444- : [tokenColor.scope]
445- : [];
446-
447- if (scopes.length === 0) {
448- Object.assign(baseStyle, mergeStyles(baseStyle, style));
449- continue;
450- }
397+ const classStyles = cloneClassStyles(DEFAULT_CLASS_STYLES);
451398
452- for (const [category, hints] of Object.entries(CATEGORY_SCOPE_HINTS) as [
453- TokenCategory,
454- readonly string[],
455- ][]) {
456- if (
457- scopes.some((scope) =>
458- hints.some((hint) => scopeMatchesHint(scope, hint)),
459- )
460- ) {
461- categoryStyles[category] = mergeStyles(categoryStyles[category], style);
462- }
463- }
464- }
399+ applyThemeTokenColors(classStyles, theme.tokenColors, baseStyle);
465400
466- return buildResolvedTheme(`custom:${cacheHash}`, baseStyle, categoryStyles);
401+ return {
402+ baseStyle,
403+ cacheKey: `custom:${cacheHash}`,
404+ classStyles,
405+ };
467406 }
468407
469408 function stateKey(
@@ -594,10 +533,7 @@ function resolveNodeStyle(
594533 let style = inherited;
595534
596535 for (const className of classNames) {
597- const normalized = className.startsWith("hljs-")
598- ? className.slice(5)
599- : className;
600- const classStyle = theme.classStyles.get(normalized);
536+ const classStyle = theme.classStyles.get(normalizeClassName(className));
601537 if (!classStyle) {
602538 continue;
603539 }
@@ -728,8 +664,8 @@ function renderHighlightedState(state: SyntaxHighlightState): ContainerNode {
728664 * the stream and replay the full snippet.
729665 *
730666 * Unknown languages render as plain text. The default theme is terminal-friendly
731- * and maps lextide token classes onto cel's ANSI palette slots while leaving
732- * base text on terminal defaults.
667+ * and maps lextide's emitted highlight.js classes onto cel's ANSI palette slots
668+ * while leaving base text on terminal defaults.
733669 *
734670 * @param content - Source code to render.
735671 * @param language - Registered lextide language id or alias.