@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.
@@ -1,71 +1,71 @@
1
- // How content images load. For every image a page's Markdown or MDX writes -
2
- // `![alt](/a.png)`, `<img src="/a.png">`, `<Image src="/a.png">`:
3
- //
4
- // - `loading="lazy"`: the browser fetches it when the reader gets near
5
- // it, not with the page. The first image of a page is left alone - it's
6
- // usually at the top, where waiting would make the page look slower.
7
- // - `decoding="async"`: decoding it doesn't hold up the text.
8
- // - `width` and `height`, read from the file: the browser keeps the
9
- // image's place before it arrives, so the text below doesn't jump
10
- // (layout shift). With them goes `data-wd-sized`, which base.css uses
11
- // to keep the height following the width (`height: auto`) - without
12
- // that, an image narrower than its file would be stretched.
13
- //
14
- // What the author wrote always wins: an image with its own `loading`,
15
- // `width` or `height` keeps them, and gets no size from here.
16
- //
17
- // `<Image>` reads its own files' sizes (it may have two, light and dark -
18
- // Image.astro); all it gets from here is `loading="eager"` when it's the
19
- // page's first image. Sizes are only known for files in the project
20
- // (addresses starting with "/"); a relative Markdown image is Astro's own
21
- // to process, and an image on another site only gets the two loading hints.
22
- import { imageFileFor, imageSize } from './image-size.js';
23
-
24
- const isJsx = (node) => node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement';
25
-
26
- /** A JSX attribute's value when it's a plain string; undefined when the
27
- * attribute is absent, null when it's an expression (`src={logo}`). */
28
- function jsxAttribute(node, name) {
29
- const attribute = node.attributes?.find((a) => a.type === 'mdxJsxAttribute' && a.name === name);
30
- if (!attribute) return undefined;
31
- return typeof attribute.value === 'string' ? attribute.value : null;
32
- }
33
-
34
- const setJsx = (node, name, value) => node.attributes.push({ type: 'mdxJsxAttribute', name, value: String(value) });
35
-
36
- export function rehypeImageLoading({ contentDir } = {}) {
37
- return (tree) => {
38
- let seen = 0;
39
- const visit = (node) => {
40
- if (node.type === 'element' && node.tagName === 'img') {
41
- const first = seen++ === 0;
42
- const p = (node.properties ??= {});
43
- if (p.loading === undefined && !first) p.loading = 'lazy';
44
- if (p.decoding === undefined) p.decoding = 'async';
45
- if (p.width === undefined && p.height === undefined && contentDir) {
46
- const file = imageFileFor(p.src, contentDir);
47
- const size = file && imageSize(file);
48
- if (size) Object.assign(p, { width: size.width, height: size.height, 'data-wd-sized': '' });
49
- }
50
- } else if (isJsx(node) && node.name === 'img') {
51
- const first = seen++ === 0;
52
- if (jsxAttribute(node, 'loading') === undefined && !first) setJsx(node, 'loading', 'lazy');
53
- if (jsxAttribute(node, 'decoding') === undefined) setJsx(node, 'decoding', 'async');
54
- if (jsxAttribute(node, 'width') === undefined && jsxAttribute(node, 'height') === undefined && contentDir) {
55
- const file = imageFileFor(jsxAttribute(node, 'src'), contentDir);
56
- const size = file && imageSize(file);
57
- if (size) {
58
- setJsx(node, 'width', size.width);
59
- setJsx(node, 'height', size.height);
60
- setJsx(node, 'data-wd-sized', '');
61
- }
62
- }
63
- } else if (isJsx(node) && node.name === 'Image') {
64
- const first = seen++ === 0;
65
- if (first && jsxAttribute(node, 'loading') === undefined) setJsx(node, 'loading', 'eager');
66
- }
67
- if (Array.isArray(node.children)) node.children.forEach(visit);
68
- };
69
- visit(tree);
70
- };
71
- }
1
+ // How content images load. For every image a page's Markdown or MDX writes -
2
+ // `![alt](/a.png)`, `<img src="/a.png">`, `<Image src="/a.png">`:
3
+ //
4
+ // - `loading="lazy"`: the browser fetches it when the reader gets near
5
+ // it, not with the page. The first image of a page is left alone - it's
6
+ // usually at the top, where waiting would make the page look slower.
7
+ // - `decoding="async"`: decoding it doesn't hold up the text.
8
+ // - `width` and `height`, read from the file: the browser keeps the
9
+ // image's place before it arrives, so the text below doesn't jump
10
+ // (layout shift). With them goes `data-wd-sized`, which base.css uses
11
+ // to keep the height following the width (`height: auto`) - without
12
+ // that, an image narrower than its file would be stretched.
13
+ //
14
+ // What the author wrote always wins: an image with its own `loading`,
15
+ // `width` or `height` keeps them, and gets no size from here.
16
+ //
17
+ // `<Image>` reads its own files' sizes (it may have two, light and dark -
18
+ // Image.astro); all it gets from here is `loading="eager"` when it's the
19
+ // page's first image. Sizes are only known for files in the project
20
+ // (addresses starting with "/"); a relative Markdown image is Astro's own
21
+ // to process, and an image on another site only gets the two loading hints.
22
+ import { imageFileFor, imageSize } from './image-size.js';
23
+
24
+ const isJsx = (node) => node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement';
25
+
26
+ /** A JSX attribute's value when it's a plain string; undefined when the
27
+ * attribute is absent, null when it's an expression (`src={logo}`). */
28
+ function jsxAttribute(node, name) {
29
+ const attribute = node.attributes?.find((a) => a.type === 'mdxJsxAttribute' && a.name === name);
30
+ if (!attribute) return undefined;
31
+ return typeof attribute.value === 'string' ? attribute.value : null;
32
+ }
33
+
34
+ const setJsx = (node, name, value) => node.attributes.push({ type: 'mdxJsxAttribute', name, value: String(value) });
35
+
36
+ export function rehypeImageLoading({ contentDir } = {}) {
37
+ return (tree) => {
38
+ let seen = 0;
39
+ const visit = (node) => {
40
+ if (node.type === 'element' && node.tagName === 'img') {
41
+ const first = seen++ === 0;
42
+ const p = (node.properties ??= {});
43
+ if (p.loading === undefined && !first) p.loading = 'lazy';
44
+ if (p.decoding === undefined) p.decoding = 'async';
45
+ if (p.width === undefined && p.height === undefined && contentDir) {
46
+ const file = imageFileFor(p.src, contentDir);
47
+ const size = file && imageSize(file, contentDir);
48
+ if (size) Object.assign(p, { width: size.width, height: size.height, 'data-wd-sized': '' });
49
+ }
50
+ } else if (isJsx(node) && node.name === 'img') {
51
+ const first = seen++ === 0;
52
+ if (jsxAttribute(node, 'loading') === undefined && !first) setJsx(node, 'loading', 'lazy');
53
+ if (jsxAttribute(node, 'decoding') === undefined) setJsx(node, 'decoding', 'async');
54
+ if (jsxAttribute(node, 'width') === undefined && jsxAttribute(node, 'height') === undefined && contentDir) {
55
+ const file = imageFileFor(jsxAttribute(node, 'src'), contentDir);
56
+ const size = file && imageSize(file, contentDir);
57
+ if (size) {
58
+ setJsx(node, 'width', size.width);
59
+ setJsx(node, 'height', size.height);
60
+ setJsx(node, 'data-wd-sized', '');
61
+ }
62
+ }
63
+ } else if (isJsx(node) && node.name === 'Image') {
64
+ const first = seen++ === 0;
65
+ if (first && jsxAttribute(node, 'loading') === undefined) setJsx(node, 'loading', 'eager');
66
+ }
67
+ if (Array.isArray(node.children)) node.children.forEach(visit);
68
+ };
69
+ visit(tree);
70
+ };
71
+ }
@@ -52,12 +52,21 @@
52
52
  // front the way there is for writedocs.json's own fields. A real static
53
53
  // build has no "later" for a live-reload concern to apply to, so a
54
54
  // single pass of each here is sufficient.
55
+ //
56
+ // A file the project's ignore rules leave out (lib/wdignore.js: the default
57
+ // exclusions and `.wdignore`) is never served nor copied, whoever names it:
58
+ // the reference is left broken, as for a file that isn't there
59
+ // (`writedocs broken-links` reports it). In `writedocs dev` that goes for
60
+ // public/ too, which Vite would otherwise serve whole; in a build,
61
+ // removeIgnoredPublicFiles() (lib/project-files-integration.js) takes such
62
+ // files out of the output.
55
63
  import fs from 'node:fs';
56
64
  import path from 'node:path';
57
65
  import { fileURLToPath } from 'node:url';
58
66
  import { loadDocsConfig, collectConfiguredAssetPaths } from './config.ts';
59
67
  import { resolveOutsidePublic } from './asset-path.js';
60
68
  import { PUBLISHED_TYPES, isPublishedFile, readSpecPaths } from './project-files.js';
69
+ import { projectIgnore, isIgnoredFile } from './project-ignore.js';
61
70
 
62
71
  const MIME_BY_EXTENSION = {
63
72
  '.svg': 'image/svg+xml',
@@ -91,7 +100,24 @@ function isPublishedFileRequest(urlPath, contentDir) {
91
100
  } catch {
92
101
  return false;
93
102
  }
94
- return isPublishedFile(rel, { specs: readSpecPaths(contentDir) });
103
+ return isPublishedFile(rel, { specs: readSpecPaths(contentDir), rules: projectIgnore(contentDir) });
104
+ }
105
+
106
+ /** Whether a request is for a file in public/ that the project's ignore
107
+ * rules leave out - one `writedocs dev` must not serve. */
108
+ function isIgnoredPublicRequest(urlPath, contentDir) {
109
+ let rel;
110
+ try {
111
+ rel = decodeURIComponent(urlPath).replace(/^\/+/, '');
112
+ } catch {
113
+ return false;
114
+ }
115
+ if (!rel || !projectIgnore(contentDir).ignores(`public/${rel}`)) return false;
116
+ try {
117
+ return fs.statSync(path.join(contentDir, 'public', ...rel.split('/'))).isFile();
118
+ } catch {
119
+ return false;
120
+ }
95
121
  }
96
122
 
97
123
 
@@ -196,6 +222,7 @@ function copyAssetIfMissing(urlPath, outDir, contentDir) {
196
222
  if (fs.existsSync(outPath)) return; // already present - via public/'s passthrough copy, or an earlier reference to the same file
197
223
  const resolved = resolveOutsidePublic(urlPath, contentDir);
198
224
  if (!resolved) return; // doesn't resolve to a real file anywhere in the project either - leave the broken reference as-is
225
+ if (isIgnoredFile(resolved, contentDir)) return; // left out by the ignore rules - never published
199
226
  fs.mkdirSync(path.dirname(outPath), { recursive: true });
200
227
  fs.copyFileSync(resolved, outPath);
201
228
  }
@@ -208,11 +235,16 @@ export function stylesAssetFallback(contentDir) {
208
235
  server.middlewares.use((req, res, next) => {
209
236
  if (!req.url) return next();
210
237
  const [urlPath, query = ''] = req.url.split('?');
238
+ if (!query && isIgnoredPublicRequest(urlPath, contentDir)) {
239
+ res.statusCode = 404;
240
+ res.end('Not found');
241
+ return;
242
+ }
211
243
  // A request with a query is Vite's own (a module: `data.json?import`).
212
244
  if (!looksLikeAssetPath(urlPath) && (query || !isPublishedFileRequest(urlPath, contentDir))) return next();
213
245
  if (fs.existsSync(publicPathFor(urlPath, contentDir))) return next(); // real public/ file - Vite's own static serving already handles it, don't shadow
214
246
  const resolved = resolveOutsidePublic(urlPath, contentDir);
215
- if (!resolved) return next();
247
+ if (!resolved || isIgnoredFile(resolved, contentDir)) return next();
216
248
  res.setHeader('Content-Type', mimeFor(resolved));
217
249
  fs.createReadStream(resolved).pipe(res);
218
250
  });
@@ -232,6 +264,7 @@ export function stylesAssetFallback(contentDir) {
232
264
  if (fs.existsSync(outPath)) continue; // already present via public/'s normal passthrough copy
233
265
  const resolved = resolveOutsidePublic(urlPath, contentDir);
234
266
  if (!resolved) continue; // not configured to a real file anywhere - same as today, just renders a broken <link>/<img>
267
+ if (isIgnoredFile(resolved, contentDir)) continue; // left out by the ignore rules - never published
235
268
  fs.mkdirSync(path.dirname(outPath), { recursive: true });
236
269
  fs.copyFileSync(resolved, outPath);
237
270
  }
@@ -0,0 +1,195 @@
1
+ {
2
+ "description": "Test cases for @writedocs/generator/wdignore. For each case: createWdignore(case.wdignore).ignores(path, directory === true) must equal `ignored` for every entry in `paths`. Paths are POSIX, relative to the project root. The generator's own tests (scripts/wdignore.test.js) run this file; any other implementation of the rules should too.",
3
+ "cases": [
4
+ {
5
+ "name": "default exclusions, no .wdignore",
6
+ "wdignore": "",
7
+ "paths": [
8
+ { "path": ".env", "ignored": true },
9
+ { "path": ".env.local", "ignored": true },
10
+ { "path": ".env.example", "ignored": true },
11
+ { "path": "docs/.env", "ignored": true },
12
+ { "path": ".git/config", "ignored": true },
13
+ { "path": ".git", "directory": true, "ignored": true },
14
+ { "path": ".gitignore", "ignored": true },
15
+ { "path": ".gitmodules", "ignored": true },
16
+ { "path": ".gitattributes", "ignored": true },
17
+ { "path": ".github/workflows/ci.yml", "ignored": true },
18
+ { "path": ".github", "directory": true, "ignored": true },
19
+ { "path": ".vscode/settings.json", "ignored": true },
20
+ { "path": ".DS_Store", "ignored": true },
21
+ { "path": "docs/.DS_Store", "ignored": true },
22
+ { "path": "docs/.drafts/plan.pdf", "ignored": true },
23
+ { "path": ".npmrc", "ignored": true },
24
+ { "path": ".wdignore", "ignored": true },
25
+ { "path": ".mintignore", "ignored": true },
26
+ { "path": ".writedocs/asset-dimensions.json", "ignored": true },
27
+ { "path": ".writedocs", "directory": true, "ignored": true },
28
+ { "path": "package.json", "ignored": true },
29
+ { "path": "examples/package.json", "ignored": true },
30
+ { "path": "package-lock.json", "ignored": true },
31
+ { "path": "yarn.lock", "ignored": true },
32
+ { "path": "pnpm-lock.yaml", "ignored": true },
33
+ { "path": "bun.lockb", "ignored": true },
34
+ { "path": "bun.lock", "ignored": true },
35
+ { "path": "composer.json", "ignored": true },
36
+ { "path": "composer.lock", "ignored": true },
37
+ { "path": "node_modules/pkg/index.js", "ignored": true },
38
+ { "path": "node_modules", "directory": true, "ignored": true },
39
+ { "path": "examples/app/node_modules/x/a.json", "ignored": true },
40
+ { "path": "dist/index.html", "ignored": true },
41
+ { "path": "dist", "directory": true, "ignored": true },
42
+ { "path": "examples/dist/app.zip", "ignored": true },
43
+ { "path": "dist", "ignored": false },
44
+ { "path": "tsconfig.json", "ignored": true },
45
+ { "path": "jsconfig.json", "ignored": true },
46
+ { "path": "vercel.json", "ignored": true },
47
+ { "path": "netlify.json", "ignored": true },
48
+ { "path": "netlify.toml", "ignored": true },
49
+ { "path": "wrangler.json", "ignored": true },
50
+ { "path": "wrangler.jsonc", "ignored": true },
51
+ { "path": "wrangler.toml", "ignored": true },
52
+ { "path": "Dockerfile", "ignored": true },
53
+ { "path": "docker-compose.yml", "ignored": true },
54
+ { "path": "docker-compose.prod.yaml", "ignored": true },
55
+ { "path": "certs/server.pem", "ignored": true },
56
+ { "path": "private.key", "ignored": true },
57
+ { "path": "store.p12", "ignored": true },
58
+ { "path": "store.pfx", "ignored": true },
59
+ { "path": "id_rsa", "ignored": true },
60
+ { "path": "id_rsa.pub", "ignored": true },
61
+ { "path": "keys/id_dsa", "ignored": true },
62
+ { "path": "keys/id_ecdsa", "ignored": true },
63
+ { "path": "keys/id_ed25519", "ignored": true },
64
+ { "path": "certs/site.crt", "ignored": true },
65
+ { "path": "android/release.jks", "ignored": true },
66
+ { "path": "android/release.keystore", "ignored": true },
67
+ { "path": "data/app.sqlite", "ignored": true },
68
+ { "path": "data/app.sqlite3", "ignored": true },
69
+ { "path": "data/app.db", "ignored": true },
70
+ { "path": "data/dump.sql", "ignored": true },
71
+ { "path": "debug.log", "ignored": true },
72
+ { "path": "Package.JSON", "ignored": true },
73
+ { "path": "certs/SERVER.PEM", "ignored": true },
74
+
75
+ { "path": "writedocs.json", "ignored": false },
76
+ { "path": "docs/intro.mdx", "ignored": false },
77
+ { "path": "docs", "directory": true, "ignored": false },
78
+ { "path": "images/hero.png", "ignored": false },
79
+ { "path": "data/rates.json", "ignored": false },
80
+ { "path": "docs/guides/export.csv", "ignored": false },
81
+ { "path": "files/terms.pdf", "ignored": false },
82
+ { "path": "robots.txt", "ignored": false },
83
+ { "path": "openapi/api.yaml", "ignored": false },
84
+ { "path": "snippets/Chart.jsx", "ignored": false },
85
+ { "path": "theme.css", "ignored": false },
86
+ { "path": "distribution/notes.txt", "ignored": false },
87
+ { "path": "docs/environment.txt", "ignored": false },
88
+ { "path": "docs/keys.txt", "ignored": false },
89
+ { "path": "docs/package.json.txt", "ignored": false },
90
+
91
+ { "path": "public/images/logo.svg", "ignored": false },
92
+ { "path": "public", "directory": true, "ignored": false },
93
+ { "path": "public/.well-known/security.txt", "ignored": false },
94
+ { "path": "public/package.json", "ignored": false },
95
+ { "path": "public/data/dump.sql", "ignored": false },
96
+ { "path": "public/.env", "ignored": true },
97
+ { "path": "public/server.key", "ignored": true },
98
+ { "path": "public/.git/config", "ignored": true },
99
+ { "path": "public/.npmrc", "ignored": true },
100
+
101
+ { "path": "", "ignored": true },
102
+ { "path": "../secret.txt", "ignored": true },
103
+ { "path": "a/../b.txt", "ignored": true },
104
+ { "path": "./a.txt", "ignored": true },
105
+ { "path": "/abs/a.txt", "ignored": true },
106
+ { "path": "a\\b.txt", "ignored": true }
107
+ ]
108
+ },
109
+ {
110
+ "name": "a .wdignore with folder patterns, an anchored pattern, a wildcard and a negation",
111
+ "wdignore": "# drafts and internal data\ndrafts/\n/internal\n\n*.csv\n!docs/public-data/*.csv\n",
112
+ "paths": [
113
+ { "path": "drafts/a.mdx", "ignored": true },
114
+ { "path": "drafts", "directory": true, "ignored": true },
115
+ { "path": "docs/drafts/b.mdx", "ignored": true },
116
+ { "path": "drafts", "ignored": false },
117
+ { "path": "internal/x.pdf", "ignored": true },
118
+ { "path": "docs/internal/x.pdf", "ignored": false },
119
+ { "path": "data/a.csv", "ignored": true },
120
+ { "path": "docs/public-data/rates.csv", "ignored": false },
121
+ { "path": "docs/public-data/deep/x.csv", "ignored": true },
122
+ { "path": "docs/a.mdx", "ignored": false },
123
+ { "path": "images/hero.png", "ignored": false },
124
+ { "path": "public/files/list.csv", "ignored": true },
125
+ { "path": "public/drafts/a.png", "ignored": true },
126
+ { "path": "package.json", "ignored": true },
127
+ { "path": ".env", "ignored": true }
128
+ ]
129
+ },
130
+ {
131
+ "name": "a .wdignore with CRLF line endings and a byte order mark",
132
+ "wdignore": "drafts/\r\n*.csv\r\n",
133
+ "paths": [
134
+ { "path": "drafts/a.mdx", "ignored": true },
135
+ { "path": "data/a.csv", "ignored": true },
136
+ { "path": "docs/a.mdx", "ignored": false }
137
+ ]
138
+ },
139
+ {
140
+ "name": "default exclusions brought back with !",
141
+ "wdignore": "!.well-known/\n!examples/package.json\n!*.sql\n!examples/dist/\n",
142
+ "paths": [
143
+ { "path": ".well-known/security.txt", "ignored": false },
144
+ { "path": ".well-known", "directory": true, "ignored": false },
145
+ { "path": ".github/workflows/ci.yml", "ignored": true },
146
+ { "path": ".gitignore", "ignored": true },
147
+ { "path": "examples/package.json", "ignored": false },
148
+ { "path": "package.json", "ignored": true },
149
+ { "path": "other/package.json", "ignored": true },
150
+ { "path": "data/schema.sql", "ignored": false },
151
+ { "path": "data/app.db", "ignored": true },
152
+ { "path": "examples/dist/bundle.zip", "ignored": false },
153
+ { "path": "dist/bundle.zip", "ignored": true },
154
+ { "path": "other/dist/bundle.zip", "ignored": true }
155
+ ]
156
+ },
157
+ {
158
+ "name": "a folder brought back, with one file in it left out again",
159
+ "wdignore": "!.well-known/\n.well-known/private.txt\n",
160
+ "paths": [
161
+ { "path": ".well-known/security.txt", "ignored": false },
162
+ { "path": ".well-known/private.txt", "ignored": true }
163
+ ]
164
+ },
165
+ {
166
+ "name": "a file can't be brought back while its folder is left out (the .gitignore rule)",
167
+ "wdignore": "!.well-known/security.txt\n",
168
+ "paths": [
169
+ { "path": ".well-known/security.txt", "ignored": true },
170
+ { "path": ".well-known", "directory": true, "ignored": true }
171
+ ]
172
+ },
173
+ {
174
+ "name": "the locked exclusions can't be brought back",
175
+ "wdignore": "!.env\n!.env.*\n!*.pem\n!*.key\n!*.p12\n!*.pfx\n!.git/\n!.npmrc\n!id_rsa*\n!id_ed25519*\n!*\n",
176
+ "paths": [
177
+ { "path": ".env", "ignored": true },
178
+ { "path": ".env.example", "ignored": true },
179
+ { "path": "certs/server.pem", "ignored": true },
180
+ { "path": "private.key", "ignored": true },
181
+ { "path": "store.p12", "ignored": true },
182
+ { "path": "store.pfx", "ignored": true },
183
+ { "path": ".git/HEAD", "ignored": true },
184
+ { "path": ".npmrc", "ignored": true },
185
+ { "path": "id_rsa.pub", "ignored": true },
186
+ { "path": "keys/id_ed25519", "ignored": true },
187
+ { "path": "public/.env", "ignored": true },
188
+ { "path": "public/server.key", "ignored": true },
189
+ { "path": "package.json", "ignored": false },
190
+ { "path": ".gitignore", "ignored": false },
191
+ { "path": "data/dump.sql", "ignored": false }
192
+ ]
193
+ }
194
+ ]
195
+ }
@@ -0,0 +1,177 @@
1
+ // Which of a project's files are left out: never published, never read as
2
+ // content. Three layers, in this order:
3
+ //
4
+ // LOCKED_PATTERNS - secrets. Always left out; no `.wdignore` line brings
5
+ // one back.
6
+ // DEFAULT_PATTERNS - tooling and data that a docs repository holds but a
7
+ // site rarely means to publish. Left out unless the
8
+ // project's `.wdignore` re-includes them with `!`.
9
+ // the project's `.wdignore` - its own patterns, in .gitignore syntax.
10
+ //
11
+ // Files under the top-level `public/` folder are the exception to the
12
+ // second layer: putting a file there is already saying "publish this", so
13
+ // only LOCKED_PATTERNS and the project's own `.wdignore` apply to them
14
+ // (`public/.well-known/security.txt` is published; `public/.env` is not).
15
+ //
16
+ // THIS FILE IS A CONTRACT. The WriteDocs platform imports it
17
+ // (`@writedocs/generator/wdignore`) and applies the same rules when it
18
+ // syncs a project, so a `.env` never reaches its storage. It runs in a
19
+ // Cloudflare Worker there: nothing from Node (`fs`, `path`, `process`,
20
+ // `Buffer`) may be imported or used here - only `ignore`, which is plain
21
+ // JavaScript. It takes paths and the text of `.wdignore`; whoever calls it
22
+ // does the reading. scripts/wdignore.test.js bundles it for a browser to
23
+ // prove it, and runs every case in wdignore-cases.json, which the platform
24
+ // runs too.
25
+ //
26
+ // The two lists below are the only place the default exclusions are
27
+ // written. The docs (site/ and docs/dev/) describe them; nothing else
28
+ // repeats them.
29
+ import ignore from 'ignore';
30
+
31
+ /** The project's own ignore file, at its root. */
32
+ export const WDIGNORE_FILE = '.wdignore';
33
+
34
+ /** Never published and never synced, whatever `.wdignore` says. */
35
+ export const LOCKED_PATTERNS = [
36
+ // Environment files: API keys and passwords.
37
+ '.env',
38
+ '.env.*',
39
+ // The repository's own history.
40
+ '.git/',
41
+ // npm's configuration, which can hold a registry token.
42
+ '.npmrc',
43
+ // Private keys and the containers that hold them.
44
+ '*.pem',
45
+ '*.key',
46
+ '*.p12',
47
+ '*.pfx',
48
+ 'id_rsa*',
49
+ 'id_dsa*',
50
+ 'id_ecdsa*',
51
+ 'id_ed25519*',
52
+ ];
53
+
54
+ /** Left out by default; a `.wdignore` line starting with `!` brings one back. */
55
+ export const DEFAULT_PATTERNS = [
56
+ // Every file and folder whose name starts with a dot: .gitignore,
57
+ // .gitmodules, .gitattributes, .github/, .vscode/, .DS_Store, .wdignore,
58
+ // .writedocs/ ...
59
+ '.*',
60
+ // Package manifests and lockfiles.
61
+ 'package.json',
62
+ 'package-lock.json',
63
+ 'yarn.lock',
64
+ 'pnpm-lock.yaml',
65
+ 'bun.lockb',
66
+ 'bun.lock',
67
+ 'composer.json',
68
+ 'composer.lock',
69
+ // Dependencies and build output, at any depth.
70
+ 'node_modules/',
71
+ 'dist/',
72
+ // Tooling and hosting configuration.
73
+ 'tsconfig.json',
74
+ 'jsconfig.json',
75
+ 'vercel.json',
76
+ 'netlify.json',
77
+ 'netlify.toml',
78
+ 'wrangler.json',
79
+ 'wrangler.jsonc',
80
+ 'wrangler.toml',
81
+ 'Dockerfile',
82
+ 'docker-compose*.yml',
83
+ 'docker-compose*.yaml',
84
+ // Certificates and key stores.
85
+ '*.crt',
86
+ '*.jks',
87
+ '*.keystore',
88
+ // Databases and dumps.
89
+ '*.sqlite',
90
+ '*.sqlite3',
91
+ '*.db',
92
+ '*.sql',
93
+ // Logs.
94
+ '*.log',
95
+ ];
96
+
97
+ /** Paths that are left out of the site (the patterns above match them) but
98
+ * that the build reads. A sync that drops ignored files must still deliver
99
+ * these, or the build runs with different rules than the sync did. */
100
+ export const BUILD_INPUT_PATHS = ['.wdignore', '.mintignore', '.writedocs/asset-dimensions.json'];
101
+
102
+ /** The top-level folder whose files skip DEFAULT_PATTERNS. */
103
+ const PUBLIC_DIR = 'public';
104
+
105
+ function clean(text) {
106
+ return typeof text === 'string' ? text.replace(/^/, '') : '';
107
+ }
108
+
109
+ /** A project-relative POSIX path ("docs/a.pdf", no leading slash) with any
110
+ * trailing slash removed, or null when it isn't one: empty, absolute,
111
+ * with a backslash, or with a `.`/`..` segment. */
112
+ function normalize(relPath) {
113
+ if (typeof relPath !== 'string') return null;
114
+ const trimmed = relPath.replace(/\/+$/, '');
115
+ if (!trimmed || trimmed.startsWith('/') || trimmed.includes('\\')) return null;
116
+ if (trimmed.split('/').some((segment) => segment === '' || segment === '.' || segment === '..')) return null;
117
+ return trimmed;
118
+ }
119
+
120
+ /** The rules for one project. `text` is the content of its `.wdignore`
121
+ * ('' or undefined when it has none).
122
+ *
123
+ * `ignores(relPath, isDirectory)` says whether a path is left out.
124
+ * `relPath` is a POSIX path from the project root, with no leading slash;
125
+ * pass `isDirectory` as true for a folder (a pattern ending in `/` only
126
+ * matches folders). A path inside an ignored folder is ignored. A path
127
+ * that isn't a valid project-relative path is ignored too - the safe
128
+ * answer. Matching ignores letter case, as it does on the file systems
129
+ * most projects are written on.
130
+ *
131
+ * `isLocked(relPath, isDirectory)` says whether LOCKED_PATTERNS is the
132
+ * reason - a path `.wdignore` can't bring back. */
133
+ export function createWdignore(text = '') {
134
+ const own = clean(text);
135
+ const locked = ignore().add(LOCKED_PATTERNS);
136
+ const everywhere = ignore().add(DEFAULT_PATTERNS).add(own);
137
+ const inPublic = ignore().add(own);
138
+
139
+ const target = (relPath, isDirectory) => {
140
+ const normal = normalize(relPath);
141
+ return normal === null ? null : isDirectory ? `${normal}/` : normal;
142
+ };
143
+
144
+ return {
145
+ ignores(relPath, isDirectory = false) {
146
+ const p = target(relPath, isDirectory);
147
+ if (p === null) return true;
148
+ if (locked.ignores(p)) return true;
149
+ const underPublic = p.toLowerCase().startsWith(`${PUBLIC_DIR}/`);
150
+ return (underPublic ? inPublic : everywhere).ignores(p);
151
+ },
152
+ isLocked(relPath, isDirectory = false) {
153
+ const p = target(relPath, isDirectory);
154
+ return p !== null && locked.ignores(p);
155
+ },
156
+ };
157
+ }
158
+
159
+ /** Splits a list of file paths by the rules: `kept` are the files a sync
160
+ * delivers and a build may publish; `ignored` are left out. `buildInputs`
161
+ * are the ignored ones the build still has to receive (BUILD_INPUT_PATHS).
162
+ * Every list keeps the order of `paths`. */
163
+ export function partitionPaths(paths, text = '') {
164
+ const rules = createWdignore(text);
165
+ const inputs = new Set(BUILD_INPUT_PATHS);
166
+ const kept = [];
167
+ const ignored = [];
168
+ const buildInputs = [];
169
+ for (const p of paths) {
170
+ if (!rules.ignores(p)) kept.push(p);
171
+ else {
172
+ ignored.push(p);
173
+ if (inputs.has(p)) buildInputs.push(p);
174
+ }
175
+ }
176
+ return { kept, ignored, buildInputs };
177
+ }