@writedocs/generator 0.10.18 → 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/cli/run-astro.js +2 -0
- package/src/cli/run-pagefind.js +10 -4
- package/src/components/Image.astro +164 -164
- package/src/layout/BaseLayout.astro +11 -8
- package/src/lib/asset-dimensions.js +89 -0
- package/src/lib/build-memo.js +22 -0
- package/src/lib/config-schema.js +5 -0
- package/src/lib/config-schema.ts +5 -0
- package/src/lib/config.ts +8 -0
- package/src/lib/image-size.js +152 -140
- package/src/lib/json-schema-descriptions.js +1 -0
- package/src/lib/link-check.js +35 -2
- package/src/lib/og-image.js +13 -3
- package/src/lib/pages.js +312 -291
- 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/pages/[...slug].astro +15 -4
- package/src/pages/og/[...slug].png.ts +3 -2
- package/writedocs.schema.json +5 -0
package/src/lib/image-size.js
CHANGED
|
@@ -1,140 +1,152 @@
|
|
|
1
|
-
// The pixel size of an image file, read from its first bytes - for the
|
|
2
|
-
// width/height the build writes on content images, so the page keeps their
|
|
3
|
-
// place while they load (lib/rehype-image-loading.js, Image.astro).
|
|
4
|
-
// PNG, JPEG, GIF, WebP and SVG; anything else, or a file that doesn't parse,
|
|
5
|
-
// gives null and the image is left as it was.
|
|
6
|
-
//
|
|
7
|
-
// Plain JavaScript and no dependency: it's a few header fields per format.
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
if (
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
if (
|
|
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
|
-
return size
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** The
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
1
|
+
// The pixel size of an image file, read from its first bytes - for the
|
|
2
|
+
// width/height the build writes on content images, so the page keeps their
|
|
3
|
+
// place while they load (lib/rehype-image-loading.js, Image.astro).
|
|
4
|
+
// PNG, JPEG, GIF, WebP and SVG; anything else, or a file that doesn't parse,
|
|
5
|
+
// gives null and the image is left as it was.
|
|
6
|
+
//
|
|
7
|
+
// Plain JavaScript and no dependency: it's a few header fields per format.
|
|
8
|
+
//
|
|
9
|
+
// A project can hand the sizes over instead (.writedocs/asset-dimensions.json,
|
|
10
|
+
// lib/asset-dimensions.js): an image listed there is never opened here. The
|
|
11
|
+
// WriteDocs platform builds with placeholder files in place of the images,
|
|
12
|
+
// so there the list is the only place a size can come from.
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { providedImageSize } from './asset-dimensions.js';
|
|
16
|
+
import { isIgnoredFile } from './project-ignore.js';
|
|
17
|
+
|
|
18
|
+
/** JPEG keeps its size in a start-of-frame segment, somewhere after the
|
|
19
|
+
* metadata - which can be long (an embedded thumbnail), so more is read. */
|
|
20
|
+
const HEAD_BYTES = 256 * 1024;
|
|
21
|
+
|
|
22
|
+
function png(b) {
|
|
23
|
+
if (b.length < 24 || b.readUInt32BE(0) !== 0x89504e47 || b.toString('ascii', 12, 16) !== 'IHDR') return null;
|
|
24
|
+
return { width: b.readUInt32BE(16), height: b.readUInt32BE(20) };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function gif(b) {
|
|
28
|
+
if (b.length < 10 || !/^GIF8[79]a$/.test(b.toString('ascii', 0, 6))) return null;
|
|
29
|
+
return { width: b.readUInt16LE(6), height: b.readUInt16LE(8) };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function jpeg(b) {
|
|
33
|
+
if (b.length < 4 || b[0] !== 0xff || b[1] !== 0xd8) return null;
|
|
34
|
+
let offset = 2;
|
|
35
|
+
while (offset + 9 < b.length) {
|
|
36
|
+
if (b[offset] !== 0xff) return null;
|
|
37
|
+
const marker = b[offset + 1];
|
|
38
|
+
// Fill bytes and markers with no length field.
|
|
39
|
+
if (marker === 0xff) {
|
|
40
|
+
offset += 1;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd9)) {
|
|
44
|
+
offset += 2;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
// Start of frame (every SOFn but the tables that share the range).
|
|
48
|
+
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
|
|
49
|
+
return { width: b.readUInt16BE(offset + 7), height: b.readUInt16BE(offset + 5) };
|
|
50
|
+
}
|
|
51
|
+
offset += 2 + b.readUInt16BE(offset + 2);
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function webp(b) {
|
|
57
|
+
if (b.length < 30 || b.toString('ascii', 0, 4) !== 'RIFF' || b.toString('ascii', 8, 12) !== 'WEBP') return null;
|
|
58
|
+
const format = b.toString('ascii', 12, 16);
|
|
59
|
+
if (format === 'VP8 ') return { width: b.readUInt16LE(26) & 0x3fff, height: b.readUInt16LE(28) & 0x3fff };
|
|
60
|
+
if (format === 'VP8L') {
|
|
61
|
+
const bits = b.readUInt32LE(21);
|
|
62
|
+
return { width: (bits & 0x3fff) + 1, height: ((bits >> 14) & 0x3fff) + 1 };
|
|
63
|
+
}
|
|
64
|
+
if (format === 'VP8X') return { width: b.readUIntLE(24, 3) + 1, height: b.readUIntLE(27, 3) + 1 };
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** An SVG's own width and height when both are plain numbers (px or none),
|
|
69
|
+
* else its viewBox - the ratio is what matters to the page. */
|
|
70
|
+
function svg(text) {
|
|
71
|
+
const tag = text.match(/<svg\b[^>]*>/i)?.[0];
|
|
72
|
+
if (!tag) return null;
|
|
73
|
+
const attr = (name) => tag.match(new RegExp(`\\s${name}\\s*=\\s*["']([^"']*)["']`, 'i'))?.[1];
|
|
74
|
+
const pixels = (value) => (value && /^\s*\d+(\.\d+)?(px)?\s*$/i.test(value) ? parseFloat(value) : null);
|
|
75
|
+
const width = pixels(attr('width'));
|
|
76
|
+
const height = pixels(attr('height'));
|
|
77
|
+
if (width && height) return { width: Math.round(width), height: Math.round(height) };
|
|
78
|
+
const box = attr('viewBox')?.trim().split(/[\s,]+/).map(Number);
|
|
79
|
+
if (box?.length === 4 && box[2] > 0 && box[3] > 0) return { width: Math.round(box[2]), height: Math.round(box[3]) };
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** `{ width, height }` of the image in `buffer`, or null. */
|
|
84
|
+
export function imageSizeOf(buffer, extension = '') {
|
|
85
|
+
let size = null;
|
|
86
|
+
try {
|
|
87
|
+
size = extension.toLowerCase() === '.svg' ? svg(buffer.toString('utf8')) : (png(buffer) ?? gif(buffer) ?? jpeg(buffer) ?? webp(buffer) ?? svg(buffer.toString('utf8')));
|
|
88
|
+
} catch {
|
|
89
|
+
return null; // a truncated or odd file
|
|
90
|
+
}
|
|
91
|
+
return size && size.width > 0 && size.height > 0 ? size : null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const cache = new Map();
|
|
95
|
+
|
|
96
|
+
/** `{ width, height }` of the image file at `file`, or null: the size the
|
|
97
|
+
* project's asset-dimensions.json gives for it, else the one read from the
|
|
98
|
+
* file - once per file and build. */
|
|
99
|
+
export function imageSize(file, contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd()) {
|
|
100
|
+
const provided = providedImageSize(file, contentDir);
|
|
101
|
+
if (provided) return provided;
|
|
102
|
+
if (cache.has(file)) return cache.get(file);
|
|
103
|
+
let size = null;
|
|
104
|
+
try {
|
|
105
|
+
const fd = fs.openSync(file, 'r');
|
|
106
|
+
try {
|
|
107
|
+
const buffer = Buffer.alloc(HEAD_BYTES);
|
|
108
|
+
const read = fs.readSync(fd, buffer, 0, HEAD_BYTES, 0);
|
|
109
|
+
size = imageSizeOf(buffer.subarray(0, read), path.extname(file));
|
|
110
|
+
} finally {
|
|
111
|
+
fs.closeSync(fd);
|
|
112
|
+
}
|
|
113
|
+
} catch {
|
|
114
|
+
size = null;
|
|
115
|
+
}
|
|
116
|
+
cache.set(file, size);
|
|
117
|
+
return size;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The width a logo takes at `height` pixels tall, from its file's own
|
|
121
|
+
* proportions - written on the <img> beside its height, so the browser
|
|
122
|
+
* keeps the logo's place before it loads. Undefined when the file isn't
|
|
123
|
+
* in the project or can't be read: the image is then left as it was. */
|
|
124
|
+
export function logoWidth(src, height = 24, contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd()) {
|
|
125
|
+
const file = imageFileFor(src, contentDir);
|
|
126
|
+
const size = file && imageSize(file, contentDir);
|
|
127
|
+
return size ? Math.round((size.width / size.height) * height) : undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The file behind a root-relative image address ("/images/a.png"): in
|
|
131
|
+
* public/, or anywhere in the project - where the build finds it too
|
|
132
|
+
* (lib/styles-asset-integration.js). Null for any other address, and for
|
|
133
|
+
* a file the project's ignore rules leave out (lib/wdignore.js): it isn't
|
|
134
|
+
* published, so it's as if it weren't there. */
|
|
135
|
+
export function imageFileFor(src, contentDir) {
|
|
136
|
+
if (typeof src !== 'string' || !src.startsWith('/') || src.startsWith('//')) return null;
|
|
137
|
+
let pathname;
|
|
138
|
+
try {
|
|
139
|
+
pathname = decodeURIComponent(src.split(/[?#]/)[0]);
|
|
140
|
+
} catch {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
const segments = pathname.replace(/^\/+/, '').split('/');
|
|
144
|
+
if (segments.some((segment) => segment === '' || segment === '.' || segment === '..')) return null;
|
|
145
|
+
for (const base of [path.join(contentDir, 'public'), contentDir]) {
|
|
146
|
+
const file = path.join(base, ...segments);
|
|
147
|
+
try {
|
|
148
|
+
if (fs.statSync(file).isFile() && !isIgnoredFile(file, contentDir)) return file;
|
|
149
|
+
} catch {}
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
@@ -168,6 +168,7 @@ export const DESCRIPTIONS = {
|
|
|
168
168
|
|
|
169
169
|
seo: 'Default metadata for every page. A page\'s own `seo` frontmatter overrides it field by field.',
|
|
170
170
|
'seo.ogImage': 'Image shown in link previews - a path or a full URL.',
|
|
171
|
+
'seo.generatedOgImage': 'Whether a page without an `ogImage` gets a link preview image generated at build time. Default true; set false to skip them, which shortens the build of a large site.',
|
|
171
172
|
'seo.ogType': 'The og:type of pages. Default "website".',
|
|
172
173
|
'seo.twitterCard': 'X/Twitter card style. Default "summary_large_image" with an `ogImage`, "summary" without.',
|
|
173
174
|
'seo.keywords': 'Keywords for the <meta name="keywords"> tag.',
|
package/src/lib/link-check.js
CHANGED
|
@@ -38,6 +38,7 @@ import { createJsonLocator, contextMenuOptions } from './config-schema.js';
|
|
|
38
38
|
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
39
39
|
import { folderAddresses } from './folder-redirects.js';
|
|
40
40
|
import { isPublishedFile, readSpecPaths } from './project-files.js';
|
|
41
|
+
import { projectIgnore } from './project-ignore.js';
|
|
41
42
|
import { readMovedApiPages } from './moved-api-pages.js';
|
|
42
43
|
|
|
43
44
|
// The same github-slugger Astro builds page URLs and heading ids with -
|
|
@@ -307,6 +308,7 @@ export async function checkLinks(contentDir, configText) {
|
|
|
307
308
|
// Where generated API pages used to be - redirects too (lib/moved-api-pages.js).
|
|
308
309
|
for (const { source } of readMovedApiPages(contentDir)) redirects.add(`${source}/`);
|
|
309
310
|
const specs = readSpecPaths(contentDir);
|
|
311
|
+
const rules = projectIgnore(contentDir);
|
|
310
312
|
const special = new Set(['/llms.txt', '/llms-full.txt', '/404.html', '/404/', '/robots.txt']);
|
|
311
313
|
if (config.domain) ['/sitemap.xml', '/sitemap-index.xml'].forEach((p) => special.add(p));
|
|
312
314
|
const knownUrls = [...pages.keys(), ...generated, ...redirects];
|
|
@@ -459,14 +461,30 @@ export async function checkLinks(contentDir, configText) {
|
|
|
459
461
|
// Any other file.
|
|
460
462
|
if (special.has(pathname)) return;
|
|
461
463
|
const segments = pathname.replace(/^\/+/, '').split('/');
|
|
462
|
-
|
|
464
|
+
// A file the ignore rules leave out (lib/wdignore.js) is never published,
|
|
465
|
+
// in public/ or anywhere else - so a link to it is a broken one.
|
|
466
|
+
const leftOut = (rel) => {
|
|
467
|
+
if (!rules.ignores(rel)) return false;
|
|
468
|
+
report(
|
|
469
|
+
`${value} - ${rel} is in the project, but the ignore rules leave it out, so it isn't published.`,
|
|
470
|
+
rules.isLocked(rel)
|
|
471
|
+
? "It's a kind of file that is never published (a key, a .env, .git/)."
|
|
472
|
+
: `To publish it, bring it back in .wdignore - a line with !${rel}, or its folder's (!folder/) when the whole folder is left out.`
|
|
473
|
+
);
|
|
474
|
+
return true;
|
|
475
|
+
};
|
|
476
|
+
if (fs.existsSync(path.join(contentDir, 'public', ...segments))) {
|
|
477
|
+
leftOut(['public', ...segments].join('/'));
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
463
480
|
const inProject = path.join(contentDir, ...segments);
|
|
464
481
|
if (fs.existsSync(inProject)) {
|
|
482
|
+
if (leftOut(segments.join('/'))) return;
|
|
465
483
|
// The build publishes the project's files at their own path
|
|
466
484
|
// (lib/project-files.js) - images, PDFs, data files. What it doesn't:
|
|
467
485
|
// the files the site is built from (writedocs.json, an OpenAPI spec),
|
|
468
486
|
// anything in a dot-folder, and kinds of file it doesn't know.
|
|
469
|
-
if (isPublishedFile(segments.join('/'), { specs })) return;
|
|
487
|
+
if (isPublishedFile(segments.join('/'), { specs, rules })) return;
|
|
470
488
|
// An image or font a page displays is copied wherever it is
|
|
471
489
|
// (lib/styles-asset-integration.js).
|
|
472
490
|
if (ASSET_EXTENSIONS.has(extension) && displayedAssets.has(pathname)) return;
|
|
@@ -489,6 +507,21 @@ export async function checkLinks(contentDir, configText) {
|
|
|
489
507
|
for (const link of scan.links) await check(link.href, { file, line: link.line, kind: link.kind, pageUrl, fileAbs: abs });
|
|
490
508
|
for (const spec of scan.imports) {
|
|
491
509
|
const snippet = snippetPath(spec, abs);
|
|
510
|
+
// A snippet the ignore rules leave out (lib/wdignore.js) isn't part of
|
|
511
|
+
// the project a site is built from: where the rules are applied before
|
|
512
|
+
// the build (the WriteDocs platform's sync), the import finds no file.
|
|
513
|
+
const snippetRel = relOf(snippet);
|
|
514
|
+
if (!snippetRel.startsWith('..') && fs.existsSync(snippet) && rules.ignores(snippetRel)) {
|
|
515
|
+
const key = [file, 'import', snippetRel].join('\n');
|
|
516
|
+
if (!reported.has(key)) {
|
|
517
|
+
reported.add(key);
|
|
518
|
+
broken.push({
|
|
519
|
+
file,
|
|
520
|
+
message: `import of ${spec} - ${snippetRel} is in the project, but the ignore rules leave it out, so the build can't count on it.`,
|
|
521
|
+
suggestion: rules.isLocked(snippetRel) ? 'Move the snippet out of that path.' : `Bring it back in .wdignore (!${snippetRel}), or move the snippet.`,
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
}
|
|
492
525
|
if (seen.has(snippet) || snippetsChecked.has(`${snippet}\n${pageUrl}`)) continue;
|
|
493
526
|
snippetsChecked.add(`${snippet}\n${pageUrl}`);
|
|
494
527
|
await checkFile(snippet, pageUrl, new Set([...seen, snippet]));
|
package/src/lib/og-image.js
CHANGED
|
@@ -27,8 +27,13 @@ export const OG_HEIGHT = 630;
|
|
|
27
27
|
|
|
28
28
|
/** Whether a page gets a generated picture: it has none of its own (`seo`
|
|
29
29
|
* is the site's with the page's merged over it), search engines index it,
|
|
30
|
-
* and it's a page - not a link to somewhere else (`url`)
|
|
31
|
-
|
|
30
|
+
* and it's a page - not a link to somewhere else (`url`) - on a site that
|
|
31
|
+
* hasn't turned generated pictures off. */
|
|
32
|
+
export function wantsGeneratedCard(seo, data = {}, siteSeo = {}) {
|
|
33
|
+
// writedocs.json `"seo": { "generatedOgImage": false }` turns them off
|
|
34
|
+
// for the whole site - `siteSeo` is the site's own `seo`, since the merged
|
|
35
|
+
// one (mergeSeo()) only carries the fields a page can set.
|
|
36
|
+
if (siteSeo?.generatedOgImage === false) return false;
|
|
32
37
|
return !seo?.ogImage && !seo?.noindex && !data.url;
|
|
33
38
|
}
|
|
34
39
|
|
|
@@ -151,8 +156,13 @@ export async function ogImageSvg(options) {
|
|
|
151
156
|
export async function ogImagePng(options) {
|
|
152
157
|
const svg = await ogImageSvg(options);
|
|
153
158
|
const { Resvg } = await loadRenderer();
|
|
154
|
-
const
|
|
159
|
+
const resvg = new Resvg(svg, { fitTo: { mode: 'width', value: OG_WIDTH } });
|
|
160
|
+
const rendered = resvg.render();
|
|
155
161
|
const png = Buffer.from(rendered.asPng());
|
|
162
|
+
// Both live in the WebAssembly memory, which nothing collects: freeing
|
|
163
|
+
// only the image left the parsed SVG of every card there until the build
|
|
164
|
+
// ended, and past ~800 cards each one took three times as long.
|
|
156
165
|
rendered.free?.();
|
|
166
|
+
resvg.free?.();
|
|
157
167
|
return png;
|
|
158
168
|
}
|