@dogsbay/docs-layout 0.2.0-beta.10 → 0.2.0-beta.101
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/package.json +7 -5
- package/src/BlogIndex.astro +179 -0
- package/src/DocsFooter.astro +27 -3
- package/src/DocsLayout.astro +541 -40
- package/src/DocsNavClient.astro +107 -0
- package/src/DocsToc.astro +1 -1
- package/src/SearchDialog.astro +301 -33
- package/src/TagList.astro +17 -2
- package/src/VersionSwitcher.astro +6 -0
- package/src/docs-nav-client.ts +419 -0
- package/src/json-ld.ts +112 -0
- package/src/link-icons.ts +54 -0
- package/src/markdown-negotiation.ts +38 -2
- package/src/nav-filter.ts +42 -129
- package/src/search-facets.ts +511 -9
- package/src/switcher.ts +83 -2
- package/src/toc-placement.ts +71 -0
- package/src/version-redirect.ts +23 -0
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side sidebar nav hydration.
|
|
3
|
+
*
|
|
4
|
+
* Fetches `/_dogsbay/nav.json` once per session, renders the tree DOM
|
|
5
|
+
* to mirror `@dogsbay/ui/sidebar/SidebarNavTree.astro`'s structure,
|
|
6
|
+
* and re-highlights the current page on each Astro view-transition
|
|
7
|
+
* page-load.
|
|
8
|
+
*
|
|
9
|
+
* Design constraints:
|
|
10
|
+
* - DOM output matches SidebarNavTree exactly (same tags, classes,
|
|
11
|
+
* data-attributes) so Tailwind's compiled CSS styles us correctly
|
|
12
|
+
* and any sidebar-system selectors (e.g. `data-sidebar="nav-tree"`
|
|
13
|
+
* hooks) keep working.
|
|
14
|
+
* - Filter by `version` / `locale` matches `nav-filter.ts`'s SSR
|
|
15
|
+
* filter so a multi-axis site renders the same nav as `ssr-full`
|
|
16
|
+
* mode (without the multiplicative HTML cost).
|
|
17
|
+
* - One fetch per session. The `nav.json` URL is served with
|
|
18
|
+
* `Cache-Control: immutable` by Astro's static build (it lives
|
|
19
|
+
* under `public/_dogsbay/`), so the browser cache holds it across
|
|
20
|
+
* navigations.
|
|
21
|
+
*
|
|
22
|
+
* See plans/client-rendered-nav.md.
|
|
23
|
+
*/
|
|
24
|
+
import { filterNavByAxis } from "./nav-filter.js";
|
|
25
|
+
|
|
26
|
+
interface NavMark {
|
|
27
|
+
kind: "added" | "changed" | "removed" | "moved";
|
|
28
|
+
label?: string;
|
|
29
|
+
subtree?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface NavItem {
|
|
33
|
+
label: string;
|
|
34
|
+
href?: string;
|
|
35
|
+
icon?: string;
|
|
36
|
+
children?: NavItem[];
|
|
37
|
+
mark?: NavMark;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A state marker beside a nav label (release comparisons mark changed /
|
|
42
|
+
* new / removed pages; the same slot serves "new since your last
|
|
43
|
+
* visit", deprecation flags, version badges).
|
|
44
|
+
*
|
|
45
|
+
* Accessibility contract — mirrors SidebarNavMark.astro, which renders
|
|
46
|
+
* the server-side tree:
|
|
47
|
+
* - **Never colour alone** (WCAG 1.4.1): a distinct GLYPH carries the
|
|
48
|
+
* meaning, so it survives greyscale and colour-blindness; colour only
|
|
49
|
+
* reinforces.
|
|
50
|
+
* - **Survives forced-colors** (High Contrast strips backgrounds): the
|
|
51
|
+
* glyph is real text, so it always renders.
|
|
52
|
+
* - **Announced**: a visually-hidden word rides along, so the row reads
|
|
53
|
+
* "MySQL, changed". It is a sibling span, NOT an aria-label on the
|
|
54
|
+
* link — an aria-label would REPLACE the page name in the
|
|
55
|
+
* accessibility tree, losing the label it exists to announce.
|
|
56
|
+
*/
|
|
57
|
+
const MARK_GLYPH: Record<NavMark["kind"], string> = {
|
|
58
|
+
added: "+",
|
|
59
|
+
changed: "•",
|
|
60
|
+
removed: "−",
|
|
61
|
+
moved: "→",
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function buildMark(mark: NavMark): HTMLElement {
|
|
65
|
+
const text = mark.label ?? (mark.subtree ? `contains ${mark.kind}` : mark.kind);
|
|
66
|
+
const wrap = document.createElement("span");
|
|
67
|
+
wrap.className = "db-nav-mark";
|
|
68
|
+
wrap.dataset.navMark = mark.kind;
|
|
69
|
+
if (mark.subtree) wrap.dataset.navMarkSubtree = "";
|
|
70
|
+
wrap.title = text;
|
|
71
|
+
|
|
72
|
+
const glyph = document.createElement("span");
|
|
73
|
+
glyph.setAttribute("aria-hidden", "true");
|
|
74
|
+
glyph.textContent = MARK_GLYPH[mark.kind];
|
|
75
|
+
wrap.appendChild(glyph);
|
|
76
|
+
|
|
77
|
+
const sr = document.createElement("span");
|
|
78
|
+
sr.className = "sr-only";
|
|
79
|
+
sr.textContent = text;
|
|
80
|
+
wrap.appendChild(sr);
|
|
81
|
+
|
|
82
|
+
return wrap;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Module-level cache so multiple page loads in the same SPA session
|
|
87
|
+
* share the same fetched nav data. Cleared by re-loads (full page
|
|
88
|
+
* navigations) but Astro's view transitions re-execute the script
|
|
89
|
+
* module without reloading the page — so on view-transition the
|
|
90
|
+
* cached promise is reused and no extra fetch fires.
|
|
91
|
+
*/
|
|
92
|
+
let navPromise: Promise<NavItem[]> | null = null;
|
|
93
|
+
|
|
94
|
+
function fetchNav(url: string): Promise<NavItem[]> {
|
|
95
|
+
if (!navPromise) {
|
|
96
|
+
navPromise = fetch(url, { credentials: "same-origin" }).then((r) => {
|
|
97
|
+
if (!r.ok) throw new Error(`nav.json fetch failed: ${r.status}`);
|
|
98
|
+
return r.json() as Promise<NavItem[]>;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return navPromise;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function normalize(path: string): string {
|
|
105
|
+
return path.replace(/\/$/, "") || "/";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function hasActiveDescendant(item: NavItem, current: string): boolean {
|
|
109
|
+
if (item.href && normalize(item.href) === current) return true;
|
|
110
|
+
return item.children?.some((c) => hasActiveDescendant(c, current)) ?? false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Render a single nav item into a `<li>` DOM node. Matches
|
|
115
|
+
* SidebarNavTree's per-level class set exactly so styling stays in
|
|
116
|
+
* sync. Padding is computed from `level` the same way (`8 + level*12`
|
|
117
|
+
* pixels) so indentation lines up across the same render.
|
|
118
|
+
*/
|
|
119
|
+
export function renderItem(item: NavItem, current: string, level: number): HTMLLIElement {
|
|
120
|
+
const li = document.createElement("li");
|
|
121
|
+
li.dataset.sidebar = "nav-tree-item";
|
|
122
|
+
|
|
123
|
+
const active = item.href ? normalize(item.href) === current : false;
|
|
124
|
+
const hasChildren = !!item.children && item.children.length > 0;
|
|
125
|
+
const padLeft = `${8 + level * 12}px`;
|
|
126
|
+
const heightClass = level === 0 ? "h-8" : "h-7";
|
|
127
|
+
|
|
128
|
+
if (hasChildren && item.href) {
|
|
129
|
+
// A branch that is ALSO a page (section landing page) uses the APG
|
|
130
|
+
// "disclosure navigation" pattern: a real link (navigates) and a
|
|
131
|
+
// SEPARATE toggle <button> (expands/collapses), as siblings. Nesting
|
|
132
|
+
// a focusable <a> inside the interactive <summary> — as this did
|
|
133
|
+
// before — is an axe `nested-interactive` violation ("interactive
|
|
134
|
+
// controls must not be nested"). Native <details> can't hold a
|
|
135
|
+
// visible-when-collapsed header link without that nesting, so a
|
|
136
|
+
// page-branch drops <details> for link + button + sibling submenu.
|
|
137
|
+
const open = active || hasActiveDescendant(item, current);
|
|
138
|
+
const submenuId = `nav-sub-${item.href.replace(/[^a-z0-9]+/gi, "-")}-${level}`;
|
|
139
|
+
|
|
140
|
+
const row = document.createElement("div");
|
|
141
|
+
row.className = [
|
|
142
|
+
"flex w-full min-w-0 items-center gap-2 rounded-md text-sm text-sidebar-foreground hover:bg-sidebar-accent hover:text-sidebar-accent-foreground",
|
|
143
|
+
heightClass,
|
|
144
|
+
active ? "bg-sidebar-accent font-medium text-sidebar-accent-foreground" : "",
|
|
145
|
+
]
|
|
146
|
+
.filter(Boolean)
|
|
147
|
+
.join(" ");
|
|
148
|
+
row.style.paddingLeft = padLeft;
|
|
149
|
+
|
|
150
|
+
const toggle = document.createElement("button");
|
|
151
|
+
toggle.type = "button";
|
|
152
|
+
toggle.dataset.navToggle = "";
|
|
153
|
+
toggle.setAttribute("aria-controls", submenuId);
|
|
154
|
+
toggle.setAttribute("aria-expanded", String(open));
|
|
155
|
+
toggle.setAttribute("aria-label", `Toggle ${item.label} section`);
|
|
156
|
+
toggle.className =
|
|
157
|
+
"shrink-0 rounded outline-none ring-sidebar-ring focus-visible:ring-2";
|
|
158
|
+
toggle.innerHTML =
|
|
159
|
+
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="size-4 shrink-0 transition-transform duration-200" data-chevron aria-hidden="true"><polyline points="9 18 15 12 9 6"/></svg>';
|
|
160
|
+
row.appendChild(toggle);
|
|
161
|
+
|
|
162
|
+
if (item.icon) {
|
|
163
|
+
const iconSpan = document.createElement("span");
|
|
164
|
+
iconSpan.className = "shrink-0 [&>svg]:size-4";
|
|
165
|
+
iconSpan.innerHTML = item.icon;
|
|
166
|
+
row.appendChild(iconSpan);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const link = document.createElement("a");
|
|
170
|
+
link.className =
|
|
171
|
+
"min-w-0 flex-1 truncate text-inherit no-underline outline-none ring-sidebar-ring focus-visible:ring-2";
|
|
172
|
+
link.href = item.href;
|
|
173
|
+
link.textContent = item.label;
|
|
174
|
+
link.dataset.navHref = item.href; // rehighlight keys off this
|
|
175
|
+
if (active) link.dataset.active = "true";
|
|
176
|
+
row.appendChild(link);
|
|
177
|
+
|
|
178
|
+
if (item.mark) {
|
|
179
|
+
// A SUBTREE mark describes DESCENDANTS — never strike through this
|
|
180
|
+
// row for it (a surviving group whose child was deleted would read
|
|
181
|
+
// as a deleted section).
|
|
182
|
+
if (!item.mark.subtree) link.dataset.navMarkRow = item.mark.kind;
|
|
183
|
+
row.appendChild(buildMark(item.mark));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const submenu = document.createElement("ul");
|
|
187
|
+
submenu.id = submenuId;
|
|
188
|
+
submenu.dataset.navSubmenu = "";
|
|
189
|
+
submenu.className = "flex min-w-0 flex-col";
|
|
190
|
+
submenu.dataset.sidebar = "nav-tree";
|
|
191
|
+
submenu.dataset.level = String(level + 1);
|
|
192
|
+
for (const child of item.children!) {
|
|
193
|
+
submenu.appendChild(renderItem(child, current, level + 1));
|
|
194
|
+
}
|
|
195
|
+
if (!open) submenu.hidden = true;
|
|
196
|
+
|
|
197
|
+
toggle.addEventListener("click", () => {
|
|
198
|
+
const isOpen = toggle.getAttribute("aria-expanded") === "true";
|
|
199
|
+
toggle.setAttribute("aria-expanded", String(!isOpen));
|
|
200
|
+
submenu.hidden = isOpen;
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
li.appendChild(row);
|
|
204
|
+
li.appendChild(submenu);
|
|
205
|
+
} else if (hasChildren) {
|
|
206
|
+
// Pure grouping label (no href): native <details>/<summary> is fully
|
|
207
|
+
// accessible here — the summary is the only interactive control, and
|
|
208
|
+
// its label is a plain <span>. Zero-JS expand/collapse.
|
|
209
|
+
const details = document.createElement("details");
|
|
210
|
+
if (active || hasActiveDescendant(item, current)) details.open = true;
|
|
211
|
+
|
|
212
|
+
const summary = document.createElement("summary");
|
|
213
|
+
summary.className = [
|
|
214
|
+
"flex w-full min-w-0 cursor-pointer items-center gap-2 rounded-md text-sm text-sidebar-foreground outline-none [list-style:none] ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 [&::-webkit-details-marker]:hidden",
|
|
215
|
+
heightClass,
|
|
216
|
+
]
|
|
217
|
+
.filter(Boolean)
|
|
218
|
+
.join(" ");
|
|
219
|
+
summary.style.paddingLeft = padLeft;
|
|
220
|
+
|
|
221
|
+
summary.innerHTML =
|
|
222
|
+
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="size-4 shrink-0 transition-transform duration-200" data-chevron aria-hidden="true"><polyline points="9 18 15 12 9 6"/></svg>';
|
|
223
|
+
|
|
224
|
+
if (item.icon) {
|
|
225
|
+
const iconSpan = document.createElement("span");
|
|
226
|
+
iconSpan.className = "shrink-0 [&>svg]:size-4";
|
|
227
|
+
iconSpan.innerHTML = item.icon;
|
|
228
|
+
summary.appendChild(iconSpan);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const label = document.createElement("span");
|
|
232
|
+
label.className = "min-w-0 flex-1 truncate";
|
|
233
|
+
label.textContent = item.label;
|
|
234
|
+
summary.appendChild(label);
|
|
235
|
+
|
|
236
|
+
// A collapsed group HIDES its children, so a change inside must
|
|
237
|
+
// signal outward — the same visibility rule the diff renderer
|
|
238
|
+
// applies to tabs.
|
|
239
|
+
if (item.mark) summary.appendChild(buildMark(item.mark));
|
|
240
|
+
|
|
241
|
+
details.appendChild(summary);
|
|
242
|
+
|
|
243
|
+
const childUl = document.createElement("ul");
|
|
244
|
+
childUl.className = "flex min-w-0 flex-col";
|
|
245
|
+
childUl.dataset.sidebar = "nav-tree";
|
|
246
|
+
childUl.dataset.level = String(level + 1);
|
|
247
|
+
for (const child of item.children!) {
|
|
248
|
+
childUl.appendChild(renderItem(child, current, level + 1));
|
|
249
|
+
}
|
|
250
|
+
details.appendChild(childUl);
|
|
251
|
+
|
|
252
|
+
li.appendChild(details);
|
|
253
|
+
} else {
|
|
254
|
+
const a = document.createElement("a");
|
|
255
|
+
a.href = item.href || "#";
|
|
256
|
+
a.className = [
|
|
257
|
+
"flex w-full min-w-0 items-center gap-2 rounded-md text-sm text-sidebar-foreground outline-none ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2",
|
|
258
|
+
heightClass,
|
|
259
|
+
active ? "bg-sidebar-accent font-medium text-sidebar-accent-foreground" : "",
|
|
260
|
+
]
|
|
261
|
+
.filter(Boolean)
|
|
262
|
+
.join(" ");
|
|
263
|
+
a.style.paddingLeft = padLeft;
|
|
264
|
+
if (active) a.dataset.active = "true";
|
|
265
|
+
if (item.href) a.dataset.navHref = item.href;
|
|
266
|
+
|
|
267
|
+
// Spacer to align leaf text with branch text (which has a chevron).
|
|
268
|
+
const spacer = document.createElement("span");
|
|
269
|
+
spacer.className = "size-4 shrink-0";
|
|
270
|
+
a.appendChild(spacer);
|
|
271
|
+
|
|
272
|
+
if (item.icon) {
|
|
273
|
+
const iconSpan = document.createElement("span");
|
|
274
|
+
iconSpan.className = "shrink-0 [&>svg]:size-4";
|
|
275
|
+
iconSpan.innerHTML = item.icon;
|
|
276
|
+
a.appendChild(iconSpan);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const label = document.createElement("span");
|
|
280
|
+
label.className = "min-w-0 flex-1 truncate";
|
|
281
|
+
label.textContent = item.label;
|
|
282
|
+
a.appendChild(label);
|
|
283
|
+
|
|
284
|
+
if (item.mark) {
|
|
285
|
+
// strikethrough only for a page that IS removed, never for an
|
|
286
|
+
// aggregate about its children
|
|
287
|
+
if (!item.mark.subtree) a.dataset.navMarkRow = item.mark.kind;
|
|
288
|
+
a.appendChild(buildMark(item.mark));
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
li.appendChild(a);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return li;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export function renderTree(items: NavItem[], current: string, root: HTMLElement): void {
|
|
298
|
+
const ul = document.createElement("ul");
|
|
299
|
+
ul.className =
|
|
300
|
+
"flex min-w-0 flex-col w-full group-data-[collapsible=icon]:hidden";
|
|
301
|
+
ul.dataset.sidebar = "nav-tree";
|
|
302
|
+
ul.dataset.level = "0";
|
|
303
|
+
for (const item of items) {
|
|
304
|
+
ul.appendChild(renderItem(item, current, 0));
|
|
305
|
+
}
|
|
306
|
+
// Replace skeleton in one DOM op so there's no flash of partial state.
|
|
307
|
+
root.replaceChildren(ul);
|
|
308
|
+
root.removeAttribute("aria-busy");
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Re-highlight the active item without rebuilding the whole tree.
|
|
313
|
+
* Used on `astro:page-load` when view transitions land on a new
|
|
314
|
+
* route — we re-toggle the `data-active` attribute and re-apply the
|
|
315
|
+
* active classes, then expand the new active branch's ancestors.
|
|
316
|
+
*
|
|
317
|
+
* Cheaper than a full re-render (no fetch, no DOM rebuild). For
|
|
318
|
+
* the wider hydration loop we still re-render when a brand-new nav
|
|
319
|
+
* structure is needed (e.g. switching versions), but path-only
|
|
320
|
+
* navigation just re-highlights.
|
|
321
|
+
*/
|
|
322
|
+
function rehighlight(root: HTMLElement, current: string): void {
|
|
323
|
+
const ACTIVE_CLASSES = [
|
|
324
|
+
"bg-sidebar-accent",
|
|
325
|
+
"font-medium",
|
|
326
|
+
"text-sidebar-accent-foreground",
|
|
327
|
+
];
|
|
328
|
+
const items = root.querySelectorAll<HTMLElement>("[data-nav-href]");
|
|
329
|
+
for (const el of Array.from(items)) {
|
|
330
|
+
const href = el.dataset.navHref;
|
|
331
|
+
const active = !!href && normalize(href) === current;
|
|
332
|
+
if (active) {
|
|
333
|
+
el.dataset.active = "true";
|
|
334
|
+
el.classList.add(...ACTIVE_CLASSES);
|
|
335
|
+
} else {
|
|
336
|
+
delete el.dataset.active;
|
|
337
|
+
el.classList.remove(...ACTIVE_CLASSES);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
// Expand ancestors of the new active item — both native <details>
|
|
341
|
+
// groups and the button-driven [data-nav-submenu] disclosures used by
|
|
342
|
+
// section-landing (href) branches.
|
|
343
|
+
const active = root.querySelector<HTMLElement>('[data-active="true"]');
|
|
344
|
+
if (active) {
|
|
345
|
+
let parent: HTMLElement | null = active.parentElement;
|
|
346
|
+
while (parent) {
|
|
347
|
+
if (parent.tagName === "DETAILS") {
|
|
348
|
+
(parent as HTMLDetailsElement).open = true;
|
|
349
|
+
}
|
|
350
|
+
if (parent.matches("[data-nav-submenu]")) {
|
|
351
|
+
parent.hidden = false;
|
|
352
|
+
const toggle = root.querySelector<HTMLElement>(
|
|
353
|
+
`[data-nav-toggle][aria-controls="${parent.id}"]`,
|
|
354
|
+
);
|
|
355
|
+
toggle?.setAttribute("aria-expanded", "true");
|
|
356
|
+
}
|
|
357
|
+
parent = parent.parentElement;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Public entry point. Called once from `<DocsNavClient />`'s inline
|
|
364
|
+
* script tag. Idempotent — subsequent calls (e.g. from view
|
|
365
|
+
* transitions) are no-ops once the tree has been rendered; they just
|
|
366
|
+
* re-highlight against the new pathname.
|
|
367
|
+
*/
|
|
368
|
+
export async function hydrateDocsNav(): Promise<void> {
|
|
369
|
+
const root = document.getElementById("docs-nav-root");
|
|
370
|
+
if (!root) return;
|
|
371
|
+
|
|
372
|
+
const navUrl = root.dataset.navUrl;
|
|
373
|
+
if (!navUrl) {
|
|
374
|
+
console.warn("docs-nav: missing data-nav-url");
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const current = normalize(root.dataset.currentPath || location.pathname);
|
|
378
|
+
const basePath = root.dataset.basePath || "";
|
|
379
|
+
const namespace = root.dataset.namespace || undefined;
|
|
380
|
+
const version = root.dataset.version || undefined;
|
|
381
|
+
const locale = root.dataset.locale || undefined;
|
|
382
|
+
|
|
383
|
+
try {
|
|
384
|
+
const nav = await fetchNav(navUrl);
|
|
385
|
+
const filtered = filterNavByAxis(nav, {
|
|
386
|
+
// Pass basePath through as-is — an empty basePath (root-served
|
|
387
|
+
// site) must stay "", matching the SSR path's `?? "/docs"`
|
|
388
|
+
// (which keeps ""). Coercing "" → "/docs" makes the filter look
|
|
389
|
+
// for `/docs/…` bucket hrefs that don't exist and blanks the
|
|
390
|
+
// whole sidebar on a root-served multi-source site.
|
|
391
|
+
basePath,
|
|
392
|
+
namespace: namespace || undefined,
|
|
393
|
+
version: version || undefined,
|
|
394
|
+
locale: locale || undefined,
|
|
395
|
+
});
|
|
396
|
+
renderTree(filtered, current, root);
|
|
397
|
+
} catch (err) {
|
|
398
|
+
console.error("docs-nav: hydration failed", err);
|
|
399
|
+
root.setAttribute("aria-busy", "false");
|
|
400
|
+
// Keep skeleton so layout doesn't collapse on failure; a real
|
|
401
|
+
// user will reload or follow the noscript link.
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Re-run on view transitions. astro:page-load fires both on initial
|
|
406
|
+
// load and after each ClientRouter transition; the module-level
|
|
407
|
+
// `navPromise` cache makes the post-transition path a fast
|
|
408
|
+
// highlight-only pass without re-fetching.
|
|
409
|
+
document.addEventListener("astro:page-load", () => {
|
|
410
|
+
const root = document.getElementById("docs-nav-root");
|
|
411
|
+
if (!root) return;
|
|
412
|
+
// If tree is already rendered (no aria-busy), just re-highlight.
|
|
413
|
+
if (root.getAttribute("aria-busy") === null) {
|
|
414
|
+
const current = normalize(root.dataset.currentPath || location.pathname);
|
|
415
|
+
rehighlight(root, current);
|
|
416
|
+
} else {
|
|
417
|
+
void hydrateDocsNav();
|
|
418
|
+
}
|
|
419
|
+
});
|
package/src/json-ld.ts
CHANGED
|
@@ -53,3 +53,115 @@ export function normalizeCustomJsonLd(
|
|
|
53
53
|
if (Array.isArray(raw)) return raw;
|
|
54
54
|
return [raw];
|
|
55
55
|
}
|
|
56
|
+
|
|
57
|
+
export interface BuildArticleJsonLdOptions {
|
|
58
|
+
/** Already-resolved Schema.org `@type` (output of jsonLdTypeFor). */
|
|
59
|
+
type: string;
|
|
60
|
+
title: string;
|
|
61
|
+
/** Site name — used as the `provider` Organization for Course. */
|
|
62
|
+
siteName: string;
|
|
63
|
+
keywords: string[];
|
|
64
|
+
description?: string;
|
|
65
|
+
image?: string;
|
|
66
|
+
url?: string;
|
|
67
|
+
/**
|
|
68
|
+
* Page headings — used to synthesize `step[]` for HowTo. Each
|
|
69
|
+
* H2 (or H3 if no H2s exist) becomes a HowToStep with a deep
|
|
70
|
+
* link to the heading id.
|
|
71
|
+
*/
|
|
72
|
+
headings?: ReadonlyArray<{ depth: number; slug: string; text: string }>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Build the JSON-LD payload for the page's primary structured-data
|
|
77
|
+
* block, shaped per `@type`. The earlier implementation emitted
|
|
78
|
+
* Article-shaped fields (`headline`) regardless of @type, which
|
|
79
|
+
* meant HowTo / Course pages failed Google's Rich Results
|
|
80
|
+
* requirements (HowTo needs `name` + `step`, Course needs `name` +
|
|
81
|
+
* `description` + `provider`). See
|
|
82
|
+
* `packages/cli/src/audit/rules/seo/json-ld-required-fields.ts`
|
|
83
|
+
* for the validator that catches this.
|
|
84
|
+
*
|
|
85
|
+
* Schema.org's `name` is a Thing-level field accepted by every
|
|
86
|
+
* @type, so we always emit it. `headline` is added on top for
|
|
87
|
+
* Article-family types (Article, TechArticle) because Google's
|
|
88
|
+
* Rich Results validator specifically requires it there. HowTo
|
|
89
|
+
* gets a synthesized `step[]` from the page's H2 headings (each
|
|
90
|
+
* H2 = one procedure step); Course gets a `provider` Organization
|
|
91
|
+
* built from `siteName`.
|
|
92
|
+
*/
|
|
93
|
+
export function buildArticleJsonLd(
|
|
94
|
+
opts: BuildArticleJsonLdOptions,
|
|
95
|
+
): Record<string, unknown> {
|
|
96
|
+
const { type, title, siteName, keywords, description, image, url, headings } =
|
|
97
|
+
opts;
|
|
98
|
+
|
|
99
|
+
const block: Record<string, unknown> = {
|
|
100
|
+
"@context": "https://schema.org",
|
|
101
|
+
"@type": type,
|
|
102
|
+
name: title,
|
|
103
|
+
};
|
|
104
|
+
// Only when there are any. An empty `keywords: ""` asserts the page
|
|
105
|
+
// has no topics, which is a different claim from staying silent.
|
|
106
|
+
if (keywords.length > 0) block.keywords = keywords.join(", ");
|
|
107
|
+
|
|
108
|
+
if (type === "Article" || type === "TechArticle") {
|
|
109
|
+
block.headline = title;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (description) block.description = description;
|
|
113
|
+
if (image) block.image = image;
|
|
114
|
+
if (url) block.url = url;
|
|
115
|
+
|
|
116
|
+
if (type === "HowTo") {
|
|
117
|
+
let stepHeadings = (headings ?? []).filter((h) => h.depth === 2);
|
|
118
|
+
if (stepHeadings.length === 0) {
|
|
119
|
+
stepHeadings = (headings ?? []).filter((h) => h.depth === 3);
|
|
120
|
+
}
|
|
121
|
+
block.step = stepHeadings.map((h) => ({
|
|
122
|
+
"@type": "HowToStep",
|
|
123
|
+
name: h.text,
|
|
124
|
+
url: url ? `${url}#${h.slug}` : `#${h.slug}`,
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (type === "Course") {
|
|
129
|
+
block.provider = { "@type": "Organization", name: siteName };
|
|
130
|
+
if (!block.description) block.description = title;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return block;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Identity structured data for a page that is not an article — a
|
|
138
|
+
* homepage, a landing page, a section index.
|
|
139
|
+
*
|
|
140
|
+
* Exists because the article block is gated on `ogType === "article"`,
|
|
141
|
+
* so those pages emitted NO structured data at all. An external agent
|
|
142
|
+
* audit reported "No JSON-LD structured data found on homepage", which
|
|
143
|
+
* was accurate: the one page most likely to be an agent's entry point
|
|
144
|
+
* was the one describing itself the least.
|
|
145
|
+
*
|
|
146
|
+
* `WebSite` rather than `Organization` or `SoftwareApplication` on
|
|
147
|
+
* purpose. Those are claims about what the site's OWNER is, and
|
|
148
|
+
* guessing wrong is worse than saying less — a docs site for a product
|
|
149
|
+
* is not itself the product. Owners wanting a stronger identity type
|
|
150
|
+
* add it through `customJsonLd`, which layers on top of this.
|
|
151
|
+
*/
|
|
152
|
+
export function buildWebSiteJsonLd(opts: {
|
|
153
|
+
siteName: string;
|
|
154
|
+
title: string;
|
|
155
|
+
description?: string;
|
|
156
|
+
url?: string;
|
|
157
|
+
}): Record<string, unknown> {
|
|
158
|
+
const block: Record<string, unknown> = {
|
|
159
|
+
"@context": "https://schema.org",
|
|
160
|
+
"@type": "WebSite",
|
|
161
|
+
name: opts.siteName,
|
|
162
|
+
};
|
|
163
|
+
if (opts.title && opts.title !== opts.siteName) block.headline = opts.title;
|
|
164
|
+
if (opts.description) block.description = opts.description;
|
|
165
|
+
if (opts.url) block.url = opts.url;
|
|
166
|
+
return block;
|
|
167
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal/external link-icon affordance — the tiny bit of logic behind
|
|
3
|
+
* the `data-link-icons` + `--db-link-icon-*` attributes DocsLayout stamps
|
|
4
|
+
* on `<body>`. Classification itself is pure CSS (by href shape); this
|
|
5
|
+
* only turns the configured glyphs into the attribute + custom-property
|
|
6
|
+
* values. See plans/link-resolution-and-icons.md.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface LinkIcons {
|
|
10
|
+
/** Glyph after external links (absolute / protocol-relative href). */
|
|
11
|
+
external?: string;
|
|
12
|
+
/** Glyph after internal links (root / relative href). */
|
|
13
|
+
internal?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface LinkIconAttrs {
|
|
17
|
+
/**
|
|
18
|
+
* Space-separated active kinds for `data-link-icons` (`"external"`,
|
|
19
|
+
* `"internal"`, or both) — undefined when the feature is off, so the
|
|
20
|
+
* attribute is omitted entirely.
|
|
21
|
+
*/
|
|
22
|
+
tokens?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Inline `style` value setting the `--db-link-icon-*` custom
|
|
25
|
+
* properties to the (single-quoted, CSS-string-safe) glyphs —
|
|
26
|
+
* undefined when nothing is active.
|
|
27
|
+
*/
|
|
28
|
+
style?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** CSS-string-escape a glyph so it's a valid single-quoted `content:` value. */
|
|
32
|
+
function cssString(glyph: string): string {
|
|
33
|
+
return glyph.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Build the `<body>` attributes for the link-icon feature. A side is
|
|
38
|
+
* active only when its glyph is a non-empty string; an absent config (or
|
|
39
|
+
* all-empty) yields `{}` so DocsLayout emits no attributes.
|
|
40
|
+
*/
|
|
41
|
+
export function linkIconAttrs(icons: LinkIcons | undefined): LinkIconAttrs {
|
|
42
|
+
const external = icons?.external?.trim() ? icons.external : "";
|
|
43
|
+
const internal = icons?.internal?.trim() ? icons.internal : "";
|
|
44
|
+
const tokens = [external ? "external" : "", internal ? "internal" : ""]
|
|
45
|
+
.filter(Boolean)
|
|
46
|
+
.join(" ");
|
|
47
|
+
const style = [
|
|
48
|
+
external ? `--db-link-icon-external: '${cssString(external)}'` : "",
|
|
49
|
+
internal ? `--db-link-icon-internal: '${cssString(internal)}'` : "",
|
|
50
|
+
]
|
|
51
|
+
.filter(Boolean)
|
|
52
|
+
.join("; ");
|
|
53
|
+
return { tokens: tokens || undefined, style: style || undefined };
|
|
54
|
+
}
|
|
@@ -7,6 +7,11 @@
|
|
|
7
7
|
* Returns the path to rewrite to (the `.md` mirror endpoint) when the
|
|
8
8
|
* request should be served as markdown; returns `null` when the normal
|
|
9
9
|
* HTML response should pass through.
|
|
10
|
+
*
|
|
11
|
+
* `basePath` is the prefix the site is SERVED at (Dogsbay's combined
|
|
12
|
+
* urlBase + basePath). It is needed only to recognise the site index,
|
|
13
|
+
* whose mirror is `<base>/index.md` rather than `<base>.md` — see
|
|
14
|
+
* `shouldRewriteToMarkdown`.
|
|
10
15
|
*/
|
|
11
16
|
|
|
12
17
|
const Q_PARAM_RE = /^\s*q\s*=\s*([0-9.]+)\s*$/i;
|
|
@@ -28,6 +33,7 @@ const Q_PARAM_RE = /^\s*q\s*=\s*([0-9.]+)\s*$/i;
|
|
|
28
33
|
export function shouldRewriteToMarkdown(
|
|
29
34
|
accept: string | null | undefined,
|
|
30
35
|
pathname: string,
|
|
36
|
+
basePath = "",
|
|
31
37
|
): string | null {
|
|
32
38
|
if (!accept) return null;
|
|
33
39
|
if (!acceptsMarkdown(accept)) return null;
|
|
@@ -35,8 +41,38 @@ export function shouldRewriteToMarkdown(
|
|
|
35
41
|
if (hasNonHtmlExtension(pathname)) return null;
|
|
36
42
|
|
|
37
43
|
const trimmed = pathname.replace(/\/$/, "");
|
|
38
|
-
const
|
|
39
|
-
|
|
44
|
+
const base = basePath.replace(/\/+$/, "");
|
|
45
|
+
|
|
46
|
+
// A request outside the served prefix is not ours to rewrite. Without
|
|
47
|
+
// this guard, `("/", "/docs")` fell through to `"" + ".md"` — a
|
|
48
|
+
// RELATIVE target, resolved against whatever the request path was.
|
|
49
|
+
if (base && trimmed !== base && !trimmed.startsWith(`${base}/`)) return null;
|
|
50
|
+
|
|
51
|
+
// The site index is emitted as `index.md.ts`, so its mirror is
|
|
52
|
+
// `<base>/index.md`. Every other page emitted by the shipped importers
|
|
53
|
+
// has its mirror at `<path>.md`: Dogsbay builds in Astro's directory
|
|
54
|
+
// format, so a leaf at `/getting-started/` maps to
|
|
55
|
+
// `/getting-started.md`, and a directory index like `guides/index.md`
|
|
56
|
+
// is NORMALIZED to slug `guides` (see import-mkdocs.ts's
|
|
57
|
+
// `.replace(/\/index$/, "")`), emitting `guides.astro` + `guides.md.ts`
|
|
58
|
+
// — so `/guides.md` exists too.
|
|
59
|
+
//
|
|
60
|
+
// Appending `.md` to the site index produced `/.md` (root-served) or
|
|
61
|
+
// `/blog.md` (mounted); neither exists, and `/blog.md` additionally
|
|
62
|
+
// falls OUTSIDE the `/blog/*` Workers route. Leaf and index URLs both
|
|
63
|
+
// carry a trailing slash, so only the base comparison distinguishes
|
|
64
|
+
// them.
|
|
65
|
+
//
|
|
66
|
+
// CAVEAT: a caller driving `exportAstroProject` directly with an
|
|
67
|
+
// unnormalized `<dir>/index` slug gets `src/pages/<dir>/index.astro`
|
|
68
|
+
// (served `/<dir>/`) whose only mirror is `/<dir>/index.md`, and this
|
|
69
|
+
// returns `/<dir>.md` — a 404. No shipped importer does that. If one
|
|
70
|
+
// ever should, generalize the sibling `.md.ts` emitter in
|
|
71
|
+
// `format-astro/src/project.ts` rather than guessing here from a URL
|
|
72
|
+
// that cannot distinguish the two shapes.
|
|
73
|
+
if (trimmed === base) return `${base}/index.md`;
|
|
74
|
+
|
|
75
|
+
return `${trimmed}.md`;
|
|
40
76
|
}
|
|
41
77
|
|
|
42
78
|
function acceptsMarkdown(accept: string): boolean {
|