@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.
- package/README.md +28 -28
- package/astro.config.mjs +645 -645
- package/bin/writedocs.js +359 -356
- package/package.json +99 -99
- package/src/cli/base-path-layout.js +44 -44
- package/src/cli/build-auth.js +135 -135
- package/src/cli/build.js +223 -203
- package/src/cli/check-built-styles.js +41 -41
- package/src/cli/convert.js +124 -124
- package/src/cli/dev.js +269 -268
- package/src/cli/exit-when-flushed.js +42 -42
- package/src/cli/generate-api-pages.js +509 -507
- package/src/cli/og-image-worker.js +20 -20
- package/src/cli/og-images.js +70 -70
- package/src/cli/page-list-watch.js +71 -69
- package/src/cli/run-astro.js +97 -97
- package/src/cli/run-pagefind.js +154 -154
- package/src/cli/write-mcp-files.js +103 -103
- package/src/cli/write-redirects-file.js +158 -158
- package/src/components/ApiReferencePanel.astro +1792 -1792
- package/src/components/AppIcon.astro +51 -51
- package/src/components/Card.astro +193 -193
- package/src/components/CodeGroup.astro +63 -39
- package/src/components/CopyPageMenu.astro +374 -374
- package/src/components/Hint.astro +188 -188
- package/src/components/Icon.astro +72 -72
- package/src/components/Image.astro +165 -165
- package/src/components/Tile.astro +67 -67
- package/src/components/Video.astro +137 -137
- package/src/content.config.ts +219 -203
- package/src/layout/BaseLayout.astro +908 -908
- package/src/layout/components/AnalyticsScripts.astro +49 -49
- package/src/layout/components/Breadcrumbs.astro +98 -98
- package/src/layout/components/ConsentBanner.astro +119 -119
- package/src/layout/components/MobileMenu.astro +225 -225
- package/src/layout/components/NavTree.astro +422 -408
- package/src/layout/components/Sidebar.astro +232 -232
- package/src/layout/components/SiteFooter.astro +87 -87
- package/src/layout/components/TopBar.astro +386 -386
- package/src/layout/styles/base.css +332 -326
- package/src/layout/styles/mobile-menu.css +285 -285
- package/src/lib/agent-markdown.js +164 -164
- package/src/lib/asset-dimensions.js +89 -89
- package/src/lib/base-path.js +90 -90
- package/src/lib/cli-report.js +22 -22
- package/src/lib/code-line-numbers.js +13 -0
- package/src/lib/config-schema.js +12 -1
- package/src/lib/config-schema.ts +2523 -2509
- package/src/lib/config.ts +1331 -1316
- package/src/lib/consent.js +108 -108
- package/src/lib/content-check.js +4 -1
- package/src/lib/content-store-reload-integration.js +101 -101
- package/src/lib/drafts.js +186 -0
- package/src/lib/icon-data-dev.js +53 -53
- package/src/lib/image-size.js +152 -152
- package/src/lib/json-schema-descriptions.js +228 -228
- package/src/lib/link-check.js +553 -544
- package/src/lib/llms-index.ts +90 -90
- package/src/lib/mcp-dev-integration.js +65 -65
- package/src/lib/mcp-index.ts +102 -102
- package/src/lib/mintlify-convert.js +605 -605
- package/src/lib/moved-api-pages.js +49 -49
- package/src/lib/og-cards.js +33 -33
- package/src/lib/og-image.js +235 -235
- package/src/lib/openapi-dev-status.js +80 -80
- package/src/lib/openapi-reload-integration.js +193 -193
- package/src/lib/pages.js +346 -312
- package/src/lib/project-files-integration.js +116 -116
- package/src/lib/project-files.js +137 -137
- package/src/lib/rehype-base-path.js +62 -62
- package/src/lib/rehype-image-loading.js +71 -71
- package/src/lib/robots-txt.js +21 -21
- package/src/lib/search-scope.js +44 -44
- package/src/lib/selector-placement.js +50 -50
- package/src/lib/seo-tags.js +157 -155
- package/src/lib/shiki-code-block.js +7 -1
- package/src/lib/site-base.ts +27 -27
- package/src/lib/styles-asset-integration.js +291 -291
- package/src/lib/wdignore-cases.json +195 -195
- package/src/lib/wdignore.js +177 -177
- package/src/pages/404.astro +63 -63
- package/src/pages/[...slug].astro +1773 -1608
- package/src/pages/[...slug].md.ts +93 -93
- package/src/pages/llms-full.txt.ts +88 -88
- package/src/pages/og/[...slug].png.ts +63 -63
- package/src/scripts/mobile-menu.ts +80 -80
- package/src/scripts/search.ts +245 -245
- package/src/scripts/sidebar-scroll.ts +61 -61
- package/src/styles/global.css +40 -40
package/src/lib/icon-data-dev.js
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
// `writedocs dev`: astro-icon's icons reach the page from a file, not through
|
|
2
|
-
// Vite's module graph.
|
|
3
|
-
//
|
|
4
|
-
// In dev, astro-icon's `virtual:astro-icon` module holds every icon of every
|
|
5
|
-
// installed @iconify-json collection - `export default {...}` with ~15 MB of
|
|
6
|
-
// JSON (a build keeps only the icons pages use). Vite then transforms that
|
|
7
|
-
// module for the server, writes a source map for it and inlines it (the 15 MB
|
|
8
|
-
// again, in base64), Node reads that map back, and V8 compiles the whole
|
|
9
|
-
// thing: ~5 s of the first page of every `writedocs dev`, whatever the site,
|
|
10
|
-
// and over 1 GB of memory (measured in WD-312's round 03 - see
|
|
11
|
-
// docs/dev/docs/dev-preview.mdx).
|
|
12
|
-
//
|
|
13
|
-
// So the data astro-icon puts in the module goes to a file instead, and the
|
|
14
|
-
// module becomes `export default JSON.parse(readFileSync(<file>))` - the same
|
|
15
|
-
// object, the same icons; only the 15 MB stops passing through Vite.
|
|
16
|
-
//
|
|
17
|
-
// Only in `writedocs dev` (`apply: 'serve'`), and only for the server: a
|
|
18
|
-
// build doesn't load this plugin at all. A module that doesn't look the way
|
|
19
|
-
// this expects (a new astro-icon) is left as it is - the preview is slower
|
|
20
|
-
// again, not broken.
|
|
21
|
-
import fs from 'node:fs';
|
|
22
|
-
import path from 'node:path';
|
|
23
|
-
|
|
24
|
-
const MODULE_ID = '\0virtual:astro-icon';
|
|
25
|
-
|
|
26
|
-
/** astro-icon's module - `export default <json>;\nexport const config = <json>` -
|
|
27
|
-
* as { data, config } (the two JSON texts), or null for anything else. */
|
|
28
|
-
export function splitAstroIconModule(code) {
|
|
29
|
-
const match = /^export default (\{[\s\S]*\});\nexport const config = (\{[\s\S]*\})\s*$/.exec(code);
|
|
30
|
-
return match ? { data: match[1], config: match[2] } : null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** The module that replaces it: the data read from `file`, `config` as it was. */
|
|
34
|
-
export function iconDataModule(file, config) {
|
|
35
|
-
return `import { readFileSync } from 'node:fs';\nexport default JSON.parse(readFileSync(${JSON.stringify(file)}, 'utf-8'));\nexport const config = ${config};\n`;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** The Vite plugin; the data goes to `<dir>/astro-icon.json`. */
|
|
39
|
-
export function iconDataFromFile(dir) {
|
|
40
|
-
return {
|
|
41
|
-
name: 'writedocs:astro-icon-data-file',
|
|
42
|
-
apply: 'serve',
|
|
43
|
-
transform(code, id) {
|
|
44
|
-
if (id !== MODULE_ID || this.environment?.config?.consumer === 'client') return null;
|
|
45
|
-
const parts = splitAstroIconModule(code);
|
|
46
|
-
if (!parts) return null;
|
|
47
|
-
const file = path.join(dir, 'astro-icon.json');
|
|
48
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
49
|
-
fs.writeFileSync(file, parts.data);
|
|
50
|
-
return { code: iconDataModule(file, parts.config), map: null };
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
}
|
|
1
|
+
// `writedocs dev`: astro-icon's icons reach the page from a file, not through
|
|
2
|
+
// Vite's module graph.
|
|
3
|
+
//
|
|
4
|
+
// In dev, astro-icon's `virtual:astro-icon` module holds every icon of every
|
|
5
|
+
// installed @iconify-json collection - `export default {...}` with ~15 MB of
|
|
6
|
+
// JSON (a build keeps only the icons pages use). Vite then transforms that
|
|
7
|
+
// module for the server, writes a source map for it and inlines it (the 15 MB
|
|
8
|
+
// again, in base64), Node reads that map back, and V8 compiles the whole
|
|
9
|
+
// thing: ~5 s of the first page of every `writedocs dev`, whatever the site,
|
|
10
|
+
// and over 1 GB of memory (measured in WD-312's round 03 - see
|
|
11
|
+
// docs/dev/docs/dev-preview.mdx).
|
|
12
|
+
//
|
|
13
|
+
// So the data astro-icon puts in the module goes to a file instead, and the
|
|
14
|
+
// module becomes `export default JSON.parse(readFileSync(<file>))` - the same
|
|
15
|
+
// object, the same icons; only the 15 MB stops passing through Vite.
|
|
16
|
+
//
|
|
17
|
+
// Only in `writedocs dev` (`apply: 'serve'`), and only for the server: a
|
|
18
|
+
// build doesn't load this plugin at all. A module that doesn't look the way
|
|
19
|
+
// this expects (a new astro-icon) is left as it is - the preview is slower
|
|
20
|
+
// again, not broken.
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
|
|
24
|
+
const MODULE_ID = '\0virtual:astro-icon';
|
|
25
|
+
|
|
26
|
+
/** astro-icon's module - `export default <json>;\nexport const config = <json>` -
|
|
27
|
+
* as { data, config } (the two JSON texts), or null for anything else. */
|
|
28
|
+
export function splitAstroIconModule(code) {
|
|
29
|
+
const match = /^export default (\{[\s\S]*\});\nexport const config = (\{[\s\S]*\})\s*$/.exec(code);
|
|
30
|
+
return match ? { data: match[1], config: match[2] } : null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The module that replaces it: the data read from `file`, `config` as it was. */
|
|
34
|
+
export function iconDataModule(file, config) {
|
|
35
|
+
return `import { readFileSync } from 'node:fs';\nexport default JSON.parse(readFileSync(${JSON.stringify(file)}, 'utf-8'));\nexport const config = ${config};\n`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The Vite plugin; the data goes to `<dir>/astro-icon.json`. */
|
|
39
|
+
export function iconDataFromFile(dir) {
|
|
40
|
+
return {
|
|
41
|
+
name: 'writedocs:astro-icon-data-file',
|
|
42
|
+
apply: 'serve',
|
|
43
|
+
transform(code, id) {
|
|
44
|
+
if (id !== MODULE_ID || this.environment?.config?.consumer === 'client') return null;
|
|
45
|
+
const parts = splitAstroIconModule(code);
|
|
46
|
+
if (!parts) return null;
|
|
47
|
+
const file = path.join(dir, 'astro-icon.json');
|
|
48
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
49
|
+
fs.writeFileSync(file, parts.data);
|
|
50
|
+
return { code: iconDataModule(file, parts.config), map: null };
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
package/src/lib/image-size.js
CHANGED
|
@@ -1,152 +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
|
-
// 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
|
-
}
|
|
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
|
+
}
|