@dogsbay/docs-layout 0.2.0-beta.151 → 0.2.0-beta.153

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.
@@ -1,67 +1,16 @@
1
1
  /**
2
- * Pure helpers for search-facet UI in `<SearchDialog>`. Splitting
3
- * the data transforms here lets vitest exercise them without
4
- * needing a Pagefind runtime or rendered DOM.
2
+ * Search-facet helpers for `<SearchDialog>`. The pure helpers (shaping,
3
+ * labels, order, trees, URL state) live in `@dogsbay/search/facets`,
4
+ * shared with dogsbay-ssg; only the Pagefind mapping is here.
5
5
  *
6
6
  * See plans/search-facets.md.
7
7
  */
8
- import { resolveTermLabel } from "./tag-list-data.js";
9
- import type { PrefixDisplay } from "./tag-list-data.js";
10
-
11
- /**
12
- * Per-taxonomy display config — same shape as
13
- * `SiteConfig.taxonomyDisplay[name]`. Keyed locally so this module
14
- * has no runtime dependency on `@dogsbay/types`.
15
- */
16
- export interface TaxonomyDisplay {
17
- prefixes?: Record<string, PrefixDisplay>;
18
- labels?: Record<string, string>;
19
- /**
20
- * Sort weight for the facet column in the search dialog. Lower
21
- * numbers appear first. Facets without an `order` sort
22
- * alphabetically *after* any pinned ones — so `{ type: { order: 1 } }`
23
- * promotes "Type" to the top while everything else stays alpha.
24
- */
25
- order?: number;
26
- /**
27
- * Render this facet as a tree instead of a flat checkbox list.
28
- * See `buildFacetTree` for the two derivation strategies (slash-
29
- * encoded vs nav-driven segment-encoded) and the canonical
30
- * @dogsbay/types definition for full semantics.
31
- */
32
- hierarchical?: boolean;
33
- }
34
-
35
- /**
36
- * Minimal nav item shape — mirrors `@dogsbay/types`' `NavItem`
37
- * locally so this module has no runtime dependency on the types
38
- * package. Used as input to `buildFacetTree` for segment-encoded
39
- * facets (the auto-`category` case), where the document tree is
40
- * the source of structure and order.
41
- */
42
- export interface NavLike {
43
- label: string;
44
- href?: string;
45
- children?: NavLike[];
46
- }
47
-
48
- /** Map of taxonomy name → display config. */
49
- export type TaxonomyDisplayMap = Record<string, TaxonomyDisplay>;
50
-
51
- /** One facet entry — value plus the count of pages tagged with it. */
52
- export interface FacetEntry {
53
- /** The slug as Pagefind indexed it (e.g. `"concept/a11y"`, `"how-to"`). */
54
- value: string;
55
- /** Page count for this value within the current corpus. */
56
- count: number;
57
- }
58
-
59
- /** Map of facet name (taxonomy) → ordered entries. */
60
- export type FacetMap = Record<string, FacetEntry[]>;
8
+ export * from "@dogsbay/search/facets";
9
+ import type { FilterState } from "@dogsbay/search/facets";
61
10
 
62
11
  /**
63
12
  * Pagefind's filters API surface — a thin subset of the runtime
64
- * shape, kept local so this module stays dependency-free.
13
+ * shape, kept local so this module needs no Pagefind types.
65
14
  *
66
15
  * `pagefind.filters()` resolves to a nested map:
67
16
  * `{ <facetName>: { <value>: <count> } }`. We sort + flatten it.
@@ -70,171 +19,6 @@ export interface PagefindLike {
70
19
  filters(): Promise<Record<string, Record<string, number>>>;
71
20
  }
72
21
 
73
- /**
74
- * Convert Pagefind's nested filter map into a sorted `FacetMap`.
75
- *
76
- * Sort order: count descending, then value ascending alphabetically
77
- * (stable tie-break). Empty value lists are dropped — a facet with
78
- * zero values isn't worth a row in the UI. Returns an empty
79
- * `FacetMap` when Pagefind reports no filters at all (Pagefind log
80
- * still says "Indexed 0 filters" — usually means the head→body fix
81
- * hasn't propagated, not a real "no-tags" site).
82
- */
83
- export function shapeFacets(
84
- raw: Record<string, Record<string, number>> | undefined | null,
85
- ): FacetMap {
86
- if (!raw) return {};
87
- const out: FacetMap = {};
88
- for (const [name, valueMap] of Object.entries(raw)) {
89
- const entries = sortFacetEntries(
90
- Object.entries(valueMap).map(([value, count]) => ({ value, count })),
91
- );
92
- if (entries.length > 0) out[name] = entries;
93
- }
94
- return out;
95
- }
96
-
97
- /**
98
- * Sort facet entries by count descending, then by value
99
- * ascending alphabetically. Stable for equal-count values so
100
- * the UI doesn't shuffle between renders.
101
- */
102
- export function sortFacetEntries(entries: FacetEntry[]): FacetEntry[] {
103
- return [...entries].sort((a, b) => {
104
- if (a.count !== b.count) return b.count - a.count;
105
- return a.value.localeCompare(b.value);
106
- });
107
- }
108
-
109
- /**
110
- * Resolve the human display label for one facet value.
111
- *
112
- * Maps the slug-form value to the configured display string by
113
- * looking up the per-taxonomy display config. For slash-nested
114
- * values (`concept/a11y`), the resolution rules match
115
- * `<TagList>` chip rendering: explicit label override wins, then
116
- * prefix label for top-level segments, then leaf segment as a
117
- * final fallback.
118
- *
119
- * Slug paths are split into segments via `/`, mirroring how
120
- * taxonomies indexes terms.
121
- */
122
- export function resolveFacetLabel(
123
- facetName: string,
124
- value: string,
125
- display?: TaxonomyDisplayMap,
126
- ): string {
127
- const taxonomy = display?.[facetName];
128
- const fullPath = value.split("/").filter((s) => s.length > 0);
129
- return resolveTermLabel(fullPath, taxonomy?.prefixes, taxonomy?.labels);
130
- }
131
-
132
- /**
133
- * Resolve the display label for a facet name itself (the column
134
- * header text). Looks up `prefixes[facetName].label` for built-in
135
- * taxonomies (`tags` → "Tags"), falling back to a humanized
136
- * version of the facet name (`by-audience` → "By audience").
137
- *
138
- * Per-taxonomy `prefixes` aren't a perfect fit here (they're keyed
139
- * by the segment, not by the taxonomy itself), so this is a
140
- * separate humanizer. Future schema work can add an explicit
141
- * `taxonomies.<name>.displayName` field if writers want full
142
- * control.
143
- */
144
- export function resolveFacetTitle(facetName: string): string {
145
- return facetName
146
- .replace(/[-_]/g, " ")
147
- .replace(/\b\w/g, (c) => c.toUpperCase());
148
- }
149
-
150
- /**
151
- * Return facet names in sidebar render order.
152
- *
153
- * Pagefind's `filters()` map iterates in index-discovery order
154
- * (effectively the first page Pagefind happened to read), so without
155
- * sorting the column order is arbitrary and shifts between builds.
156
- * Order rule:
157
- * 1. Facets with a numeric `order` in their `TaxonomyDisplay`
158
- * come first, ascending — use this to pin "Type" or "Audience"
159
- * to the top regardless of name.
160
- * 2. Everything else sorts alphabetically by facet name.
161
- * Stable within each tier so ties don't shuffle.
162
- */
163
- export function sortFacetNames(
164
- names: string[],
165
- display?: TaxonomyDisplayMap,
166
- ): string[] {
167
- const withOrder = (name: string): number | undefined => {
168
- const o = display?.[name]?.order;
169
- return typeof o === "number" ? o : undefined;
170
- };
171
- return [...names].sort((a, b) => {
172
- const oa = withOrder(a);
173
- const ob = withOrder(b);
174
- if (oa !== undefined && ob !== undefined) {
175
- if (oa !== ob) return oa - ob;
176
- return a.localeCompare(b);
177
- }
178
- if (oa !== undefined) return -1;
179
- if (ob !== undefined) return 1;
180
- return a.localeCompare(b);
181
- });
182
- }
183
-
184
- // ── URL persistence ──────────────────────────────────────────────
185
-
186
- /**
187
- * Active filter state — keyed by facet name, value is the set of
188
- * selected slugs (multi-select per facet, AND across facets).
189
- */
190
- export type FilterState = Record<string, string[]>;
191
-
192
- /**
193
- * Build URLSearchParams from the current filter state. Each
194
- * facet:value combination produces one `filter=<name>:<value>`
195
- * entry, repeated per selection. The `q` query param is added
196
- * only when query is non-empty.
197
- *
198
- * Resulting URL is shareable — re-loading restores both the
199
- * search query AND active filters.
200
- */
201
- export function filterStateToUrlParams(
202
- query: string,
203
- filters: FilterState,
204
- ): URLSearchParams {
205
- const params = new URLSearchParams();
206
- if (query.trim().length > 0) params.set("q", query.trim());
207
- for (const [name, values] of Object.entries(filters)) {
208
- for (const value of values) {
209
- params.append("filter", `${name}:${value}`);
210
- }
211
- }
212
- return params;
213
- }
214
-
215
- /**
216
- * Parse `URLSearchParams` back into `{ query, filters }`. The
217
- * inverse of `filterStateToUrlParams`. Tolerant of malformed
218
- * `filter=...` entries — anything missing the `:` separator is
219
- * silently dropped (forward-compat with future schema).
220
- */
221
- export function parseFiltersFromUrl(
222
- params: URLSearchParams,
223
- ): { query: string; filters: FilterState } {
224
- const query = params.get("q") ?? "";
225
- const filters: FilterState = {};
226
- for (const raw of params.getAll("filter")) {
227
- const colonIdx = raw.indexOf(":");
228
- if (colonIdx < 1) continue; // need at least 1 char before the colon
229
- const name = raw.slice(0, colonIdx);
230
- const value = raw.slice(colonIdx + 1);
231
- if (!value) continue;
232
- if (!filters[name]) filters[name] = [];
233
- if (!filters[name].includes(value)) filters[name].push(value);
234
- }
235
- return { query, filters };
236
- }
237
-
238
22
  /**
239
23
  * Build Pagefind's `filters` argument from internal facet state.
240
24
  *
@@ -265,470 +49,3 @@ export function filterStateToPagefindFilters(
265
49
  }
266
50
  return out;
267
51
  }
268
-
269
- /**
270
- * Toggle a single (facetName, value) pair in the filter state.
271
- * Returns a new state object — the input is never mutated, so
272
- * callers can safely use this with React-style "set state to a
273
- * new value" patterns or vanilla state diffing.
274
- */
275
- export function toggleFilter(
276
- filters: FilterState,
277
- name: string,
278
- value: string,
279
- ): FilterState {
280
- const next: FilterState = { ...filters };
281
- const current = next[name] ?? [];
282
- if (current.includes(value)) {
283
- const dropped = current.filter((v) => v !== value);
284
- if (dropped.length === 0) {
285
- delete next[name];
286
- } else {
287
- next[name] = dropped;
288
- }
289
- } else {
290
- next[name] = [...current, value];
291
- }
292
- return next;
293
- }
294
-
295
- /**
296
- * Count the total number of active filter selections across all
297
- * facets. Used to render a "Clear all" button only when there's
298
- * something to clear.
299
- */
300
- export function countActiveFilters(filters: FilterState): number {
301
- let n = 0;
302
- for (const values of Object.values(filters)) n += values.length;
303
- return n;
304
- }
305
-
306
- // ── Hierarchical facets ──────────────────────────────────────────
307
-
308
- /**
309
- * One node in a hierarchical facet tree (see `buildFacetTree`).
310
- *
311
- * `value` is the canonical Pagefind filter value for this node —
312
- * for slash-encoded facets the full slash-joined path
313
- * (`concept/a11y`); for segment-encoded facets a single segment
314
- * (`installing_aws`). `value` matches Pagefind's value key 1:1
315
- * for "real" entries; synthetic parents (intermediate trie nodes
316
- * that don't correspond to a Pagefind entry) reuse the same
317
- * derived value but carry `hasValue: false` and `count: 0`.
318
- */
319
- export interface FacetTreeNode {
320
- value: string;
321
- label: string;
322
- count: number;
323
- depth: number;
324
- /** True if Pagefind reported this exact value; false for synthetic parents. */
325
- hasValue: boolean;
326
- children: FacetTreeNode[];
327
- }
328
-
329
- /**
330
- * Build a hierarchical facet tree from Pagefind's flat entry list.
331
- *
332
- * Two strategies depending on the values' shape:
333
- *
334
- * - **Slash-encoded.** When at least one entry value contains `/`,
335
- * each value is treated as a path through the tree (`concept/a11y`
336
- * → `concept → a11y`). Splits on `/`, inserts into a trie.
337
- * Parent entries that don't have a Pagefind value of their own
338
- * (e.g. `concept` standalone is unindexed but `concept/a11y` is)
339
- * appear as synthetic intermediate nodes with `hasValue: false`.
340
- * Within a parent, children sort by count desc then alpha — the
341
- * same rule as flat facets.
342
- *
343
- * - **Segment-encoded.** When NO entry contains `/`, each value is
344
- * a single path segment. Hierarchy comes from `nav` — every nav
345
- * leaf's href segments become a chain in the trie. Counts come
346
- * from Pagefind's entry list, attached to nodes whose segment
347
- * name matches an entry value. **Caveat:** Pagefind reports one
348
- * count per distinct segment name regardless of where in the
349
- * tree it lives, so a segment that appears at multiple positions
350
- * (`installing/.../ipi`, `installing_azure/.../ipi`) gets the
351
- * same total count at every position — clicking selects the
352
- * shared value, returning pages from every position. This
353
- * matches the underlying Pagefind filter behaviour and is
354
- * documented in plans/hierarchical-facets.md.
355
- *
356
- * Order is nav-document order, NOT count desc. Pagefind entries
357
- * not represented in nav are appended as orphans at the root in
358
- * alpha order.
359
- *
360
- * When `nav` is undefined for the segment-encoded case, falls
361
- * back to a flat list (every entry a root-level node with no
362
- * children, in count-desc order) — preserves render-ability
363
- * when nav.json hasn't loaded yet.
364
- *
365
- * Returns the top-level nodes. Empty `entries` → empty array.
366
- */
367
- export function buildFacetTree(
368
- facetName: string,
369
- entries: FacetEntry[],
370
- options: {
371
- nav?: NavLike[];
372
- display?: TaxonomyDisplayMap;
373
- } = {},
374
- ): FacetTreeNode[] {
375
- if (entries.length === 0) return [];
376
-
377
- const slashEncoded = entries.some((e) => e.value.includes("/"));
378
- if (slashEncoded) {
379
- // When nav is supplied, derive document-order. Otherwise fall
380
- // back to count-desc + alpha. The map keys are full cumulative
381
- // paths (matching Pagefind entry values 1:1).
382
- const navOrder = options.nav
383
- ? buildNavOrderMap(
384
- options.nav,
385
- new Set(entries.map((e) => e.value)),
386
- )
387
- : undefined;
388
- return buildSlashTree(facetName, entries, options.display, navOrder);
389
- }
390
- return buildSegmentTree(facetName, entries, options.nav, options.display);
391
- }
392
-
393
- /**
394
- * Slash-encoded tree builder. Each entry value is a slash-joined
395
- * path; split, walk a trie, attach Pagefind counts to terminal
396
- * nodes. Synthetic intermediate nodes (a path component used by a
397
- * descendant but absent from `entries`) get `hasValue: false` and
398
- * `count: 0` so they render but don't contribute to selection.
399
- *
400
- * Sort order at every depth:
401
- * 1. `navOrder` index if present (document order from nav.json).
402
- * Without this, deep API-reference subtrees with high page
403
- * counts would dominate the top of the tree even though they
404
- * sit at the BOTTOM of the rendered nav.
405
- * 2. Count desc (more pages → higher).
406
- * 3. Alpha (final tiebreak).
407
- */
408
- function buildSlashTree(
409
- facetName: string,
410
- entries: FacetEntry[],
411
- display?: TaxonomyDisplayMap,
412
- navOrder?: Map<string, number>,
413
- ): FacetTreeNode[] {
414
- // Trie keyed by full slash-path. Children are tracked on each
415
- // node's own `children` array (built unsorted in pass 1, sorted
416
- // recursively in pass 2).
417
- const byPath = new Map<string, FacetTreeNode>();
418
- const rootValues: string[] = [];
419
-
420
- for (const entry of entries) {
421
- const segments = entry.value.split("/").filter((s) => s.length > 0);
422
- if (segments.length === 0) continue;
423
- let pathSoFar = "";
424
- let parent: FacetTreeNode | null = null;
425
- for (let i = 0; i < segments.length; i++) {
426
- pathSoFar = pathSoFar ? `${pathSoFar}/${segments[i]}` : segments[i];
427
- let node = byPath.get(pathSoFar);
428
- if (!node) {
429
- node = {
430
- value: pathSoFar,
431
- label: resolveFacetLabel(facetName, pathSoFar, display),
432
- count: 0,
433
- depth: i,
434
- hasValue: false,
435
- children: [],
436
- };
437
- byPath.set(pathSoFar, node);
438
- if (parent) parent.children.push(node);
439
- else rootValues.push(pathSoFar);
440
- }
441
- if (i === segments.length - 1) {
442
- node.count = entry.count;
443
- node.hasValue = true;
444
- }
445
- parent = node;
446
- }
447
- }
448
-
449
- // Pass 2: sort children at every depth.
450
- const cmp = (a: FacetTreeNode, b: FacetTreeNode): number => {
451
- if (navOrder) {
452
- const oa = navOrder.get(a.value);
453
- const ob = navOrder.get(b.value);
454
- if (oa !== undefined && ob !== undefined && oa !== ob) return oa - ob;
455
- // Nodes IN the nav sort before nodes not in nav
456
- if (oa !== undefined && ob === undefined) return -1;
457
- if (oa === undefined && ob !== undefined) return 1;
458
- }
459
- if (a.count !== b.count) return b.count - a.count;
460
- return a.value.localeCompare(b.value);
461
- };
462
- const sortChildren = (node: FacetTreeNode): void => {
463
- if (node.children.length === 0) return;
464
- node.children.sort(cmp);
465
- for (const c of node.children) sortChildren(c);
466
- };
467
-
468
- const rootList = rootValues.map((v) => byPath.get(v)!);
469
- rootList.sort(cmp);
470
- for (const r of rootList) sortChildren(r);
471
- return rootList;
472
- }
473
-
474
- /**
475
- * Walk `nav` and assign each cumulative slash-path (matching a
476
- * Pagefind facet value in `knownValues`) a sequential index in
477
- * first-encounter order. Used by `buildSlashTree` to sort children
478
- * in document order rather than count-desc.
479
- *
480
- * Auto-detects and strips a leading basePath: for each nav leaf, we
481
- * try each starting offset of the href's parent segments. The first
482
- * offset whose initial cumulative path is in `knownValues` is the
483
- * "real" content root; the dropped prefix is the basePath. This way
484
- * the helper works on any site without needing the basePath threaded
485
- * through explicitly.
486
- *
487
- * Hrefs whose parents don't match anything in `knownValues` at any
488
- * offset are skipped (e.g. top-level pages with no auto-derived
489
- * category, or pages outside the indexed corpus).
490
- *
491
- * Exported for use by `buildFacetTree`. Pure — no I/O.
492
- */
493
- export function buildNavOrderMap(
494
- nav: NavLike[],
495
- knownValues: Set<string>,
496
- ): Map<string, number> {
497
- const order = new Map<string, number>();
498
- let n = 0;
499
- const walk = (items: NavLike[]): void => {
500
- for (const item of items) {
501
- if (item.href) {
502
- const parents = hrefToCategorySegments(item.href);
503
- // Try each starting offset; first one whose initial cumulative
504
- // path is in knownValues wins (auto-detects basePath).
505
- for (let start = 0; start < parents.length; start++) {
506
- const first = parents[start];
507
- if (!knownValues.has(first)) continue;
508
- // Record all cumulative paths starting from `start` that
509
- // exist in knownValues. The check at line below filters out
510
- // synthetic intermediates the corpus didn't tag (e.g. a
511
- // user-supplied frontmatter category that skipped a level).
512
- let cumulative = "";
513
- for (let i = start; i < parents.length; i++) {
514
- cumulative = cumulative ? `${cumulative}/${parents[i]}` : parents[i];
515
- if (knownValues.has(cumulative) && !order.has(cumulative)) {
516
- order.set(cumulative, n++);
517
- }
518
- }
519
- break;
520
- }
521
- }
522
- if (item.children && item.children.length > 0) walk(item.children);
523
- }
524
- };
525
- walk(nav);
526
- return order;
527
- }
528
-
529
- /**
530
- * Segment-encoded tree builder. Walks `nav` to discover the
531
- * canonical document hierarchy, then attaches Pagefind counts to
532
- * any node whose segment name matches an entry. Pagefind entries
533
- * not in nav are appended as alpha-sorted root orphans so they
534
- * don't disappear.
535
- */
536
- function buildSegmentTree(
537
- facetName: string,
538
- entries: FacetEntry[],
539
- nav: NavLike[] | undefined,
540
- display?: TaxonomyDisplayMap,
541
- ): FacetTreeNode[] {
542
- const byValue = new Map<string, FacetEntry>();
543
- for (const e of entries) byValue.set(e.value, e);
544
-
545
- if (!nav || nav.length === 0) {
546
- // Flat fallback in count-desc order — preserves render-ability
547
- // when nav hasn't loaded.
548
- return entries.map((e) => ({
549
- value: e.value,
550
- label: resolveFacetLabel(facetName, e.value, display),
551
- count: e.count,
552
- depth: 0,
553
- hasValue: true,
554
- children: [],
555
- }));
556
- }
557
-
558
- const seenValues = new Set<string>();
559
- const roots: FacetTreeNode[] = [];
560
- const rootIndex = new Map<string, FacetTreeNode>();
561
-
562
- /** Insert a path's parent segments (excluding the leaf filename) into the trie. */
563
- const insertPath = (segments: string[]): void => {
564
- if (segments.length === 0) return;
565
- let parentList: FacetTreeNode[] = roots;
566
- let parentIndex: Map<string, FacetTreeNode> = rootIndex;
567
- for (let i = 0; i < segments.length; i++) {
568
- const seg = segments[i];
569
- let node = parentIndex.get(seg);
570
- if (!node) {
571
- const entry = byValue.get(seg);
572
- node = {
573
- value: seg,
574
- label: resolveFacetLabel(facetName, seg, display),
575
- count: entry ? entry.count : 0,
576
- depth: i,
577
- hasValue: entry !== undefined,
578
- children: [],
579
- };
580
- parentList.push(node);
581
- parentIndex.set(seg, node);
582
- if (entry) seenValues.add(seg);
583
- }
584
- // Walk into the child list for the next iteration. Each
585
- // node carries its own child index lazily via a WeakMap-like
586
- // closure; here we use a regular Map keyed off the node.
587
- let nextIndex = childIndex.get(node);
588
- if (!nextIndex) {
589
- nextIndex = new Map();
590
- childIndex.set(node, nextIndex);
591
- }
592
- parentList = node.children;
593
- parentIndex = nextIndex;
594
- }
595
- };
596
-
597
- const childIndex = new Map<FacetTreeNode, Map<string, FacetTreeNode>>();
598
-
599
- // Walk every nav leaf. Use href segments (excluding the leaf
600
- // filename) — those are the auto-derived `category` values
601
- // emitted by parseMeta's deriveCategoryFromSlug.
602
- const walkNav = (items: NavLike[]): void => {
603
- for (const item of items) {
604
- if (item.href) {
605
- const parents = hrefToCategorySegments(item.href);
606
- insertPath(parents);
607
- }
608
- if (item.children && item.children.length > 0) walkNav(item.children);
609
- }
610
- };
611
- walkNav(nav);
612
-
613
- // Pagefind values not represented in nav → root-level orphans in
614
- // alpha order. Without this they vanish from the UI silently.
615
- const orphans: FacetTreeNode[] = [];
616
- for (const e of entries) {
617
- if (seenValues.has(e.value)) continue;
618
- orphans.push({
619
- value: e.value,
620
- label: resolveFacetLabel(facetName, e.value, display),
621
- count: e.count,
622
- depth: 0,
623
- hasValue: true,
624
- children: [],
625
- });
626
- }
627
- orphans.sort((a, b) => a.value.localeCompare(b.value));
628
- return [...roots, ...orphans];
629
- }
630
-
631
- /**
632
- * Strip an href down to its parent path segments — the `category`
633
- * derivation logic from `parseMeta.deriveCategoryFromSlug`. Drops
634
- * leading/trailing slashes, drops the leaf filename segment.
635
- */
636
- function hrefToCategorySegments(href: string): string[] {
637
- // Drop scheme/host if absolute (we only treat path segments)
638
- let path = href;
639
- try {
640
- if (/^[a-z]+:\/\//i.test(href)) path = new URL(href).pathname;
641
- } catch {
642
- // not a URL — treat as-is
643
- }
644
- // Drop hash + query — facets care about the URL path only
645
- path = path.split("#")[0].split("?")[0];
646
- const segments = path.split("/").filter((s) => s.length > 0);
647
- // Match deriveCategoryFromSlug: parents = everything except the leaf
648
- if (segments.length < 2) return [];
649
- return segments.slice(0, -1);
650
- }
651
-
652
- /**
653
- * Flat list of every "real" Pagefind value at or under `node`
654
- * (i.e. every node where `hasValue === true`). Synthetic parents
655
- * are excluded since adding them to the filter would be a no-op
656
- * (Pagefind has no pages tagged with the synthetic value).
657
- *
658
- * Used by `toggleTreeNode` when the user ticks a parent — we add
659
- * every real descendant value to the selection in one shot.
660
- */
661
- export function collectDescendantValues(node: FacetTreeNode): string[] {
662
- const out: string[] = [];
663
- const visit = (n: FacetTreeNode): void => {
664
- if (n.hasValue) out.push(n.value);
665
- for (const c of n.children) visit(c);
666
- };
667
- visit(node);
668
- return out;
669
- }
670
-
671
- /**
672
- * Tri-state of a tree node given the current filter state.
673
- *
674
- * - `"checked"` — every real descendant value (and self if
675
- * self has a value) is in `filters[facetName]`.
676
- * - `"unchecked"` — none of them are.
677
- * - `"indeterminate"`— at least one is, but not all.
678
- *
679
- * Synthetic parents with no real-value descendants resolve to
680
- * "unchecked" (defensive default — shouldn't occur in valid trees).
681
- */
682
- export function computeTreeState(
683
- node: FacetTreeNode,
684
- facetName: string,
685
- filters: FilterState,
686
- ): "checked" | "unchecked" | "indeterminate" {
687
- const descendants = collectDescendantValues(node);
688
- if (descendants.length === 0) return "unchecked";
689
- const selected = new Set(filters[facetName] ?? []);
690
- let hits = 0;
691
- for (const v of descendants) if (selected.has(v)) hits++;
692
- if (hits === 0) return "unchecked";
693
- if (hits === descendants.length) return "checked";
694
- return "indeterminate";
695
- }
696
-
697
- /**
698
- * Toggle a tree node's selection.
699
- *
700
- * - If currently checked → remove self + all descendant values
701
- * from `filters[facetName]`.
702
- * - If unchecked or indeterminate → add self + all descendant
703
- * values (auto-select). Matches the decided UX: parent click
704
- * fans out to children.
705
- *
706
- * Returns a new `FilterState` — input is not mutated. Same
707
- * immutability contract as `toggleFilter`.
708
- */
709
- export function toggleTreeNode(
710
- filters: FilterState,
711
- facetName: string,
712
- node: FacetTreeNode,
713
- currentState: "checked" | "unchecked" | "indeterminate",
714
- ): FilterState {
715
- const descendants = collectDescendantValues(node);
716
- if (descendants.length === 0) return filters;
717
- const current = filters[facetName] ?? [];
718
- let nextValues: string[];
719
- if (currentState === "checked") {
720
- const drop = new Set(descendants);
721
- nextValues = current.filter((v) => !drop.has(v));
722
- } else {
723
- const merged = new Set(current);
724
- for (const v of descendants) merged.add(v);
725
- nextValues = Array.from(merged);
726
- }
727
- const next: FilterState = { ...filters };
728
- if (nextValues.length === 0) {
729
- delete next[facetName];
730
- } else {
731
- next[facetName] = nextValues;
732
- }
733
- return next;
734
- }