@dogsbay/site-files 0.2.0-beta.172 → 0.2.0-beta.174

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/index.d.ts CHANGED
@@ -25,3 +25,4 @@ export * from "./site-chrome.js";
25
25
  export * from "./facets.js";
26
26
  export * from "./blog-order.js";
27
27
  export * from "./nav-sections.js";
28
+ export * from "./unlisted.js";
package/dist/index.js CHANGED
@@ -25,3 +25,4 @@ export * from "./site-chrome.js";
25
25
  export * from "./facets.js";
26
26
  export * from "./blog-order.js";
27
27
  export * from "./nav-sections.js";
28
+ export * from "./unlisted.js";
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Unlisted pages (plans/unlisted-pages.md): built and reachable by their URL, listed nowhere —
3
+ * not in the menu, previous / next, `::child-links`, the book view, `llms.txt`, search, the
4
+ * sitemap, the blog index, its feed or the tag pages, and `noindex`. It protects nothing: anyone
5
+ * with the URL reads the page.
6
+ *
7
+ * Two ways to say it, joined here so both pipelines (the Astro build and dogsbay-ssg) agree:
8
+ *
9
+ * - **A nav entry** `unlisted: true` (nav.yml's annotated form): that entry AND everything under
10
+ * it (a GitBook hidden space, a hidden SUMMARY group). `unlisted: false` below it does not list
11
+ * an entry again (v1: the flag only adds). The PAGE is unlisted when every entry linking it is;
12
+ * one listed entry keeps it listed (only the unlisted entries go), and an entry to a part of a
13
+ * page (`#fragment`) never unlists the page.
14
+ * - **A page's front matter** `unlisted: true`: that page only. Its entries lose their link; a
15
+ * group whose own page it is keeps its label and its listed children, as draft pruning keeps a
16
+ * group's label.
17
+ *
18
+ * The entries stay in the nav the build routes from (`routablesFrom: nav` builds them) and
19
+ * locates pages in (`listedLocation`: a page's section); `listedNav` is the nav
20
+ * that is DRAWN and listed. The nav shape is generic: any entry with `href` and `children`.
21
+ */
22
+ /** The nav shape both pipelines read: an optional link, children, the flag. */
23
+ export interface UnlistedNavItem {
24
+ href?: string;
25
+ children?: readonly UnlistedNavItem[];
26
+ /** This entry and everything under it are unlisted. */
27
+ unlisted?: boolean;
28
+ }
29
+ export interface UnlistedOptions {
30
+ /**
31
+ * The page slug a nav href names, in the caller's form; undefined for a link to no page of the
32
+ * site. Default `navHrefSlug`: query and fragment dropped, slashes trimmed, the root `index`.
33
+ */
34
+ slugOf?: (href: string) => string | undefined;
35
+ }
36
+ /** A page as `markUnlisted` reads it: its slug and the front-matter keys that matter here. */
37
+ export interface UnlistedPage {
38
+ slug: string;
39
+ unlisted?: unknown;
40
+ noindex?: unknown;
41
+ excludeFromSearch?: unknown;
42
+ }
43
+ export interface UnlistedResult<N> {
44
+ /** The nav with every entry under an unlisted entry flagged too (a new tree). */
45
+ nav: N[];
46
+ /** Every unlisted page: front matter, or linked by an unlisted entry. */
47
+ slugs: Set<string>;
48
+ /**
49
+ * Pages unlisted in front matter that no nav entry links: under `routablesFrom: nav` they are
50
+ * not built at all, which is worth a warning naming them.
51
+ */
52
+ outsideNav: string[];
53
+ /** One line per page that asks to be indexed or searched while unlisted: it stays unlisted. */
54
+ warnings: {
55
+ slug: string;
56
+ message: string;
57
+ }[];
58
+ }
59
+ /** Default href → slug: `/guide/auth/#x` → `guide/auth`, `/` → `index`; a URL with a scheme → none. */
60
+ export declare function navHrefSlug(href: string): string | undefined;
61
+ /**
62
+ * Join the nav's flags and the pages' front matter (`pages`: every page of the unit, with its
63
+ * front matter's `unlisted`, `noindex` and `excludeFromSearch`).
64
+ */
65
+ export declare function markUnlisted<N extends UnlistedNavItem>(nav: readonly N[], pages: Iterable<UnlistedPage>, opts?: UnlistedOptions): UnlistedResult<N>;
66
+ /**
67
+ * The nav as it is drawn and listed: entries flagged `unlisted` gone with everything under them;
68
+ * an entry linking an unlisted page (`slugs`) loses its link, and goes when it has no listed
69
+ * children left. A group left with no link and no children goes too.
70
+ */
71
+ export declare function listedNav<N extends UnlistedNavItem>(nav: readonly N[], slugs?: ReadonlySet<string>, opts?: UnlistedOptions): N[];
72
+ /**
73
+ * Where each unlisted page sits in the LISTED nav: the link of its nearest ancestor that is
74
+ * still listed (or that ancestor's first listed link), for locating the page's section and
75
+ * sidebar. A page with none — a whole unlisted section, a page at the top — is left out of the
76
+ * map: it shows the list of sections, as a page in no section does. A group whose own page is
77
+ * unlisted is located by its first listed child. `nav` is `markUnlisted`'s; `home` (the site's
78
+ * home page, as scopeNav's) is never a location: it lists the sections. A page in several entries
79
+ * takes its first. One walk of the nav (a walk per page was quadratic: 20,000 unlisted pages in
80
+ * one hidden group took 159 s — security review).
81
+ */
82
+ export declare function listedLocations(nav: readonly UnlistedNavItem[], slugs: ReadonlySet<string>, opts?: UnlistedOptions & {
83
+ home?: string;
84
+ }): Map<string, string>;
85
+ /** `listedLocations` for one page. */
86
+ export declare function listedLocation(nav: readonly UnlistedNavItem[], slug: string, slugs: ReadonlySet<string>, opts?: UnlistedOptions & {
87
+ home?: string;
88
+ }): string | undefined;
@@ -0,0 +1,164 @@
1
+ /**
2
+ * Unlisted pages (plans/unlisted-pages.md): built and reachable by their URL, listed nowhere —
3
+ * not in the menu, previous / next, `::child-links`, the book view, `llms.txt`, search, the
4
+ * sitemap, the blog index, its feed or the tag pages, and `noindex`. It protects nothing: anyone
5
+ * with the URL reads the page.
6
+ *
7
+ * Two ways to say it, joined here so both pipelines (the Astro build and dogsbay-ssg) agree:
8
+ *
9
+ * - **A nav entry** `unlisted: true` (nav.yml's annotated form): that entry AND everything under
10
+ * it (a GitBook hidden space, a hidden SUMMARY group). `unlisted: false` below it does not list
11
+ * an entry again (v1: the flag only adds). The PAGE is unlisted when every entry linking it is;
12
+ * one listed entry keeps it listed (only the unlisted entries go), and an entry to a part of a
13
+ * page (`#fragment`) never unlists the page.
14
+ * - **A page's front matter** `unlisted: true`: that page only. Its entries lose their link; a
15
+ * group whose own page it is keeps its label and its listed children, as draft pruning keeps a
16
+ * group's label.
17
+ *
18
+ * The entries stay in the nav the build routes from (`routablesFrom: nav` builds them) and
19
+ * locates pages in (`listedLocation`: a page's section); `listedNav` is the nav
20
+ * that is DRAWN and listed. The nav shape is generic: any entry with `href` and `children`.
21
+ */
22
+ /** Default href → slug: `/guide/auth/#x` → `guide/auth`, `/` → `index`; a URL with a scheme → none. */
23
+ export function navHrefSlug(href) {
24
+ if (/^[a-z][a-z0-9+.-]*:/i.test(href) || href.startsWith("//"))
25
+ return undefined;
26
+ const bare = href.replace(/[?#].*$/, "").replace(/^\/+|\/+$/g, "");
27
+ return bare || "index";
28
+ }
29
+ /**
30
+ * Join the nav's flags and the pages' front matter (`pages`: every page of the unit, with its
31
+ * front matter's `unlisted`, `noindex` and `excludeFromSearch`).
32
+ */
33
+ export function markUnlisted(nav, pages, opts = {}) {
34
+ const slugOf = opts.slugOf ?? navHrefSlug;
35
+ const slugs = new Set();
36
+ const byFrontMatter = [];
37
+ for (const p of pages) {
38
+ if (p.unlisted !== true)
39
+ continue;
40
+ slugs.add(p.slug);
41
+ byFrontMatter.push(p);
42
+ }
43
+ const linked = new Set();
44
+ // A page is unlisted by the nav when EVERY entry linking it is (a page also in a listed group
45
+ // stays listed there); an entry to a part of a page (`#fragment`) unlists only itself
46
+ const unlistedBy = new Set();
47
+ const listedBy = new Set();
48
+ const walk = (items, inherited) => items.map((item) => {
49
+ const unlisted = inherited || item.unlisted === true;
50
+ if (item.href !== undefined) {
51
+ const slug = slugOf(item.href);
52
+ if (slug !== undefined) {
53
+ linked.add(slug);
54
+ if (!item.href.includes("#"))
55
+ (unlisted ? unlistedBy : listedBy).add(slug);
56
+ }
57
+ }
58
+ const children = item.children ? walk(item.children, unlisted) : undefined;
59
+ const out = { ...item, ...(children ? { children } : {}) };
60
+ if (unlisted)
61
+ out.unlisted = true;
62
+ return out;
63
+ });
64
+ const marked = walk(nav, false);
65
+ for (const slug of unlistedBy)
66
+ if (!listedBy.has(slug))
67
+ slugs.add(slug);
68
+ const warnings = [];
69
+ // A page unlisted by its nav entry carries no front matter here: its own keys are the caller's
70
+ // to pass; only what this call was given is checked
71
+ for (const p of byFrontMatter) {
72
+ const keep = [p.noindex === false ? "noindex: false" : "", p.excludeFromSearch === false ? "excludeFromSearch: false" : ""].filter(Boolean);
73
+ if (keep.length)
74
+ warnings.push({ slug: p.slug, message: `${p.slug}: unlisted: true with ${keep.join(" and ")} — an unlisted page is noindex and out of search; it stays unlisted` });
75
+ }
76
+ return { nav: marked, slugs, outsideNav: byFrontMatter.map((p) => p.slug).filter((s) => !linked.has(s)), warnings };
77
+ }
78
+ /**
79
+ * The nav as it is drawn and listed: entries flagged `unlisted` gone with everything under them;
80
+ * an entry linking an unlisted page (`slugs`) loses its link, and goes when it has no listed
81
+ * children left. A group left with no link and no children goes too.
82
+ */
83
+ export function listedNav(nav, slugs = new Set(), opts = {}) {
84
+ const slugOf = opts.slugOf ?? navHrefSlug;
85
+ const out = [];
86
+ for (const item of nav) {
87
+ if (item.unlisted === true)
88
+ continue;
89
+ const children = item.children ? listedNav(item.children, slugs, opts) : undefined;
90
+ const slug = item.href === undefined ? undefined : slugOf(item.href);
91
+ const hidden = slug !== undefined && slugs.has(slug);
92
+ const hadChildren = (item.children?.length ?? 0) > 0;
93
+ if (hidden && !children?.length)
94
+ continue;
95
+ if (!hidden && item.href === undefined && hadChildren && !children?.length)
96
+ continue;
97
+ const { href: _href, children: _children, ...rest } = item;
98
+ out.push({
99
+ ...rest,
100
+ ...(hidden || item.href === undefined ? {} : { href: item.href }),
101
+ ...(children && (children.length || !hadChildren) ? { children } : {}),
102
+ });
103
+ }
104
+ return out;
105
+ }
106
+ /**
107
+ * Where each unlisted page sits in the LISTED nav: the link of its nearest ancestor that is
108
+ * still listed (or that ancestor's first listed link), for locating the page's section and
109
+ * sidebar. A page with none — a whole unlisted section, a page at the top — is left out of the
110
+ * map: it shows the list of sections, as a page in no section does. A group whose own page is
111
+ * unlisted is located by its first listed child. `nav` is `markUnlisted`'s; `home` (the site's
112
+ * home page, as scopeNav's) is never a location: it lists the sections. A page in several entries
113
+ * takes its first. One walk of the nav (a walk per page was quadratic: 20,000 unlisted pages in
114
+ * one hidden group took 159 s — security review).
115
+ */
116
+ export function listedLocations(nav, slugs, opts = {}) {
117
+ const slugOf = opts.slugOf ?? navHrefSlug;
118
+ const home = opts.home === undefined ? undefined : slugOf(opts.home);
119
+ // Each entry's first listed link (its own, else its children's), worked out once
120
+ const first = new Map();
121
+ const firstListed = (item) => {
122
+ if (first.has(item))
123
+ return first.get(item);
124
+ let found;
125
+ if (item.unlisted !== true) {
126
+ if (item.href !== undefined) {
127
+ const s = slugOf(item.href);
128
+ if ((s === undefined || !slugs.has(s)) && (home === undefined || s !== home))
129
+ found = item.href;
130
+ }
131
+ for (const c of item.children ?? []) {
132
+ if (found !== undefined)
133
+ break;
134
+ found = firstListed(c);
135
+ }
136
+ }
137
+ first.set(item, found);
138
+ return found;
139
+ };
140
+ const out = new Map();
141
+ const seen = new Set(); // the first entry decides
142
+ const walk = (items, nearest) => {
143
+ for (const item of items) {
144
+ const own = item.unlisted === true ? undefined : firstListed(item);
145
+ if (item.href !== undefined) {
146
+ const s = slugOf(item.href);
147
+ if (s !== undefined && slugs.has(s) && !seen.has(s)) {
148
+ seen.add(s);
149
+ const at = own ?? nearest;
150
+ if (at !== undefined)
151
+ out.set(s, at);
152
+ }
153
+ }
154
+ if (item.children)
155
+ walk(item.children, own ?? nearest);
156
+ }
157
+ };
158
+ walk(nav, undefined);
159
+ return out;
160
+ }
161
+ /** `listedLocations` for one page. */
162
+ export function listedLocation(nav, slug, slugs, opts = {}) {
163
+ return listedLocations(nav, slugs, opts).get(slug);
164
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/site-files",
3
- "version": "0.2.0-beta.172",
3
+ "version": "0.2.0-beta.174",
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",