@dogsbay/site-files 0.2.0-beta.152 → 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.
- package/dist/facets.d.ts +47 -0
- package/dist/facets.js +82 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
package/dist/facets.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A page's search facets: facet name → values, from its metadata. ONE mapping for both pipelines:
|
|
3
|
+
* DocsLayout writes each pair as a Pagefind filter (`<div data-pagefind-filter="name:value">`),
|
|
4
|
+
* dogsbay-ssg puts them in its search index's `SearchDoc.facets`. Before this they disagreed on
|
|
5
|
+
* tags and the built-in fields, so the same site faceted differently in the two (asked by
|
|
6
|
+
* dogsbay-ssg, 2026-10-31; plans/page-facets.md).
|
|
7
|
+
*
|
|
8
|
+
* The names are DocsLayout's, because readers see them in filter addresses (`?filter=topic:ai`):
|
|
9
|
+
* - `tags`: a tag whose prefix is declared (`tagPrefixes`) is a facet of its own (`topic/ai` →
|
|
10
|
+
* `topic: ai`); every other tag pools under `tag`.
|
|
11
|
+
* - `audience`, `category`, `type`, `status`: always, declared or not.
|
|
12
|
+
* - every other declared taxonomy under its own name (`meta.taxonomies`). A declared `type` or
|
|
13
|
+
* `status` taxonomy joins the facet of that name, without duplicates (`parseMeta` keeps only a
|
|
14
|
+
* known status and a single string type in the typed fields, so the taxonomy can hold more).
|
|
15
|
+
* - `product` and `version` from the page's unit, when the site has those axes.
|
|
16
|
+
*
|
|
17
|
+
* In a taxonomy named in `hierarchical`, a value also lists every level above it (`ai/llm` →
|
|
18
|
+
* `ai`, `ai/llm`), so filtering on a parent finds its children, as the taxonomy's term pages count
|
|
19
|
+
* them; for tags the levels are counted below the prefix. A value with an empty, `.` or `..`
|
|
20
|
+
* segment, a backslash or a NUL is left out (a term becomes a folder of the term pages).
|
|
21
|
+
*
|
|
22
|
+
* Pure: no Node, no dependencies. A host that can only query some names (the SSG's `FACET_NAME`)
|
|
23
|
+
* drops the others itself.
|
|
24
|
+
*/
|
|
25
|
+
/** The metadata a page's facets come from: `parseMeta`'s result (@dogsbay/types `PageMeta`). */
|
|
26
|
+
export interface PageFacetMeta {
|
|
27
|
+
tags?: readonly string[];
|
|
28
|
+
category?: readonly string[];
|
|
29
|
+
audience?: readonly string[];
|
|
30
|
+
type?: string;
|
|
31
|
+
status?: string;
|
|
32
|
+
/** Declared custom taxonomies, as `parseMeta` lifts them. */
|
|
33
|
+
taxonomies?: Readonly<Record<string, readonly string[]>>;
|
|
34
|
+
}
|
|
35
|
+
export interface PageFacetOptions {
|
|
36
|
+
/** Declared tag prefixes (`taxonomies.tags.prefixes`): the keys are what counts. */
|
|
37
|
+
tagPrefixes?: Readonly<Record<string, unknown>>;
|
|
38
|
+
/** Taxonomies whose nested values list every level (`hierarchical: true`; `tags` for tags). */
|
|
39
|
+
hierarchical?: readonly string[];
|
|
40
|
+
/** The page's product and version, when the site has those axes. */
|
|
41
|
+
unit?: {
|
|
42
|
+
product?: string;
|
|
43
|
+
version?: string;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export type PageFacets = Record<string, string[]>;
|
|
47
|
+
export declare function pageFacets(meta: PageFacetMeta | undefined, options?: PageFacetOptions): PageFacets;
|
package/dist/facets.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A page's search facets: facet name → values, from its metadata. ONE mapping for both pipelines:
|
|
3
|
+
* DocsLayout writes each pair as a Pagefind filter (`<div data-pagefind-filter="name:value">`),
|
|
4
|
+
* dogsbay-ssg puts them in its search index's `SearchDoc.facets`. Before this they disagreed on
|
|
5
|
+
* tags and the built-in fields, so the same site faceted differently in the two (asked by
|
|
6
|
+
* dogsbay-ssg, 2026-10-31; plans/page-facets.md).
|
|
7
|
+
*
|
|
8
|
+
* The names are DocsLayout's, because readers see them in filter addresses (`?filter=topic:ai`):
|
|
9
|
+
* - `tags`: a tag whose prefix is declared (`tagPrefixes`) is a facet of its own (`topic/ai` →
|
|
10
|
+
* `topic: ai`); every other tag pools under `tag`.
|
|
11
|
+
* - `audience`, `category`, `type`, `status`: always, declared or not.
|
|
12
|
+
* - every other declared taxonomy under its own name (`meta.taxonomies`). A declared `type` or
|
|
13
|
+
* `status` taxonomy joins the facet of that name, without duplicates (`parseMeta` keeps only a
|
|
14
|
+
* known status and a single string type in the typed fields, so the taxonomy can hold more).
|
|
15
|
+
* - `product` and `version` from the page's unit, when the site has those axes.
|
|
16
|
+
*
|
|
17
|
+
* In a taxonomy named in `hierarchical`, a value also lists every level above it (`ai/llm` →
|
|
18
|
+
* `ai`, `ai/llm`), so filtering on a parent finds its children, as the taxonomy's term pages count
|
|
19
|
+
* them; for tags the levels are counted below the prefix. A value with an empty, `.` or `..`
|
|
20
|
+
* segment, a backslash or a NUL is left out (a term becomes a folder of the term pages).
|
|
21
|
+
*
|
|
22
|
+
* Pure: no Node, no dependencies. A host that can only query some names (the SSG's `FACET_NAME`)
|
|
23
|
+
* drops the others itself.
|
|
24
|
+
*/
|
|
25
|
+
/** `parseMeta` leaves these out of `meta.taxonomies`; `type` and `status` it keeps there too. */
|
|
26
|
+
const BUILT_IN = new Set(["tags", "category", "audience", "type", "status"]);
|
|
27
|
+
function segments(value) {
|
|
28
|
+
if (/[\\\0]/.test(value))
|
|
29
|
+
return undefined;
|
|
30
|
+
const segs = value.split("/");
|
|
31
|
+
return segs.every((s) => s !== "" && s !== "." && s !== "..") ? segs : undefined;
|
|
32
|
+
}
|
|
33
|
+
export function pageFacets(meta, options = {}) {
|
|
34
|
+
// No prototype: a facet named `constructor` or `toString` must not find an inherited function.
|
|
35
|
+
const out = Object.create(null);
|
|
36
|
+
const nested = new Set(options.hierarchical ?? []);
|
|
37
|
+
const add = (name, value, hierarchical) => {
|
|
38
|
+
if (typeof value !== "string" || !value)
|
|
39
|
+
return;
|
|
40
|
+
const segs = segments(value);
|
|
41
|
+
if (!segs)
|
|
42
|
+
return;
|
|
43
|
+
const values = (out[name] ??= []);
|
|
44
|
+
const levels = hierarchical ? segs.map((_, i) => segs.slice(0, i + 1).join("/")) : [segs.join("/")];
|
|
45
|
+
for (const v of levels)
|
|
46
|
+
if (!values.includes(v))
|
|
47
|
+
values.push(v);
|
|
48
|
+
};
|
|
49
|
+
const list = (v) => (Array.isArray(v) ? v : []);
|
|
50
|
+
const tagsNested = nested.has("tags");
|
|
51
|
+
for (const tag of list(meta?.tags)) {
|
|
52
|
+
if (typeof tag !== "string")
|
|
53
|
+
continue;
|
|
54
|
+
const slash = tag.indexOf("/");
|
|
55
|
+
const prefix = slash > 0 ? tag.slice(0, slash) : "";
|
|
56
|
+
if (prefix && options.tagPrefixes && Object.prototype.hasOwnProperty.call(options.tagPrefixes, prefix) && options.tagPrefixes[prefix]) {
|
|
57
|
+
add(prefix, tag.slice(slash + 1), tagsNested);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
add("tag", tag, tagsNested);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const v of list(meta?.audience))
|
|
64
|
+
add("audience", v, nested.has("audience"));
|
|
65
|
+
for (const v of list(meta?.category))
|
|
66
|
+
add("category", v, nested.has("category"));
|
|
67
|
+
add("status", meta?.status, nested.has("status"));
|
|
68
|
+
for (const v of list(meta?.taxonomies?.status))
|
|
69
|
+
add("status", v, nested.has("status"));
|
|
70
|
+
add("type", meta?.type, nested.has("type"));
|
|
71
|
+
for (const v of list(meta?.taxonomies?.type))
|
|
72
|
+
add("type", v, nested.has("type"));
|
|
73
|
+
add("product", options.unit?.product, false);
|
|
74
|
+
add("version", options.unit?.version, false);
|
|
75
|
+
for (const [name, values] of Object.entries(meta?.taxonomies ?? {})) {
|
|
76
|
+
if (BUILT_IN.has(name))
|
|
77
|
+
continue;
|
|
78
|
+
for (const v of list(values))
|
|
79
|
+
add(name, v, nested.has(name));
|
|
80
|
+
}
|
|
81
|
+
return { ...out };
|
|
82
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dogsbay/site-files",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.153",
|
|
4
4
|
"description": "robots.txt, _headers, llms.txt, sitemaps, redirects and JSON-LD for docs sites; shared by the dogsbay Astro pipeline and dogsbay-ssg, Worker-safe",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|