@uniweb/theming 1.2.0 → 1.2.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/theming",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Theming utilities for the Uniweb platform — shade generation, CSS generation, theme processing",
5
5
  "type": "module",
6
6
  "exports": {
@@ -0,0 +1,157 @@
1
+ /**
2
+ * `theme.yml`'s `code:` — what it may say, and what of it travels.
3
+ *
4
+ * A site says how its code blocks look in one of three forms, which kit's
5
+ * <Code> reads (`parseCodeConfig` in kit's Code renderer):
6
+ *
7
+ * code: dracula a theme, by name
8
+ * code: { light: github-light, dark: … } a theme per scheme — each side
9
+ * a name, or a map of the next form
10
+ * code: { theme?: github-dark, <colour>: … } a theme, with colours overridden
11
+ *
12
+ * This layer carries the declaration as written — the default belongs to
13
+ * whoever renders code — and says what is wrong with it: a key kit does not
14
+ * read, a theme named with anything but a name, a colour that is not one. Kit
15
+ * would ignore each of them, or refuse it, without a word. ⛔ Until 2026-10-19
16
+ * the check read every value as a palette colour, so a pair of names, `theme:`,
17
+ * `transparent` and `var(…)` were reported as invalid colours (and the build
18
+ * printed none of the report), and the safety filter left out a pair whose
19
+ * sides were maps — so kit drew the default theme.
20
+ *
21
+ * @module @uniweb/theming/code-theme
22
+ */
23
+
24
+ import { keepWritable } from './values.js'
25
+
26
+ /**
27
+ * The colours a `code:` block may set: the block's ground and text, and the
28
+ * token scopes kit's <Code> maps. A cross-package test holds this list and
29
+ * kit's together, so a key kit learns is a key this check accepts.
30
+ */
31
+ export const CODE_COLOR_KEYS = Object.freeze([
32
+ 'background',
33
+ 'foreground',
34
+ 'comment',
35
+ 'string',
36
+ 'keyword',
37
+ 'operator',
38
+ 'function',
39
+ 'variable',
40
+ 'number',
41
+ 'constant',
42
+ 'type',
43
+ 'property',
44
+ 'tag',
45
+ 'attribute',
46
+ 'punctuation',
47
+ ])
48
+
49
+ // A theme's name, as Shiki names every theme it ships: lowercase words joined by
50
+ // single hyphens. Kit refuses any other, since the name becomes part of a URL.
51
+ const THEME_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
52
+
53
+ // A colour, in the forms CSS has — which is what a declared colour becomes, in
54
+ // the highlighter's inline styles. Not only the forms a palette can be shaded
55
+ // from: `transparent`, a named colour and `var(--x)` are colours here.
56
+ const HEX = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i
57
+ const COLOR_FUNCTION = /^(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|color-mix|light-dark|var)\(.+\)$/i
58
+ const COLOR_WORD = /^[a-z]+$/i
59
+
60
+ /**
61
+ * Whether a string can name a code theme.
62
+ *
63
+ * @param {*} name
64
+ * @returns {boolean}
65
+ */
66
+ export function isCodeThemeName(name) {
67
+ return typeof name === 'string' && THEME_NAME.test(name)
68
+ }
69
+
70
+ /**
71
+ * Whether a value is a colour a code block can be painted with.
72
+ *
73
+ * @param {*} value
74
+ * @returns {boolean}
75
+ */
76
+ export function isCodeColor(value) {
77
+ return typeof value === 'string' && (HEX.test(value) || COLOR_FUNCTION.test(value) || COLOR_WORD.test(value))
78
+ }
79
+
80
+ const THEME_HINT = "name one of Shiki's themes, such as github-dark"
81
+ const describe = (value) => (Array.isArray(value) ? 'a list' : value === null ? 'null' : typeof value)
82
+
83
+ /** One side — the whole block, or `light` / `dark` of a pair: a theme name, or a map of `theme` and colours. */
84
+ function checkSide(side, where, errors) {
85
+ if (typeof side === 'string') {
86
+ if (!isCodeThemeName(side)) errors.push(`${where}: ${JSON.stringify(side)} is not a theme name — ${THEME_HINT}`)
87
+ return
88
+ }
89
+ if (!side || typeof side !== 'object' || Array.isArray(side)) {
90
+ errors.push(`${where} must be a theme name or a map of colours, got ${describe(side)}`)
91
+ return
92
+ }
93
+ for (const [key, value] of Object.entries(side)) {
94
+ if (key === 'theme') {
95
+ if (!isCodeThemeName(value)) errors.push(`${where}.theme: ${JSON.stringify(value)} is not a theme name — ${THEME_HINT}`)
96
+ } else if (key === 'light' || key === 'dark') {
97
+ errors.push(`${where}.${key}: a side of a pair cannot hold another pair`)
98
+ } else if (!CODE_COLOR_KEYS.includes(key)) {
99
+ errors.push(`${where}.${key} is not a code colour — the colours are ${CODE_COLOR_KEYS.join(', ')}`)
100
+ } else if (typeof value !== 'string') {
101
+ errors.push(`${where}.${key} must be a colour, got ${describe(value)}`)
102
+ } else if (!isCodeColor(value)) {
103
+ errors.push(`${where}.${key} has invalid color value: ${value}`)
104
+ }
105
+ }
106
+ }
107
+
108
+ /**
109
+ * What is wrong with a `code:` declaration, as messages for the build to print.
110
+ *
111
+ * @param {*} code - `theme.yml`'s `code:`
112
+ * @returns {{ valid: boolean, errors: string[] }}
113
+ */
114
+ export function validateCodeTheme(code) {
115
+ const errors = []
116
+
117
+ if (code === undefined || code === null) return { valid: true, errors } // the renderer's default
118
+ if (typeof code === 'string') {
119
+ checkSide(code, 'code', errors)
120
+ } else if (typeof code !== 'object' || Array.isArray(code)) {
121
+ errors.push(`code must be a theme name, a light/dark pair, or a map of colours, got ${describe(code)}`)
122
+ } else if ('light' in code || 'dark' in code) {
123
+ for (const [key, value] of Object.entries(code)) {
124
+ if (key === 'light' || key === 'dark') checkSide(value, `code.${key}`, errors)
125
+ else errors.push(`code.${key} beside light and dark does nothing — set it on a side, as code.light.${key}`)
126
+ }
127
+ } else {
128
+ checkSide(code, 'code', errors)
129
+ }
130
+
131
+ return { valid: errors.length === 0, errors }
132
+ }
133
+
134
+ /**
135
+ * The declaration as it travels: as written, less any value a stylesheet could
136
+ * not hold (`keepWritable`, which says what it left out). A side of a pair that
137
+ * is a map is filtered the same way, value by value — not left out whole for
138
+ * not being one value.
139
+ *
140
+ * @param {*} code - `theme.yml`'s `code:`
141
+ * @param {string[]} warnings - where a warning is added
142
+ * @returns {*} what travels to the renderer, or null for nothing declared
143
+ */
144
+ export function keepWritableCode(code, warnings) {
145
+ if (code === undefined || code === null) return null
146
+ if (typeof code !== 'object' || Array.isArray(code)) return code
147
+
148
+ const kept = {}
149
+ for (const [key, value] of Object.entries(code)) {
150
+ if ((key === 'light' || key === 'dark') && value && typeof value === 'object' && !Array.isArray(value)) {
151
+ kept[key] = keepWritable(value, `code.${key}`, warnings)
152
+ } else {
153
+ Object.assign(kept, keepWritable({ [key]: value }, 'code', warnings))
154
+ }
155
+ }
156
+ return kept
157
+ }
package/src/processor.js CHANGED
@@ -12,6 +12,7 @@ import { isValidColor, generatePalettes } from './shade-generator.js'
12
12
  import { getDefaultColors, getDefaultContextTokens } from './css-generator.js'
13
13
  import { normalizeTokenValue } from './normalize.js'
14
14
  import { isCssName, isCssProperty, isCssValue, isLoadableUrl, keepWritable } from './values.js'
15
+ import { validateCodeTheme, keepWritableCode } from './code-theme.js'
15
16
 
16
17
  /**
17
18
  * Named neutral presets mapping to Tailwind gray families
@@ -251,35 +252,6 @@ function validateAppearance(appearance) {
251
252
  return { valid: errors.length === 0, errors }
252
253
  }
253
254
 
254
- /**
255
- * Validate code block theme configuration
256
- *
257
- * @param {Object} code - Code theme configuration
258
- * @returns {{ valid: boolean, errors: string[] }}
259
- */
260
- function validateCodeTheme(code) {
261
- const errors = []
262
-
263
- if (!code || typeof code !== 'object') {
264
- return { valid: true, errors } // No code config is valid (use defaults)
265
- }
266
-
267
- // Validate color values
268
- for (const [name, value] of Object.entries(code)) {
269
- if (typeof value !== 'string') {
270
- errors.push(`code.${name} must be a string, got ${typeof value}`)
271
- continue
272
- }
273
-
274
- // Basic color format check (hex, rgb, hsl, or color name)
275
- if (!isValidColor(value)) {
276
- errors.push(`code.${name} has invalid color value: ${value}`)
277
- }
278
- }
279
-
280
- return { valid: errors.length === 0, errors }
281
- }
282
-
283
255
  /**
284
256
  * Context-aware var types (stored per light/dark context)
285
257
  */
@@ -747,10 +719,9 @@ export function processTheme(rawConfig = {}, options = {}) {
747
719
  // site naming a bundled theme and overriding one colour would arrive carrying
748
720
  // fifteen, and the theme it named would be overridden in full by values it
749
721
  // never asked for. Whoever renders code owns the default; this only carries
750
- // what the site said.
751
- const code = rawConfig.code && typeof rawConfig.code === 'object'
752
- ? keepWritable(rawConfig.code, 'code', warnings)
753
- : rawConfig.code ?? null
722
+ // what the site said — less what a stylesheet could not hold, side by side
723
+ // for a pair (`keepWritableCode`, code-theme.js).
724
+ const code = keepWritableCode(rawConfig.code, warnings)
754
725
 
755
726
  // Site background (pass through as CSS value)
756
727
  let background = rawConfig.background || null