@dogsbay/site-files 0.2.0-beta.178 → 0.2.0-beta.180

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,128 @@
1
+ /**
2
+ * Book views ("Read as single page"): the PLAN of each book, shared by the Astro pipeline
3
+ * (format-astro `granularity.ts`, which assembles one tree) and dogsbay-ssg (which streams a book
4
+ * section by section in a Worker). Worker-safe, no dependencies: the nav and trees are read by
5
+ * their shape.
6
+ *
7
+ * A nav entry opts in with `single-page: yes | children | no` (nav.yml's annotated form):
8
+ *
9
+ * - `yes`: one book for the entry, holding its own page (the landing page, right under the book's
10
+ * title) and every entry under it, each a heading at its nav depth (H2 for the first level,
11
+ * capped at H6) with its page's body after it;
12
+ * - `children`: each entry directly under it defaults to `yes` (one book per chapter);
13
+ * - `no`: none (the default), and it overrides an inherited `children`.
14
+ *
15
+ * `book: true` (config `granularity.book`) makes every top-level entry `yes`; an entry's own value
16
+ * still wins. Only an entry with children has a book.
17
+ *
18
+ * The book is at `<the entry's route>/~book` (its landing page's slug; with no landing page, the
19
+ * folder its pages share). Every id on it is unique: the section anchors first, then each page's
20
+ * heading ids in book order (`bookHeadingIds`, a repeat taking `-1`, `-2`…), and a link between two
21
+ * pages of the book becomes an in-page link to the renamed id (`bookTarget`).
22
+ *
23
+ * Fixed 2026-11-17 against dogsbay-ssg's recorded Astro reference (its plans/book-views.md): the
24
+ * landing page was left out of its book, the route came from the first page (`reference/api/
25
+ * auth/~book` for the API group), and two pages' `#prerequisites` repeated on the book.
26
+ */
27
+ /**
28
+ * The route segment of a book. Not `_`-prefixed (Astro does not route a `_` file); no content slug
29
+ * can produce it, since slugs drop `~`.
30
+ */
31
+ export declare const BOOK_SEGMENT = "~book";
32
+ /** A nav entry as the planner reads it. */
33
+ export interface BookNavItem {
34
+ label: string;
35
+ href?: string;
36
+ children?: readonly BookNavItem[];
37
+ singlePage?: "yes" | "no" | "children";
38
+ }
39
+ /** One section of a book, in reading order. */
40
+ export interface BookEntry {
41
+ /** The nav entry's label: the section's heading. */
42
+ label: string;
43
+ /** Its heading level: 1 for the landing page (under the book's title, no heading of its own), else 2–6. */
44
+ level: number;
45
+ /** The page it shows; "" for a group with no page of its own (a heading only). */
46
+ slug: string;
47
+ /** The section's id on the book; "" for the landing page (the top of the book). */
48
+ anchor: string;
49
+ /** The book entry's own page. */
50
+ landing?: boolean;
51
+ }
52
+ export interface BookPlan {
53
+ /** The book's title: the entry's label. */
54
+ label: string;
55
+ /** The book's slug, `<route>/~book`, without the href prefix. */
56
+ slug: string;
57
+ /** Its sections in order: the landing page first when there is one. */
58
+ entries: BookEntry[];
59
+ /** Page slug → its section's anchor ("" for the landing page: the top of the book). */
60
+ topicAnchors: Map<string, string>;
61
+ /** The pages in it, in order. */
62
+ members: string[];
63
+ }
64
+ export interface PlanBooksOptions {
65
+ /** Every top-level entry is `yes` (config `granularity.book: true`). */
66
+ book?: boolean;
67
+ /** The prefix nav hrefs carry (`/docs`, or ""): taken off to give a page's slug. */
68
+ hrefPrefix?: string;
69
+ /** Whether a slug is a page of the site (a nav entry may link elsewhere). Default: every slug is. */
70
+ isPage?: (slug: string) => boolean;
71
+ }
72
+ /** The books a nav asks for, in nav order. */
73
+ export declare function planBooks(nav: readonly BookNavItem[], options?: PlanBooksOptions): BookPlan[];
74
+ /** The book of one nav entry, its own page included; null when it shows no page. */
75
+ export declare function planBook(group: BookNavItem, options?: PlanBooksOptions): BookPlan | null;
76
+ /**
77
+ * Each page's heading ids on the book: the section anchors are taken first, then each page's ids
78
+ * in book order, an id already taken getting `-1`, `-2`… (Install's and Configure's
79
+ * `#prerequisites` were the same id twice). `idsOf(slug)` gives a page's heading ids in order.
80
+ * Returns page slug → (its id → the id on the book), every id of the page listed.
81
+ */
82
+ export declare function bookHeadingIds(plan: BookPlan, idsOf: (slug: string) => readonly string[]): Map<string, Map<string, string>>;
83
+ /**
84
+ * Where a link to `slug#fragment` goes on the book: `#<id>` when the page is in the book (the
85
+ * renamed heading id; the page's section when the fragment is none of its ids, or there is none;
86
+ * `#` for the landing page's top), else undefined — the link keeps its target.
87
+ */
88
+ export declare function bookTarget(plan: BookPlan, ids: Map<string, Map<string, string>>, slug: string, fragment?: string): string | undefined;
89
+ /**
90
+ * A root-relative or fragment-only link in a page `from` of the book, rewritten for the book; undefined
91
+ * when it stays (another page, another site, a fragment the page does not have).
92
+ */
93
+ export declare function bookLink(plan: BookPlan, ids: Map<string, Map<string, string>>, href: string, from: string, hrefPrefix?: string): string | undefined;
94
+ /** A page's "Read as single page" link: the book's route and the page's section (none for the landing page). */
95
+ export declare function bookHref(bookRoute: string, anchor: string): string;
96
+ /**
97
+ * The landing page's body on its book: the book's title is its H1, so a leading H1 of the page is
98
+ * left out (two H1s otherwise — review), and any other H1 becomes an H2. Mutates `tree`; returns
99
+ * the id of the heading left out ("" for none), which then names the top of the book.
100
+ */
101
+ export declare function landingBody(tree: BookTreeNode[], idOf: (heading: BookTreeNode) => string): string;
102
+ /** A tree node as these helpers read it (`@dogsbay/types` TreeNode). */
103
+ export interface BookTreeNode {
104
+ type: string;
105
+ props?: Record<string, unknown>;
106
+ meta?: Record<string, unknown>;
107
+ html?: string;
108
+ inline?: unknown[];
109
+ children?: BookTreeNode[];
110
+ }
111
+ /** The heading a section starts with: its label, at its level, with its anchor as the id. */
112
+ export declare function bookHeading(level: number, title: string, anchor: string): BookTreeNode;
113
+ /** "Open as page ↗": the way from a section back to its page (`p.dsb-open-as-page`). */
114
+ export declare function openAsPageNode(slug: string, hrefPrefix: string): BookTreeNode;
115
+ /** Every heading in a tree `offset` levels down (at most H6). Mutates the tree. */
116
+ export declare function shiftHeadings(tree: BookTreeNode[], offset: number): void;
117
+ /**
118
+ * Roughly a tree's text in bytes: prose, pre-rendered html and string props (code). The input of
119
+ * the size cap (`granularity.maxSinglePageMB`). It is a sum over nodes, so a book's size is the sum
120
+ * over its sections (the headings and "Open as page" lines included): a streaming host adds pages
121
+ * up without building the book.
122
+ */
123
+ export declare function estimateTreeBytes(tree: readonly BookTreeNode[]): number;
124
+ /** A nav href's page slug: prefix, leading/trailing slashes, query and fragment off. */
125
+ export declare function slugFromHref(href: string | undefined, hrefPrefix: string): string;
126
+ /** The longest common path (by segment) of some slugs; `fallback` when they share none. */
127
+ export declare function bookSlugFrom(slugs: readonly string[], fallback: string): string;
128
+ export declare function slugify(text: string): string;
package/dist/books.js ADDED
@@ -0,0 +1,308 @@
1
+ /**
2
+ * Book views ("Read as single page"): the PLAN of each book, shared by the Astro pipeline
3
+ * (format-astro `granularity.ts`, which assembles one tree) and dogsbay-ssg (which streams a book
4
+ * section by section in a Worker). Worker-safe, no dependencies: the nav and trees are read by
5
+ * their shape.
6
+ *
7
+ * A nav entry opts in with `single-page: yes | children | no` (nav.yml's annotated form):
8
+ *
9
+ * - `yes`: one book for the entry, holding its own page (the landing page, right under the book's
10
+ * title) and every entry under it, each a heading at its nav depth (H2 for the first level,
11
+ * capped at H6) with its page's body after it;
12
+ * - `children`: each entry directly under it defaults to `yes` (one book per chapter);
13
+ * - `no`: none (the default), and it overrides an inherited `children`.
14
+ *
15
+ * `book: true` (config `granularity.book`) makes every top-level entry `yes`; an entry's own value
16
+ * still wins. Only an entry with children has a book.
17
+ *
18
+ * The book is at `<the entry's route>/~book` (its landing page's slug; with no landing page, the
19
+ * folder its pages share). Every id on it is unique: the section anchors first, then each page's
20
+ * heading ids in book order (`bookHeadingIds`, a repeat taking `-1`, `-2`…), and a link between two
21
+ * pages of the book becomes an in-page link to the renamed id (`bookTarget`).
22
+ *
23
+ * Fixed 2026-11-17 against dogsbay-ssg's recorded Astro reference (its plans/book-views.md): the
24
+ * landing page was left out of its book, the route came from the first page (`reference/api/
25
+ * auth/~book` for the API group), and two pages' `#prerequisites` repeated on the book.
26
+ */
27
+ /**
28
+ * The route segment of a book. Not `_`-prefixed (Astro does not route a `_` file); no content slug
29
+ * can produce it, since slugs drop `~`.
30
+ */
31
+ export const BOOK_SEGMENT = "~book";
32
+ /** The books a nav asks for, in nav order. */
33
+ export function planBooks(nav, options = {}) {
34
+ const plans = [];
35
+ const walk = (items, inheritYes) => {
36
+ for (const item of items) {
37
+ const effective = item.singlePage ?? (inheritYes ? "yes" : "no");
38
+ if (effective === "yes" && item.children?.length) {
39
+ const plan = planBook(item, options);
40
+ if (plan)
41
+ plans.push(plan);
42
+ }
43
+ if (item.children?.length)
44
+ walk(item.children, effective === "children");
45
+ }
46
+ };
47
+ walk(nav, options.book === true);
48
+ // Two books never share a route. A book at its own page's route keeps it; one at a fallback
49
+ // route (a shared folder, its label) gives way, wherever it sits in the nav (review)
50
+ const routes = new Set();
51
+ const own = (p) => p.entries[0]?.landing === true;
52
+ for (const plan of [...plans.filter(own), ...plans.filter((p) => !own(p))]) {
53
+ const base = plan.slug;
54
+ for (let n = 2; routes.has(plan.slug); n++)
55
+ plan.slug = base.replace(new RegExp(`/${escapeRe(BOOK_SEGMENT)}$`), `-${n}/${BOOK_SEGMENT}`);
56
+ routes.add(plan.slug);
57
+ }
58
+ return plans;
59
+ }
60
+ /** The book of one nav entry, its own page included; null when it shows no page. */
61
+ export function planBook(group, options = {}) {
62
+ const prefix = options.hrefPrefix ?? "";
63
+ const isPage = options.isPage ?? (() => true);
64
+ const entries = [];
65
+ const topicAnchors = new Map();
66
+ const members = [];
67
+ const used = new Set();
68
+ const unique = (base) => uniqueId(base || "section", used);
69
+ // A nav may name a folder's page `…/index` (a directory-built or imported nav); the page's slug
70
+ // is the folder's, and so is the book's route (dogsbay-ssg's review)
71
+ const slugOf = (href) => {
72
+ const raw = slugFromHref(href, prefix);
73
+ if (raw === "index" || raw.endsWith("/index")) {
74
+ const folder = raw.slice(0, -"index".length).replace(/\/$/, "");
75
+ if (!isPage(raw) || isPage(folder))
76
+ return folder;
77
+ }
78
+ return raw;
79
+ };
80
+ const groupSlug = slugOf(group.href);
81
+ if (groupSlug && isPage(groupSlug)) {
82
+ entries.push({ label: group.label, level: 1, slug: groupSlug, anchor: "", landing: true });
83
+ topicAnchors.set(groupSlug, "");
84
+ members.push(groupSlug);
85
+ }
86
+ const assign = (items, depth) => {
87
+ for (const item of items) {
88
+ const slug = slugOf(item.href);
89
+ // The landing page listed again under its own entry (an "Overview" child): it is already
90
+ // the top of the book, so no empty section for it (review)
91
+ if (slug !== "" && slug === groupSlug && topicAnchors.has(slug) && !item.children?.length)
92
+ continue;
93
+ const page = slug !== "" && isPage(slug) && !topicAnchors.has(slug);
94
+ const anchor = unique(slugify(slug ? lastSegment(slug) : item.label));
95
+ entries.push({ label: item.label, level: Math.min(depth + 2, 6), slug: page ? slug : "", anchor });
96
+ if (page) {
97
+ topicAnchors.set(slug, anchor);
98
+ members.push(slug);
99
+ }
100
+ if (item.children?.length)
101
+ assign(item.children, depth + 1);
102
+ }
103
+ };
104
+ assign(group.children ?? [], 0);
105
+ if (!members.length)
106
+ return null;
107
+ // The entry's route: its own page's slug; else the folder its pages share
108
+ const base = groupSlug.replace(/(^|\/)index$/, "") || sharedFolder(members) || slugify(group.label);
109
+ return { label: group.label, slug: `${base}/${BOOK_SEGMENT}`, entries, topicAnchors, members };
110
+ }
111
+ /**
112
+ * Each page's heading ids on the book: the section anchors are taken first, then each page's ids
113
+ * in book order, an id already taken getting `-1`, `-2`… (Install's and Configure's
114
+ * `#prerequisites` were the same id twice). `idsOf(slug)` gives a page's heading ids in order.
115
+ * Returns page slug → (its id → the id on the book), every id of the page listed.
116
+ */
117
+ export function bookHeadingIds(plan, idsOf) {
118
+ const used = new Set(plan.entries.map((e) => e.anchor).filter(Boolean));
119
+ const out = new Map();
120
+ for (const slug of plan.members) {
121
+ const map = new Map();
122
+ for (const id of idsOf(slug)) {
123
+ if (!id || map.has(id))
124
+ continue;
125
+ map.set(id, uniqueId(id, used));
126
+ }
127
+ out.set(slug, map);
128
+ }
129
+ return out;
130
+ }
131
+ /**
132
+ * Where a link to `slug#fragment` goes on the book: `#<id>` when the page is in the book (the
133
+ * renamed heading id; the page's section when the fragment is none of its ids, or there is none;
134
+ * `#` for the landing page's top), else undefined — the link keeps its target.
135
+ */
136
+ export function bookTarget(plan, ids, slug, fragment = "") {
137
+ const anchor = plan.topicAnchors.get(slug);
138
+ if (anchor === undefined)
139
+ return undefined;
140
+ const frag = safeDecode(fragment.replace(/^#/, ""));
141
+ const id = frag ? ids.get(slug)?.get(frag) : undefined;
142
+ return `#${id ?? anchor}`;
143
+ }
144
+ /**
145
+ * A root-relative or fragment-only link in a page `from` of the book, rewritten for the book; undefined
146
+ * when it stays (another page, another site, a fragment the page does not have).
147
+ */
148
+ export function bookLink(plan, ids, href, from, hrefPrefix = "") {
149
+ if (href.startsWith("#")) {
150
+ const id = ids.get(from)?.get(safeDecode(href.slice(1)));
151
+ return id === undefined ? undefined : `#${id}`;
152
+ }
153
+ if (href.startsWith("//") || (isExternal(href) && !href.startsWith("/")))
154
+ return undefined;
155
+ const hash = href.indexOf("#");
156
+ return bookTarget(plan, ids, slugFromHref(href, hrefPrefix), hash < 0 ? "" : href.slice(hash));
157
+ }
158
+ /** A page's "Read as single page" link: the book's route and the page's section (none for the landing page). */
159
+ export function bookHref(bookRoute, anchor) {
160
+ return anchor ? `${bookRoute}#${anchor}` : bookRoute;
161
+ }
162
+ // ── Tree helpers, by shape (TreeNode) ──────────────────────────────────────
163
+ /**
164
+ * The landing page's body on its book: the book's title is its H1, so a leading H1 of the page is
165
+ * left out (two H1s otherwise — review), and any other H1 becomes an H2. Mutates `tree`; returns
166
+ * the id of the heading left out ("" for none), which then names the top of the book.
167
+ */
168
+ export function landingBody(tree, idOf) {
169
+ let dropped = "";
170
+ if (tree[0]?.type === "heading" && (tree[0].props?.level ?? 1) === 1) {
171
+ dropped = idOf(tree[0]);
172
+ tree.shift();
173
+ }
174
+ const demote = (ns) => {
175
+ for (const n of ns) {
176
+ if (n.type === "heading" && (n.props?.level ?? 1) === 1)
177
+ n.props = { ...(n.props ?? {}), level: 2 };
178
+ if (n.children)
179
+ demote(n.children);
180
+ }
181
+ };
182
+ demote(tree);
183
+ return dropped;
184
+ }
185
+ /** The heading a section starts with: its label, at its level, with its anchor as the id. */
186
+ export function bookHeading(level, title, anchor) {
187
+ return {
188
+ type: "heading",
189
+ props: { level, slug: anchor, text: title },
190
+ children: [{ type: "prose", inline: [{ type: "text", text: title }] }],
191
+ };
192
+ }
193
+ /** "Open as page ↗": the way from a section back to its page (`p.dsb-open-as-page`). */
194
+ export function openAsPageNode(slug, hrefPrefix) {
195
+ const route = hrefPrefix ? `${hrefPrefix}/${slug}` : `/${slug}`;
196
+ return {
197
+ type: "paragraph",
198
+ props: { class: "dsb-open-as-page not-prose" },
199
+ children: [{ type: "prose", inline: [{ type: "link", href: route, children: [{ type: "text", text: "Open as page ↗" }] }] }],
200
+ };
201
+ }
202
+ /** Every heading in a tree `offset` levels down (at most H6). Mutates the tree. */
203
+ export function shiftHeadings(tree, offset) {
204
+ if (offset <= 0)
205
+ return;
206
+ for (const n of tree) {
207
+ if (n.type === "heading") {
208
+ const lvl = n.props?.level ?? 1;
209
+ if (!n.props)
210
+ n.props = {};
211
+ n.props.level = Math.min(lvl + offset, 6);
212
+ }
213
+ if (n.children)
214
+ shiftHeadings(n.children, offset);
215
+ }
216
+ }
217
+ /**
218
+ * Roughly a tree's text in bytes: prose, pre-rendered html and string props (code). The input of
219
+ * the size cap (`granularity.maxSinglePageMB`). It is a sum over nodes, so a book's size is the sum
220
+ * over its sections (the headings and "Open as page" lines included): a streaming host adds pages
221
+ * up without building the book.
222
+ */
223
+ export function estimateTreeBytes(tree) {
224
+ let bytes = 0;
225
+ const walk = (nodes) => {
226
+ for (const n of nodes) {
227
+ if (typeof n.html === "string")
228
+ bytes += n.html.length;
229
+ if (n.props)
230
+ for (const v of Object.values(n.props))
231
+ if (typeof v === "string")
232
+ bytes += v.length;
233
+ if (Array.isArray(n.inline)) {
234
+ for (const i of n.inline) {
235
+ if (typeof i.text === "string")
236
+ bytes += i.text.length;
237
+ else if (typeof i.value === "string")
238
+ bytes += i.value.length;
239
+ }
240
+ }
241
+ if (n.children?.length)
242
+ walk(n.children);
243
+ }
244
+ };
245
+ walk(tree);
246
+ return bytes;
247
+ }
248
+ // ── Paths ──────────────────────────────────────────────────────────────────
249
+ /** A nav href's page slug: prefix, leading/trailing slashes, query and fragment off. */
250
+ export function slugFromHref(href, hrefPrefix) {
251
+ if (!href || href.startsWith("//") || (isExternal(href) && !href.startsWith("/")))
252
+ return "";
253
+ let s = href.split("#")[0].split("?")[0].trim();
254
+ if (hrefPrefix && (s === hrefPrefix || s.startsWith(`${hrefPrefix}/`)))
255
+ s = s.slice(hrefPrefix.length);
256
+ return s.replace(/^\/+/, "").replace(/\/+$/, "");
257
+ }
258
+ /** The longest common path (by segment) of some slugs; `fallback` when they share none. */
259
+ export function bookSlugFrom(slugs, fallback) {
260
+ return commonPath(slugs.filter(Boolean)) || fallback;
261
+ }
262
+ export function slugify(text) {
263
+ return (text
264
+ .toLowerCase()
265
+ .replace(/[^\w\s-]/g, "")
266
+ .replace(/\s+/g, "-")
267
+ .replace(/-+/g, "-")
268
+ .replace(/^-+|-+$/g, "")
269
+ .trim() || "section");
270
+ }
271
+ /** The folder the pages share: the common path of each page's folder (a lone page's folder, not the page). */
272
+ function sharedFolder(slugs) {
273
+ return commonPath(slugs.map((s) => s.split("/").slice(0, -1).join("/")));
274
+ }
275
+ function commonPath(slugs) {
276
+ const parts = slugs.map((s) => (s ? s.split("/") : []));
277
+ if (!parts.length)
278
+ return "";
279
+ const first = parts[0];
280
+ let i = 0;
281
+ while (i < first.length && parts.every((p) => p[i] === first[i]))
282
+ i++;
283
+ return first.slice(0, i).join("/");
284
+ }
285
+ function uniqueId(base, used) {
286
+ let id = base;
287
+ for (let n = 1; used.has(id); n++)
288
+ id = `${base}-${n}`;
289
+ used.add(id);
290
+ return id;
291
+ }
292
+ function lastSegment(slug) {
293
+ return slug.split("/").pop() ?? slug;
294
+ }
295
+ function isExternal(href) {
296
+ return !!href && /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/)/i.test(href);
297
+ }
298
+ function safeDecode(s) {
299
+ try {
300
+ return decodeURIComponent(s);
301
+ }
302
+ catch {
303
+ return s;
304
+ }
305
+ }
306
+ function escapeRe(s) {
307
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
308
+ }
@@ -13,7 +13,7 @@
13
13
  * (`article.docs-prose`, else `<main>`, else the page).
14
14
  * - `#id` on a heading: its text as the label, and the first paragraph of its section.
15
15
  * - `#id` on anything else (a figure, a table, a callout: a link target): its caption or title as
16
- * the label, and the first paragraph inside it.
16
+ * the label (a component's `data-part="title"`), and the first paragraph inside it.
17
17
  * - An `#id` the page lacks: undefined, so a host can say the link goes nowhere.
18
18
  * - A redirect page (`<meta http-equiv="refresh">` naming a URL, as Astro's stubs and dogsbay-ssg's redirect
19
19
  * entries are built): undefined. Its title is the old page's; the reader would land elsewhere.
@@ -25,8 +25,11 @@ export interface LinkSummary {
25
25
  label?: string;
26
26
  /** About `EXCERPT_LENGTH` characters of the first paragraph, cut on a word; may be empty. */
27
27
  excerpt: string;
28
- /** What the link names: the page, a heading on it, or another target (a figure, a table). */
29
- kind: "page" | "heading" | "target";
28
+ /**
29
+ * What the link names: the page, a heading on it, another target (a figure, a table), or a
30
+ * footnote (its text is the excerpt, and the title is empty: the card shows the note alone).
31
+ */
32
+ kind: "page" | "heading" | "target" | "footnote";
30
33
  }
31
34
  /** An excerpt is cut near this many characters, on a word. */
32
35
  export declare const EXCERPT_LENGTH = 200;
@@ -13,7 +13,7 @@
13
13
  * (`article.docs-prose`, else `<main>`, else the page).
14
14
  * - `#id` on a heading: its text as the label, and the first paragraph of its section.
15
15
  * - `#id` on anything else (a figure, a table, a callout: a link target): its caption or title as
16
- * the label, and the first paragraph inside it.
16
+ * the label (a component's `data-part="title"`), and the first paragraph inside it.
17
17
  * - An `#id` the page lacks: undefined, so a host can say the link goes nowhere.
18
18
  * - A redirect page (`<meta http-equiv="refresh">` naming a URL, as Astro's stubs and dogsbay-ssg's redirect
19
19
  * entries are built): undefined. Its title is the old page's; the reader would land elsewhere.
@@ -204,7 +204,7 @@ function* elements(root) {
204
204
  }
205
205
  const hidden = (e) => e.attrs["aria-hidden"] === "true" || "hidden" in e.attrs || e.tag === "template";
206
206
  /** An element's text as a reader sees it: entities decoded, hidden parts left out, spaces collapsed. */
207
- function textOf(el) {
207
+ function textOf(el, skip) {
208
208
  let out = "";
209
209
  const stack = [el];
210
210
  while (stack.length) {
@@ -213,7 +213,7 @@ function textOf(el) {
213
213
  out += decode(n);
214
214
  continue;
215
215
  }
216
- if (n !== el && (hidden(n) || NO_TEXT.has(n.tag) || n.tag === "script" || n.tag === "style"))
216
+ if (n !== el && (hidden(n) || NO_TEXT.has(n.tag) || n.tag === "script" || n.tag === "style" || skip?.(n)))
217
217
  continue;
218
218
  if (n.tag === "br")
219
219
  out += " ";
@@ -222,6 +222,20 @@ function textOf(el) {
222
222
  }
223
223
  return out.replace(/[\s ]+/g, " ").trim();
224
224
  }
225
+ /** A footnote is cut near this many characters (a note is read whole where it is short enough). */
226
+ const FOOTNOTE_LENGTH = 600;
227
+ /** Whether `li` is an item of a footnotes list (its list in a section marked as the page's notes). */
228
+ function inFootnotes(li) {
229
+ for (let e = li.parent, depth = 0; e && depth < 4; e = e.parent, depth++) {
230
+ if (e.attrs.role === "doc-endnotes" || "data-footnotes" in e.attrs || /(?:^|\s)footnotes(?:\s|$)/.test(e.attrs.class ?? ""))
231
+ return true;
232
+ }
233
+ return false;
234
+ }
235
+ /** A footnote's text without its back-link (`role="doc-backlink"`, or a link that is only `↩`). */
236
+ function footnoteText(li) {
237
+ return textOf(li, (e) => e.tag === "a" && (e.attrs.role === "doc-backlink" || (e.children.length === 1 && typeof e.children[0] === "string" && /^\s*↩\uFE0E?\s*$/.test(decode(e.children[0])))));
238
+ }
225
239
  /** Whether `a` is `b` or holds it. */
226
240
  const holds = (a, b) => a.start <= b.start && b.end <= a.end;
227
241
  /** Whether nothing between `el` and `top` (an aside, a figure, chrome, hidden) makes its text not the page's prose. */
@@ -262,14 +276,42 @@ function find(root, test) {
262
276
  return e;
263
277
  return undefined;
264
278
  }
265
- /** A target's own name: its caption, title, or first heading. */
279
+ /**
280
+ * A target's own name: its caption, title, or first heading. A component's title is its
281
+ * `data-part="title"` (a callout's, a code block's file name), as render-html and the Astro
282
+ * components mark it (playground item 30: a callout's title was no label).
283
+ */
266
284
  function targetLabel(el) {
267
285
  const own = el.attrs["data-title"] ?? el.attrs["aria-label"] ?? el.attrs.title;
268
286
  if (own?.trim())
269
287
  return name(own.replace(/\s+/g, " ").trim());
270
- const named = find(el, (e) => e.tag === "figcaption" || e.tag === "caption" || e.tag === "summary" || "data-card-title" in e.attrs || "data-title" in e.attrs || HEADING.test(e.tag));
288
+ const named = find(el, (e) => e.tag === "figcaption" || e.tag === "caption" || e.tag === "summary" || "data-card-title" in e.attrs || "data-title" in e.attrs || HEADING.test(e.tag))
289
+ ?? ownTitle(el);
271
290
  return named ? name(textOf(named)) : "";
272
291
  }
292
+ /** An element that starts a component of its own: a title inside it is that component's. */
293
+ const isComponent = (e) => "data-component" in e.attrs || "data-type" in e.attrs || e.attrs.role === "note" || e.tag === "details" || e.tag === "figure" || e.tag.startsWith("db-");
294
+ /**
295
+ * The target's own `data-part="title"`: the first not inside another component within it. A figure
296
+ * holding a code block with a file name is named by its caption, not by the file name (review: a
297
+ * titled code block took over a figure's and a tab group's label). One pass in document order, each
298
+ * element's answer from its parent's (a walk up per candidate was quadratic on nested titles).
299
+ */
300
+ function ownTitle(target) {
301
+ const inner = new Set();
302
+ for (const e of elements(target)) {
303
+ if (e === target)
304
+ continue;
305
+ const p = e.parent;
306
+ if (p && p !== target && (inner.has(p) || isComponent(p))) {
307
+ inner.add(e);
308
+ continue;
309
+ }
310
+ if (e.attrs["data-part"] === "title")
311
+ return e;
312
+ }
313
+ return undefined;
314
+ }
273
315
  /**
274
316
  * The summary of a page (`html`, the whole built page) or of the place `id` names on it (the
275
317
  * fragment of a link, decoded, without `#`); undefined when the page has no such id.
@@ -315,6 +357,13 @@ export function linkSummary(html, id) {
315
357
  kind: "heading",
316
358
  };
317
359
  }
360
+ // A footnote (an item of a footnotes list: render-html's `role="doc-endnotes"`, format-astro's
361
+ // `data-footnotes`): its whole text, without its `↩` back-link, as the card. GitBook's
362
+ // annotations are footnotes in its `.md`, and a popover on GitBook's site (2026-11-19)
363
+ if (target.tag === "li" && inFootnotes(target)) {
364
+ const text = footnoteText(target);
365
+ return text ? { title: "", excerpt: cut(text, FOOTNOTE_LENGTH), kind: "footnote" } : undefined;
366
+ }
318
367
  const inside = firstParagraph(elements(target), target);
319
368
  const own = !inside && TEXT_BLOCK.has(target.tag) ? cut(textOf(target)) : "";
320
369
  return { title, label: targetLabel(target), excerpt: inside || own, kind: "target" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/site-files",
3
- "version": "0.2.0-beta.178",
3
+ "version": "0.2.0-beta.180",
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",
@@ -13,6 +13,10 @@
13
13
  "./link-summary": {
14
14
  "types": "./dist/link-summary.d.ts",
15
15
  "import": "./dist/link-summary.js"
16
+ },
17
+ "./books": {
18
+ "types": "./dist/books.d.ts",
19
+ "import": "./dist/books.js"
16
20
  }
17
21
  },
18
22
  "files": [