@writedocs/generator 0.10.19 → 0.10.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +96 -93
- package/src/cli/generate-api-pages.js +9 -5
- package/src/cli/page-list-watch.js +69 -66
- package/src/components/Image.astro +164 -164
- package/src/lib/asset-dimensions.js +89 -0
- package/src/lib/image-size.js +152 -140
- package/src/lib/link-check.js +35 -2
- package/src/lib/pages.js +312 -304
- package/src/lib/project-files-integration.js +116 -61
- package/src/lib/project-files.js +137 -126
- package/src/lib/project-ignore.js +46 -0
- package/src/lib/rehype-image-loading.js +71 -71
- package/src/lib/styles-asset-integration.js +35 -2
- package/src/lib/wdignore-cases.json +195 -0
- package/src/lib/wdignore.js +177 -0
package/src/lib/pages.js
CHANGED
|
@@ -1,304 +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
|
-
|
|
11
|
-
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
.
|
|
50
|
-
.
|
|
51
|
-
.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
*
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* `
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* astro
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
continue;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
//
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* `
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
* in
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
continue;
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
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
|
+
}
|