@astryxdesign/cli 0.7.16 → 0.7.17-canary.gb36275e

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.
Files changed (239) hide show
  1. package/README.md +13 -23
  2. package/api/build/_adapter.d.mts +5 -0
  3. package/api/build/_adapter.mjs +49 -34
  4. package/api/build/blocks/blocks.d.mts +7 -5
  5. package/api/build/blocks/blocks.mjs +113 -26
  6. package/api/build/build.doc.mjs +2 -1
  7. package/api/build/kit/rank.mjs +59 -12
  8. package/api/build/kit/weights.d.mts +16 -3
  9. package/api/build/kit/weights.json +1 -1
  10. package/api/build/kit/weights.mjs +238 -31
  11. package/api/build/kit/word-model.d.mts +23 -0
  12. package/api/build/kit/word-model.mjs +46 -0
  13. package/api/build/kit/words.d.mts +42 -0
  14. package/api/build/kit/words.mjs +82 -1
  15. package/api/component/__fixtures__/resolutions/modules.txt +4 -5
  16. package/api/component/list/list.d.mts +1 -1
  17. package/api/component/list/list.mjs +46 -5
  18. package/api/discover/_adapter.d.mts +1 -1
  19. package/api/docs/_adapter.d.mts +26 -2
  20. package/api/docs/_adapter.mjs +217 -5
  21. package/api/docs/docs.d.mts +3 -0
  22. package/api/docs/docs.doc.mjs +7 -1
  23. package/api/docs/docs.mjs +12 -0
  24. package/api/docs/docs.type.d.mts +5 -0
  25. package/api/docs/docs.type.mjs +2 -0
  26. package/api/docs/node/node.mjs +1 -1
  27. package/api/doctor/_adapter.d.mts +27 -5
  28. package/api/doctor/_adapter.mjs +77 -6
  29. package/api/doctor/built-checks.mjs +38 -13
  30. package/api/doctor/doctor.d.mts +4 -3
  31. package/api/doctor/doctor.mjs +14 -7
  32. package/api/doctor/theme-checks.mjs +121 -17
  33. package/api/render/_adapter.mjs +531 -192
  34. package/api/render/render.doc.mjs +11 -3
  35. package/api/render/render.type.d.mts +30 -0
  36. package/api/render/render.type.mjs +19 -0
  37. package/api/search/ORDERING.md +1 -1
  38. package/api/search/_candidate-score.mjs +51 -32
  39. package/api/search/_fit-order.mjs +3 -3
  40. package/api/search/_result.d.mts +5 -5
  41. package/api/search/_score.d.mts +6 -1
  42. package/api/search/_score.mjs +15 -3
  43. package/api/search/search.d.mts +1 -1
  44. package/api/theme/_adapter.mjs +10 -0
  45. package/api/theme/build/build.d.mts +47 -0
  46. package/api/theme/build/build.mjs +147 -25
  47. package/api/theme/theme.type.d.mts +10 -0
  48. package/api/theme/theme.type.mjs +7 -1
  49. package/api/theme-edit/_adapter.d.mts +48 -0
  50. package/api/theme-edit/_adapter.mjs +675 -42
  51. package/api/theme-edit/_check.d.mts +28 -22
  52. package/api/theme-edit/_check.mjs +1481 -252
  53. package/api/theme-edit/_color.d.mts +9 -0
  54. package/api/theme-edit/_color.mjs +41 -1
  55. package/api/theme-edit/_compiler.mjs +9 -0
  56. package/api/theme-edit/_context.mjs +60 -3
  57. package/api/theme-edit/_css.mjs +21 -1
  58. package/api/theme-edit/_edit.d.mts +22 -0
  59. package/api/theme-edit/_edit.mjs +1196 -88
  60. package/api/theme-edit/_how-commands.d.mts +57 -0
  61. package/api/theme-edit/_how-commands.mjs +536 -1
  62. package/api/theme-edit/_how-question.d.mts +65 -0
  63. package/api/theme-edit/_how-question.mjs +4401 -225
  64. package/api/theme-edit/_how.mjs +104 -4
  65. package/api/theme-edit/_marks.d.mts +31 -5
  66. package/api/theme-edit/_marks.mjs +57 -5
  67. package/api/theme-edit/_normal.d.mts +153 -0
  68. package/api/theme-edit/_normal.mjs +661 -0
  69. package/api/theme-edit/_palette.mjs +10 -4
  70. package/api/theme-edit/_parts.d.mts +8 -0
  71. package/api/theme-edit/_parts.mjs +103 -2
  72. package/api/theme-edit/_preview-app.d.mts +7 -1
  73. package/api/theme-edit/_preview-app.mjs +205 -17
  74. package/api/theme-edit/_preview.d.mts +64 -8
  75. package/api/theme-edit/_preview.mjs +447 -89
  76. package/api/theme-edit/_settings.d.mts +176 -5
  77. package/api/theme-edit/_settings.mjs +1936 -128
  78. package/api/theme-edit/_tables.d.mts +6 -0
  79. package/api/theme-edit/_tables.mjs +32 -1
  80. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  81. package/api/theme-edit/theme-edit.type.mjs +4 -1
  82. package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
  83. package/assets/codemods/run-codemod.mjs +73 -6
  84. package/assets/codemods/transforms/next/index.mjs +11 -1
  85. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  86. package/assets/docs/author-a-theme.doc.mjs +1 -0
  87. package/assets/docs/theme.doc.dense.mjs +2 -2
  88. package/assets/docs/theme.doc.mjs +1 -1
  89. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  90. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  91. package/assets/docs/tree/render-pages.doc.mjs +43 -2
  92. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  93. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  94. package/assets/docs/use-a-theme.doc.mjs +1 -1
  95. package/assets/docs.generated.json +2 -2
  96. package/assets/self-audit.generated.json +1 -1
  97. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  98. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  99. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  100. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  101. package/assets/templates/components.generated.json +1 -1
  102. package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
  103. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
  104. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  105. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  106. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  107. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  108. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  109. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  110. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  111. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  112. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  113. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  114. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  115. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  116. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  117. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  118. package/assets/templates/pages/editor/template.doc.mjs +2 -8
  119. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  120. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  121. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  122. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  123. package/assets/templates/pages/ide/page.tsx +24 -2
  124. package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
  125. package/assets/templates/pages/library/template.doc.mjs +4 -1
  126. package/assets/templates/pages/login/template.doc.mjs +2 -1
  127. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  128. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  129. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  130. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  131. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  132. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  133. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  134. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  135. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  136. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  137. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  138. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  139. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  140. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  141. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  142. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  143. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
  144. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  145. package/authoring/debug/parse.d.mts +3 -3
  146. package/authoring/doctypes/_schema.d.mts +3 -3
  147. package/authoring/doctypes/command/command.doc.mjs +1 -1
  148. package/authoring/doctypes/template/template.doc.mjs +1 -1
  149. package/authoring/doctypes/template/type.ts +9 -0
  150. package/clients/cli/bin/astryx.mjs +6 -9
  151. package/clients/cli/build-id.generated.json +1 -1
  152. package/clients/cli/commands/component.doc.mjs +1 -1
  153. package/clients/cli/commands/component.mjs +20 -2
  154. package/clients/cli/commands/docs.doc.mjs +7 -1
  155. package/clients/cli/commands/docs.mjs +3 -1
  156. package/clients/cli/commands/doctor.mjs +22 -14
  157. package/clients/cli/commands/experimental/render.mjs +32 -1
  158. package/clients/cli/commands/experimental/theme.mjs +65 -14
  159. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  160. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  161. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  162. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  163. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  164. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  165. package/clients/cli/commands/search.doc.mjs +2 -1
  166. package/clients/cli/commands/search.mjs +6 -1
  167. package/clients/cli/commands/template.mjs +4 -2
  168. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  169. package/clients/cli/commands/theme.doc.mjs +1 -1
  170. package/clients/cli/commands/theme.mjs +153 -5
  171. package/clients/cli/commands/upgrade.mjs +42 -7
  172. package/clients/cli/index.mjs +1 -1
  173. package/clients/cli/lib/component-format.mjs +205 -98
  174. package/clients/cli/lib/hook-format.mjs +9 -19
  175. package/clients/cli/manifest.generated.json +1 -1
  176. package/foundation/agent-docs/agent-docs.mjs +1 -1
  177. package/foundation/config/theme-state.d.mts +13 -1
  178. package/foundation/config/theme-state.mjs +47 -7
  179. package/foundation/discovery/component-discovery.d.mts +11 -0
  180. package/foundation/discovery/component-discovery.mjs +27 -2
  181. package/foundation/discovery/component-replacement.mjs +130 -1
  182. package/foundation/discovery/entry-name-files.d.mts +43 -0
  183. package/foundation/discovery/entry-name-files.mjs +264 -0
  184. package/foundation/discovery/hook-discovery.d.mts +8 -2
  185. package/foundation/discovery/hook-discovery.mjs +20 -7
  186. package/foundation/discovery/package-exports.d.mts +10 -0
  187. package/foundation/discovery/package-exports.mjs +56 -3
  188. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  189. package/foundation/discovery/runner-allowlist.mjs +2 -0
  190. package/foundation/discovery/template-adapter.d.mts +52 -0
  191. package/foundation/discovery/template-adapter.mjs +173 -10
  192. package/foundation/discovery/theme-imports.d.mts +14 -0
  193. package/foundation/discovery/theme-imports.mjs +43 -1
  194. package/foundation/doc-compiler/import.mjs +7 -1
  195. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  196. package/foundation/doc-compiler/stored-docs.d.mts +22 -0
  197. package/foundation/doc-compiler/stored-docs.mjs +49 -0
  198. package/foundation/fs/cache-eviction.d.mts +21 -0
  199. package/foundation/fs/cache-eviction.mjs +73 -0
  200. package/foundation/fs/gather-cache.d.mts +25 -4
  201. package/foundation/fs/gather-cache.mjs +163 -53
  202. package/foundation/fs/jiti-cache.d.mts +19 -1
  203. package/foundation/fs/jiti-cache.mjs +130 -6
  204. package/foundation/fs/kept-tree.d.mts +60 -0
  205. package/foundation/fs/kept-tree.mjs +154 -0
  206. package/foundation/fs/module-loader.d.mts +18 -2
  207. package/foundation/fs/module-loader.mjs +92 -20
  208. package/foundation/identity/provider-identity.d.mts +6 -0
  209. package/foundation/identity/provider-identity.mjs +10 -1
  210. package/foundation/render/embedded-imports.d.mts +5 -4
  211. package/foundation/render/embedded-imports.mjs +7 -16
  212. package/foundation/render/file-header.d.mts +66 -0
  213. package/foundation/render/file-header.mjs +118 -0
  214. package/foundation/render/host-answer.d.mts +18 -0
  215. package/foundation/render/host-answer.mjs +46 -0
  216. package/foundation/render/host-exit.d.mts +1 -0
  217. package/foundation/render/host-exit.mjs +6 -1
  218. package/foundation/render/html-reader.d.mts +55 -2
  219. package/foundation/render/html-reader.mjs +145 -26
  220. package/foundation/render/markup-edits.d.mts +57 -0
  221. package/foundation/render/markup-edits.mjs +182 -0
  222. package/foundation/render/page-host.mjs +29 -12
  223. package/foundation/render/page-worker.mjs +83 -13
  224. package/foundation/render/render-page.d.mts +34 -2
  225. package/foundation/render/render-page.mjs +210 -29
  226. package/foundation/render/source-block.d.mts +73 -6
  227. package/foundation/render/source-block.mjs +170 -19
  228. package/foundation/render/ts-paths.d.mts +53 -6
  229. package/foundation/render/ts-paths.mjs +286 -41
  230. package/foundation/response/error-codes.d.mts +3 -1
  231. package/foundation/response/error-codes.d.ts +1 -0
  232. package/foundation/response/error-codes.doc.mjs +12 -1
  233. package/foundation/response/error-codes.mjs +10 -0
  234. package/foundation/text/string-utils.d.mts +3 -2
  235. package/foundation/text/string-utils.mjs +16 -3
  236. package/foundation/text/suggest-order.d.mts +9 -0
  237. package/foundation/text/suggest-order.mjs +13 -0
  238. package/host/index.mjs +8 -6
  239. package/package.json +6 -6
@@ -0,0 +1,661 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file The theme editor's input normaliser: how a typed or saved thing and
5
+ * value are read before any check (Unicode spaces, delimiting quotes, a
6
+ * pasted declaration, JSON and copy debris, a font stack's families). Typed
7
+ * input (`set`, `undo`) and saved settings (`loadTheme`, the stored checks)
8
+ * both read through it.
9
+ * @position packages/cli/api/theme-edit — pure helpers, no environment access
10
+ */
11
+
12
+ import {levenshteinDistance} from '../../foundation/text/levenshtein.mjs';
13
+ import {DIALS} from './_tables.mjs';
14
+
15
+ /**
16
+ * A value as CSS reads it, for every value type: every Unicode space
17
+ * (no-break, thin, ideographic…) a plain space and zero-width and format
18
+ * characters dropped, and quotes that delimit a value (curly ones, «», ‹›,
19
+ * full-width, backticks) straight. A straight-quoted CSS string keeps what
20
+ * it holds as written (`content: "it’s"`). Curly quotes pair by kind
21
+ * (double with double, single with single, in any of their shapes: „Inter“,
22
+ * ‚Inter‘); one left unpaired is read as the straight quote it stands for.
23
+ * @param {string} v
24
+ * @returns {string}
25
+ */
26
+ export function normalValueText(v) {
27
+ let out = '';
28
+ let plain = '';
29
+ const flush = () => {
30
+ out += plain
31
+ .replace(/[\u200B-\u200D\u2060\uFEFF]/g, '')
32
+ .replace(/[\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, ' ');
33
+ plain = '';
34
+ };
35
+ for (let i = 0; i < v.length;) {
36
+ const c = v[i];
37
+ // A straight-quoted CSS string keeps everything inside it.
38
+ if (c === '"' || c === "'") {
39
+ let j = i + 1;
40
+ while (j < v.length && v[j] !== c) j += v[j] === '\\' ? 2 : 1;
41
+ if (j < v.length) {
42
+ flush();
43
+ out += v.slice(i, j + 1);
44
+ i = j + 1;
45
+ continue;
46
+ }
47
+ }
48
+ const kind = CURLY_DOUBLE.has(c)
49
+ ? CURLY_DOUBLE
50
+ : CURLY_SINGLE.has(c)
51
+ ? CURLY_SINGLE
52
+ : null;
53
+ /** @type {number} */
54
+ let j = -1;
55
+ if (kind) {
56
+ for (let k = i + 1; k < v.length; k++)
57
+ if (kind.has(v[k])) {
58
+ j = k;
59
+ break;
60
+ }
61
+ } else if (QUOTE_PAIRS[c]) j = v.indexOf(QUOTE_PAIRS[c], i + 1);
62
+ if (j > i) {
63
+ flush();
64
+ // What the quotes hold, as typed (`“ / ”` is a slash between
65
+ // spaces); a font family trims it (normalizeEntry).
66
+ const inner = v.slice(i + 1, j);
67
+ const single = kind === CURLY_SINGLE || c === '\uFF07';
68
+ const mark = single
69
+ ? inner.includes("'")
70
+ ? '"'
71
+ : "'"
72
+ : inner.includes('"')
73
+ ? "'"
74
+ : '"';
75
+ out += `${mark}${inner}${mark}`;
76
+ i = j + 1;
77
+ continue;
78
+ }
79
+ // An unpaired curly quote is the straight one it stands for.
80
+ plain += kind === CURLY_DOUBLE ? '"' : kind === CURLY_SINGLE ? "'" : c;
81
+ i += 1;
82
+ }
83
+ flush();
84
+ return out;
85
+ }
86
+
87
+ /** Curly double quotes, in every shape; any one closes any other. */
88
+ const CURLY_DOUBLE = new Set(['\u201C', '\u201D', '\u201E', '\u201F']);
89
+ /** Curly single quotes, in every shape. */
90
+ const CURLY_SINGLE = new Set(['\u2018', '\u2019', '\u201A', '\u201B']);
91
+ /** The other delimiting quotes and the one that closes each. */
92
+ /** @type {Record<string, string>} */
93
+ const QUOTE_PAIRS = {
94
+ '\u300C': '\u300D',
95
+ '\u300E': '\u300F',
96
+ '\u00AB': '\u00BB',
97
+ '\u00BB': '\u00AB',
98
+ '\u2039': '\u203A',
99
+ '\u203A': '\u2039',
100
+ '\uFF02': '\uFF02',
101
+ '\uFF07': '\uFF07',
102
+ '`': '`',
103
+ };
104
+
105
+ /** CSS functions whose names a value may write in any case. */
106
+ const CSS_FUNCTIONS =
107
+ /\b(light-dark|rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|color-mix|calc|min|max|clamp|var)\(/gi;
108
+
109
+ /**
110
+ * A value as meant when typed with escapes or in another notation: CSS
111
+ * function names in lower case (`LIGHT-DARK(` is `light-dark(`, as CSS reads
112
+ * it); a shell or JSON backslash before `#`, a quote or a space taken off;
113
+ * a JavaScript escape in a `content` string written as CSS writes it
114
+ * (`\u2022` is `\2022 `, `\n` a line break, `\t` a tab); and the JSON
115
+ * shapes a design-tokens file or an array writes (`{"$value": "#e11d48"}`,
116
+ * `{"value": 18, "unit": "px"}`, `["#e11d48"]`, `["Inter", "sans-serif"]`).
117
+ * @param {string} thing
118
+ * @param {string} value
119
+ * @returns {string}
120
+ */
121
+ export function escapesMeant(thing, value) {
122
+ let v = value;
123
+ const content = /^content$/i.test(thing);
124
+ if (content)
125
+ return (
126
+ v
127
+ .replace(
128
+ /\\u\{?([0-9a-f]{2,6})\}?/gi,
129
+ (_, h) => `\\${h.toUpperCase()} `,
130
+ )
131
+ // `\x` with four hex digits is written for the code point (as
132
+ // `\u`), with two for that character.
133
+ .replace(
134
+ /\\x([0-9a-f]{4}|[0-9a-f]{2})/gi,
135
+ (_, h) => `\\${h.toUpperCase()} `,
136
+ )
137
+ .replace(/\\n/g, '\\A ')
138
+ .replace(/\\t/g, '\\9 ')
139
+ .replace(/\\r/g, '\\D ')
140
+ );
141
+ v = v.replace(CSS_FUNCTIONS, f => f.toLowerCase());
142
+ // A JS escape of a printable character in a font family (`Inter\u0020
143
+ // Tight`) is that character, as CSS would read its own escape.
144
+ if (/font/i.test(thing))
145
+ v = v.replace(
146
+ /\\(?:u\{?([0-9a-f]{4,6})\}?|x([0-9a-f]{2}))/gi,
147
+ (m, u, x) => {
148
+ const c = String.fromCodePoint(parseInt(u ?? x, 16));
149
+ return /^[\p{L}\p{N} _-]$/u.test(c) ? c : m;
150
+ },
151
+ );
152
+ // (A JSON string holding escaped quotes is unwrapped whole later.)
153
+ if (!/^\s*"(?:[^"\\]|\\.)*"\s*$/.test(v)) v = v.replace(/\\([#"' ])/g, '$1');
154
+ else v = v.replace(/\\([# ])/g, '$1');
155
+ const t = v.trim();
156
+ if (/^[[{]/.test(t)) {
157
+ /** @type {any} */
158
+ let j;
159
+ try {
160
+ j = JSON.parse(t);
161
+ } catch {
162
+ // A list in single quotes, as a JS config writes it (`['Inter',
163
+ // 'serif']`): read like the JSON one.
164
+ const items =
165
+ /^\[\s*('(?:[^'\\]|\\.)*'\s*(?:,\s*'(?:[^'\\]|\\.)*'\s*)*),?\s*\]$/.exec(
166
+ t,
167
+ );
168
+ if (!items) return v;
169
+ j = [...items[1].matchAll(/'((?:[^'\\]|\\.)*)'/g)].map(x =>
170
+ x[1].replace(/\\(.)/g, '$1'),
171
+ );
172
+ }
173
+ /** @param {any} x */
174
+ const text = x =>
175
+ typeof x === 'string' || typeof x === 'number' ? String(x) : null;
176
+ if (Array.isArray(j) && j.length && j.every(x => text(x) != null)) {
177
+ if (j.length === 1) return String(j[0]);
178
+ if (/font/i.test(thing))
179
+ return j
180
+ .map(x =>
181
+ /\s/.test(String(x)) && !/^["']/.test(String(x))
182
+ ? `"${x}"`
183
+ : String(x),
184
+ )
185
+ .join(', ');
186
+ return v;
187
+ }
188
+ if (j && typeof j === 'object' && !Array.isArray(j)) {
189
+ const inner =
190
+ '$value' in j ? j.$value : 'value' in j ? j.value : undefined;
191
+ if (text(inner) != null)
192
+ return `${inner}${typeof j.unit === 'string' ? j.unit : ''}`;
193
+ // One labeled entry (`{"color": "#e11d48"}`) is read as the same
194
+ // label in its CSS form (`color: #e11d48`): one reader for both.
195
+ const keys = Object.keys(j);
196
+ if (
197
+ keys.length === 1 &&
198
+ /^(--)?[a-z][a-z0-9-]*$/i.test(keys[0]) &&
199
+ text(j[keys[0]]) != null
200
+ )
201
+ return `${keys[0]}: ${j[keys[0]]}`;
202
+ }
203
+ }
204
+ return v;
205
+ }
206
+
207
+ /**
208
+ * The thing and value a setting was typed as, put right before any check: a
209
+ * declaration pasted whole or split (`accent: #e11d48`, `--color-accent=red`,
210
+ * `font:inter`, `accent:` with the value next), a token typed with an em or
211
+ * en dash (`—color-accent`) or as `var(--color-accent)`, and the value's
212
+ * Unicode spaces. A thing it cannot read is left as typed.
213
+ * @param {string} thing
214
+ * @param {string | undefined} value
215
+ * @returns {{thing: string, value: string | undefined, dropped?: string}} `dropped`: a second declaration pasted after the first, which is not set
216
+ */
217
+ export function normalizeEntry(thing, value) {
218
+ let t = normalValueText(String(thing ?? '')).trim();
219
+ let v = value == null ? value : normalValueText(String(value));
220
+ // A pasted declaration (the whole of it in the thing, or the thing with
221
+ // its colon and the value next): its `;` and `!important` go with it.
222
+ let pasted = false;
223
+ // A JSON object around one entry (`{"accent": "#e11d48"}`), whole or
224
+ // split at its colon.
225
+ if ((v == null || v.trim() === '') && /^\{[\s\S]*\}$/.test(t))
226
+ t = t.slice(1, -1).trim().replace(/,\s*$/, '');
227
+ else if (t.startsWith('{') && typeof v === 'string' && /\}\s*$/.test(v)) {
228
+ t = t.slice(1).trim();
229
+ v = v.replace(/\s*\}\s*$/, '');
230
+ }
231
+ if (v == null || v.trim() === '') {
232
+ const m = /^(\S+?)\s*[:=]\s*(.+?)\s*;?\s*$/.exec(t);
233
+ if (m && !/^https?$/i.test(m[1])) {
234
+ t = m[1];
235
+ v = m[2];
236
+ pasted = true;
237
+ }
238
+ }
239
+ // A thing ending in its colon or equals sign, the value given next.
240
+ if (/\s*[:=]+$/.test(t)) {
241
+ t = t.replace(/\s*[:=]+$/, '');
242
+ pasted = true;
243
+ }
244
+ // A thing in quotes, as a JSON or design-tokens file writes it
245
+ // (`"accent": "#e11d48"`).
246
+ const quotedThing = /^(["'])(.+)\1$/.exec(t);
247
+ if (quotedThing) {
248
+ t = quotedThing[2].trim();
249
+ pasted = true;
250
+ // Its value in JSON's double quotes is the value (`"#e11d48"`); a font
251
+ // list keeps its own quotes.
252
+ if (typeof v === 'string' && !/font/i.test(t)) {
253
+ const jv = /^\s*"([^"]*)"\s*,?\s*$/.exec(v);
254
+ if (jv) v = jv[1];
255
+ }
256
+ }
257
+ // Dash-like characters where a token's two dashes go (an em or en dash,
258
+ // a non-breaking hyphen, a minus sign, a full-width hyphen, alone or
259
+ // mixed with hyphens): `--`.
260
+ t = t.replace(/^[-\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]+/, run =>
261
+ /[^-]/.test(run) ? '--' : run,
262
+ );
263
+ // A dash-like character inside a token's name is its hyphen.
264
+ if (t.startsWith('--'))
265
+ t = `--${t.slice(2).replace(/[\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]/g, '-')}`;
266
+ // A token typed as a var() reference.
267
+ const ref = /^var\(\s*(--[a-z0-9-]+)\s*\)$/i.exec(t);
268
+ if (ref) t = ref[1];
269
+ // A dial name written in another style (textSize, text_size, a dash-like
270
+ // character for its hyphen) is the dial.
271
+ if (
272
+ !t.startsWith('-') &&
273
+ /[_\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]|[a-z][A-Z]/.test(t)
274
+ ) {
275
+ const dial = t
276
+ .replace(/[_\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]/g, '-')
277
+ .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
278
+ .toLowerCase();
279
+ if (DIALS[dial]) t = dial;
280
+ }
281
+ if (typeof v === 'string') v = escapesMeant(t, v);
282
+ // A second declaration pasted after the first (`accent: red; text-size:
283
+ // 18`): the first is this setting.
284
+ /** A second declaration pasted after the first, which this does not set. @type {string | null} */
285
+ let dropped = null;
286
+ if (typeof v === 'string') {
287
+ const cut = withoutSecondDeclaration(v);
288
+ if (cut !== v)
289
+ dropped = v
290
+ .slice(cut.length)
291
+ .replace(/^\s*;\s*/, '')
292
+ .trim();
293
+ v = cut;
294
+ }
295
+ if (pasted && typeof v === 'string') {
296
+ v = v.replace(/\s*!\s*important\s*;?\s*$/i, '').replace(/;+\s*$/, '');
297
+ }
298
+ if (typeof v === 'string') v = withoutDebris(v);
299
+ // A font stack written as a JSON string (`"\\"Open Sans\\", serif"`): the
300
+ // stack it holds.
301
+ if (
302
+ typeof v === 'string' &&
303
+ /font/i.test(t) &&
304
+ /^"(?:[^"\\]|\\.)*"$/.test(v.trim()) &&
305
+ v.includes('\\"')
306
+ ) {
307
+ try {
308
+ const inner = JSON.parse(v.trim());
309
+ if (typeof inner === 'string') v = inner;
310
+ } catch {
311
+ // Not a JSON string.
312
+ }
313
+ }
314
+ // A font stack with its quotes escaped, as it sits inside a JSON or JS
315
+ // string (`\"Inter\", sans-serif`): the stack.
316
+ if (typeof v === 'string' && /font/i.test(t) && /\\["']/.test(v))
317
+ v = v.replace(/\\(["'])/g, '$1');
318
+ // A font family's name never starts or ends with a space: French quotes
319
+ // (« Inter », with a no-break or narrow no-break space) hold the name.
320
+ if (typeof v === 'string' && /font/i.test(t)) v = trimInsideQuotes(v);
321
+ return {thing: t, value: v, ...(dropped ? {dropped} : {})};
322
+ }
323
+
324
+ /**
325
+ * A font stack's pieces, split on the commas outside quotes, with each
326
+ * piece's name and whether it was quoted; null when a quote is left open.
327
+ * @param {string} v
328
+ * @returns {Array<{name: string, quoted: boolean}> | null}
329
+ */
330
+ export function familyPieces(v) {
331
+ /** @type {Array<{name: string, quoted: boolean}>} */
332
+ const out = [];
333
+ let cur = '';
334
+ let quoted = false;
335
+ /** @type {string | null} */
336
+ let q = null;
337
+ let closed = false;
338
+ for (const c of String(v)) {
339
+ if (q) {
340
+ if (c === q) {
341
+ q = null;
342
+ closed = true;
343
+ } else cur += c;
344
+ continue;
345
+ }
346
+ if (c === '"' || c === "'") {
347
+ // A quote opening after text, or a second quoted name before a
348
+ // comma: not a stack of whole pieces.
349
+ if (cur.trim() || closed) return null;
350
+ q = c;
351
+ quoted = true;
352
+ continue;
353
+ }
354
+ // Text after a quoted name before its comma (`"Open Sans" serif`).
355
+ if (closed && c !== ',' && c.trim()) return null;
356
+ if (c === ',') {
357
+ out.push({name: cur.trim(), quoted});
358
+ cur = '';
359
+ quoted = false;
360
+ closed = false;
361
+ continue;
362
+ }
363
+ cur += c;
364
+ }
365
+ if (q) return null;
366
+ out.push({name: cur.trim(), quoted});
367
+ return out;
368
+ }
369
+
370
+ /**
371
+ * The families a typed font stack names: a trailing comma and empty pieces
372
+ * dropped, and a stack wrapped in one more pair of the quotes it uses
373
+ * inside (`""SF Mono", Monaco, monospace"`, as a JSON string) unwrapped.
374
+ * Null when the stack does not read.
375
+ * @param {string} v
376
+ * @returns {string[] | null}
377
+ */
378
+ export function familiesOf(v) {
379
+ let s = String(v)
380
+ .trim()
381
+ .replace(/,+\s*$/, '');
382
+ const w = /^(["'])([\s\S]*)\1$/.exec(s);
383
+ if (w && w[2].includes(w[1])) s = w[2].trim().replace(/,+\s*$/, '');
384
+ const pieces = familyPieces(s);
385
+ if (!pieces) return null;
386
+ return pieces.map(f => f.name).filter(Boolean);
387
+ }
388
+
389
+ /**
390
+ * A value with the debris of a bad copy taken off: a leading `=` or `:`, a
391
+ * trailing comma (a JSON or CSS list), and a quote or a closing bracket left
392
+ * over at either end. A value without any is returned as it was.
393
+ * @param {string} v
394
+ * @returns {string}
395
+ */
396
+ export function withoutDebris(v) {
397
+ let s = v.replace(/^\s*[=:]\s*/, '').replace(/\s*,+\s*$/, '');
398
+ // Quotes and brackets counted as CSS reads them: an escaped quote (\")
399
+ // is no quote, and a bracket inside a string is the string's.
400
+ const scan = (/** @type {string} */ t) => {
401
+ /** @type {Record<string, number>} */
402
+ const quotes = {'"': 0, "'": 0};
403
+ let open = 0;
404
+ let close = 0;
405
+ /** @type {string | null} */
406
+ let q = null;
407
+ for (let i = 0; i < t.length; i++) {
408
+ const c = t[i];
409
+ if (c === '\\') {
410
+ i++;
411
+ continue;
412
+ }
413
+ if (q) {
414
+ if (c === q) {
415
+ q = null;
416
+ quotes[c]++;
417
+ }
418
+ continue;
419
+ }
420
+ if (c === '"' || c === "'") {
421
+ q = c;
422
+ quotes[c]++;
423
+ } else if (c === '(') open++;
424
+ else if (c === ')') close++;
425
+ }
426
+ return {quotes, open, close};
427
+ };
428
+ for (const mark of /** @type {const} */ (['"', "'"])) {
429
+ if (scan(s).quotes[mark] % 2 === 1) {
430
+ if (s.endsWith(mark) && !s.endsWith(`\\${mark}`)) s = s.slice(0, -1);
431
+ else if (s.startsWith(mark)) s = s.slice(1);
432
+ }
433
+ }
434
+ const {open, close} = scan(s);
435
+ if (close > open && s.endsWith(')')) s = s.slice(0, -1);
436
+ return s === v.trim() ? v : s.trim();
437
+ }
438
+
439
+ /**
440
+ * A value without a second declaration pasted after it (`#e11d48;
441
+ * text-size: 18`): cut at the first `;` outside quotes that a `name:`
442
+ * follows. A `;` inside a quoted string (`"Tip; note: x"`) is the string's.
443
+ * @param {string} v
444
+ * @returns {string}
445
+ */
446
+ export function withoutSecondDeclaration(v) {
447
+ /** @type {string | null} */
448
+ let q = null;
449
+ for (let i = 0; i < v.length; i++) {
450
+ const c = v[i];
451
+ if (q) {
452
+ if (c === '\\') i++;
453
+ else if (c === q) q = null;
454
+ continue;
455
+ }
456
+ if (c === '"' || c === "'") q = c;
457
+ else if (
458
+ c === ';' &&
459
+ /^\s*[a-z-]+\s*:/i.test(v.slice(i + 1)) &&
460
+ // `light: red; dark: blue` is one value for two modes.
461
+ !/^\s*(?:light|dark)\s*:/i.test(v.slice(i + 1))
462
+ )
463
+ return v.slice(0, i);
464
+ }
465
+ return v;
466
+ }
467
+
468
+ /**
469
+ * A saved dial value read as typed input is (invisible or stray spaces,
470
+ * quotes around it, a pasted `corners: pill`, a trailing `;`, other
471
+ * letters): the value the dial may have meant.
472
+ * @param {string} dial
473
+ * @param {string} value
474
+ * @returns {string}
475
+ */
476
+ export function cleanDialValue(dial, value) {
477
+ const read = normalizeEntry(dial, value).value ?? '';
478
+ return read
479
+ .trim()
480
+ .replace(new RegExp(`^${dial.replace(/-/g, '\\-')}\\s*[:=]\\s*`, 'i'), '')
481
+ .replace(/\s*!\s*important\s*$/i, '')
482
+ .replace(/;+\s*$/, '')
483
+ .replace(/^(["'])(.*)\1$/, '$2')
484
+ .trim()
485
+ .toLowerCase();
486
+ }
487
+
488
+ /**
489
+ * A value with the spaces just inside each straight-quoted string taken off
490
+ * (`" Inter "` is "Inter"), for a font family, whose name holds none there.
491
+ * @param {string} v
492
+ * @returns {string}
493
+ */
494
+ export function trimInsideQuotes(v) {
495
+ const SP = '[\\s\\u00A0\\u202F\\u2000-\\u200A\\u3000]*';
496
+ return v.replace(
497
+ new RegExp(`(["'])${SP}((?:(?!\\1)[^\\\\]|\\\\.)*?)${SP}\\1`, 'g'),
498
+ '$1$2$1',
499
+ );
500
+ }
501
+
502
+ /**
503
+ * The other setting a dial's value is labeled with, when pasted with a
504
+ * label that names a different dial or a token (`neutral: #64748b` given
505
+ * to accent): that label and the value after it, else null. A label that
506
+ * names this dial, or a CSS property (`color: #e11d48`), is no other
507
+ * setting.
508
+ * @param {string} dial
509
+ * @param {string} value
510
+ * @returns {{thing: string, value: string} | null}
511
+ */
512
+ export function otherSettingLabel(dial, value) {
513
+ const m =
514
+ /^\s*["']?([a-z][a-z0-9_-]*|--[a-z0-9-]+)["']?\s*[:=]\s*["']?(.+?)["']?\s*;?\s*$/i.exec(
515
+ String(value),
516
+ );
517
+ if (!m) return null;
518
+ // A label in camelCase or snake_case (textSize, text_size) is the dial.
519
+ const label = m[1]
520
+ .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
521
+ .replace(/_/g, '-')
522
+ .toLowerCase();
523
+ if (label === dial) return null;
524
+ if (!DIALS[label] && !label.startsWith('--')) return null;
525
+ return {thing: label, value: m[2]};
526
+ }
527
+
528
+ /**
529
+ * A light and a dark color typed as a pair: labeled (`light: red; dark:
530
+ * blue`), a JSON object (`{"light": "red", "dark": "blue"}`), or two colors
531
+ * with a slash or a comma between (`red / blue`, `red, blue`). Null when the
532
+ * value is no such pair; the caller checks that both are colors.
533
+ * @param {string} value
534
+ * @returns {{light: string, dark: string} | null}
535
+ */
536
+ export function colorPair(value) {
537
+ const v = String(value).trim();
538
+ const unq = (/** @type {string} */ x) =>
539
+ x
540
+ .trim()
541
+ .replace(/^(["'])(.*)\1$/, '$2')
542
+ .trim();
543
+ const labeled =
544
+ /^\{?\s*["']?(light|dark)["']?\s*[:=]\s*(.+?)\s*(?:[;,]\s*|\s+)["']?(light|dark)["']?\s*[:=]\s*(.+?)\s*;?\s*\}?$/i.exec(
545
+ v,
546
+ );
547
+ if (labeled && labeled[1].toLowerCase() !== labeled[3].toLowerCase()) {
548
+ const a = unq(labeled[2]);
549
+ const b = unq(labeled[4]);
550
+ return labeled[1].toLowerCase() === 'light'
551
+ ? {light: a, dark: b}
552
+ : {light: b, dark: a};
553
+ }
554
+ const two = /^([^\s,/()]+)\s*(?:\s\/\s|,)\s*([^\s,/()]+)$/.exec(v);
555
+ if (two) return {light: unq(two[1]), dark: unq(two[2])};
556
+ return null;
557
+ }
558
+
559
+ /**
560
+ * The stock values a repair would put in when it could read nothing from
561
+ * the input: the example of each value type, and the dials' defaults. A
562
+ * line with one of these is offered only when the input held it.
563
+ */
564
+ export const STOCK_VALUES = new Set([
565
+ '#1f2937',
566
+ '#7c3aed',
567
+ '#ffffff',
568
+ 'inter',
569
+ 'inter, sans-serif',
570
+ 'system',
571
+ '1px',
572
+ '6px',
573
+ '12px',
574
+ '16',
575
+ '600',
576
+ '1.4',
577
+ '150ms',
578
+ 'solid',
579
+ // The dials' fallback words and the size tokens' Core defaults, offered
580
+ // when nothing could be read.
581
+ 'medium',
582
+ 'roomy',
583
+ 'compact',
584
+ 'round',
585
+ 'light-dark(#1f2937, #f9fafb)',
586
+ ]);
587
+
588
+ /**
589
+ * Whether a suggested value carries what was typed: each of its words and
590
+ * numbers is in the typed text (cleaned: case, quotes, escapes), apart from
591
+ * a unit added to a bare number and the words of the CSS form it is written
592
+ * in (`light-dark()`, `var()`, `calc()`, a generic family). A stock value
593
+ * the input never held (a gray, a violet, `system`, `small`) does not.
594
+ * @param {string} offered
595
+ * @param {string} typed
596
+ */
597
+ export function carriesTyped(offered, typed) {
598
+ const flat = String(typed)
599
+ .toLowerCase()
600
+ .replace(/\\(?=[#"' ])/g, '')
601
+ .replace(/[\s"'\u201C\u201D\u2018\u2019\u300C\u300D]/g, '');
602
+ const FORM = new Set(['light', 'dark', 'var', 'calc', 'px', 'rem', 'em']);
603
+ return (
604
+ String(offered)
605
+ .toLowerCase()
606
+ .match(/[a-z0-9.]+/g) ?? []
607
+ )
608
+ .map(w => w.replace(/^([0-9.]+)(px|rem|em)$/, '$1'))
609
+ .filter(w => !FORM.has(w))
610
+ .every(w => flat.includes(w));
611
+ }
612
+
613
+ /**
614
+ * Whether a repair may offer `offered` for `typed`: a stock value only when
615
+ * the input held it; any other value (one read from the input, or mapped
616
+ * from it by a documented rule) may be.
617
+ * @param {string} offered
618
+ * @param {string} typed
619
+ */
620
+ export const offersTyped = (offered, typed) =>
621
+ !STOCK_VALUES.has(String(offered).toLowerCase()) ||
622
+ carriesTyped(offered, typed);
623
+
624
+ /**
625
+ * Whether a typed word is a typo of a word: both words of four letters or
626
+ * more, within one edit for a typed word of five letters or fewer and two
627
+ * for a longer one (`nnone` is none, `normall` normal; `line` is no none).
628
+ * @param {string} typed
629
+ * @param {string} word
630
+ */
631
+ export function typoOf(typed, word) {
632
+ const t = String(typed).trim().toLowerCase();
633
+ const w = String(word).trim().toLowerCase();
634
+ return (
635
+ /^[a-z][a-z-]{3,}$/.test(t) && /^[a-z][a-z-]{3,}$/.test(w) && typoNear(t, w)
636
+ );
637
+ }
638
+
639
+ /**
640
+ * The edits a typo of a typed word may hold: one for a word of five letters
641
+ * or fewer, two for a longer one. Every typo reading of the editor (a value,
642
+ * a setting, a part, a state, a theme or token name) uses this limit.
643
+ * @param {string} typed
644
+ */
645
+ export const typoLimit = typed => (String(typed).trim().length <= 5 ? 1 : 2);
646
+
647
+ /**
648
+ * Whether `word` is within the typo limit of `typed` (two letters swapped
649
+ * count as one edit, as typed: flxe is flex).
650
+ * @param {string} typed
651
+ * @param {string} word
652
+ */
653
+ export function typoNear(typed, word) {
654
+ const t = String(typed).trim().toLowerCase();
655
+ const limit = typoLimit(t);
656
+ return (
657
+ levenshteinDistance(t, String(word).trim().toLowerCase(), limit, {
658
+ swaps: true,
659
+ }) <= limit
660
+ );
661
+ }
@@ -156,7 +156,11 @@ function sweepTemps(dir) {
156
156
  }
157
157
  continue;
158
158
  }
159
- const m = /^\..+\.astryx-(\d+)-[a-z0-9]+\.tmp$/.exec(n);
159
+ // A picture taken under its own name (`.<picture>.part-<pid>.png`) by a
160
+ // run stopped while the browser ran.
161
+ const m =
162
+ /^\..+\.astryx-(\d+)-[a-z0-9]+\.tmp$/.exec(n) ??
163
+ /^\.[\w-]+\.part-(\d+)\.png$/.exec(n);
160
164
  if (!m) continue;
161
165
  const p = path.join(dir, n);
162
166
  try {
@@ -203,12 +207,14 @@ export function removeEmptyFolder(dir, opts = {}) {
203
207
 
204
208
  /**
205
209
  * Whether a file name is one of the editor's own temp files: a whole-file
206
- * write's `.<name>.astryx-<pid>-<random>.tmp`, or a build's
207
- * `.<name>.tmp-<id>`. A command stopped part way can leave one behind.
210
+ * write's `.<name>.astryx-<pid>-<random>.tmp`, a build's `.<name>.tmp-<id>`,
211
+ * or a picture's `.<picture>.part-<pid>.png`. A command stopped part way can
212
+ * leave one behind.
208
213
  * @param {string} name
209
214
  */
210
215
  export const isEditorTemp = name =>
211
- /^\..+\.(astryx-\d+-[a-z0-9]+\.tmp|tmp-[\w-]+)$/.test(name);
216
+ /^\..+\.(astryx-\d+-[a-z0-9]+\.tmp|tmp-[\w-]+)$/.test(name) ||
217
+ /^\.[\w-]+\.part-\d+\.png$/.test(name);
212
218
 
213
219
  /**
214
220
  * A temp file beside `file` for a whole write, named so only the editor