@writedocs/generator 0.13.4 → 0.14.0

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 (89) hide show
  1. package/README.md +28 -28
  2. package/astro.config.mjs +645 -645
  3. package/bin/writedocs.js +359 -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 +223 -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 +269 -268
  11. package/src/cli/exit-when-flushed.js +42 -42
  12. package/src/cli/generate-api-pages.js +509 -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 +71 -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 +219 -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 +422 -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 +12 -1
  48. package/src/lib/config-schema.ts +2523 -2509
  49. package/src/lib/config.ts +1331 -1316
  50. package/src/lib/consent.js +108 -108
  51. package/src/lib/content-check.js +4 -1
  52. package/src/lib/content-store-reload-integration.js +101 -101
  53. package/src/lib/drafts.js +186 -0
  54. package/src/lib/icon-data-dev.js +53 -53
  55. package/src/lib/image-size.js +152 -152
  56. package/src/lib/json-schema-descriptions.js +228 -228
  57. package/src/lib/link-check.js +553 -544
  58. package/src/lib/llms-index.ts +90 -90
  59. package/src/lib/mcp-dev-integration.js +65 -65
  60. package/src/lib/mcp-index.ts +102 -102
  61. package/src/lib/mintlify-convert.js +605 -605
  62. package/src/lib/moved-api-pages.js +49 -49
  63. package/src/lib/og-cards.js +33 -33
  64. package/src/lib/og-image.js +235 -235
  65. package/src/lib/openapi-dev-status.js +80 -80
  66. package/src/lib/openapi-reload-integration.js +193 -193
  67. package/src/lib/pages.js +346 -312
  68. package/src/lib/project-files-integration.js +116 -116
  69. package/src/lib/project-files.js +137 -137
  70. package/src/lib/rehype-base-path.js +62 -62
  71. package/src/lib/rehype-image-loading.js +71 -71
  72. package/src/lib/robots-txt.js +21 -21
  73. package/src/lib/search-scope.js +44 -44
  74. package/src/lib/selector-placement.js +50 -50
  75. package/src/lib/seo-tags.js +157 -155
  76. package/src/lib/shiki-code-block.js +7 -1
  77. package/src/lib/site-base.ts +27 -27
  78. package/src/lib/styles-asset-integration.js +291 -291
  79. package/src/lib/wdignore-cases.json +195 -195
  80. package/src/lib/wdignore.js +177 -177
  81. package/src/pages/404.astro +63 -63
  82. package/src/pages/[...slug].astro +1773 -1608
  83. package/src/pages/[...slug].md.ts +93 -93
  84. package/src/pages/llms-full.txt.ts +88 -88
  85. package/src/pages/og/[...slug].png.ts +63 -63
  86. package/src/scripts/mobile-menu.ts +80 -80
  87. package/src/scripts/search.ts +245 -245
  88. package/src/scripts/sidebar-scroll.ts +61 -61
  89. package/src/styles/global.css +40 -40
package/src/lib/pages.js CHANGED
@@ -1,312 +1,346 @@
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
+ *
126
+ * Draft pages (`draft: true`) are left out unless `drafts` is true - by
127
+ * default, draftsIncluded() below. This is where `writedocs build` leaves
128
+ * them out of the site: every route, the content collection and the
129
+ * sitemap start from this list (lib/drafts.js). */
130
+ export function findAllPages(contentDir, { drafts = draftsIncluded() } = {}) {
131
+ // A new list, so a caller that changes its list doesn't change everyone's.
132
+ return scannedPages(contentDir)
133
+ .filter((page) => drafts || !page.draft)
134
+ .map((page) => page.file);
135
+ }
136
+
137
+ /** Whether draft pages (`draft: true` - lib/drafts.js) are pages here: in
138
+ * `writedocs dev` and `writedocs build --drafts`, which tell the Astro
139
+ * process so (src/cli/run-astro.js's WRITEDOCS_COMMAND, src/cli/build.js's
140
+ * WRITEDOCS_DRAFTS). Everywhere else - `writedocs build`, and `writedocs
141
+ * broken-links`, which checks the site the build makes - a draft isn't a
142
+ * page. The CLI's own calls in dev, build and validate pass `drafts`
143
+ * instead. */
144
+ export function draftsIncluded(env = process.env) {
145
+ return env.WRITEDOCS_COMMAND === 'dev' || env.WRITEDOCS_DRAFTS === '1';
146
+ }
147
+
148
+ /** The draft pages findAllPages() finds (whether it leaves them out or not):
149
+ * `{ file, slug }` - the file's path as findAllPages() gives it, and its
150
+ * frontmatter `slug`, if any. */
151
+ export function findDraftPages(contentDir) {
152
+ return scannedPages(contentDir)
153
+ .filter((page) => page.draft)
154
+ .map(({ file, slug }) => ({ file, slug }));
155
+ }
156
+
157
+ function scannedPages(contentDir) {
158
+ return oncePerBuild(`pages:${contentDir}`, () => scanAllPages(contentDir));
159
+ }
160
+
161
+ function scanAllPages(contentDir) {
162
+ const results = [];
163
+ const ignored = readIgnoreFile(contentDir);
164
+ const rules = projectIgnore(contentDir);
165
+ const listed = navigationPageIds(contentDir);
166
+ function walk(dir, relBase) {
167
+ let entries;
168
+ try {
169
+ entries = fs.readdirSync(dir, { withFileTypes: true });
170
+ } catch {
171
+ return;
172
+ }
173
+ for (const entry of entries) {
174
+ const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
175
+ const abs = path.join(dir, entry.name);
176
+ if (entry.isDirectory()) {
177
+ if (relBase === '' && EXCLUDED_TOP_LEVEL_DIRS.has(entry.name)) continue;
178
+ if (entry.name.startsWith('.')) continue;
179
+ if (ignored(rel, true) || rules.ignores(rel, true)) continue;
180
+ walk(abs, rel);
181
+ continue;
182
+ }
183
+ if (!entry.isFile() || !/\.mdx?$/i.test(entry.name)) continue;
184
+ if (ignored(rel, false) || rules.ignores(rel)) continue;
185
+ let raw;
186
+ try {
187
+ raw = fs.readFileSync(abs, 'utf-8');
188
+ } catch {
189
+ continue;
190
+ }
191
+ let data;
192
+ try {
193
+ ({ data } = matter(raw));
194
+ } catch {
195
+ // Frontmatter that isn't valid YAML: still a page (it opens a
196
+ // frontmatter block), so the error surfaces with this file's name -
197
+ // from `writedocs validate`, or Astro's own loader in the build -
198
+ // instead of as a bare YAML error thrown from here.
199
+ if (/^---\r?\n/.test(raw)) results.push({ file: rel, draft: false });
200
+ continue;
201
+ }
202
+ if (Object.keys(data).length === 0 && !listed.has(fileIdForPath(rel))) continue; // not a page
203
+ // Only `draft: true` - `draft: "yes"` stays a page, and the schema
204
+ // rejects it.
205
+ const draft = data.draft === true;
206
+ results.push({ file: rel, draft, slug: draft && typeof data.slug === 'string' ? data.slug : undefined });
207
+ }
208
+ }
209
+ walk(contentDir, '');
210
+ return results;
211
+ }
212
+
213
+ /** The id writedocs.json's navigation refers to a page by: its path from
214
+ * the content directory, without the extension or a trailing `/index`
215
+ * (same algorithm as fileIdForEntry() in lib/config.ts). */
216
+ export function fileIdForPath(relativePath) {
217
+ return relativePath.replace(/\.mdx?$/i, '').replace(/\/index$/, '');
218
+ }
219
+
220
+ /** Any `.css`/`.js` file anywhere in the project - root or any subfolder,
221
+ * `public/` included - auto-loaded site-wide with zero `writedocs.json`
222
+ * config, on top of (not instead of) the explicit `scripts` field
223
+ * (bannerSchema and friends, above). Drop a file in, it loads;
224
+ * there's no field naming which ones to use, matching the same "just
225
+ * works" convention `docs/`'s own file discovery already follows (see
226
+ * findAllPages() above / `content-pipeline.mdx`) - a site author already
227
+ * drops content files in and expects them found, rather than also
228
+ * listing every one in writedocs.json.
229
+ *
230
+ * Two separate walks, because `public/` needs different treatment than
231
+ * everywhere else:
232
+ *
233
+ * - `css`/`js`: every `.css`/`.js` file outside `public/` (root, `docs/`,
234
+ * `snippets/`, any custom folder) - reused as the walk-with-exclusions
235
+ * shape from findAllPages() above, and its exact `EXCLUDED_TOP_LEVEL_DIRS`
236
+ * set (skipped only at the project root, same as there), so `dist/`,
237
+ * `node_modules/`, `.astro/`, `.writedocs/`, `.git/`, and (for this
238
+ * half only - see below) `public/` are never walked into. BaseLayout.astro
239
+ * reads each one's raw content and inlines it as a `<style>`/
240
+ * `<script is:inline>` tag.
241
+ * - `publicCss`/`publicJs`: every `.css`/`.js` file *inside* `public/`,
242
+ * walked separately (starting from `<contentDir>/public` rather than
243
+ * `contentDir` itself, so the same `EXCLUDED_TOP_LEVEL_DIRS` check
244
+ * doesn't apply here - there's no `public/public/` or `public/dist/`
245
+ * convention to guard against). Returned as public-URL-rooted hrefs
246
+ * (a leading `/`, no `public` segment - `public/custom.css` becomes
247
+ * `/custom.css`) rather than content-dir-relative paths, since these
248
+ * files are already served as static assets at exactly that URL once
249
+ * Astro copies `public/` into the build output. BaseLayout.astro
250
+ * renders these as ordinary `<link rel="stylesheet">`/`<script src>`
251
+ * tags pointing at that URL instead of inlining their content -
252
+ * inlining would duplicate every byte (once in the page's own HTML,
253
+ * once more as the independently-fetchable static file at that same
254
+ * URL) for no benefit, where a `<link>`/`<script src>` gets normal
255
+ * browser caching across pages instead of repeating the content on
256
+ * every single page's markup.
257
+ *
258
+ * Both halves are broader than they might sound - a stray `.js` file
259
+ * kept in `docs/`, `snippets/`, or `public/` for an unrelated reason
260
+ * (a snippet's own local helper, an image gallery's lightbox script
261
+ * someone dropped in `public/` to reference from a raw `<script src>`
262
+ * in an .mdx file, say) gets auto-injected sitewide the same as a
263
+ * deliberate one; there's no separate "this one's just tooling" signal
264
+ * to opt out of the convention short of renaming its extension.
265
+ *
266
+ * Both `css`/`js` and `publicCss`/`publicJs` are sorted alphabetically
267
+ * by their respective path for deterministic load order across rebuilds
268
+ * - filesystem readdir order isn't guaranteed portable across OSes or
269
+ * directory-walk order otherwise.
270
+ *
271
+ * `css`/`js` return POSIX-separated paths relative to `contentDir`, not
272
+ * absolute paths or file contents - BaseLayout.astro (the sole caller)
273
+ * resolves and reads each one's content itself, right before inlining
274
+ * it, so a file's content is always current as of that specific
275
+ * request/build rather than cached here across a `writedocs dev`
276
+ * session. `publicCss`/`publicJs` return the public-URL hrefs described
277
+ * above - nothing to read, Astro's own static-file serving/copy already
278
+ * handles those. */
279
+ export function findRootAssets(contentDir) {
280
+ // Once per build (lib/build-memo.js); on every call in `writedocs dev`.
281
+ return oncePerBuild(`root-assets:${contentDir}`, () => scanRootAssets(contentDir));
282
+ }
283
+
284
+ function scanRootAssets(contentDir) {
285
+ // What the project's ignore rules leave out (lib/wdignore.js) is never
286
+ // loaded into the pages either - in public/ too.
287
+ const rules = projectIgnore(contentDir);
288
+ const css = [];
289
+ const js = [];
290
+ function walk(dir, relBase) {
291
+ let entries;
292
+ try {
293
+ entries = fs.readdirSync(dir, { withFileTypes: true });
294
+ } catch {
295
+ return;
296
+ }
297
+ for (const entry of entries) {
298
+ const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
299
+ const abs = path.join(dir, entry.name);
300
+ if (entry.isDirectory()) {
301
+ if (relBase === '' && EXCLUDED_TOP_LEVEL_DIRS.has(entry.name)) continue;
302
+ // Dot-folders (.github, .claude, .husky, .vscode, ...) hold tooling,
303
+ // never site assets - the same rule findAllPages() applies to pages.
304
+ // Without it, a CI script under .github/ ran on every page.
305
+ if (entry.name.startsWith('.')) continue;
306
+ if (rules.ignores(rel, true)) continue;
307
+ walk(abs, rel);
308
+ continue;
309
+ }
310
+ if (!entry.isFile() || entry.name.startsWith('.')) continue; // .eslintrc.js and friends
311
+ if (rules.ignores(rel)) continue;
312
+ if (/\.css$/i.test(entry.name)) css.push(rel);
313
+ else if (/\.js$/i.test(entry.name)) js.push(rel);
314
+ }
315
+ }
316
+ walk(contentDir, '');
317
+ css.sort();
318
+ js.sort();
319
+
320
+ const publicCss = [];
321
+ const publicJs = [];
322
+ function walkPublic(dir, relBase) {
323
+ let entries;
324
+ try {
325
+ entries = fs.readdirSync(dir, { withFileTypes: true });
326
+ } catch {
327
+ return;
328
+ }
329
+ for (const entry of entries) {
330
+ const rel = relBase ? `${relBase}/${entry.name}` : entry.name;
331
+ const abs = path.join(dir, entry.name);
332
+ if (entry.isDirectory()) {
333
+ walkPublic(abs, rel);
334
+ continue;
335
+ }
336
+ if (!entry.isFile() || rules.ignores(`public/${rel}`)) continue;
337
+ if (/\.css$/i.test(entry.name)) publicCss.push(`/${rel}`);
338
+ else if (/\.js$/i.test(entry.name)) publicJs.push(`/${rel}`);
339
+ }
340
+ }
341
+ walkPublic(path.join(contentDir, 'public'), '');
342
+ publicCss.sort();
343
+ publicJs.sort();
344
+
345
+ return { css, js, publicCss, publicJs };
346
+ }