@writedocs/generator 0.10.13 → 0.10.15
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/astro.config.mjs +610 -562
- package/package.json +32 -32
- package/src/cli/generate-api-pages.js +418 -384
- package/src/cli/write-redirects-file.js +125 -116
- package/src/components/Image.astro +19 -3
- package/src/icons/README.md +12 -0
- package/src/layout/BaseLayout.astro +877 -831
- package/src/layout/components/AnalyticsScripts.astro +45 -77
- package/src/layout/components/ConsentBanner.astro +118 -0
- package/src/layout/styles/base.css +8 -0
- package/src/lib/config-schema.js +97 -10
- package/src/lib/config-schema.ts +1882 -1768
- package/src/lib/consent.js +102 -0
- package/src/lib/image-size.js +130 -0
- package/src/lib/item-pages.js +63 -0
- package/src/lib/json-ld.js +127 -0
- package/src/lib/json-schema-descriptions.js +226 -212
- package/src/lib/last-modified.js +52 -0
- package/src/lib/link-check.js +506 -500
- package/src/lib/meta-description.js +41 -0
- package/src/lib/mintlify-convert.js +605 -603
- package/src/lib/moved-api-pages.js +54 -0
- package/src/lib/project-files-integration.js +61 -0
- package/src/lib/project-files.js +126 -0
- package/src/lib/rehype-image-loading.js +71 -0
- package/src/lib/robots-txt.js +16 -0
- package/src/lib/seo-tags.js +137 -0
- package/src/lib/styles-asset-integration.js +21 -3
- package/src/pages/[...slug].astro +19 -0
- package/src/scripts/consent.ts +62 -0
- package/writedocs.schema.json +100 -0
|
@@ -1,116 +1,125 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { readConfigText } from '../lib/config-file.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
// config
|
|
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
|
-
const
|
|
37
|
-
const
|
|
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
|
-
const
|
|
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
|
-
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { readConfigText } from '../lib/config-file.js';
|
|
4
|
+
import { readMovedApiPages } from '../lib/moved-api-pages.js';
|
|
5
|
+
|
|
6
|
+
// Astro's own `output: 'static'` redirects (both writedocs.json's `redirects`
|
|
7
|
+
// feature and the automatic "/" -> first-nav-page redirect in
|
|
8
|
+
// [...slug].astro) are never real server/edge redirects - with no adapter
|
|
9
|
+
// installed, Astro can only ever emit a client-side
|
|
10
|
+
// `<meta http-equiv="refresh">` page (see redirectSchema's own comment in
|
|
11
|
+
// lib/config.ts for why `permanent`/status-code isn't even offered as a
|
|
12
|
+
// config option). That page is a real, working redirect, but it's a flash
|
|
13
|
+
// of unstyled content before the browser honors the meta-refresh - visibly
|
|
14
|
+
// bad on a deployed site, and exactly what a reader lands on for a second
|
|
15
|
+
// or more depending on network/host latency.
|
|
16
|
+
//
|
|
17
|
+
// Cloudflare Pages and Netlify both read a `_redirects` file at the root
|
|
18
|
+
// of the deployed output and turn each line into a real edge-level
|
|
19
|
+
// redirect - no page ever renders, no flash. This step generates that file
|
|
20
|
+
// as a purely additive improvement: hosts that honor `_redirects` get an
|
|
21
|
+
// instant real redirect, and every other static host (S3, GitHub Pages,
|
|
22
|
+
// etc.) falls back to the meta-refresh page Astro already generates, which
|
|
23
|
+
// keeps working exactly as before. Neither this file nor its absence ever
|
|
24
|
+
// changes what Astro itself builds.
|
|
25
|
+
// `contentDir` rather than a pre-loaded config object: this runs from
|
|
26
|
+
// build.js, invoked by plain `node`, not through Astro/Vite's TS
|
|
27
|
+
// transform - loadDocsConfig() (lib/config.ts) can't be imported here the
|
|
28
|
+
// way astro.config.mjs and every .astro file import it. writedocs.json has
|
|
29
|
+
// already been through preflightCheck() (valid JSON) and Astro's own
|
|
30
|
+
// loadDocsConfig() call inside astro.config.mjs (schema-valid, or the
|
|
31
|
+
// build above would already have failed) by the time this runs, so a
|
|
32
|
+
// second, un-validated JSON.parse() here is safe - this only ever reads
|
|
33
|
+
// the one `redirects` array back out, nothing that needs zod's defaults
|
|
34
|
+
// or transforms.
|
|
35
|
+
export function writeRedirectsFile(distDir, contentDir) {
|
|
36
|
+
const writedocsJson = JSON.parse(readConfigText(contentDir));
|
|
37
|
+
const redirects = writedocsJson.redirects ?? [];
|
|
38
|
+
const lines = [];
|
|
39
|
+
|
|
40
|
+
// writedocs.json's own `redirects` array - one line per entry, always 301
|
|
41
|
+
// (there's nowhere else in this project to source a status code from;
|
|
42
|
+
// see redirectSchema's comment in lib/config.ts on why the schema itself
|
|
43
|
+
// has no field for one - a redirect a site author deliberately
|
|
44
|
+
// configured is safe to treat as permanent).
|
|
45
|
+
for (const { source, destination } of redirects) {
|
|
46
|
+
lines.push(`${source} ${destination} 301`);
|
|
47
|
+
}
|
|
48
|
+
// Generated API pages that changed address (lib/moved-api-pages.js) -
|
|
49
|
+
// after the site's own, which win for the same address (first match).
|
|
50
|
+
// With and without the trailing slash: `_redirects` matches the path
|
|
51
|
+
// as requested.
|
|
52
|
+
for (const { source, destination } of readMovedApiPages(contentDir)) {
|
|
53
|
+
if (redirects.some((r) => r.source.replace(/\/+$/, '') === source)) continue;
|
|
54
|
+
lines.push(`${source} ${destination} 301`, `${source}/ ${destination} 301`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The automatic root "/" redirect (see the `rootRedirect` block in
|
|
58
|
+
// [...slug].astro's getStaticPaths()) isn't reachable from here without
|
|
59
|
+
// re-deriving the same first-nav-page resolution Astro already did -
|
|
60
|
+
// instead, read it straight back out of the meta-refresh tag in the
|
|
61
|
+
// already-built dist/index.html. If writedocs.json already defines its own
|
|
62
|
+
// `/` redirect above, that one wins (first match wins in `_redirects`,
|
|
63
|
+
// and it's already listed first) - skip the automatic one entirely
|
|
64
|
+
// rather than emit a second, unreachable rule for the same path.
|
|
65
|
+
const hasExplicitRootRedirect = redirects.some((r) => r.source === '/');
|
|
66
|
+
const indexHtmlPath = path.join(distDir, 'index.html');
|
|
67
|
+
if (!hasExplicitRootRedirect && fs.existsSync(indexHtmlPath)) {
|
|
68
|
+
const html = fs.readFileSync(indexHtmlPath, 'utf8');
|
|
69
|
+
const match = html.match(/<meta http-equiv="refresh" content="\d+;\s*url=([^"]+)"/i);
|
|
70
|
+
// 302, not 301: see [...slug].astro - a browser keeps a 301 for good,
|
|
71
|
+
// and "/" stops redirecting once the site gets a home page.
|
|
72
|
+
if (match) lines.push(`/ ${match[1]} 302`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Folder addresses redirecting to the first page under them
|
|
76
|
+
// (lib/folder-redirects.js). Read back from the built pages like "/"
|
|
77
|
+
// above: Astro writes a temporary (302) redirect's page with a 2-second
|
|
78
|
+
// refresh and a permanent one's with 0 - writedocs.json's own redirects
|
|
79
|
+
// and `url` pages are permanent, so the 2-second ones are exactly the
|
|
80
|
+
// automatic folder redirects. (Read before rewrite-redirect-pages.js makes
|
|
81
|
+
// every redirect page go at once.)
|
|
82
|
+
for (const { url, target } of temporaryRedirectPages(distDir)) {
|
|
83
|
+
lines.push(`${url} ${target} 302`, `${url.replace(/\/$/, '')} ${target} 302`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (lines.length === 0) return;
|
|
87
|
+
|
|
88
|
+
const generated = [
|
|
89
|
+
'# Generated by writedocs (see src/cli/write-redirects-file.js).',
|
|
90
|
+
'# Gives Cloudflare Pages / Netlify a real edge redirect for the same',
|
|
91
|
+
"# paths Astro's own static-mode <meta http-equiv=\"refresh\"> pages",
|
|
92
|
+
'# already cover - other static hosts keep using those pages as-is.',
|
|
93
|
+
...lines,
|
|
94
|
+
'',
|
|
95
|
+
].join('\n');
|
|
96
|
+
|
|
97
|
+
const redirectsPath = path.join(distDir, '_redirects');
|
|
98
|
+
// A hand-written public/_redirects is already copied into dist by Astro
|
|
99
|
+
// by this point - preserve it (and its priority: first match wins) by
|
|
100
|
+
// appending after it rather than overwriting.
|
|
101
|
+
const existing = fs.existsSync(redirectsPath) ? fs.readFileSync(redirectsPath, 'utf8').replace(/\s*$/, '\n\n') : '';
|
|
102
|
+
fs.writeFileSync(redirectsPath, existing + generated);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Astro's pages for temporary redirects below the site's root, as
|
|
106
|
+
* `{ url: "/docs/creator/", target }`. */
|
|
107
|
+
function temporaryRedirectPages(distDir) {
|
|
108
|
+
const found = [];
|
|
109
|
+
const walk = (dir) => {
|
|
110
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
111
|
+
const full = path.join(dir, entry.name);
|
|
112
|
+
if (entry.isDirectory()) {
|
|
113
|
+
if (!['_astro', 'pagefind', 'pagefind-spaces'].includes(entry.name)) walk(full);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (entry.name !== 'index.html' || dir === distDir) continue;
|
|
117
|
+
const html = fs.readFileSync(full, 'utf8');
|
|
118
|
+
if (!html.includes('<title>Redirecting to:')) continue;
|
|
119
|
+
const match = html.match(/<meta http-equiv="refresh" content="2;\s*url=([^"]+)"/i);
|
|
120
|
+
if (match) found.push({ url: `/${path.relative(distDir, dir).split(path.sep).join('/')}/`, target: match[1] });
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
walk(distDir);
|
|
124
|
+
return found.sort((a, b) => a.url.localeCompare(b.url));
|
|
125
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
import { extraClasses } from './class-names';
|
|
3
|
+
import { imageFileFor, imageSize } from '../lib/image-size.js';
|
|
3
4
|
// Block-level (own line, centered) - unlike Hint (inline/text-level),
|
|
4
5
|
// this is meant to be dropped in on its own the way Callout/Card are.
|
|
5
6
|
//
|
|
@@ -60,8 +61,23 @@ interface Props {
|
|
|
60
61
|
// lib/image-zoom-rules.js handles both cases identically rather than
|
|
61
62
|
// needing to special-case this component.
|
|
62
63
|
noZoom?: boolean;
|
|
64
|
+
// "lazy" (the default): the browser fetches the image when the reader
|
|
65
|
+
// gets near it. "eager": with the page - for an image at the very top.
|
|
66
|
+
// The page's first image gets "eager" by itself
|
|
67
|
+
// (lib/rehype-image-loading.js).
|
|
68
|
+
loading?: "lazy" | "eager";
|
|
63
69
|
}
|
|
64
|
-
const { src, srcDark, size, alt = "", caption, noZoom } = Astro.props as Props;
|
|
70
|
+
const { src, srcDark, size, alt = "", caption, noZoom, loading = "lazy" } = Astro.props as Props;
|
|
71
|
+
// Each file's own pixel size, as width/height: the browser keeps the
|
|
72
|
+
// image's place before it arrives, so the text below doesn't jump. The
|
|
73
|
+
// CSS below already has height: auto, so the image still scales.
|
|
74
|
+
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
75
|
+
const sizeOf = (address?: string) => {
|
|
76
|
+
const file = address ? imageFileFor(address, contentDir) : null;
|
|
77
|
+
return (file && imageSize(file)) || { width: undefined, height: undefined };
|
|
78
|
+
};
|
|
79
|
+
const light = sizeOf(src);
|
|
80
|
+
const dark = sizeOf(srcDark);
|
|
65
81
|
const sizeStyle = size ? `--wd-image-size:${size}` : undefined;
|
|
66
82
|
---
|
|
67
83
|
|
|
@@ -69,8 +85,8 @@ const sizeStyle = size ? `--wd-image-size:${size}` : undefined;
|
|
|
69
85
|
class:list={["wd-image-figure", caption && "wd-image-figure-framed", srcDark && "wd-image-has-dark", extraClasses(Astro.props)]}
|
|
70
86
|
style={sizeStyle}
|
|
71
87
|
>
|
|
72
|
-
<img src={src} alt={alt} class="wd-image wd-image-light" nozoom={noZoom || undefined} />
|
|
73
|
-
{srcDark && <img src={srcDark} alt={alt} class="wd-image wd-image-dark" nozoom={noZoom || undefined} />}
|
|
88
|
+
<img src={src} alt={alt} class="wd-image wd-image-light" nozoom={noZoom || undefined} loading={loading} decoding="async" width={light.width} height={light.height} />
|
|
89
|
+
{srcDark && <img src={srcDark} alt={alt} class="wd-image wd-image-dark" nozoom={noZoom || undefined} loading={loading} decoding="async" width={dark.width} height={dark.height} />}
|
|
74
90
|
{caption && <figcaption class="wd-image-caption">{caption}</figcaption>}
|
|
75
91
|
</figure>
|
|
76
92
|
<style>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
This folder is here on purpose, and stays empty of icons.
|
|
2
|
+
|
|
3
|
+
The icon integration (astro-icon) looks for local SVG icons in `src/icons`
|
|
4
|
+
under the project root - which, for writedocs, is this package. Since
|
|
5
|
+
astro-icon 1.2.0, a missing folder prints a warning on every `writedocs dev`
|
|
6
|
+
and `writedocs build`:
|
|
7
|
+
|
|
8
|
+
Failed to load icons from "src/icons": ENOENT: no such file or directory
|
|
9
|
+
|
|
10
|
+
writedocs has no local icons: every icon comes from the installed icon sets
|
|
11
|
+
(see src/lib/icons.js). Don't delete the folder; `scripts/package-files.test.js`
|
|
12
|
+
checks it ships in the npm package.
|