@writedocs/generator 0.13.4 → 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 (86) hide show
  1. package/README.md +28 -28
  2. package/astro.config.mjs +645 -645
  3. package/bin/writedocs.js +356 -356
  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 +124 -124
  10. package/src/cli/dev.js +268 -268
  11. package/src/cli/exit-when-flushed.js +42 -42
  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.ts +2509 -2509
  48. package/src/lib/config.ts +1316 -1316
  49. package/src/lib/consent.js +108 -108
  50. package/src/lib/content-store-reload-integration.js +101 -101
  51. package/src/lib/icon-data-dev.js +53 -53
  52. package/src/lib/image-size.js +152 -152
  53. package/src/lib/json-schema-descriptions.js +228 -228
  54. package/src/lib/link-check.js +544 -544
  55. package/src/lib/llms-index.ts +90 -90
  56. package/src/lib/mcp-dev-integration.js +65 -65
  57. package/src/lib/mcp-index.ts +102 -102
  58. package/src/lib/mintlify-convert.js +605 -605
  59. package/src/lib/moved-api-pages.js +49 -49
  60. package/src/lib/og-cards.js +33 -33
  61. package/src/lib/og-image.js +235 -235
  62. package/src/lib/openapi-dev-status.js +80 -80
  63. package/src/lib/openapi-reload-integration.js +193 -193
  64. package/src/lib/pages.js +312 -312
  65. package/src/lib/project-files-integration.js +116 -116
  66. package/src/lib/project-files.js +137 -137
  67. package/src/lib/rehype-base-path.js +62 -62
  68. package/src/lib/rehype-image-loading.js +71 -71
  69. package/src/lib/robots-txt.js +21 -21
  70. package/src/lib/search-scope.js +44 -44
  71. package/src/lib/selector-placement.js +50 -50
  72. package/src/lib/seo-tags.js +155 -155
  73. package/src/lib/shiki-code-block.js +7 -1
  74. package/src/lib/site-base.ts +27 -27
  75. package/src/lib/styles-asset-integration.js +291 -291
  76. package/src/lib/wdignore-cases.json +195 -195
  77. package/src/lib/wdignore.js +177 -177
  78. package/src/pages/404.astro +63 -63
  79. package/src/pages/[...slug].astro +1762 -1608
  80. package/src/pages/[...slug].md.ts +93 -93
  81. package/src/pages/llms-full.txt.ts +88 -88
  82. package/src/pages/og/[...slug].png.ts +63 -63
  83. package/src/scripts/mobile-menu.ts +80 -80
  84. package/src/scripts/search.ts +245 -245
  85. package/src/scripts/sidebar-scroll.ts +61 -61
  86. package/src/styles/global.css +40 -40
package/src/lib/pages.js CHANGED
@@ -1,312 +1,312 @@
1
- // Which files in a content directory are pages. Plain JavaScript, not
2
- // TypeScript, so `writedocs validate` can load it with plain Node (Node
3
- // refuses to strip types under node_modules - see lib/icons.js's comment);
4
- // lib/config.ts re-exports findAllPages() for everything inside Astro.
5
- import fs from 'node:fs';
6
- import path from 'node:path';
7
- import matter from 'gray-matter';
8
- import { readConfigText } from './config-file.js';
9
- import { oncePerBuild } from './build-memo.js';
10
- import { projectIgnore } from './project-ignore.js';
11
-
12
- // Top-level folders of a content directory that are never scanned for
13
- // pages - build output, dependencies, and static assets. Any folder whose
14
- // name starts with a dot (.git, .github, .claude, .mintlify, ...) is
15
- // skipped too, at any depth - see findAllPages().
16
- export const EXCLUDED_TOP_LEVEL_DIRS = new Set(['node_modules', 'dist', '.astro', '.writedocs', 'public', '.git']);
17
-
18
- /** A page's title when its frontmatter has none - Mintlify's rule, so a
19
- * migrated page that relied on it keeps the same title: the file name
20
- * without its extension, dashes and underscores as spaces, first letter
21
- * capitalized. `guides/getting-started.mdx` -> "Getting started". A
22
- * folder's index page is named for the folder (`api/index.mdx` -> "Api"),
23
- * and the site's own index.mdx is "Home" - "Index" names neither. */
24
- export function titleFromPath(filePath) {
25
- const parts = String(filePath).split(/[\\/]/).filter(Boolean);
26
- let base = (parts.pop() ?? '').replace(/\.mdx?$/i, '');
27
- if (base.toLowerCase() === 'index') {
28
- if (parts.length === 0) return 'Home';
29
- base = parts.pop();
30
- }
31
- const words = base.replace(/[-_]+/g, ' ').trim();
32
- return words ? words.charAt(0).toUpperCase() + words.slice(1) : 'Untitled';
33
- }
34
-
35
- /** Mintlify's .mintignore, if the content directory has one: gitignore-
36
- * style patterns for files and folders that aren't published. Supports
37
- * comments, blank lines, `*`, `**`, `?`, a leading `/` (anchored to the
38
- * content directory) and a trailing `/` (folders only); negation (`!`)
39
- * isn't supported and such lines are skipped. Returns (relPath, isDir) =>
40
- * boolean. */
41
- function readIgnoreFile(contentDir) {
42
- let text;
43
- try {
44
- text = fs.readFileSync(path.join(contentDir, '.mintignore'), 'utf-8');
45
- } catch {
46
- return () => false;
47
- }
48
- const rules = text
49
- .split(/\r?\n/)
50
- .map((l) => l.trim())
51
- .filter((l) => l && !l.startsWith('#') && !l.startsWith('!'))
52
- .map((pattern) => {
53
- const dirOnly = pattern.endsWith('/');
54
- let p = pattern.replace(/\/+$/, '');
55
- const anchored = p.startsWith('/') || p.includes('/');
56
- p = p.replace(/^\/+/, '');
57
- const source = p
58
- .split(/(\*\*\/?|\*|\?)/)
59
- .map((part) =>
60
- part === '**/' || part === '**' ? '(?:.*/)?' : part === '*' ? '[^/]*' : part === '?' ? '[^/]' : part.replace(/[.+^${}()|[\]\\]/g, '\\$&')
61
- )
62
- .join('');
63
- const re = new RegExp(anchored ? `^${source}$` : `(?:^|/)${source}$`);
64
- return { re, dirOnly };
65
- });
66
- return (rel, isDir) => rules.some((r) => (!r.dirOnly || isDir) && r.re.test(rel));
67
- }
68
-
69
- /** Page ids writedocs.json's navigation lists (strings in arrays, and
70
- * groups' `page`) - read straight from the file, since this runs before
71
- * (and independently of) config validation. Empty when there's no
72
- * readable writedocs.json. */
73
- /** The page id a navigation entry names. "guides/index" and "guides" are
74
- * the same page: a folder's index.mdx has the folder's id (fileIdForPath()
75
- * below), and Mintlify projects write it either way. The root "index"
76
- * stays as it is. */
77
- export function navPageId(id) {
78
- return id.endsWith('/index') ? id.slice(0, -'/index'.length) : id;
79
- }
80
-
81
- /** Every page id a `navigation` lists, in the order a reader meets them -
82
- * top to bottom, each group's own `page` before its `pages` - without
83
- * repeats. */
84
- export function navigationPageOrder(navigation) {
85
- const ids = new Set();
86
- (function walk(node) {
87
- if (Array.isArray(node)) node.forEach((item) => (typeof item === 'string' ? ids.add(navPageId(item)) : walk(item)));
88
- else if (node && typeof node === 'object') {
89
- if (typeof node.page === 'string') ids.add(navPageId(node.page));
90
- for (const [key, value] of Object.entries(node)) {
91
- if (key !== 'page' && key !== 'openapi' && key !== 'href') walk(value);
92
- }
93
- }
94
- })(navigation);
95
- return [...ids];
96
- }
97
-
98
- function navigationPageIds(contentDir) {
99
- let config;
100
- try {
101
- config = JSON.parse(readConfigText(contentDir));
102
- } catch {
103
- return new Set();
104
- }
105
- return new Set(navigationPageOrder(config.navigation));
106
- }
107
-
108
- /** Recursively finds every .md/.mdx file under `contentDir` that is a page:
109
- * one with a frontmatter block, or one writedocs.json's navigation lists
110
- * (a Mintlify page needs no frontmatter at all - see titleFromPath()).
111
- * Skips EXCLUDED_TOP_LEVEL_DIRS, every dot-folder at any depth, whatever
112
- * the project's ignore rules leave out (lib/wdignore.js: the default
113
- * exclusions and its `.wdignore`) and anything a .mintignore file lists. docs/ is scanned exactly like any
114
- * other folder, no special-casing. Returns POSIX-relative paths (from
115
- * `contentDir`) suitable to hand straight to Astro's `glob()` loader as a
116
- * literal `pattern` array - see content.config.ts's `pages` collection,
117
- * and [...slug].astro, which needs the identical list to decide whether
118
- * calling `getCollection('pages')` is worth doing at all. Also reused by
119
- * astro.config.mjs's noindex/sitemap scan and by `writedocs validate`, so
120
- * all of them see exactly the same file set. Synchronous. In a build the
121
- * project is scanned once and every later call gets that list
122
- * (lib/build-memo.js) - a page that scanned it again made the build N x N;
123
- * everywhere else (`writedocs dev`, `validate`) every call scans afresh,
124
- * so a page added or removed is seen. */
125
- export function findAllPages(contentDir) {
126
- // A copy, so a caller that changes its list doesn't change everyone's.
127
- return oncePerBuild(`pages:${contentDir}`, () => scanAllPages(contentDir)).slice();
128
- }
129
-
130
- function scanAllPages(contentDir) {
131
- const results = [];
132
- const ignored = readIgnoreFile(contentDir);
133
- const rules = projectIgnore(contentDir);
134
- const listed = navigationPageIds(contentDir);
135
- function walk(dir, relBase) {
136
- let entries;
137
- try {
138
- entries = fs.readdirSync(dir, { withFileTypes: true });
139
- } catch {
140
- return;
141
- }
142
- for (const entry of entries) {
143
- const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
144
- const abs = path.join(dir, entry.name);
145
- if (entry.isDirectory()) {
146
- if (relBase === '' && EXCLUDED_TOP_LEVEL_DIRS.has(entry.name)) continue;
147
- if (entry.name.startsWith('.')) continue;
148
- if (ignored(rel, true) || rules.ignores(rel, true)) continue;
149
- walk(abs, rel);
150
- continue;
151
- }
152
- if (!entry.isFile() || !/\.mdx?$/i.test(entry.name)) continue;
153
- if (ignored(rel, false) || rules.ignores(rel)) continue;
154
- let raw;
155
- try {
156
- raw = fs.readFileSync(abs, 'utf-8');
157
- } catch {
158
- continue;
159
- }
160
- let data;
161
- try {
162
- ({ data } = matter(raw));
163
- } catch {
164
- // Frontmatter that isn't valid YAML: still a page (it opens a
165
- // frontmatter block), so the error surfaces with this file's name -
166
- // from `writedocs validate`, or Astro's own loader in the build -
167
- // instead of as a bare YAML error thrown from here.
168
- if (/^---\r?\n/.test(raw)) results.push(rel);
169
- continue;
170
- }
171
- if (Object.keys(data).length === 0 && !listed.has(fileIdForPath(rel))) continue; // not a page
172
- results.push(rel);
173
- }
174
- }
175
- walk(contentDir, '');
176
- return results;
177
- }
178
-
179
- /** The id writedocs.json's navigation refers to a page by: its path from
180
- * the content directory, without the extension or a trailing `/index`
181
- * (same algorithm as fileIdForEntry() in lib/config.ts). */
182
- export function fileIdForPath(relativePath) {
183
- return relativePath.replace(/\.mdx?$/i, '').replace(/\/index$/, '');
184
- }
185
-
186
- /** Any `.css`/`.js` file anywhere in the project - root or any subfolder,
187
- * `public/` included - auto-loaded site-wide with zero `writedocs.json`
188
- * config, on top of (not instead of) the explicit `scripts` field
189
- * (bannerSchema and friends, above). Drop a file in, it loads;
190
- * there's no field naming which ones to use, matching the same "just
191
- * works" convention `docs/`'s own file discovery already follows (see
192
- * findAllPages() above / `content-pipeline.mdx`) - a site author already
193
- * drops content files in and expects them found, rather than also
194
- * listing every one in writedocs.json.
195
- *
196
- * Two separate walks, because `public/` needs different treatment than
197
- * everywhere else:
198
- *
199
- * - `css`/`js`: every `.css`/`.js` file outside `public/` (root, `docs/`,
200
- * `snippets/`, any custom folder) - reused as the walk-with-exclusions
201
- * shape from findAllPages() above, and its exact `EXCLUDED_TOP_LEVEL_DIRS`
202
- * set (skipped only at the project root, same as there), so `dist/`,
203
- * `node_modules/`, `.astro/`, `.writedocs/`, `.git/`, and (for this
204
- * half only - see below) `public/` are never walked into. BaseLayout.astro
205
- * reads each one's raw content and inlines it as a `<style>`/
206
- * `<script is:inline>` tag.
207
- * - `publicCss`/`publicJs`: every `.css`/`.js` file *inside* `public/`,
208
- * walked separately (starting from `<contentDir>/public` rather than
209
- * `contentDir` itself, so the same `EXCLUDED_TOP_LEVEL_DIRS` check
210
- * doesn't apply here - there's no `public/public/` or `public/dist/`
211
- * convention to guard against). Returned as public-URL-rooted hrefs
212
- * (a leading `/`, no `public` segment - `public/custom.css` becomes
213
- * `/custom.css`) rather than content-dir-relative paths, since these
214
- * files are already served as static assets at exactly that URL once
215
- * Astro copies `public/` into the build output. BaseLayout.astro
216
- * renders these as ordinary `<link rel="stylesheet">`/`<script src>`
217
- * tags pointing at that URL instead of inlining their content -
218
- * inlining would duplicate every byte (once in the page's own HTML,
219
- * once more as the independently-fetchable static file at that same
220
- * URL) for no benefit, where a `<link>`/`<script src>` gets normal
221
- * browser caching across pages instead of repeating the content on
222
- * every single page's markup.
223
- *
224
- * Both halves are broader than they might sound - a stray `.js` file
225
- * kept in `docs/`, `snippets/`, or `public/` for an unrelated reason
226
- * (a snippet's own local helper, an image gallery's lightbox script
227
- * someone dropped in `public/` to reference from a raw `<script src>`
228
- * in an .mdx file, say) gets auto-injected sitewide the same as a
229
- * deliberate one; there's no separate "this one's just tooling" signal
230
- * to opt out of the convention short of renaming its extension.
231
- *
232
- * Both `css`/`js` and `publicCss`/`publicJs` are sorted alphabetically
233
- * by their respective path for deterministic load order across rebuilds
234
- * - filesystem readdir order isn't guaranteed portable across OSes or
235
- * directory-walk order otherwise.
236
- *
237
- * `css`/`js` return POSIX-separated paths relative to `contentDir`, not
238
- * absolute paths or file contents - BaseLayout.astro (the sole caller)
239
- * resolves and reads each one's content itself, right before inlining
240
- * it, so a file's content is always current as of that specific
241
- * request/build rather than cached here across a `writedocs dev`
242
- * session. `publicCss`/`publicJs` return the public-URL hrefs described
243
- * above - nothing to read, Astro's own static-file serving/copy already
244
- * handles those. */
245
- export function findRootAssets(contentDir) {
246
- // Once per build (lib/build-memo.js); on every call in `writedocs dev`.
247
- return oncePerBuild(`root-assets:${contentDir}`, () => scanRootAssets(contentDir));
248
- }
249
-
250
- function scanRootAssets(contentDir) {
251
- // What the project's ignore rules leave out (lib/wdignore.js) is never
252
- // loaded into the pages either - in public/ too.
253
- const rules = projectIgnore(contentDir);
254
- const css = [];
255
- const js = [];
256
- function walk(dir, relBase) {
257
- let entries;
258
- try {
259
- entries = fs.readdirSync(dir, { withFileTypes: true });
260
- } catch {
261
- return;
262
- }
263
- for (const entry of entries) {
264
- const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
265
- const abs = path.join(dir, entry.name);
266
- if (entry.isDirectory()) {
267
- if (relBase === '' && EXCLUDED_TOP_LEVEL_DIRS.has(entry.name)) continue;
268
- // Dot-folders (.github, .claude, .husky, .vscode, ...) hold tooling,
269
- // never site assets - the same rule findAllPages() applies to pages.
270
- // Without it, a CI script under .github/ ran on every page.
271
- if (entry.name.startsWith('.')) continue;
272
- if (rules.ignores(rel, true)) continue;
273
- walk(abs, rel);
274
- continue;
275
- }
276
- if (!entry.isFile() || entry.name.startsWith('.')) continue; // .eslintrc.js and friends
277
- if (rules.ignores(rel)) continue;
278
- if (/\.css$/i.test(entry.name)) css.push(rel);
279
- else if (/\.js$/i.test(entry.name)) js.push(rel);
280
- }
281
- }
282
- walk(contentDir, '');
283
- css.sort();
284
- js.sort();
285
-
286
- const publicCss = [];
287
- const publicJs = [];
288
- function walkPublic(dir, relBase) {
289
- let entries;
290
- try {
291
- entries = fs.readdirSync(dir, { withFileTypes: true });
292
- } catch {
293
- return;
294
- }
295
- for (const entry of entries) {
296
- const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
297
- const abs = path.join(dir, entry.name);
298
- if (entry.isDirectory()) {
299
- walkPublic(abs, rel);
300
- continue;
301
- }
302
- if (!entry.isFile() || rules.ignores(`public/${rel}`)) continue;
303
- if (/\.css$/i.test(entry.name)) publicCss.push(`/${rel}`);
304
- else if (/\.js$/i.test(entry.name)) publicJs.push(`/${rel}`);
305
- }
306
- }
307
- walkPublic(path.join(contentDir, 'public'), '');
308
- publicCss.sort();
309
- publicJs.sort();
310
-
311
- return { css, js, publicCss, publicJs };
312
- }
1
+ // Which files in a content directory are pages. Plain JavaScript, not
2
+ // TypeScript, so `writedocs validate` can load it with plain Node (Node
3
+ // refuses to strip types under node_modules - see lib/icons.js's comment);
4
+ // lib/config.ts re-exports findAllPages() for everything inside Astro.
5
+ import fs from 'node:fs';
6
+ import path from 'node:path';
7
+ import matter from 'gray-matter';
8
+ import { readConfigText } from './config-file.js';
9
+ import { oncePerBuild } from './build-memo.js';
10
+ import { projectIgnore } from './project-ignore.js';
11
+
12
+ // Top-level folders of a content directory that are never scanned for
13
+ // pages - build output, dependencies, and static assets. Any folder whose
14
+ // name starts with a dot (.git, .github, .claude, .mintlify, ...) is
15
+ // skipped too, at any depth - see findAllPages().
16
+ export const EXCLUDED_TOP_LEVEL_DIRS = new Set(['node_modules', 'dist', '.astro', '.writedocs', 'public', '.git']);
17
+
18
+ /** A page's title when its frontmatter has none - Mintlify's rule, so a
19
+ * migrated page that relied on it keeps the same title: the file name
20
+ * without its extension, dashes and underscores as spaces, first letter
21
+ * capitalized. `guides/getting-started.mdx` -> "Getting started". A
22
+ * folder's index page is named for the folder (`api/index.mdx` -> "Api"),
23
+ * and the site's own index.mdx is "Home" - "Index" names neither. */
24
+ export function titleFromPath(filePath) {
25
+ const parts = String(filePath).split(/[\\/]/).filter(Boolean);
26
+ let base = (parts.pop() ?? '').replace(/\.mdx?$/i, '');
27
+ if (base.toLowerCase() === 'index') {
28
+ if (parts.length === 0) return 'Home';
29
+ base = parts.pop();
30
+ }
31
+ const words = base.replace(/[-_]+/g, ' ').trim();
32
+ return words ? words.charAt(0).toUpperCase() + words.slice(1) : 'Untitled';
33
+ }
34
+
35
+ /** Mintlify's .mintignore, if the content directory has one: gitignore-
36
+ * style patterns for files and folders that aren't published. Supports
37
+ * comments, blank lines, `*`, `**`, `?`, a leading `/` (anchored to the
38
+ * content directory) and a trailing `/` (folders only); negation (`!`)
39
+ * isn't supported and such lines are skipped. Returns (relPath, isDir) =>
40
+ * boolean. */
41
+ function readIgnoreFile(contentDir) {
42
+ let text;
43
+ try {
44
+ text = fs.readFileSync(path.join(contentDir, '.mintignore'), 'utf-8');
45
+ } catch {
46
+ return () => false;
47
+ }
48
+ const rules = text
49
+ .split(/\r?\n/)
50
+ .map((l) => l.trim())
51
+ .filter((l) => l && !l.startsWith('#') && !l.startsWith('!'))
52
+ .map((pattern) => {
53
+ const dirOnly = pattern.endsWith('/');
54
+ let p = pattern.replace(/\/+$/, '');
55
+ const anchored = p.startsWith('/') || p.includes('/');
56
+ p = p.replace(/^\/+/, '');
57
+ const source = p
58
+ .split(/(\*\*\/?|\*|\?)/)
59
+ .map((part) =>
60
+ part === '**/' || part === '**' ? '(?:.*/)?' : part === '*' ? '[^/]*' : part === '?' ? '[^/]' : part.replace(/[.+^${}()|[\]\\]/g, '\\$&')
61
+ )
62
+ .join('');
63
+ const re = new RegExp(anchored ? `^${source}$` : `(?:^|/)${source}$`);
64
+ return { re, dirOnly };
65
+ });
66
+ return (rel, isDir) => rules.some((r) => (!r.dirOnly || isDir) && r.re.test(rel));
67
+ }
68
+
69
+ /** Page ids writedocs.json's navigation lists (strings in arrays, and
70
+ * groups' `page`) - read straight from the file, since this runs before
71
+ * (and independently of) config validation. Empty when there's no
72
+ * readable writedocs.json. */
73
+ /** The page id a navigation entry names. "guides/index" and "guides" are
74
+ * the same page: a folder's index.mdx has the folder's id (fileIdForPath()
75
+ * below), and Mintlify projects write it either way. The root "index"
76
+ * stays as it is. */
77
+ export function navPageId(id) {
78
+ return id.endsWith('/index') ? id.slice(0, -'/index'.length) : id;
79
+ }
80
+
81
+ /** Every page id a `navigation` lists, in the order a reader meets them -
82
+ * top to bottom, each group's own `page` before its `pages` - without
83
+ * repeats. */
84
+ export function navigationPageOrder(navigation) {
85
+ const ids = new Set();
86
+ (function walk(node) {
87
+ if (Array.isArray(node)) node.forEach((item) => (typeof item === 'string' ? ids.add(navPageId(item)) : walk(item)));
88
+ else if (node && typeof node === 'object') {
89
+ if (typeof node.page === 'string') ids.add(navPageId(node.page));
90
+ for (const [key, value] of Object.entries(node)) {
91
+ if (key !== 'page' && key !== 'openapi' && key !== 'href') walk(value);
92
+ }
93
+ }
94
+ })(navigation);
95
+ return [...ids];
96
+ }
97
+
98
+ function navigationPageIds(contentDir) {
99
+ let config;
100
+ try {
101
+ config = JSON.parse(readConfigText(contentDir));
102
+ } catch {
103
+ return new Set();
104
+ }
105
+ return new Set(navigationPageOrder(config.navigation));
106
+ }
107
+
108
+ /** Recursively finds every .md/.mdx file under `contentDir` that is a page:
109
+ * one with a frontmatter block, or one writedocs.json's navigation lists
110
+ * (a Mintlify page needs no frontmatter at all - see titleFromPath()).
111
+ * Skips EXCLUDED_TOP_LEVEL_DIRS, every dot-folder at any depth, whatever
112
+ * the project's ignore rules leave out (lib/wdignore.js: the default
113
+ * exclusions and its `.wdignore`) and anything a .mintignore file lists. docs/ is scanned exactly like any
114
+ * other folder, no special-casing. Returns POSIX-relative paths (from
115
+ * `contentDir`) suitable to hand straight to Astro's `glob()` loader as a
116
+ * literal `pattern` array - see content.config.ts's `pages` collection,
117
+ * and [...slug].astro, which needs the identical list to decide whether
118
+ * calling `getCollection('pages')` is worth doing at all. Also reused by
119
+ * astro.config.mjs's noindex/sitemap scan and by `writedocs validate`, so
120
+ * all of them see exactly the same file set. Synchronous. In a build the
121
+ * project is scanned once and every later call gets that list
122
+ * (lib/build-memo.js) - a page that scanned it again made the build N x N;
123
+ * everywhere else (`writedocs dev`, `validate`) every call scans afresh,
124
+ * so a page added or removed is seen. */
125
+ export function findAllPages(contentDir) {
126
+ // A copy, so a caller that changes its list doesn't change everyone's.
127
+ return oncePerBuild(`pages:${contentDir}`, () => scanAllPages(contentDir)).slice();
128
+ }
129
+
130
+ function scanAllPages(contentDir) {
131
+ const results = [];
132
+ const ignored = readIgnoreFile(contentDir);
133
+ const rules = projectIgnore(contentDir);
134
+ const listed = navigationPageIds(contentDir);
135
+ function walk(dir, relBase) {
136
+ let entries;
137
+ try {
138
+ entries = fs.readdirSync(dir, { withFileTypes: true });
139
+ } catch {
140
+ return;
141
+ }
142
+ for (const entry of entries) {
143
+ const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
144
+ const abs = path.join(dir, entry.name);
145
+ if (entry.isDirectory()) {
146
+ if (relBase === '' && EXCLUDED_TOP_LEVEL_DIRS.has(entry.name)) continue;
147
+ if (entry.name.startsWith('.')) continue;
148
+ if (ignored(rel, true) || rules.ignores(rel, true)) continue;
149
+ walk(abs, rel);
150
+ continue;
151
+ }
152
+ if (!entry.isFile() || !/\.mdx?$/i.test(entry.name)) continue;
153
+ if (ignored(rel, false) || rules.ignores(rel)) continue;
154
+ let raw;
155
+ try {
156
+ raw = fs.readFileSync(abs, 'utf-8');
157
+ } catch {
158
+ continue;
159
+ }
160
+ let data;
161
+ try {
162
+ ({ data } = matter(raw));
163
+ } catch {
164
+ // Frontmatter that isn't valid YAML: still a page (it opens a
165
+ // frontmatter block), so the error surfaces with this file's name -
166
+ // from `writedocs validate`, or Astro's own loader in the build -
167
+ // instead of as a bare YAML error thrown from here.
168
+ if (/^---\r?\n/.test(raw)) results.push(rel);
169
+ continue;
170
+ }
171
+ if (Object.keys(data).length === 0 && !listed.has(fileIdForPath(rel))) continue; // not a page
172
+ results.push(rel);
173
+ }
174
+ }
175
+ walk(contentDir, '');
176
+ return results;
177
+ }
178
+
179
+ /** The id writedocs.json's navigation refers to a page by: its path from
180
+ * the content directory, without the extension or a trailing `/index`
181
+ * (same algorithm as fileIdForEntry() in lib/config.ts). */
182
+ export function fileIdForPath(relativePath) {
183
+ return relativePath.replace(/\.mdx?$/i, '').replace(/\/index$/, '');
184
+ }
185
+
186
+ /** Any `.css`/`.js` file anywhere in the project - root or any subfolder,
187
+ * `public/` included - auto-loaded site-wide with zero `writedocs.json`
188
+ * config, on top of (not instead of) the explicit `scripts` field
189
+ * (bannerSchema and friends, above). Drop a file in, it loads;
190
+ * there's no field naming which ones to use, matching the same "just
191
+ * works" convention `docs/`'s own file discovery already follows (see
192
+ * findAllPages() above / `content-pipeline.mdx`) - a site author already
193
+ * drops content files in and expects them found, rather than also
194
+ * listing every one in writedocs.json.
195
+ *
196
+ * Two separate walks, because `public/` needs different treatment than
197
+ * everywhere else:
198
+ *
199
+ * - `css`/`js`: every `.css`/`.js` file outside `public/` (root, `docs/`,
200
+ * `snippets/`, any custom folder) - reused as the walk-with-exclusions
201
+ * shape from findAllPages() above, and its exact `EXCLUDED_TOP_LEVEL_DIRS`
202
+ * set (skipped only at the project root, same as there), so `dist/`,
203
+ * `node_modules/`, `.astro/`, `.writedocs/`, `.git/`, and (for this
204
+ * half only - see below) `public/` are never walked into. BaseLayout.astro
205
+ * reads each one's raw content and inlines it as a `<style>`/
206
+ * `<script is:inline>` tag.
207
+ * - `publicCss`/`publicJs`: every `.css`/`.js` file *inside* `public/`,
208
+ * walked separately (starting from `<contentDir>/public` rather than
209
+ * `contentDir` itself, so the same `EXCLUDED_TOP_LEVEL_DIRS` check
210
+ * doesn't apply here - there's no `public/public/` or `public/dist/`
211
+ * convention to guard against). Returned as public-URL-rooted hrefs
212
+ * (a leading `/`, no `public` segment - `public/custom.css` becomes
213
+ * `/custom.css`) rather than content-dir-relative paths, since these
214
+ * files are already served as static assets at exactly that URL once
215
+ * Astro copies `public/` into the build output. BaseLayout.astro
216
+ * renders these as ordinary `<link rel="stylesheet">`/`<script src>`
217
+ * tags pointing at that URL instead of inlining their content -
218
+ * inlining would duplicate every byte (once in the page's own HTML,
219
+ * once more as the independently-fetchable static file at that same
220
+ * URL) for no benefit, where a `<link>`/`<script src>` gets normal
221
+ * browser caching across pages instead of repeating the content on
222
+ * every single page's markup.
223
+ *
224
+ * Both halves are broader than they might sound - a stray `.js` file
225
+ * kept in `docs/`, `snippets/`, or `public/` for an unrelated reason
226
+ * (a snippet's own local helper, an image gallery's lightbox script
227
+ * someone dropped in `public/` to reference from a raw `<script src>`
228
+ * in an .mdx file, say) gets auto-injected sitewide the same as a
229
+ * deliberate one; there's no separate "this one's just tooling" signal
230
+ * to opt out of the convention short of renaming its extension.
231
+ *
232
+ * Both `css`/`js` and `publicCss`/`publicJs` are sorted alphabetically
233
+ * by their respective path for deterministic load order across rebuilds
234
+ * - filesystem readdir order isn't guaranteed portable across OSes or
235
+ * directory-walk order otherwise.
236
+ *
237
+ * `css`/`js` return POSIX-separated paths relative to `contentDir`, not
238
+ * absolute paths or file contents - BaseLayout.astro (the sole caller)
239
+ * resolves and reads each one's content itself, right before inlining
240
+ * it, so a file's content is always current as of that specific
241
+ * request/build rather than cached here across a `writedocs dev`
242
+ * session. `publicCss`/`publicJs` return the public-URL hrefs described
243
+ * above - nothing to read, Astro's own static-file serving/copy already
244
+ * handles those. */
245
+ export function findRootAssets(contentDir) {
246
+ // Once per build (lib/build-memo.js); on every call in `writedocs dev`.
247
+ return oncePerBuild(`root-assets:${contentDir}`, () => scanRootAssets(contentDir));
248
+ }
249
+
250
+ function scanRootAssets(contentDir) {
251
+ // What the project's ignore rules leave out (lib/wdignore.js) is never
252
+ // loaded into the pages either - in public/ too.
253
+ const rules = projectIgnore(contentDir);
254
+ const css = [];
255
+ const js = [];
256
+ function walk(dir, relBase) {
257
+ let entries;
258
+ try {
259
+ entries = fs.readdirSync(dir, { withFileTypes: true });
260
+ } catch {
261
+ return;
262
+ }
263
+ for (const entry of entries) {
264
+ const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
265
+ const abs = path.join(dir, entry.name);
266
+ if (entry.isDirectory()) {
267
+ if (relBase === '' && EXCLUDED_TOP_LEVEL_DIRS.has(entry.name)) continue;
268
+ // Dot-folders (.github, .claude, .husky, .vscode, ...) hold tooling,
269
+ // never site assets - the same rule findAllPages() applies to pages.
270
+ // Without it, a CI script under .github/ ran on every page.
271
+ if (entry.name.startsWith('.')) continue;
272
+ if (rules.ignores(rel, true)) continue;
273
+ walk(abs, rel);
274
+ continue;
275
+ }
276
+ if (!entry.isFile() || entry.name.startsWith('.')) continue; // .eslintrc.js and friends
277
+ if (rules.ignores(rel)) continue;
278
+ if (/\.css$/i.test(entry.name)) css.push(rel);
279
+ else if (/\.js$/i.test(entry.name)) js.push(rel);
280
+ }
281
+ }
282
+ walk(contentDir, '');
283
+ css.sort();
284
+ js.sort();
285
+
286
+ const publicCss = [];
287
+ const publicJs = [];
288
+ function walkPublic(dir, relBase) {
289
+ let entries;
290
+ try {
291
+ entries = fs.readdirSync(dir, { withFileTypes: true });
292
+ } catch {
293
+ return;
294
+ }
295
+ for (const entry of entries) {
296
+ const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
297
+ const abs = path.join(dir, entry.name);
298
+ if (entry.isDirectory()) {
299
+ walkPublic(abs, rel);
300
+ continue;
301
+ }
302
+ if (!entry.isFile() || rules.ignores(`public/${rel}`)) continue;
303
+ if (/\.css$/i.test(entry.name)) publicCss.push(`/${rel}`);
304
+ else if (/\.js$/i.test(entry.name)) publicJs.push(`/${rel}`);
305
+ }
306
+ }
307
+ walkPublic(path.join(contentDir, 'public'), '');
308
+ publicCss.sort();
309
+ publicJs.sort();
310
+
311
+ return { css, js, publicCss, publicJs };
312
+ }