@writedocs/generator 0.13.3 → 0.13.5

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.
Files changed (87) hide show
  1. package/README.md +28 -28
  2. package/astro.config.mjs +645 -645
  3. package/bin/writedocs.js +43 -12
  4. package/package.json +99 -99
  5. package/src/cli/base-path-layout.js +44 -44
  6. package/src/cli/build-auth.js +135 -135
  7. package/src/cli/build.js +203 -203
  8. package/src/cli/check-built-styles.js +41 -41
  9. package/src/cli/convert.js +1 -1
  10. package/src/cli/dev.js +268 -268
  11. package/src/cli/exit-when-flushed.js +4 -0
  12. package/src/cli/generate-api-pages.js +507 -507
  13. package/src/cli/og-image-worker.js +20 -20
  14. package/src/cli/og-images.js +70 -70
  15. package/src/cli/page-list-watch.js +69 -69
  16. package/src/cli/run-astro.js +97 -97
  17. package/src/cli/run-pagefind.js +154 -154
  18. package/src/cli/write-mcp-files.js +103 -103
  19. package/src/cli/write-redirects-file.js +158 -158
  20. package/src/components/ApiReferencePanel.astro +1792 -1792
  21. package/src/components/AppIcon.astro +51 -51
  22. package/src/components/Card.astro +193 -193
  23. package/src/components/CodeGroup.astro +63 -39
  24. package/src/components/CopyPageMenu.astro +374 -374
  25. package/src/components/Hint.astro +188 -188
  26. package/src/components/Icon.astro +72 -72
  27. package/src/components/Image.astro +165 -165
  28. package/src/components/Tile.astro +67 -67
  29. package/src/components/Video.astro +137 -137
  30. package/src/content.config.ts +203 -203
  31. package/src/layout/BaseLayout.astro +908 -908
  32. package/src/layout/components/AnalyticsScripts.astro +49 -49
  33. package/src/layout/components/Breadcrumbs.astro +98 -98
  34. package/src/layout/components/ConsentBanner.astro +119 -119
  35. package/src/layout/components/MobileMenu.astro +225 -225
  36. package/src/layout/components/NavTree.astro +408 -408
  37. package/src/layout/components/Sidebar.astro +232 -232
  38. package/src/layout/components/SiteFooter.astro +87 -87
  39. package/src/layout/components/TopBar.astro +386 -386
  40. package/src/layout/styles/base.css +332 -326
  41. package/src/layout/styles/mobile-menu.css +285 -285
  42. package/src/lib/agent-markdown.js +164 -164
  43. package/src/lib/asset-dimensions.js +89 -89
  44. package/src/lib/base-path.js +90 -90
  45. package/src/lib/cli-report.js +22 -22
  46. package/src/lib/code-line-numbers.js +13 -0
  47. package/src/lib/config-schema.js +414 -75
  48. package/src/lib/config-schema.ts +2509 -1905
  49. package/src/lib/config.ts +1316 -1314
  50. package/src/lib/consent.js +108 -108
  51. package/src/lib/content-store-reload-integration.js +101 -101
  52. package/src/lib/icon-data-dev.js +53 -53
  53. package/src/lib/image-size.js +152 -152
  54. package/src/lib/json-schema-descriptions.js +228 -228
  55. package/src/lib/link-check.js +544 -544
  56. package/src/lib/llms-index.ts +90 -90
  57. package/src/lib/mcp-dev-integration.js +65 -65
  58. package/src/lib/mcp-index.ts +102 -102
  59. package/src/lib/mintlify-convert.js +605 -605
  60. package/src/lib/moved-api-pages.js +49 -49
  61. package/src/lib/og-cards.js +33 -33
  62. package/src/lib/og-image.js +235 -235
  63. package/src/lib/openapi-dev-status.js +80 -80
  64. package/src/lib/openapi-reload-integration.js +193 -193
  65. package/src/lib/pages.js +312 -312
  66. package/src/lib/project-files-integration.js +116 -116
  67. package/src/lib/project-files.js +137 -137
  68. package/src/lib/rehype-base-path.js +62 -62
  69. package/src/lib/rehype-image-loading.js +71 -71
  70. package/src/lib/robots-txt.js +21 -21
  71. package/src/lib/search-scope.js +44 -44
  72. package/src/lib/selector-placement.js +50 -50
  73. package/src/lib/seo-tags.js +155 -155
  74. package/src/lib/shiki-code-block.js +7 -1
  75. package/src/lib/site-base.ts +27 -27
  76. package/src/lib/styles-asset-integration.js +291 -291
  77. package/src/lib/wdignore-cases.json +195 -195
  78. package/src/lib/wdignore.js +177 -177
  79. package/src/pages/404.astro +63 -63
  80. package/src/pages/[...slug].astro +1762 -1608
  81. package/src/pages/[...slug].md.ts +93 -93
  82. package/src/pages/llms-full.txt.ts +88 -88
  83. package/src/pages/og/[...slug].png.ts +63 -63
  84. package/src/scripts/mobile-menu.ts +80 -80
  85. package/src/scripts/search.ts +245 -245
  86. package/src/scripts/sidebar-scroll.ts +61 -61
  87. package/src/styles/global.css +40 -40
@@ -1,245 +1,245 @@
1
- // SearchModal.astro's own trigger button (rendered inside TopBar.astro) +
2
- // overlay/modal. Owns opening/closing the modal, the ⌘K/Ctrl K shortcut,
3
- // lazy-loading Pagefind on first open, and rendering results.
4
-
5
- // The current page's modal, for the ⌘K/Ctrl K shortcut. ClientRouter swaps
6
- // in a new modal on every navigation and initSearch() runs again for it; the
7
- // shortcut is one document listener, attached once, that always acts on
8
- // this. (Attached per page, it piled up one listener per visited page, each
9
- // still driving that page's detached modal - on a page without a modal of
10
- // its own, Ctrl K then locked the page's scrolling.)
11
- declare const __WD_BASE__: string;
12
-
13
- let active: { toggle(): void; closeIfOpen(): void } | null = null;
14
- let shortcutBound = false;
15
-
16
- export function initSearch(root: ParentNode) {
17
- // No trigger button on a `blank` page (no topbar) - the modal and its
18
- // shortcut still work there, as BaseLayout.astro intends.
19
- const trigger = root.querySelector<HTMLButtonElement>('[data-search-trigger]');
20
- const overlay = root.querySelector<HTMLElement>('#wd-search-overlay');
21
- const input = root.querySelector<HTMLInputElement>('#wd-search-input');
22
- const resultsEl = root.querySelector<HTMLElement>('#wd-search-results');
23
- if (!overlay || !input || !resultsEl) {
24
- active = null;
25
- return;
26
- }
27
- if (overlay.dataset.wdInit) return;
28
- overlay.dataset.wdInit = 'true';
29
-
30
- // Most keyboards outside macOS/iOS don't have a command key - "Ctrl K"
31
- // reads more naturally there than the ⌘ glyph.
32
- const kbd = trigger?.querySelector('.wd-search-kbd');
33
- if (kbd && !/Mac|iPhone|iPad/.test(navigator.userAgent)) {
34
- kbd.textContent = 'Ctrl K';
35
- }
36
-
37
- // Loaded lazily on first open rather than on every page load - most
38
- // visits never open search, and the module + its wasm binary are pure
39
- // overhead until someone actually wants to search. Pagefind only writes
40
- // this file as a postbuild step, after Astro's own build (and therefore
41
- // Vite's bundling) has already finished - see run-pagefind.js - so it
42
- // never exists at `writedocs dev` time, and doesn't exist yet even
43
- // during a production build's own Vite transform pass.
44
- //
45
- // `/* @vite-ignore */` alone doesn't stop Vite from trying to resolve it
46
- // anyway: that comment only suppresses static analysis for a
47
- // *non-literal* dynamic import specifier - a literal string like
48
- // '/pagefind/pagefind.js' still gets resolved at transform time
49
- // regardless of the comment, which crashes `writedocs dev` outright once
50
- // this file exists in the template. A variable alone isn't enough either:
51
- // rolldown 1.2.15 inlines it and folds `${__WD_BASE__}/pagefind/...` back
52
- // into a literal, and the build fails with "Can't find
53
- // /pagefind/pagefind.js". So the specifier is a URL made in the browser -
54
- // the same address the path resolves to, but nothing a bundler can
55
- // evaluate - deferring resolution to runtime, where the .catch() below
56
- // handles "not built yet" gracefully instead. The path itself stays a
57
- // literal in the bundle (scripts/base-path-dist.js checks it).
58
- let pagefind: any = null;
59
- let loadPromise: Promise<any> | null = null;
60
- function ensurePagefind() {
61
- if (!loadPromise) {
62
- // A page under a hidden navigation item searches its own space's index
63
- // (cli/run-pagefind.js, lib/hidden-sections.js); every other page, the site's.
64
- const space = document.querySelector<HTMLElement>('[data-wd-search-scope]')?.dataset.wdSearchSpace;
65
- // Under writedocs.json's `basePath` ("" or "/docs", astro.config.mjs) -
66
- // written into the strings so a site without one gets the same code.
67
- const pagefindUrl = space ? `${__WD_BASE__}/pagefind-spaces/${encodeURIComponent(space)}/pagefind.js` : `${__WD_BASE__}/pagefind/pagefind.js`;
68
- loadPromise = import(/* @vite-ignore */ new URL(pagefindUrl, location.href).href)
69
- .then((mod: any) => {
70
- pagefind = mod;
71
- return mod.init();
72
- })
73
- .then(() => pagefind)
74
- .catch(() => {
75
- resultsEl.innerHTML =
76
- '<div class="wd-search-empty">Search isn\'t available in this preview - run a full build to index it.</div>';
77
- return null;
78
- });
79
- }
80
- return loadPromise;
81
- }
82
-
83
- let currentResults: HTMLElement[] = [];
84
- let activeIndex = -1;
85
-
86
- // Down on the last result (or Up on the first) stays there - Enter always
87
- // has a result to open.
88
- function setActive(index: number) {
89
- currentResults.forEach((el) => el.classList.remove('wd-search-result-active'));
90
- activeIndex = currentResults.length ? Math.min(Math.max(index, 0), currentResults.length - 1) : -1;
91
- if (activeIndex >= 0) {
92
- currentResults[activeIndex].classList.add('wd-search-result-active');
93
- currentResults[activeIndex].scrollIntoView({ block: 'nearest' });
94
- }
95
- }
96
-
97
- // Search looks in the version, language and product of the page the reader
98
- // is on (plus pages outside any of them), with a switch to search
99
- // everything. [...slug].astro marks each page's scope for Pagefind and
100
- // for this - see lib/search-scope.js.
101
- let everything = false;
102
- const pageScope = () => {
103
- const el = document.querySelector<HTMLElement>('[data-wd-search-scope]');
104
- const key = el?.dataset.wdSearchScope ?? 'all';
105
- return key === 'all' ? null : { key, label: el?.dataset.wdSearchScopeLabel || key };
106
- };
107
- function scopeNote(scope: { label: string } | null, found: boolean) {
108
- if (!scope) return null;
109
- const note = document.createElement('div');
110
- note.className = 'wd-search-scope';
111
- const text = document.createElement('span');
112
- const button = document.createElement('button');
113
- button.type = 'button';
114
- button.className = 'wd-search-scope-toggle';
115
- if (everything) {
116
- text.textContent = 'Showing results from the whole site.';
117
- button.textContent = `Only ${scope.label}`;
118
- } else {
119
- text.textContent = found ? `Showing results in ${scope.label}.` : `No results in ${scope.label}.`;
120
- button.textContent = 'Search everything';
121
- }
122
- button.addEventListener('click', (e) => {
123
- e.stopPropagation();
124
- everything = !everything;
125
- input.focus();
126
- runSearch(input.value);
127
- });
128
- note.append(text, ' ', button);
129
- return note;
130
- }
131
-
132
- async function runSearch(term: string) {
133
- if (!term.trim()) {
134
- resultsEl.innerHTML = '<div class="wd-search-hint">Start typing to search...</div>';
135
- currentResults = [];
136
- activeIndex = -1;
137
- return;
138
- }
139
- const pf = await ensurePagefind();
140
- if (!pf) return;
141
- // debouncedSearch resolves to null when a newer keystroke has already
142
- // superseded this call - pagefind's own debounce, on top of (not
143
- // instead of) the plain `input` listener below, so a fast typist never
144
- // races two searches rendering out of order.
145
- const scope = pageScope();
146
- const options = scope && !everything ? { filters: { any: [{ scope: scope.key }, { scope: 'all' }] } } : {};
147
- const search = await pf.debouncedSearch(term, options, 150);
148
- if (search === null) return;
149
- if (search.results.length === 0) {
150
- resultsEl.innerHTML = '<div class="wd-search-empty">No results.</div>';
151
- const note = scopeNote(scope, false);
152
- if (note) resultsEl.replaceChildren(note);
153
- currentResults = [];
154
- activeIndex = -1;
155
- return;
156
- }
157
- const entries = await Promise.all(search.results.slice(0, 12).map((r: any) => r.data()));
158
- resultsEl.innerHTML = '';
159
- for (const data of entries) {
160
- const a = document.createElement('a');
161
- a.className = 'wd-search-result';
162
- a.href = data.url;
163
- const title = document.createElement('div');
164
- title.className = 'wd-search-result-title';
165
- title.textContent = data.meta?.title ?? data.url;
166
- // Where the page sits: "API Reference › Webhooks".
167
- const section = data.meta?.section ? document.createElement('div') : null;
168
- if (section) {
169
- section.className = 'wd-search-result-section';
170
- section.textContent = data.meta.section;
171
- }
172
- const excerpt = document.createElement('div');
173
- excerpt.className = 'wd-search-result-excerpt';
174
- // innerHTML (not textContent) is deliberate: pagefind returns this
175
- // pre-sanitized with <mark> wrapping the matched terms, which is the
176
- // whole point - a plain-text excerpt couldn't highlight anything.
177
- // The section is indexed as the page's last words, so a short page's
178
- // excerpt ends with it - shown once, on its own line, unless it's what
179
- // matched (then the excerpt keeps it, highlighted).
180
- const trailing = data.meta?.section ? ` ${data.meta.section}.` : null;
181
- excerpt.innerHTML = trailing && data.excerpt.endsWith(trailing) ? data.excerpt.slice(0, -trailing.length) : data.excerpt;
182
- a.append(title, ...(section ? [section] : []), excerpt);
183
- resultsEl.appendChild(a);
184
- }
185
- const note = scopeNote(scope, true);
186
- if (note) resultsEl.appendChild(note);
187
- currentResults = Array.from(resultsEl.querySelectorAll('.wd-search-result'));
188
- setActive(0);
189
- }
190
-
191
- function open() {
192
- overlay.hidden = false;
193
- document.body.style.overflow = 'hidden';
194
- input.value = '';
195
- everything = false;
196
- resultsEl.innerHTML = '<div class="wd-search-hint">Start typing to search...</div>';
197
- currentResults = [];
198
- activeIndex = -1;
199
- ensurePagefind();
200
- input.focus();
201
- }
202
-
203
- function close() {
204
- overlay.hidden = true;
205
- document.body.style.overflow = '';
206
- trigger?.focus();
207
- }
208
-
209
- trigger?.addEventListener('click', open);
210
- overlay.addEventListener('click', (e) => {
211
- if (e.target === overlay) close();
212
- });
213
- input.addEventListener('input', () => runSearch(input.value));
214
- input.addEventListener('keydown', (e) => {
215
- if (e.key === 'ArrowDown') {
216
- e.preventDefault();
217
- setActive(activeIndex + 1);
218
- } else if (e.key === 'ArrowUp') {
219
- e.preventDefault();
220
- setActive(activeIndex - 1);
221
- } else if (e.key === 'Enter' && activeIndex >= 0) {
222
- currentResults[activeIndex]?.click();
223
- } else if (e.key === 'Escape') {
224
- close();
225
- }
226
- });
227
- active = {
228
- toggle: () => (overlay.hidden ? open() : close()),
229
- closeIfOpen: () => {
230
- if (!overlay.hidden) close();
231
- },
232
- };
233
- if (!shortcutBound) {
234
- shortcutBound = true;
235
- document.addEventListener('keydown', (e) => {
236
- if (!active) return;
237
- if ((e.key === 'k' || e.key === 'K') && (e.metaKey || e.ctrlKey)) {
238
- e.preventDefault();
239
- active.toggle();
240
- } else if (e.key === 'Escape') {
241
- active.closeIfOpen();
242
- }
243
- });
244
- }
245
- }
1
+ // SearchModal.astro's own trigger button (rendered inside TopBar.astro) +
2
+ // overlay/modal. Owns opening/closing the modal, the ⌘K/Ctrl K shortcut,
3
+ // lazy-loading Pagefind on first open, and rendering results.
4
+
5
+ // The current page's modal, for the ⌘K/Ctrl K shortcut. ClientRouter swaps
6
+ // in a new modal on every navigation and initSearch() runs again for it; the
7
+ // shortcut is one document listener, attached once, that always acts on
8
+ // this. (Attached per page, it piled up one listener per visited page, each
9
+ // still driving that page's detached modal - on a page without a modal of
10
+ // its own, Ctrl K then locked the page's scrolling.)
11
+ declare const __WD_BASE__: string;
12
+
13
+ let active: { toggle(): void; closeIfOpen(): void } | null = null;
14
+ let shortcutBound = false;
15
+
16
+ export function initSearch(root: ParentNode) {
17
+ // No trigger button on a `blank` page (no topbar) - the modal and its
18
+ // shortcut still work there, as BaseLayout.astro intends.
19
+ const trigger = root.querySelector<HTMLButtonElement>('[data-search-trigger]');
20
+ const overlay = root.querySelector<HTMLElement>('#wd-search-overlay');
21
+ const input = root.querySelector<HTMLInputElement>('#wd-search-input');
22
+ const resultsEl = root.querySelector<HTMLElement>('#wd-search-results');
23
+ if (!overlay || !input || !resultsEl) {
24
+ active = null;
25
+ return;
26
+ }
27
+ if (overlay.dataset.wdInit) return;
28
+ overlay.dataset.wdInit = 'true';
29
+
30
+ // Most keyboards outside macOS/iOS don't have a command key - "Ctrl K"
31
+ // reads more naturally there than the ⌘ glyph.
32
+ const kbd = trigger?.querySelector('.wd-search-kbd');
33
+ if (kbd && !/Mac|iPhone|iPad/.test(navigator.userAgent)) {
34
+ kbd.textContent = 'Ctrl K';
35
+ }
36
+
37
+ // Loaded lazily on first open rather than on every page load - most
38
+ // visits never open search, and the module + its wasm binary are pure
39
+ // overhead until someone actually wants to search. Pagefind only writes
40
+ // this file as a postbuild step, after Astro's own build (and therefore
41
+ // Vite's bundling) has already finished - see run-pagefind.js - so it
42
+ // never exists at `writedocs dev` time, and doesn't exist yet even
43
+ // during a production build's own Vite transform pass.
44
+ //
45
+ // `/* @vite-ignore */` alone doesn't stop Vite from trying to resolve it
46
+ // anyway: that comment only suppresses static analysis for a
47
+ // *non-literal* dynamic import specifier - a literal string like
48
+ // '/pagefind/pagefind.js' still gets resolved at transform time
49
+ // regardless of the comment, which crashes `writedocs dev` outright once
50
+ // this file exists in the template. A variable alone isn't enough either:
51
+ // rolldown 1.2.15 inlines it and folds `${__WD_BASE__}/pagefind/...` back
52
+ // into a literal, and the build fails with "Can't find
53
+ // /pagefind/pagefind.js". So the specifier is a URL made in the browser -
54
+ // the same address the path resolves to, but nothing a bundler can
55
+ // evaluate - deferring resolution to runtime, where the .catch() below
56
+ // handles "not built yet" gracefully instead. The path itself stays a
57
+ // literal in the bundle (scripts/base-path-dist.js checks it).
58
+ let pagefind: any = null;
59
+ let loadPromise: Promise<any> | null = null;
60
+ function ensurePagefind() {
61
+ if (!loadPromise) {
62
+ // A page under a hidden navigation item searches its own space's index
63
+ // (cli/run-pagefind.js, lib/hidden-sections.js); every other page, the site's.
64
+ const space = document.querySelector<HTMLElement>('[data-wd-search-scope]')?.dataset.wdSearchSpace;
65
+ // Under writedocs.json's `basePath` ("" or "/docs", astro.config.mjs) -
66
+ // written into the strings so a site without one gets the same code.
67
+ const pagefindUrl = space ? `${__WD_BASE__}/pagefind-spaces/${encodeURIComponent(space)}/pagefind.js` : `${__WD_BASE__}/pagefind/pagefind.js`;
68
+ loadPromise = import(/* @vite-ignore */ new URL(pagefindUrl, location.href).href)
69
+ .then((mod: any) => {
70
+ pagefind = mod;
71
+ return mod.init();
72
+ })
73
+ .then(() => pagefind)
74
+ .catch(() => {
75
+ resultsEl.innerHTML =
76
+ '<div class="wd-search-empty">Search isn\'t available in this preview - run a full build to index it.</div>';
77
+ return null;
78
+ });
79
+ }
80
+ return loadPromise;
81
+ }
82
+
83
+ let currentResults: HTMLElement[] = [];
84
+ let activeIndex = -1;
85
+
86
+ // Down on the last result (or Up on the first) stays there - Enter always
87
+ // has a result to open.
88
+ function setActive(index: number) {
89
+ currentResults.forEach((el) => el.classList.remove('wd-search-result-active'));
90
+ activeIndex = currentResults.length ? Math.min(Math.max(index, 0), currentResults.length - 1) : -1;
91
+ if (activeIndex >= 0) {
92
+ currentResults[activeIndex].classList.add('wd-search-result-active');
93
+ currentResults[activeIndex].scrollIntoView({ block: 'nearest' });
94
+ }
95
+ }
96
+
97
+ // Search looks in the version, language and product of the page the reader
98
+ // is on (plus pages outside any of them), with a switch to search
99
+ // everything. [...slug].astro marks each page's scope for Pagefind and
100
+ // for this - see lib/search-scope.js.
101
+ let everything = false;
102
+ const pageScope = () => {
103
+ const el = document.querySelector<HTMLElement>('[data-wd-search-scope]');
104
+ const key = el?.dataset.wdSearchScope ?? 'all';
105
+ return key === 'all' ? null : { key, label: el?.dataset.wdSearchScopeLabel || key };
106
+ };
107
+ function scopeNote(scope: { label: string } | null, found: boolean) {
108
+ if (!scope) return null;
109
+ const note = document.createElement('div');
110
+ note.className = 'wd-search-scope';
111
+ const text = document.createElement('span');
112
+ const button = document.createElement('button');
113
+ button.type = 'button';
114
+ button.className = 'wd-search-scope-toggle';
115
+ if (everything) {
116
+ text.textContent = 'Showing results from the whole site.';
117
+ button.textContent = `Only ${scope.label}`;
118
+ } else {
119
+ text.textContent = found ? `Showing results in ${scope.label}.` : `No results in ${scope.label}.`;
120
+ button.textContent = 'Search everything';
121
+ }
122
+ button.addEventListener('click', (e) => {
123
+ e.stopPropagation();
124
+ everything = !everything;
125
+ input.focus();
126
+ runSearch(input.value);
127
+ });
128
+ note.append(text, ' ', button);
129
+ return note;
130
+ }
131
+
132
+ async function runSearch(term: string) {
133
+ if (!term.trim()) {
134
+ resultsEl.innerHTML = '<div class="wd-search-hint">Start typing to search...</div>';
135
+ currentResults = [];
136
+ activeIndex = -1;
137
+ return;
138
+ }
139
+ const pf = await ensurePagefind();
140
+ if (!pf) return;
141
+ // debouncedSearch resolves to null when a newer keystroke has already
142
+ // superseded this call - pagefind's own debounce, on top of (not
143
+ // instead of) the plain `input` listener below, so a fast typist never
144
+ // races two searches rendering out of order.
145
+ const scope = pageScope();
146
+ const options = scope && !everything ? { filters: { any: [{ scope: scope.key }, { scope: 'all' }] } } : {};
147
+ const search = await pf.debouncedSearch(term, options, 150);
148
+ if (search === null) return;
149
+ if (search.results.length === 0) {
150
+ resultsEl.innerHTML = '<div class="wd-search-empty">No results.</div>';
151
+ const note = scopeNote(scope, false);
152
+ if (note) resultsEl.replaceChildren(note);
153
+ currentResults = [];
154
+ activeIndex = -1;
155
+ return;
156
+ }
157
+ const entries = await Promise.all(search.results.slice(0, 12).map((r: any) => r.data()));
158
+ resultsEl.innerHTML = '';
159
+ for (const data of entries) {
160
+ const a = document.createElement('a');
161
+ a.className = 'wd-search-result';
162
+ a.href = data.url;
163
+ const title = document.createElement('div');
164
+ title.className = 'wd-search-result-title';
165
+ title.textContent = data.meta?.title ?? data.url;
166
+ // Where the page sits: "API Reference › Webhooks".
167
+ const section = data.meta?.section ? document.createElement('div') : null;
168
+ if (section) {
169
+ section.className = 'wd-search-result-section';
170
+ section.textContent = data.meta.section;
171
+ }
172
+ const excerpt = document.createElement('div');
173
+ excerpt.className = 'wd-search-result-excerpt';
174
+ // innerHTML (not textContent) is deliberate: pagefind returns this
175
+ // pre-sanitized with <mark> wrapping the matched terms, which is the
176
+ // whole point - a plain-text excerpt couldn't highlight anything.
177
+ // The section is indexed as the page's last words, so a short page's
178
+ // excerpt ends with it - shown once, on its own line, unless it's what
179
+ // matched (then the excerpt keeps it, highlighted).
180
+ const trailing = data.meta?.section ? ` ${data.meta.section}.` : null;
181
+ excerpt.innerHTML = trailing && data.excerpt.endsWith(trailing) ? data.excerpt.slice(0, -trailing.length) : data.excerpt;
182
+ a.append(title, ...(section ? [section] : []), excerpt);
183
+ resultsEl.appendChild(a);
184
+ }
185
+ const note = scopeNote(scope, true);
186
+ if (note) resultsEl.appendChild(note);
187
+ currentResults = Array.from(resultsEl.querySelectorAll('.wd-search-result'));
188
+ setActive(0);
189
+ }
190
+
191
+ function open() {
192
+ overlay.hidden = false;
193
+ document.body.style.overflow = 'hidden';
194
+ input.value = '';
195
+ everything = false;
196
+ resultsEl.innerHTML = '<div class="wd-search-hint">Start typing to search...</div>';
197
+ currentResults = [];
198
+ activeIndex = -1;
199
+ ensurePagefind();
200
+ input.focus();
201
+ }
202
+
203
+ function close() {
204
+ overlay.hidden = true;
205
+ document.body.style.overflow = '';
206
+ trigger?.focus();
207
+ }
208
+
209
+ trigger?.addEventListener('click', open);
210
+ overlay.addEventListener('click', (e) => {
211
+ if (e.target === overlay) close();
212
+ });
213
+ input.addEventListener('input', () => runSearch(input.value));
214
+ input.addEventListener('keydown', (e) => {
215
+ if (e.key === 'ArrowDown') {
216
+ e.preventDefault();
217
+ setActive(activeIndex + 1);
218
+ } else if (e.key === 'ArrowUp') {
219
+ e.preventDefault();
220
+ setActive(activeIndex - 1);
221
+ } else if (e.key === 'Enter' && activeIndex >= 0) {
222
+ currentResults[activeIndex]?.click();
223
+ } else if (e.key === 'Escape') {
224
+ close();
225
+ }
226
+ });
227
+ active = {
228
+ toggle: () => (overlay.hidden ? open() : close()),
229
+ closeIfOpen: () => {
230
+ if (!overlay.hidden) close();
231
+ },
232
+ };
233
+ if (!shortcutBound) {
234
+ shortcutBound = true;
235
+ document.addEventListener('keydown', (e) => {
236
+ if (!active) return;
237
+ if ((e.key === 'k' || e.key === 'K') && (e.metaKey || e.ctrlKey)) {
238
+ e.preventDefault();
239
+ active.toggle();
240
+ } else if (e.key === 'Escape') {
241
+ active.closeIfOpen();
242
+ }
243
+ });
244
+ }
245
+ }
@@ -1,61 +1,61 @@
1
- // ClientRouter swaps <body> (and everything in it) on every navigation -
2
- // [...slug].astro re-renders Sidebar.astro/NavTree.astro fresh per page,
3
- // since the active-link highlight and each collapsible group's open/closed
4
- // state are computed server-side from the *new* URL (see NavTree.astro's
5
- // own comment on `open = isActive || navTreeContainsSlug(...)`). That's
6
- // exactly the behavior a reader wants for highlighting/auto-expanding the
7
- // right branch - but it also means .wd-sidebar is a brand-new DOM node on
8
- // every navigation, with its own independent scroll position (it's
9
- // `overflow-y: auto`, not the window) starting back at 0, same as a full
10
- // page reload would. For a page nested deep in a long nav tree, that reads
11
- // as the sidebar "jumping back to the top" on every click - disorienting,
12
- // since the reader loses their place in the tree they were navigating.
13
- // This module carries the old sidebar's scroll offset across the swap and
14
- // re-applies it to the new one, without touching anything about how the
15
- // highlight/expand state itself is computed (that server-rendered-per-page
16
- // behavior stays exactly as it is - only the scroll position is preserved).
17
- //
18
- // A module-scoped variable (not sessionStorage) is enough: this script's
19
- // own module graph is loaded once and never re-evaluated by a ClientRouter
20
- // swap (only <body>'s content is replaced), so the value naturally
21
- // survives from one navigation to the next without needing to serialize it
22
- // anywhere. It deliberately does NOT survive a real full-page reload
23
- // (Cmd+R/F5) - resetting to the top on an actual reload is ordinary,
24
- // expected browser behavior, not something this needs to fight.
25
- let savedScrollTop: number | null = null;
26
-
27
- /** The page's one `.wd-sidebar` - in `.wd-sidebar-col` on desktop, or
28
- * moved into MobileMenu.astro's drawer once that has been opened at phone
29
- * width (src/scripts/mobile-menu.ts) - when it has a real layout box: in
30
- * the column at phone width it's hidden, and there's no scroll position to
31
- * carry. Returns null then, and on a page with no sidebar at all (`mode:
32
- * custom`/`blank`, see BaseLayout.astro). */
33
- function visibleSidebar(): HTMLElement | null {
34
- const candidates = document.querySelectorAll<HTMLElement>('.wd-sidebar');
35
- for (const el of candidates) {
36
- if (el.offsetWidth > 0 || el.offsetHeight > 0) return el;
37
- }
38
- return null;
39
- }
40
-
41
- /** Reads the current scroll offset right before ClientRouter tears down
42
- * the old page's DOM - `astro:before-swap` is the last moment the old
43
- * `.wd-sidebar` is still attached and its `scrollTop` still means
44
- * anything. */
45
- export function saveSidebarScroll() {
46
- const sidebar = visibleSidebar();
47
- savedScrollTop = sidebar ? sidebar.scrollTop : null;
48
- }
49
-
50
- /** Re-applies the saved offset onto the new page's own `.wd-sidebar`.
51
- * Bound to `astro:after-swap` (fires synchronously right after the swap,
52
- * before the browser's next paint - see BaseLayout.astro's own
53
- * `wdApplyStoredPreferences` for the identical timing rationale) rather
54
- * than the later `astro:page-load`, so the sidebar never visibly flashes
55
- * at the top before jumping to the preserved position. */
56
- export function restoreSidebarScroll() {
57
- if (savedScrollTop === null) return;
58
- const sidebar = visibleSidebar();
59
- if (sidebar) sidebar.scrollTop = savedScrollTop;
60
- savedScrollTop = null;
61
- }
1
+ // ClientRouter swaps <body> (and everything in it) on every navigation -
2
+ // [...slug].astro re-renders Sidebar.astro/NavTree.astro fresh per page,
3
+ // since the active-link highlight and each collapsible group's open/closed
4
+ // state are computed server-side from the *new* URL (see NavTree.astro's
5
+ // own comment on `open = isActive || navTreeContainsSlug(...)`). That's
6
+ // exactly the behavior a reader wants for highlighting/auto-expanding the
7
+ // right branch - but it also means .wd-sidebar is a brand-new DOM node on
8
+ // every navigation, with its own independent scroll position (it's
9
+ // `overflow-y: auto`, not the window) starting back at 0, same as a full
10
+ // page reload would. For a page nested deep in a long nav tree, that reads
11
+ // as the sidebar "jumping back to the top" on every click - disorienting,
12
+ // since the reader loses their place in the tree they were navigating.
13
+ // This module carries the old sidebar's scroll offset across the swap and
14
+ // re-applies it to the new one, without touching anything about how the
15
+ // highlight/expand state itself is computed (that server-rendered-per-page
16
+ // behavior stays exactly as it is - only the scroll position is preserved).
17
+ //
18
+ // A module-scoped variable (not sessionStorage) is enough: this script's
19
+ // own module graph is loaded once and never re-evaluated by a ClientRouter
20
+ // swap (only <body>'s content is replaced), so the value naturally
21
+ // survives from one navigation to the next without needing to serialize it
22
+ // anywhere. It deliberately does NOT survive a real full-page reload
23
+ // (Cmd+R/F5) - resetting to the top on an actual reload is ordinary,
24
+ // expected browser behavior, not something this needs to fight.
25
+ let savedScrollTop: number | null = null;
26
+
27
+ /** The page's one `.wd-sidebar` - in `.wd-sidebar-col` on desktop, or
28
+ * moved into MobileMenu.astro's drawer once that has been opened at phone
29
+ * width (src/scripts/mobile-menu.ts) - when it has a real layout box: in
30
+ * the column at phone width it's hidden, and there's no scroll position to
31
+ * carry. Returns null then, and on a page with no sidebar at all (`mode:
32
+ * custom`/`blank`, see BaseLayout.astro). */
33
+ function visibleSidebar(): HTMLElement | null {
34
+ const candidates = document.querySelectorAll<HTMLElement>('.wd-sidebar');
35
+ for (const el of candidates) {
36
+ if (el.offsetWidth > 0 || el.offsetHeight > 0) return el;
37
+ }
38
+ return null;
39
+ }
40
+
41
+ /** Reads the current scroll offset right before ClientRouter tears down
42
+ * the old page's DOM - `astro:before-swap` is the last moment the old
43
+ * `.wd-sidebar` is still attached and its `scrollTop` still means
44
+ * anything. */
45
+ export function saveSidebarScroll() {
46
+ const sidebar = visibleSidebar();
47
+ savedScrollTop = sidebar ? sidebar.scrollTop : null;
48
+ }
49
+
50
+ /** Re-applies the saved offset onto the new page's own `.wd-sidebar`.
51
+ * Bound to `astro:after-swap` (fires synchronously right after the swap,
52
+ * before the browser's next paint - see BaseLayout.astro's own
53
+ * `wdApplyStoredPreferences` for the identical timing rationale) rather
54
+ * than the later `astro:page-load`, so the sidebar never visibly flashes
55
+ * at the top before jumping to the preserved position. */
56
+ export function restoreSidebarScroll() {
57
+ if (savedScrollTop === null) return;
58
+ const sidebar = visibleSidebar();
59
+ if (sidebar) sidebar.scrollTop = savedScrollTop;
60
+ savedScrollTop = null;
61
+ }