@writedocs/generator 0.10.19 → 0.11.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/astro.config.mjs +32 -8
- package/package.json +96 -93
- package/src/cli/base-path-layout.js +44 -0
- package/src/cli/build.js +177 -168
- package/src/cli/check-built-styles.js +41 -33
- package/src/cli/dev.js +261 -244
- package/src/cli/generate-api-pages.js +9 -5
- package/src/cli/page-list-watch.js +69 -66
- package/src/cli/run-pagefind.js +154 -148
- package/src/cli/write-mcp-files.js +103 -96
- package/src/cli/write-redirects-file.js +25 -8
- package/src/components/AppIcon.astro +51 -50
- package/src/components/Card.astro +193 -192
- package/src/components/CopyPageMenu.astro +374 -370
- package/src/components/Hint.astro +188 -187
- package/src/components/Icon.astro +72 -71
- package/src/components/Image.astro +165 -164
- package/src/components/Tile.astro +67 -66
- package/src/components/Video.astro +137 -136
- package/src/layout/BaseLayout.astro +21 -12
- package/src/layout/components/AnalyticsScripts.astro +6 -2
- package/src/layout/components/Breadcrumbs.astro +98 -97
- package/src/layout/components/ConsentBanner.astro +119 -118
- package/src/layout/components/MobileMenu.astro +229 -228
- package/src/layout/components/NavTree.astro +393 -392
- package/src/layout/components/Sidebar.astro +232 -231
- package/src/layout/components/SiteFooter.astro +87 -86
- package/src/layout/components/TopBar.astro +386 -385
- package/src/lib/agent-markdown.js +164 -162
- package/src/lib/asset-dimensions.js +89 -0
- package/src/lib/base-path.js +90 -0
- package/src/lib/config-schema.js +10 -0
- package/src/lib/config-schema.ts +17 -0
- package/src/lib/config.ts +1314 -1289
- package/src/lib/consent.js +108 -102
- package/src/lib/image-size.js +152 -140
- package/src/lib/json-schema-descriptions.js +1 -0
- package/src/lib/link-check.js +42 -4
- package/src/lib/llms-index.ts +90 -88
- package/src/lib/mcp-dev-integration.js +65 -60
- package/src/lib/mcp-index.ts +102 -101
- 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-base-path.js +62 -0
- package/src/lib/rehype-image-loading.js +71 -71
- package/src/lib/robots-txt.js +21 -16
- package/src/lib/seo-tags.js +155 -140
- package/src/lib/site-base.ts +27 -0
- package/src/lib/styles-asset-integration.js +58 -12
- package/src/lib/wdignore-cases.json +195 -0
- package/src/lib/wdignore.js +177 -0
- package/src/pages/404.astro +63 -62
- package/src/pages/[...slug].astro +1604 -1597
- package/src/pages/[...slug].md.ts +93 -93
- package/src/pages/llms-full.txt.ts +88 -86
- package/src/scripts/search.ts +241 -237
- package/writedocs.schema.json +6 -0
|
@@ -1,162 +1,164 @@
|
|
|
1
|
-
// The Markdown an AI agent reads for a page - llms-full.txt and the per-page
|
|
2
|
-
// .md route. It starts from the page's raw MDX, so everything the build does
|
|
3
|
-
// to that source on the way to HTML has to be redone here, or the agent reads
|
|
4
|
-
// something other than the page: `<Visibility>` blocks, imported .mdx
|
|
5
|
-
// snippets (inlined, as the page shows them) and writedocs.json `variables`.
|
|
6
|
-
//
|
|
7
|
-
// Code is left exactly as written - fenced blocks and inline `code` - the same
|
|
8
|
-
// way the build never substitutes inside code: a page documenting the
|
|
9
|
-
// `[[key]]` syntax, or showing `<Snippet />` in an example, must keep it.
|
|
10
|
-
// Plain JavaScript, so it can be tested with plain Node.
|
|
11
|
-
import fs from 'node:fs';
|
|
12
|
-
import path from 'node:path';
|
|
13
|
-
import matter from 'gray-matter';
|
|
14
|
-
import { applyVisibilityForAgents } from './visibility.js';
|
|
15
|
-
import { componentsToMarkdown } from './components-to-markdown.js';
|
|
16
|
-
|
|
17
|
-
/** `text` with `transform` applied to everything outside fenced code blocks
|
|
18
|
-
* and inline code spans; code comes back untouched. */
|
|
19
|
-
export function mapOutsideCode(text, transform) {
|
|
20
|
-
const out = [];
|
|
21
|
-
let prose = [];
|
|
22
|
-
let fence = null; // the opening marker (``` or ~~~, maybe longer) while inside a block
|
|
23
|
-
const flush = () => {
|
|
24
|
-
if (prose.length) out.push(mapOutsideInlineCode(prose.join('\n'), transform));
|
|
25
|
-
prose = [];
|
|
26
|
-
};
|
|
27
|
-
for (const line of text.split('\n')) {
|
|
28
|
-
const marker = /^\s*(`{3,}|~{3,})/.exec(line)?.[1];
|
|
29
|
-
if (fence) {
|
|
30
|
-
out.push(line);
|
|
31
|
-
if (marker && marker[0] === fence[0] && marker.length >= fence.length && line.trim() === marker) fence = null;
|
|
32
|
-
} else if (marker) {
|
|
33
|
-
flush();
|
|
34
|
-
fence = marker;
|
|
35
|
-
out.push(line);
|
|
36
|
-
} else {
|
|
37
|
-
prose.push(line);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
flush();
|
|
41
|
-
return out.join('\n');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function mapOutsideInlineCode(text, transform) {
|
|
45
|
-
// Backtick runs of equal length delimit a code span (CommonMark).
|
|
46
|
-
return text
|
|
47
|
-
// A closing run is a whole run: in "` ```bash `" the three backticks
|
|
48
|
-
// inside don't close the span that one backtick opened.
|
|
49
|
-
.split(/(?<!`)(`+)(?!`)([\s\S]*?)(?<!`)(\1)(?!`)/)
|
|
50
|
-
.reduce((acc, part, i, parts) => {
|
|
51
|
-
const k = i % 4;
|
|
52
|
-
if (k === 0) acc.push(transform(part));
|
|
53
|
-
else if (k === 1) acc.push(part + parts[i + 1] + parts[i + 2]);
|
|
54
|
-
return acc;
|
|
55
|
-
}, [])
|
|
56
|
-
.join('');
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const PLACEHOLDER = /\[\[\s*([\w.-]+)\s*\]\]/g;
|
|
60
|
-
// A standalone `{key}` or `{{key}}` expression (Mintlify writes the latter;
|
|
61
|
-
// MDX reads it as `{key}` inside an expression) - not an attribute value
|
|
62
|
-
// (`prop={key}`), which the build leaves alone too. The double form first,
|
|
63
|
-
// or its outer braces would be left around the value.
|
|
64
|
-
const MINTLIFY_PLACEHOLDER = /(?<!=\s*)\{\s*\{\s*([A-Za-z_$][\w$]*)\s*\}\s*\}|(?<!=\s*)\{\s*([A-Za-z_$][\w$]*)\s*\}/g;
|
|
65
|
-
|
|
66
|
-
/** writedocs.json `variables` - `[[key]]`, and Mintlify's `{key}` - replaced
|
|
67
|
-
* the way the build replaces them (lib/mdx-substitute-variables.js): outside
|
|
68
|
-
* code, and only for keys that exist. */
|
|
69
|
-
export function substituteVariables(text, variables) {
|
|
70
|
-
if (!variables || Object.keys(variables).length === 0) return text;
|
|
71
|
-
const has = (key) => Object.prototype.hasOwnProperty.call(variables, key);
|
|
72
|
-
return mapOutsideCode(text, (prose) =>
|
|
73
|
-
prose
|
|
74
|
-
.replace(PLACEHOLDER, (match, key) => (has(key) ? String(variables[key]) : match))
|
|
75
|
-
.replace(MINTLIFY_PLACEHOLDER, (match, doubled, single) => {
|
|
76
|
-
const key = doubled ?? single;
|
|
77
|
-
return has(key) ? String(variables[key]) : match;
|
|
78
|
-
})
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const IMPORT_LINE = /^[ \t]*import\s+([A-Za-z_$][\w$]*)\s+from\s+['"]([^'"]+\.mdx?)['"];?[ \t]*$/gm;
|
|
83
|
-
|
|
84
|
-
function resolveSnippet(spec, fromFile, contentDir) {
|
|
85
|
-
if (spec.startsWith('/snippets/')) return path.join(contentDir, 'snippets', spec.slice('/snippets/'.length));
|
|
86
|
-
if (spec.startsWith('./') || spec.startsWith('../')) return path.resolve(path.dirname(fromFile), spec);
|
|
87
|
-
if (spec.startsWith('/')) return path.join(contentDir, spec.slice(1));
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function attributesOf(source) {
|
|
92
|
-
const props = {};
|
|
93
|
-
for (const m of source.matchAll(/([A-Za-z_$][\w$-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|\{\s*["']([^"']*)["']\s*\})/g)) {
|
|
94
|
-
props[m[1]] = m[2] ?? m[3] ?? m[4];
|
|
95
|
-
}
|
|
96
|
-
return props;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function fillProps(snippet, props, children) {
|
|
100
|
-
return snippet
|
|
101
|
-
.replace(/\{\s*props\.children\s*\}/g, children ?? '')
|
|
102
|
-
.replace(/\{\s*props\.([A-Za-z_$][\w$]*)\s*\}/g, (match, key) => (key in props ? props[key] : match));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** Imported .mdx snippets inlined where the page uses them - the snippet's
|
|
106
|
-
* own text, with `{props.x}` filled from the tag's string attributes, and
|
|
107
|
-
* its own imports inlined in turn. The import line goes too. A snippet that
|
|
108
|
-
* can't be read, or a .jsx/.tsx component, stays as written. */
|
|
109
|
-
export function inlineSnippets(text, { file, contentDir, depth = 0 }) {
|
|
110
|
-
if (depth > 5) return text;
|
|
111
|
-
const snippets = new Map();
|
|
112
|
-
let result = mapOutsideCode(text, (prose) =>
|
|
113
|
-
prose.replace(IMPORT_LINE, (line, name, spec) => {
|
|
114
|
-
const target = resolveSnippet(spec, file, contentDir);
|
|
115
|
-
let raw;
|
|
116
|
-
try {
|
|
117
|
-
raw = target && fs.readFileSync(target, 'utf-8');
|
|
118
|
-
} catch {
|
|
119
|
-
raw = null;
|
|
120
|
-
}
|
|
121
|
-
if (!raw) return line;
|
|
122
|
-
const body = inlineSnippets(matter(raw).content.trim(), { file: target, contentDir, depth: depth + 1 });
|
|
123
|
-
snippets.set(name, body);
|
|
124
|
-
return '';
|
|
125
|
-
})
|
|
126
|
-
);
|
|
127
|
-
for (const [name, body] of snippets) {
|
|
128
|
-
result = mapOutsideCode(result, (prose) =>
|
|
129
|
-
prose
|
|
130
|
-
.replace(new RegExp(`<${name}\\b([^>]*?)\\/>`, 'g'), (_, attrs) => fillProps(body, attributesOf(attrs)))
|
|
131
|
-
.replace(new RegExp(`<${name}\\b([^>]*)>([\\s\\S]*?)<\\/${name}>`, 'g'), (_, attrs, children) =>
|
|
132
|
-
fillProps(body, attributesOf(attrs), children.trim())
|
|
133
|
-
)
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
return result.replace(/\n{3,}/g, '\n\n');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/** Links and images written from the site's root (`/docs/setup/`) as full
|
|
140
|
-
* URLs. A page copied out of the site - pasted into a chat, fetched by an
|
|
141
|
-
* agent, read from llms-full.txt - has no site to resolve them against.
|
|
142
|
-
* Markdown links and images, and the `href`/`src`/`img` of a component;
|
|
143
|
-
* code is left as written, and so is everything without a `siteUrl`.
|
|
144
|
-
|
|
145
|
-
if
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
text =
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
1
|
+
// The Markdown an AI agent reads for a page - llms-full.txt and the per-page
|
|
2
|
+
// .md route. It starts from the page's raw MDX, so everything the build does
|
|
3
|
+
// to that source on the way to HTML has to be redone here, or the agent reads
|
|
4
|
+
// something other than the page: `<Visibility>` blocks, imported .mdx
|
|
5
|
+
// snippets (inlined, as the page shows them) and writedocs.json `variables`.
|
|
6
|
+
//
|
|
7
|
+
// Code is left exactly as written - fenced blocks and inline `code` - the same
|
|
8
|
+
// way the build never substitutes inside code: a page documenting the
|
|
9
|
+
// `[[key]]` syntax, or showing `<Snippet />` in an example, must keep it.
|
|
10
|
+
// Plain JavaScript, so it can be tested with plain Node.
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import matter from 'gray-matter';
|
|
14
|
+
import { applyVisibilityForAgents } from './visibility.js';
|
|
15
|
+
import { componentsToMarkdown } from './components-to-markdown.js';
|
|
16
|
+
|
|
17
|
+
/** `text` with `transform` applied to everything outside fenced code blocks
|
|
18
|
+
* and inline code spans; code comes back untouched. */
|
|
19
|
+
export function mapOutsideCode(text, transform) {
|
|
20
|
+
const out = [];
|
|
21
|
+
let prose = [];
|
|
22
|
+
let fence = null; // the opening marker (``` or ~~~, maybe longer) while inside a block
|
|
23
|
+
const flush = () => {
|
|
24
|
+
if (prose.length) out.push(mapOutsideInlineCode(prose.join('\n'), transform));
|
|
25
|
+
prose = [];
|
|
26
|
+
};
|
|
27
|
+
for (const line of text.split('\n')) {
|
|
28
|
+
const marker = /^\s*(`{3,}|~{3,})/.exec(line)?.[1];
|
|
29
|
+
if (fence) {
|
|
30
|
+
out.push(line);
|
|
31
|
+
if (marker && marker[0] === fence[0] && marker.length >= fence.length && line.trim() === marker) fence = null;
|
|
32
|
+
} else if (marker) {
|
|
33
|
+
flush();
|
|
34
|
+
fence = marker;
|
|
35
|
+
out.push(line);
|
|
36
|
+
} else {
|
|
37
|
+
prose.push(line);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
flush();
|
|
41
|
+
return out.join('\n');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function mapOutsideInlineCode(text, transform) {
|
|
45
|
+
// Backtick runs of equal length delimit a code span (CommonMark).
|
|
46
|
+
return text
|
|
47
|
+
// A closing run is a whole run: in "` ```bash `" the three backticks
|
|
48
|
+
// inside don't close the span that one backtick opened.
|
|
49
|
+
.split(/(?<!`)(`+)(?!`)([\s\S]*?)(?<!`)(\1)(?!`)/)
|
|
50
|
+
.reduce((acc, part, i, parts) => {
|
|
51
|
+
const k = i % 4;
|
|
52
|
+
if (k === 0) acc.push(transform(part));
|
|
53
|
+
else if (k === 1) acc.push(part + parts[i + 1] + parts[i + 2]);
|
|
54
|
+
return acc;
|
|
55
|
+
}, [])
|
|
56
|
+
.join('');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const PLACEHOLDER = /\[\[\s*([\w.-]+)\s*\]\]/g;
|
|
60
|
+
// A standalone `{key}` or `{{key}}` expression (Mintlify writes the latter;
|
|
61
|
+
// MDX reads it as `{key}` inside an expression) - not an attribute value
|
|
62
|
+
// (`prop={key}`), which the build leaves alone too. The double form first,
|
|
63
|
+
// or its outer braces would be left around the value.
|
|
64
|
+
const MINTLIFY_PLACEHOLDER = /(?<!=\s*)\{\s*\{\s*([A-Za-z_$][\w$]*)\s*\}\s*\}|(?<!=\s*)\{\s*([A-Za-z_$][\w$]*)\s*\}/g;
|
|
65
|
+
|
|
66
|
+
/** writedocs.json `variables` - `[[key]]`, and Mintlify's `{key}` - replaced
|
|
67
|
+
* the way the build replaces them (lib/mdx-substitute-variables.js): outside
|
|
68
|
+
* code, and only for keys that exist. */
|
|
69
|
+
export function substituteVariables(text, variables) {
|
|
70
|
+
if (!variables || Object.keys(variables).length === 0) return text;
|
|
71
|
+
const has = (key) => Object.prototype.hasOwnProperty.call(variables, key);
|
|
72
|
+
return mapOutsideCode(text, (prose) =>
|
|
73
|
+
prose
|
|
74
|
+
.replace(PLACEHOLDER, (match, key) => (has(key) ? String(variables[key]) : match))
|
|
75
|
+
.replace(MINTLIFY_PLACEHOLDER, (match, doubled, single) => {
|
|
76
|
+
const key = doubled ?? single;
|
|
77
|
+
return has(key) ? String(variables[key]) : match;
|
|
78
|
+
})
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const IMPORT_LINE = /^[ \t]*import\s+([A-Za-z_$][\w$]*)\s+from\s+['"]([^'"]+\.mdx?)['"];?[ \t]*$/gm;
|
|
83
|
+
|
|
84
|
+
function resolveSnippet(spec, fromFile, contentDir) {
|
|
85
|
+
if (spec.startsWith('/snippets/')) return path.join(contentDir, 'snippets', spec.slice('/snippets/'.length));
|
|
86
|
+
if (spec.startsWith('./') || spec.startsWith('../')) return path.resolve(path.dirname(fromFile), spec);
|
|
87
|
+
if (spec.startsWith('/')) return path.join(contentDir, spec.slice(1));
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function attributesOf(source) {
|
|
92
|
+
const props = {};
|
|
93
|
+
for (const m of source.matchAll(/([A-Za-z_$][\w$-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|\{\s*["']([^"']*)["']\s*\})/g)) {
|
|
94
|
+
props[m[1]] = m[2] ?? m[3] ?? m[4];
|
|
95
|
+
}
|
|
96
|
+
return props;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function fillProps(snippet, props, children) {
|
|
100
|
+
return snippet
|
|
101
|
+
.replace(/\{\s*props\.children\s*\}/g, children ?? '')
|
|
102
|
+
.replace(/\{\s*props\.([A-Za-z_$][\w$]*)\s*\}/g, (match, key) => (key in props ? props[key] : match));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Imported .mdx snippets inlined where the page uses them - the snippet's
|
|
106
|
+
* own text, with `{props.x}` filled from the tag's string attributes, and
|
|
107
|
+
* its own imports inlined in turn. The import line goes too. A snippet that
|
|
108
|
+
* can't be read, or a .jsx/.tsx component, stays as written. */
|
|
109
|
+
export function inlineSnippets(text, { file, contentDir, depth = 0 }) {
|
|
110
|
+
if (depth > 5) return text;
|
|
111
|
+
const snippets = new Map();
|
|
112
|
+
let result = mapOutsideCode(text, (prose) =>
|
|
113
|
+
prose.replace(IMPORT_LINE, (line, name, spec) => {
|
|
114
|
+
const target = resolveSnippet(spec, file, contentDir);
|
|
115
|
+
let raw;
|
|
116
|
+
try {
|
|
117
|
+
raw = target && fs.readFileSync(target, 'utf-8');
|
|
118
|
+
} catch {
|
|
119
|
+
raw = null;
|
|
120
|
+
}
|
|
121
|
+
if (!raw) return line;
|
|
122
|
+
const body = inlineSnippets(matter(raw).content.trim(), { file: target, contentDir, depth: depth + 1 });
|
|
123
|
+
snippets.set(name, body);
|
|
124
|
+
return '';
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
for (const [name, body] of snippets) {
|
|
128
|
+
result = mapOutsideCode(result, (prose) =>
|
|
129
|
+
prose
|
|
130
|
+
.replace(new RegExp(`<${name}\\b([^>]*?)\\/>`, 'g'), (_, attrs) => fillProps(body, attributesOf(attrs)))
|
|
131
|
+
.replace(new RegExp(`<${name}\\b([^>]*)>([\\s\\S]*?)<\\/${name}>`, 'g'), (_, attrs, children) =>
|
|
132
|
+
fillProps(body, attributesOf(attrs), children.trim())
|
|
133
|
+
)
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
return result.replace(/\n{3,}/g, '\n\n');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Links and images written from the site's root (`/docs/setup/`) as full
|
|
140
|
+
* URLs. A page copied out of the site - pasted into a chat, fetched by an
|
|
141
|
+
* agent, read from llms-full.txt - has no site to resolve them against.
|
|
142
|
+
* Markdown links and images, and the `href`/`src`/`img` of a component;
|
|
143
|
+
* code is left as written, and so is everything without a `siteUrl`.
|
|
144
|
+
* `siteUrl` can be just the site's base ("/docs", lib/base-path.js) when
|
|
145
|
+
* there's no `domain`: the links then keep the base, if not the domain. */
|
|
146
|
+
export function absolutizeLinks(text, siteUrl) {
|
|
147
|
+
if (!siteUrl) return text;
|
|
148
|
+
return mapOutsideCode(text, (prose) =>
|
|
149
|
+
prose
|
|
150
|
+
.replace(/(\]\()\/(?!\/)/g, `$1${siteUrl}/`)
|
|
151
|
+
.replace(/(\b(?:href|src|img)=["'])\/(?!\/)/g, `$1${siteUrl}/`)
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A page's body as an agent should read it. `file` is the page's absolute
|
|
156
|
+
* path (to resolve relative snippet imports). */
|
|
157
|
+
export function markdownForAgents(body, { file, contentDir, variables }) {
|
|
158
|
+
let text = applyVisibilityForAgents(body ?? '');
|
|
159
|
+
if (file) text = inlineSnippets(text, { file, contentDir });
|
|
160
|
+
text = substituteVariables(text, variables);
|
|
161
|
+
// Components as the Markdown that says what they show
|
|
162
|
+
// (lib/components-to-markdown.js) - in an .mdx page; a .md page has none.
|
|
163
|
+
return file && /\.md$/i.test(file) ? text : componentsToMarkdown(text);
|
|
164
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Image sizes handed to the build in a file, so it doesn't open the images.
|
|
2
|
+
//
|
|
3
|
+
// The WriteDocs platform builds a project with its images swapped for small
|
|
4
|
+
// placeholder files (it puts the real ones back after the build), so the
|
|
5
|
+
// build can't read a width and a height from them. It writes this file
|
|
6
|
+
// instead:
|
|
7
|
+
//
|
|
8
|
+
// <project>/.writedocs/asset-dimensions.json
|
|
9
|
+
// {
|
|
10
|
+
// "images/hero.png": { "width": 1200, "height": 630 },
|
|
11
|
+
// "public/images/logo.svg": { "width": 120, "height": 32 }
|
|
12
|
+
// }
|
|
13
|
+
//
|
|
14
|
+
// THIS FORMAT IS A CONTRACT with the platform:
|
|
15
|
+
// - one JSON object; each key is a file's path from the project root -
|
|
16
|
+
// where the file really is, so a file in public/ starts with "public/" -
|
|
17
|
+
// with forward slashes, no leading slash, spelled exactly as on disk;
|
|
18
|
+
// - each value is an object with `width` and `height`, both whole numbers
|
|
19
|
+
// greater than zero (pixels). Other fields in it are ignored.
|
|
20
|
+
// - A path that's listed is never opened to be measured. A path that isn't
|
|
21
|
+
// listed is read from its file, as when there's no such file at all.
|
|
22
|
+
// - An entry that isn't valid is skipped and the image is read; a file
|
|
23
|
+
// that isn't valid JSON, or isn't an object, counts as absent. Nothing
|
|
24
|
+
// here can fail a build.
|
|
25
|
+
//
|
|
26
|
+
// `.writedocs/` is never published (EXCLUDED_TOP_LEVEL_DIRS, lib/pages.js).
|
|
27
|
+
import fs from 'node:fs';
|
|
28
|
+
import path from 'node:path';
|
|
29
|
+
|
|
30
|
+
/** Where the file is, from the project root. */
|
|
31
|
+
export const ASSET_DIMENSIONS_FILE = '.writedocs/asset-dimensions.json';
|
|
32
|
+
|
|
33
|
+
const isSize = (n) => Number.isSafeInteger(n) && n > 0;
|
|
34
|
+
|
|
35
|
+
/** The valid entries of the file's text, as a Map of path -> `{ width,
|
|
36
|
+
* height }`. Empty for text that isn't a JSON object. */
|
|
37
|
+
export function parseAssetDimensions(text) {
|
|
38
|
+
const sizes = new Map();
|
|
39
|
+
let data;
|
|
40
|
+
try {
|
|
41
|
+
data = JSON.parse(String(text).replace(/^\uFEFF/, ''));
|
|
42
|
+
} catch {
|
|
43
|
+
return sizes;
|
|
44
|
+
}
|
|
45
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) return sizes;
|
|
46
|
+
for (const [key, value] of Object.entries(data)) {
|
|
47
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) continue;
|
|
48
|
+
if (!isSize(value.width) || !isSize(value.height)) continue;
|
|
49
|
+
sizes.set(key, { width: value.width, height: value.height });
|
|
50
|
+
}
|
|
51
|
+
return sizes;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Per project, parsed again only when the file changes - `writedocs dev`
|
|
55
|
+
// keeps running while it's edited.
|
|
56
|
+
const loaded = new Map();
|
|
57
|
+
|
|
58
|
+
function dimensionsFor(contentDir) {
|
|
59
|
+
const file = path.join(contentDir, ...ASSET_DIMENSIONS_FILE.split('/'));
|
|
60
|
+
let stamp;
|
|
61
|
+
try {
|
|
62
|
+
const stat = fs.statSync(file);
|
|
63
|
+
stamp = `${stat.mtimeMs}:${stat.size}`;
|
|
64
|
+
} catch {
|
|
65
|
+
loaded.delete(contentDir);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const known = loaded.get(contentDir);
|
|
69
|
+
if (known?.stamp === stamp) return known.sizes;
|
|
70
|
+
let sizes;
|
|
71
|
+
try {
|
|
72
|
+
sizes = parseAssetDimensions(fs.readFileSync(file, 'utf8'));
|
|
73
|
+
} catch {
|
|
74
|
+
sizes = new Map();
|
|
75
|
+
}
|
|
76
|
+
loaded.set(contentDir, { stamp, sizes });
|
|
77
|
+
return sizes;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** `{ width, height }` for the image file at `file` (an absolute path) when
|
|
81
|
+
* the project's asset-dimensions.json lists it, else null. */
|
|
82
|
+
export function providedImageSize(file, contentDir) {
|
|
83
|
+
if (!file || !contentDir) return null;
|
|
84
|
+
const sizes = dimensionsFor(contentDir);
|
|
85
|
+
if (!sizes || sizes.size === 0) return null;
|
|
86
|
+
const relative = path.relative(contentDir, file);
|
|
87
|
+
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) return null;
|
|
88
|
+
return sizes.get(relative.split(path.sep).join('/')) ?? null;
|
|
89
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// writedocs.json's `basePath` - the site served under a path of a domain,
|
|
2
|
+
// `empresa.com/docs`, usually behind a proxy that passes `/docs/*` on to the
|
|
3
|
+
// site as it is. Every address the site makes starts with it, and the build
|
|
4
|
+
// writes the site into dist/<basePath>/ (src/cli/base-path-layout.js), so a
|
|
5
|
+
// static server or a proxy that keeps the path finds every file where its
|
|
6
|
+
// address says.
|
|
7
|
+
//
|
|
8
|
+
// The base is either "" (no basePath: the site at the root, as before) or
|
|
9
|
+
// "/docs" - a leading slash and none at the end. Addresses come in two
|
|
10
|
+
// kinds, and only one of them takes the base:
|
|
11
|
+
// - site addresses: "/guides/a/", "/images/x.png" - a path in the site,
|
|
12
|
+
// written by the project (writedocs.json, page content) or by writedocs
|
|
13
|
+
// itself (navigation, assets). withBase() prefixes them, once, where the
|
|
14
|
+
// final address is written - never twice: a page can live at "/docs/..."
|
|
15
|
+
// inside a site whose base is "/docs", so "already starts with the base"
|
|
16
|
+
// can't tell whether an address has been prefixed.
|
|
17
|
+
// - everything else - "https://...", "#a", "mailto:", "//cdn...", a relative
|
|
18
|
+
// "../a" - is left as it is.
|
|
19
|
+
//
|
|
20
|
+
// Plain JS so the tests can import it; lib/config.ts reads the field
|
|
21
|
+
// (resolveBasePath()), and astro.config.mjs hands it to Astro (`base`) and to
|
|
22
|
+
// the page scripts (__WD_BASE__).
|
|
23
|
+
|
|
24
|
+
/** The accepted form: "/docs", "/help/docs" - lowercase letters, digits, `-`
|
|
25
|
+
* and `_` in each segment. "" and "/" are accepted too and mean no base
|
|
26
|
+
* (BASE_PATH_FIELD_PATTERN). */
|
|
27
|
+
export const BASE_PATH_PATTERN = /^(\/[a-z0-9_-]+)+$/;
|
|
28
|
+
|
|
29
|
+
/** What writedocs.json may hold in `basePath` - the form above, "" or "/". */
|
|
30
|
+
export const BASE_PATH_FIELD_PATTERN = /^(\/|(\/[a-z0-9_-]+)+)?$/;
|
|
31
|
+
|
|
32
|
+
/** The base from writedocs.json's value: "" when unset, "" or "/", else the
|
|
33
|
+
* value itself. Assumes a value the schema accepted. */
|
|
34
|
+
export function normalizeBasePath(value) {
|
|
35
|
+
if (typeof value !== 'string' || value === '' || value === '/') return '';
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Whether `url` is a site address: a path from the site's root ("/a/b"),
|
|
40
|
+
* not a protocol-relative "//host/a". */
|
|
41
|
+
export function isSiteAddress(url) {
|
|
42
|
+
return typeof url === 'string' && url.startsWith('/') && !url.startsWith('//');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** `url` with the base in front, if it's a site address - "/a/" -> "/docs/a/",
|
|
46
|
+
* "/" -> "/docs/". Anything else, and everything when there's no base,
|
|
47
|
+
* comes back as it is. */
|
|
48
|
+
export function withBase(base, url) {
|
|
49
|
+
if (!base || !isSiteAddress(url)) return url;
|
|
50
|
+
return base + url;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A `srcset` with the base in front of each site address in it. */
|
|
54
|
+
export function srcsetWithBase(base, srcset) {
|
|
55
|
+
if (!base || typeof srcset !== 'string') return srcset;
|
|
56
|
+
return srcset
|
|
57
|
+
.split(',')
|
|
58
|
+
.map((candidate) => {
|
|
59
|
+
const [url, ...descriptor] = candidate.trim().split(/\s+/);
|
|
60
|
+
return [withBase(base, url), ...descriptor].join(' ');
|
|
61
|
+
})
|
|
62
|
+
.join(', ');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The address inside the site for a request path - "/docs/a/" -> "/a/",
|
|
66
|
+
* "/docs" -> "/" - or null when the path isn't under the base. With no base,
|
|
67
|
+
* the path itself. For `writedocs dev`'s own middleware, which sees the full
|
|
68
|
+
* request path. */
|
|
69
|
+
export function withoutBase(base, pathname) {
|
|
70
|
+
if (!base) return pathname;
|
|
71
|
+
if (pathname === base) return '/';
|
|
72
|
+
if (pathname.startsWith(`${base}/`)) return pathname.slice(base.length);
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The site's URL - "https://empresa.com/docs" - from writedocs.json's
|
|
77
|
+
* `domain` (scheme optional, trailing slash dropped) and the base; null
|
|
78
|
+
* without a `domain`. resolveSiteUrl() in lib/config.ts is this, for a
|
|
79
|
+
* loaded config. */
|
|
80
|
+
export function siteUrlFor(domain, base = '') {
|
|
81
|
+
const origin = siteOriginFor(domain);
|
|
82
|
+
return origin === null ? null : origin + base;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The `domain` alone as a full origin ("https://docs.example.com"), or null. */
|
|
86
|
+
export function siteOriginFor(domain) {
|
|
87
|
+
if (!domain) return null;
|
|
88
|
+
const withScheme = /^https?:\/\//i.test(domain) ? domain : `https://${domain}`;
|
|
89
|
+
return withScheme.replace(/\/+$/, '');
|
|
90
|
+
}
|
package/src/lib/config-schema.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { findNodeAtLocation, parseTree } from "jsonc-parser";
|
|
3
|
+
import { BASE_PATH_FIELD_PATTERN } from "./base-path.js";
|
|
3
4
|
const navPageSchema = z.string();
|
|
4
5
|
const navGroupPagesSchema = z.lazy(
|
|
5
6
|
() => z.object({
|
|
@@ -682,6 +683,14 @@ const docsConfigSchema = z.object({
|
|
|
682
683
|
// every one of those call sites when unset - most fixtures/local builds
|
|
683
684
|
// have no real deployed domain yet.
|
|
684
685
|
domain: z.string().optional(),
|
|
686
|
+
// The path the site is served under on `domain`, like "/docs" for
|
|
687
|
+
// empresa.com/docs (usually behind the customer's own proxy). Every
|
|
688
|
+
// address the site makes starts with it, and the build writes the site to
|
|
689
|
+
// dist/<basePath>/ - see src/lib/base-path.js. Absent, "" or "/": the site
|
|
690
|
+
// at the root, exactly as without the field.
|
|
691
|
+
basePath: z.string().regex(BASE_PATH_FIELD_PATTERN, {
|
|
692
|
+
message: '"basePath" must be a path like "/docs" or "/help/docs": it starts with "/", has no "/" at the end, and each part has only lowercase letters, digits, "-" and "_".'
|
|
693
|
+
}).optional(),
|
|
685
694
|
// Site-wide meta tag defaults - see seoFieldsSchema above. A page's own
|
|
686
695
|
// frontmatter `seo` (content.config.ts's docsSchema) overrides these
|
|
687
696
|
// field by field via mergeSeo(), rather than needing to repeat every
|
|
@@ -956,6 +965,7 @@ function humanizar(issue, raiz) {
|
|
|
956
965
|
return { humanMessage: `${campo} allows at most ${maximo} ${unidade}${maximo === 1 ? "" : "s"}.` };
|
|
957
966
|
}
|
|
958
967
|
case "invalid_format":
|
|
968
|
+
if (issue.format === "regex" && issue.message) return { humanMessage: issue.message };
|
|
959
969
|
return {
|
|
960
970
|
humanMessage: `${campo} is not a valid ${issue.format ?? "value"}.`,
|
|
961
971
|
suggestion: "Check the format against the documentation for this field."
|
package/src/lib/config-schema.ts
CHANGED
|
@@ -31,6 +31,8 @@ import { z } from 'zod';
|
|
|
31
31
|
// foi tentado e deu errado (perdia todo caminho aninhado, que e justamente o que
|
|
32
32
|
// importa aqui) - nao repita.
|
|
33
33
|
import { findNodeAtLocation, parseTree } from 'jsonc-parser';
|
|
34
|
+
// The form of `basePath`. Plain JS with no `node:` import, like this file.
|
|
35
|
+
import { BASE_PATH_FIELD_PATTERN } from './base-path.js';
|
|
34
36
|
|
|
35
37
|
// ---------------------------------------------------------------------
|
|
36
38
|
// Pages & groups - the leaf content any container ultimately bottoms out
|
|
@@ -1224,6 +1226,18 @@ export const docsConfigSchema = z.object({
|
|
|
1224
1226
|
// every one of those call sites when unset - most fixtures/local builds
|
|
1225
1227
|
// have no real deployed domain yet.
|
|
1226
1228
|
domain: z.string().optional(),
|
|
1229
|
+
// The path the site is served under on `domain`, like "/docs" for
|
|
1230
|
+
// empresa.com/docs (usually behind the customer's own proxy). Every
|
|
1231
|
+
// address the site makes starts with it, and the build writes the site to
|
|
1232
|
+
// dist/<basePath>/ - see src/lib/base-path.js. Absent, "" or "/": the site
|
|
1233
|
+
// at the root, exactly as without the field.
|
|
1234
|
+
basePath: z
|
|
1235
|
+
.string()
|
|
1236
|
+
.regex(BASE_PATH_FIELD_PATTERN, {
|
|
1237
|
+
message:
|
|
1238
|
+
'"basePath" must be a path like "/docs" or "/help/docs": it starts with "/", has no "/" at the end, and each part has only lowercase letters, digits, "-" and "_".',
|
|
1239
|
+
})
|
|
1240
|
+
.optional(),
|
|
1227
1241
|
// Site-wide meta tag defaults - see seoFieldsSchema above. A page's own
|
|
1228
1242
|
// frontmatter `seo` (content.config.ts's docsSchema) overrides these
|
|
1229
1243
|
// field by field via mergeSeo(), rather than needing to repeat every
|
|
@@ -1753,6 +1767,9 @@ function humanizar(issue: IssuePlano, raiz: unknown): { humanMessage?: string; s
|
|
|
1753
1767
|
return { humanMessage: `${campo} allows at most ${maximo} ${unidade}${maximo === 1 ? '' : 's'}.` };
|
|
1754
1768
|
}
|
|
1755
1769
|
case 'invalid_format':
|
|
1770
|
+
// A field with a pattern of its own (`basePath`) says what's wrong in
|
|
1771
|
+
// its own words.
|
|
1772
|
+
if (issue.format === 'regex' && issue.message) return { humanMessage: issue.message };
|
|
1756
1773
|
return {
|
|
1757
1774
|
humanMessage: `${campo} is not a valid ${issue.format ?? 'value'}.`,
|
|
1758
1775
|
suggestion: 'Check the format against the documentation for this field.',
|