@pantoken/utils 1.2.0 → 1.3.0

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.
@@ -0,0 +1,12 @@
1
+ //#region src/example-targets.d.ts
2
+ /** Consumer supported by a cssdoc example's audience flag. */
3
+ export type ExampleAudience = "automatica11y" | "canvas-rce" | "docs";
4
+ /** Example indexes selected for each cssdoc consumer. */
5
+ export interface ExampleTargetSelection {
6
+ automatica11y: number[];
7
+ canvasRce: number[];
8
+ docs: number[];
9
+ }
10
+ /** Resolve example indexes for each consumer while preserving the pre-flag defaults. */
11
+ export declare function resolveExampleTargets(examples: readonly string[]): ExampleTargetSelection;
12
+ //#endregion
@@ -0,0 +1,43 @@
1
+ //#region src/example-targets.ts
2
+ const AUDIENCE_FLAGS = /* @__PURE__ */ new Map([
3
+ ["-automatica11y", "automatica11y"],
4
+ ["-canvas-rce", "canvas-rce"],
5
+ ["-docs", "docs"]
6
+ ]);
7
+ function audienceFlags(example) {
8
+ const fenceStart = example.indexOf("```");
9
+ const metadata = fenceStart < 0 ? example : example.slice(0, fenceStart);
10
+ const fenceInfo = fenceStart < 0 ? "" : example.slice(fenceStart + 3).split(/\r?\n/u, 1)[0] ?? "";
11
+ const metadataTokens = metadata.trim().split(/\s+/u).filter(Boolean);
12
+ const metadataIsFlags = metadataTokens.every((token) => /^-[a-z][a-z0-9-]*$/u.test(token));
13
+ const flags = /* @__PURE__ */ new Set();
14
+ for (const token of [...metadataIsFlags ? metadataTokens : [], ...fenceInfo.split(/\s+/u)]) {
15
+ const audience = AUDIENCE_FLAGS.get(token);
16
+ if (audience) flags.add(audience);
17
+ }
18
+ return flags;
19
+ }
20
+ /** Resolve example indexes for each consumer while preserving the pre-flag defaults. */
21
+ function resolveExampleTargets(examples) {
22
+ const flags = examples.map(audienceFlags);
23
+ const explicit = (audience) => flags.flatMap((exampleFlags, index) => exampleFlags.has(audience) ? [index] : []);
24
+ const automatica11y = explicit("automatica11y");
25
+ const canvasRce = explicit("canvas-rce");
26
+ const docs = explicit("docs");
27
+ if (automatica11y.length + canvasRce.length + docs.length === 0) return {
28
+ automatica11y: [],
29
+ canvasRce: examples.length === 0 ? [] : [0],
30
+ docs: examples.map((_, index) => index)
31
+ };
32
+ const unassigned = flags.flatMap((exampleFlags, index) => exampleFlags.size === 0 ? [index] : []);
33
+ const fallbackCanvas = canvasRce.length === 0 ? unassigned.slice(0, 1) : [];
34
+ const assignedCanvas = canvasRce.length > 0 ? canvasRce : fallbackCanvas;
35
+ const fallbackDocs = docs.length > 0 ? [] : unassigned.filter((index) => !fallbackCanvas.includes(index));
36
+ return {
37
+ automatica11y,
38
+ canvasRce: assignedCanvas,
39
+ docs: docs.length > 0 ? docs : fallbackDocs
40
+ };
41
+ }
42
+ //#endregion
43
+ export { resolveExampleTargets };
package/dist/index.mjs CHANGED
@@ -132,6 +132,28 @@ const VAR_RE = regex(VAR_SOURCE, "g");
132
132
  /** A `light-dark(<light>, <dark>)` value; capture groups 1 and 2 are the two branches. */
133
133
  const LIGHT_DARK_RE = regex(LIGHT_DARK_SOURCE);
134
134
  const MAX_DEPTH = 12;
135
+ function lightDarkBranches(value) {
136
+ const trimmed = value.trim();
137
+ if (!trimmed.startsWith("light-dark(") || !trimmed.endsWith(")")) return void 0;
138
+ const content = trimmed.slice(11, -1);
139
+ let depth = 0;
140
+ let separator = -1;
141
+ for (let index = 0; index < content.length; index += 1) {
142
+ const char = content[index];
143
+ if (char === "(") depth += 1;
144
+ else if (char === ")") {
145
+ depth -= 1;
146
+ if (depth < 0) return void 0;
147
+ } else if (char === "," && depth === 0) {
148
+ if (separator !== -1) return void 0;
149
+ separator = index;
150
+ }
151
+ }
152
+ if (depth !== 0 || separator === -1) return void 0;
153
+ const light = content.slice(0, separator).trim();
154
+ const dark = content.slice(separator + 1).trim();
155
+ return light && dark ? [light, dark] : void 0;
156
+ }
135
157
  /**
136
158
  * Build a resolver that expands `var(--x)` references to concrete leaf values against `base` (plus
137
159
  * any `overrides`). With `mode` it collapses `light-dark()` to that branch; without, it leaves
@@ -191,8 +213,8 @@ function makeResolver(base, options = {}) {
191
213
  for (const t of options.overrides ?? []) map.set(t.name, t.flatValue ?? t.value);
192
214
  const pickMode = (value) => {
193
215
  if (!options.mode) return value;
194
- const m = LIGHT_DARK_RE.exec(value.trim());
195
- return m ? options.mode === "light" ? m[1] : m[2] : value;
216
+ const branches = lightDarkBranches(value);
217
+ return branches ? branches[options.mode === "light" ? 0 : 1] : value;
196
218
  };
197
219
  const expand = (value, depth) => {
198
220
  const picked = pickMode(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pantoken/utils",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Shared pantoken helpers: token reference resolver, typed regexes (arkregex), name-case, and hex-colour parsing.",
5
5
  "homepage": "https://pantoken.app",
6
6
  "bugs": "https://github.com/thedannywahl/pantoken/issues",
@@ -18,6 +18,7 @@
18
18
  "exports": {
19
19
  ".": "./dist/index.mjs",
20
20
  "./token-syntax": "./dist/token-syntax.mjs",
21
+ "./example-targets": "./dist/example-targets.mjs",
21
22
  "./scope": "./dist/scope.mjs",
22
23
  "./package.json": "./package.json"
23
24
  },
@@ -27,15 +28,15 @@
27
28
  },
28
29
  "dependencies": {
29
30
  "@pantoken/model": "0.5.0",
30
- "arkregex": "^0.0.8",
31
+ "arkregex": "^0.0.12",
31
32
  "css-tree": "^3.2.1"
32
33
  },
33
34
  "devDependencies": {
34
35
  "@types/css-tree": "^3.2.0",
35
- "@types/node": "^26.6.3",
36
+ "@types/node": "^26.6.5",
36
37
  "typescript": "^7.0.2",
37
- "vite": "npm:@voidzero-dev/vite-plus-core@1.0.0-rc.0",
38
- "vite-plus": "1.0.0-rc.0"
38
+ "vite": "npm:@voidzero-dev/vite-plus-core@1.2.0",
39
+ "vite-plus": "1.2.0"
39
40
  },
40
41
  "engines": {
41
42
  "node": ">=22.18.0"