@lariocpt/crust-site 0.0.0-stage → 0.4.2
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/LICENSE +21 -0
- package/README.md +99 -2
- package/docs/USAGE.md +1128 -0
- package/package.json +33 -4
- package/src/assets.ts +875 -0
- package/src/build-lock.ts +214 -0
- package/src/build.ts +755 -0
- package/src/cli.ts +269 -0
- package/src/config.ts +538 -0
- package/src/content.ts +267 -0
- package/src/dev.ts +821 -0
- package/src/diagnostics.ts +382 -0
- package/src/emit.ts +260 -0
- package/src/fence.ts +593 -0
- package/src/frontmatter.ts +240 -0
- package/src/fs-path.ts +54 -0
- package/src/html.ts +400 -0
- package/src/index.ts +128 -0
- package/src/markdown.ts +444 -0
- package/src/pages.ts +169 -0
- package/src/publication.ts +245 -0
- package/src/render.ts +940 -0
- package/src/routes.ts +363 -0
- package/src/types.ts +250 -0
- package/src/url.ts +273 -0
- package/src/validate.ts +367 -0
package/src/routes.ts
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
// Routes: the URL side of the contract. Routes are file-write inputs, so
|
|
2
|
+
// validation is STRICT and total — nothing auto-normalizes (plan §2: "No
|
|
3
|
+
// auto-normalization of route() returns"). A route that is not already in
|
|
4
|
+
// normalized form is an error naming the offender, never a silent fix: a
|
|
5
|
+
// generator that quietly rewrites routes produces two URLs for one page.
|
|
6
|
+
//
|
|
7
|
+
// `normalizeRoute` throws a bare Diagnostic (not a BuildError): callers
|
|
8
|
+
// (frontmatter, the page collector) collect many diagnostics into one
|
|
9
|
+
// BuildError, and `isDiagnostic` separates these from real exceptions.
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
BuildError,
|
|
13
|
+
compareStrings,
|
|
14
|
+
type Diagnostic,
|
|
15
|
+
type DiagnosticCode,
|
|
16
|
+
isDiagnostic,
|
|
17
|
+
} from "./diagnostics";
|
|
18
|
+
import type { RouteEntry } from "./types";
|
|
19
|
+
|
|
20
|
+
/** Windows device names, rejected per segment regardless of extension. */
|
|
21
|
+
const RESERVED_NAMES = new Set([
|
|
22
|
+
"con",
|
|
23
|
+
"nul",
|
|
24
|
+
"prn",
|
|
25
|
+
"aux",
|
|
26
|
+
...["com", "lpt"].flatMap((prefix) => [1, 2, 3, 4, 5, 6, 7, 8, 9].map((n) => `${prefix}${n}`)),
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
function routeDiagnostic(code: DiagnosticCode, sourcePath: string, message: string): Diagnostic {
|
|
30
|
+
return { code, message, severity: "error", sourcePath };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function shape(sourcePath: string, message: string): Diagnostic {
|
|
34
|
+
return routeDiagnostic("route.shape", sourcePath, message);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Control and invisible format characters: C0 + C1 controls, DEL, soft
|
|
39
|
+
* hyphen, bidi/zero-width formatters, BOM, and the noncharacters. None may
|
|
40
|
+
* appear in a route — raw or after percent-decoding — because they are
|
|
41
|
+
* invisible in a printed URL (two routes that display identically but are
|
|
42
|
+
* different strings) and they break line-based processing (U+2028/2029 are
|
|
43
|
+
* LINE/PARAGRAPH SEPARATOR). Astral-plane code points contain no Cc; the
|
|
44
|
+
* handful of Cf there are obscure script characters no route uses.
|
|
45
|
+
*/
|
|
46
|
+
const CONTROL_OR_FORMAT =
|
|
47
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: intentional — this class is the definition of the control/format characters a route must not contain, so it has to name them
|
|
48
|
+
/[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u180e\u200b-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\ufffb\ufffe\uffff]/;
|
|
49
|
+
|
|
50
|
+
function hasControlChars(value: string): boolean {
|
|
51
|
+
return CONTROL_OR_FORMAT.test(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* WHATWG path percent-encode set (C0 controls, space, `"`, `#`, `<`, `>`,
|
|
56
|
+
* backtick, `?`, `{`, `}`) plus `%` itself — `%` must round-trip as `%25`
|
|
57
|
+
* because we decode before re-encoding. Applied to ASCII only: non-ASCII
|
|
58
|
+
* letters stay literal (the plan keeps unicode routes readable; the raw
|
|
59
|
+
* control/backslash rejections above still fence the dangerous bytes).
|
|
60
|
+
*/
|
|
61
|
+
function mustEncode(ch: string): boolean {
|
|
62
|
+
const c = ch.codePointAt(0) ?? 0;
|
|
63
|
+
// C0 controls (0x00-0x1F), DEL and the C1 block (0x7F-0x9F) all
|
|
64
|
+
// percent-encode. The WHATWG path set stops at C0/DEL, but a C1 control
|
|
65
|
+
// that reached the encoder would be a raw control byte in a file name;
|
|
66
|
+
// the rejections above make that unreachable, this range is defense in
|
|
67
|
+
// depth. (NOT 0x20-0x7E — printable ASCII stays literal.)
|
|
68
|
+
if (c <= 0x20 || (c >= 0x7f && c <= 0x9f)) return true;
|
|
69
|
+
return (
|
|
70
|
+
ch === "%" ||
|
|
71
|
+
ch === '"' ||
|
|
72
|
+
ch === "<" ||
|
|
73
|
+
ch === ">" ||
|
|
74
|
+
ch === "`" ||
|
|
75
|
+
ch === "?" ||
|
|
76
|
+
ch === "#" ||
|
|
77
|
+
ch === "{" ||
|
|
78
|
+
ch === "}" ||
|
|
79
|
+
ch === "\\"
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function encodeSegment(segment: string): string {
|
|
84
|
+
let out = "";
|
|
85
|
+
for (const ch of segment) {
|
|
86
|
+
out += mustEncode(ch) ? encodeURIComponent(ch) : ch;
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Validate a route and return it, STRICTLY: the input must already be the
|
|
93
|
+
* normalized form, or a Diagnostic is thrown (no silent fixing).
|
|
94
|
+
*
|
|
95
|
+
* Normal form: starts with `/`; segments percent-decoded then NFKC-normalized
|
|
96
|
+
* (so `%2e%2e`, `..` and U+2024 all become the traversal they hide behind),
|
|
97
|
+
* re-encoded per the WHATWG path set plus C1 controls (non-ASCII letters stay
|
|
98
|
+
* literal, so unicode routes remain readable), joined with single slashes,
|
|
99
|
+
* no trailing slash, root is `/`. Case is preserved. Rejected outright:
|
|
100
|
+
* non-strings → route.invalid
|
|
101
|
+
* literal `?`/`#` → route.query-fragment
|
|
102
|
+
* and, all route.shape:
|
|
103
|
+
* control or invisible format characters (C0/C1, DEL, bidi/zero-width
|
|
104
|
+
* formatters, soft hyphen, BOM, noncharacters) — raw or after decoding
|
|
105
|
+
* backslashes, drive letters, empty segments, decoded slashes, invalid
|
|
106
|
+
* percent-escapes or malformed UTF-8, Windows reserved device names
|
|
107
|
+
* (con/nul/prn/aux/com1-9/lpt1-9, case-insensitive, checked on the stem
|
|
108
|
+
* before the first dot — `nul.html` is just as reserved as `nul` — with
|
|
109
|
+
* trailing dots/spaces ignored), and anything whose normalized form
|
|
110
|
+
* differs from the input.
|
|
111
|
+
*
|
|
112
|
+
* The final round-trip comparison is the backstop: ANY transformation
|
|
113
|
+
* (decode, NFKC, re-encode) that changes the string rejects it, so a new
|
|
114
|
+
* NFKC surprise cannot silently slip into a file name.
|
|
115
|
+
*/
|
|
116
|
+
export function normalizeRoute(input: unknown, opts: { sourcePath: string }): string {
|
|
117
|
+
const sourcePath = opts.sourcePath;
|
|
118
|
+
if (typeof input !== "string") {
|
|
119
|
+
const kind = input === null ? "null" : typeof input;
|
|
120
|
+
throw routeDiagnostic("route.invalid", sourcePath, `route must be a string, received ${kind}`);
|
|
121
|
+
}
|
|
122
|
+
if (hasControlChars(input)) {
|
|
123
|
+
throw shape(
|
|
124
|
+
sourcePath,
|
|
125
|
+
`route ${JSON.stringify(input)} contains a control or invisible format character`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
if (input.includes("\\")) {
|
|
129
|
+
throw shape(sourcePath, `route ${JSON.stringify(input)} contains a backslash`);
|
|
130
|
+
}
|
|
131
|
+
if (input.includes("?") || input.includes("#")) {
|
|
132
|
+
throw routeDiagnostic(
|
|
133
|
+
"route.query-fragment",
|
|
134
|
+
sourcePath,
|
|
135
|
+
`route ${JSON.stringify(input)} must not contain a query or fragment`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (!input.startsWith("/")) {
|
|
139
|
+
throw shape(sourcePath, `route ${JSON.stringify(input)} must start with "/"`);
|
|
140
|
+
}
|
|
141
|
+
if (input === "/") return "/";
|
|
142
|
+
|
|
143
|
+
const segments: string[] = [];
|
|
144
|
+
for (const raw of input.slice(1).split("/")) {
|
|
145
|
+
if (raw === "") {
|
|
146
|
+
throw shape(
|
|
147
|
+
sourcePath,
|
|
148
|
+
`route ${JSON.stringify(input)} has an empty segment (double or trailing slash)`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
let segment: string;
|
|
152
|
+
try {
|
|
153
|
+
segment = decodeURIComponent(raw).normalize("NFKC");
|
|
154
|
+
} catch {
|
|
155
|
+
throw shape(
|
|
156
|
+
sourcePath,
|
|
157
|
+
`route ${JSON.stringify(input)} has an invalid percent-escape or malformed UTF-8 sequence in ${JSON.stringify(raw)}`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
if (segment === "." || segment === "..") {
|
|
161
|
+
throw routeDiagnostic(
|
|
162
|
+
"route.traversal",
|
|
163
|
+
sourcePath,
|
|
164
|
+
`route ${JSON.stringify(input)} contains a "." or ".." segment`,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
if (segment === "" || segment.includes("/") || segment.includes("\\")) {
|
|
168
|
+
throw shape(
|
|
169
|
+
sourcePath,
|
|
170
|
+
`route ${JSON.stringify(input)} has a segment that decodes to a slash or nothing`,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
if (hasControlChars(segment)) {
|
|
174
|
+
throw shape(
|
|
175
|
+
sourcePath,
|
|
176
|
+
`route ${JSON.stringify(input)} has a segment that decodes to a control or invisible format character`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
if (/^[A-Za-z]:/.test(segment)) {
|
|
180
|
+
throw shape(sourcePath, `route ${JSON.stringify(input)} has a Windows drive-letter segment`);
|
|
181
|
+
}
|
|
182
|
+
// Windows reserves the name before the first dot, and ignores trailing
|
|
183
|
+
// dots/spaces: "CON", "con.", "con ", "nul.txt" are all unwritable.
|
|
184
|
+
const stem =
|
|
185
|
+
segment
|
|
186
|
+
.toLowerCase()
|
|
187
|
+
.replace(/[ .]+$/, "")
|
|
188
|
+
.split(".")[0] ?? "";
|
|
189
|
+
if (RESERVED_NAMES.has(stem)) {
|
|
190
|
+
throw shape(
|
|
191
|
+
sourcePath,
|
|
192
|
+
`route ${JSON.stringify(input)} has the Windows-reserved segment ${JSON.stringify(segment)}`,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
segments.push(segment);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const normalized = `/${segments.map(encodeSegment).join("/")}`;
|
|
199
|
+
// The backstop (see the header comment): ANY transformation (decode, NFKC,
|
|
200
|
+
// re-encode) that changes the string means the input was not already in
|
|
201
|
+
// normalized form — reject it, never fix it.
|
|
202
|
+
if (normalized !== input) {
|
|
203
|
+
throw shape(
|
|
204
|
+
sourcePath,
|
|
205
|
+
`route ${JSON.stringify(input)} is not normalized; expected ${JSON.stringify(normalized)}`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
return normalized;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** True iff `normalizeRoute` would accept the input unchanged. */
|
|
212
|
+
export function isNormalizedRoute(input: unknown): boolean {
|
|
213
|
+
try {
|
|
214
|
+
normalizeRoute(input, { sourcePath: "" });
|
|
215
|
+
return true;
|
|
216
|
+
} catch {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The normative URL↔file table (plan §4.5): `/`→`index.html`, `/a`→`a.html`,
|
|
223
|
+
* `/a/b`→`a/b.html`. One table shared by emit, sitemap, dev server.
|
|
224
|
+
*
|
|
225
|
+
* The input is RE-VALIDATED, not trusted: a caller that hands over a
|
|
226
|
+
* non-normalized route (a cross-chunk bug) must get a diagnostic, not a
|
|
227
|
+
* broken file write — `"/a/"` would map to `"a/.html"`, a dotfile that no
|
|
228
|
+
* URL addresses. A false pass is worse than a failure, so normalizeRoute's
|
|
229
|
+
* own diagnostics (route.shape/invalid/…) are the only exit for bad input,
|
|
230
|
+
* wrapped in the BuildError every other throw from this table already is.
|
|
231
|
+
*/
|
|
232
|
+
export function routeToFile(route: string): string {
|
|
233
|
+
let normalized: string;
|
|
234
|
+
try {
|
|
235
|
+
normalized = normalizeRoute(route, { sourcePath: "" });
|
|
236
|
+
} catch (d) {
|
|
237
|
+
throw new BuildError([
|
|
238
|
+
isDiagnostic(d)
|
|
239
|
+
? d
|
|
240
|
+
: routeDiagnostic(
|
|
241
|
+
"route.invalid",
|
|
242
|
+
"",
|
|
243
|
+
`routeToFile expects a normalized route, got ${JSON.stringify(route)}`,
|
|
244
|
+
),
|
|
245
|
+
]);
|
|
246
|
+
}
|
|
247
|
+
return normalized === "/" ? "index.html" : `${normalized.slice(1)}.html`;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The one canonical URL resolver (plan §4.5), shared by sitemap, `ctx.url()`,
|
|
252
|
+
* the link validator and the dev server. `site` carries no trailing slash
|
|
253
|
+
* (defineSite enforces), so root is the only case that adds one.
|
|
254
|
+
*/
|
|
255
|
+
export function canonicalUrl(site: string, route: string): string {
|
|
256
|
+
return route === "/" ? `${site}/` : `${site}${route}`;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The URL → path direction used by the dev server: the INVERSE of what a
|
|
261
|
+
* browser does to a canonical form when it sends it. Each segment is
|
|
262
|
+
* percent-decoded, NFKC-normalized and re-encoded with the SAME segment
|
|
263
|
+
* encoder the route table uses — so the result is exactly the form the
|
|
264
|
+
* on-disk files and the route table carry (non-ASCII letters stay literal,
|
|
265
|
+
* reserved characters stay percent-encoded: `/caf%C3%A9` round-trips to the
|
|
266
|
+
* literal `café` file name).
|
|
267
|
+
*
|
|
268
|
+
* Returns NULL (the request answers 404) when the path cannot be a route or
|
|
269
|
+
* a file name:
|
|
270
|
+
* - no leading "/", or a query/fragment survived into the path;
|
|
271
|
+
* - an EMPTY segment (double or trailing slash) — this is what pins
|
|
272
|
+
* `"/a/"` → 404 (v1, plan §4.5);
|
|
273
|
+
* - a segment that decodes to a slash or backslash (`%2F`, `%5C`), or to
|
|
274
|
+
* `.`/`..`, or to a control/invisible format character, or to a
|
|
275
|
+
* Windows drive-letter prefix;
|
|
276
|
+
* - an invalid percent-escape or malformed UTF-8;
|
|
277
|
+
* - a Windows-reserved name (con/nul/prn/aux/com1-9/lpt1-9, on the stem
|
|
278
|
+
* before the first dot) — on WIN32 ONLY: off-Windows those are ordinary
|
|
279
|
+
* file names and rejecting them would be a false 404 (the Windows table
|
|
280
|
+
* row is about devices, and devices exist only there).
|
|
281
|
+
*/
|
|
282
|
+
export function normalizeRequestPath(input: string): string | null {
|
|
283
|
+
if (!input.startsWith("/")) return null;
|
|
284
|
+
if (input.includes("?") || input.includes("#")) return null;
|
|
285
|
+
if (input === "/") return "/";
|
|
286
|
+
const segments: string[] = [];
|
|
287
|
+
for (const raw of input.slice(1).split("/")) {
|
|
288
|
+
let decoded: string;
|
|
289
|
+
try {
|
|
290
|
+
decoded = decodeURIComponent(raw).normalize("NFKC");
|
|
291
|
+
} catch {
|
|
292
|
+
return null; // invalid percent-escape or malformed UTF-8
|
|
293
|
+
}
|
|
294
|
+
if (decoded === "" || decoded === "." || decoded === "..") return null;
|
|
295
|
+
if (decoded.includes("/") || decoded.includes("\\")) return null;
|
|
296
|
+
if (hasControlChars(decoded)) return null;
|
|
297
|
+
if (/^[A-Za-z]:/.test(decoded)) return null;
|
|
298
|
+
if (process.platform === "win32") {
|
|
299
|
+
const stem =
|
|
300
|
+
decoded
|
|
301
|
+
.toLowerCase()
|
|
302
|
+
.replace(/[ .]+$/, "")
|
|
303
|
+
.split(".")[0] ?? "";
|
|
304
|
+
if (RESERVED_NAMES.has(stem)) return null;
|
|
305
|
+
}
|
|
306
|
+
segments.push(encodeSegment(decoded));
|
|
307
|
+
}
|
|
308
|
+
return `/${segments.join("/")}`;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Route-table collisions, both passes (plan, Windows table):
|
|
313
|
+
* exact duplicates → route.duplicate, and the case-insensitive pass for
|
|
314
|
+
* case-insensitive filesystems → route.case-collision. Every diagnostic names
|
|
315
|
+
* ALL colliding sourcePaths; output is deterministic (groups sorted, source
|
|
316
|
+
* lists sorted, final sortDiagnostics).
|
|
317
|
+
*/
|
|
318
|
+
export function findRouteCollisions(entries: readonly RouteEntry[]): Diagnostic[] {
|
|
319
|
+
const diagnostics: Diagnostic[] = [];
|
|
320
|
+
|
|
321
|
+
const exact = new Map<string, string[]>();
|
|
322
|
+
for (const entry of entries) {
|
|
323
|
+
const paths = exact.get(entry.route);
|
|
324
|
+
if (paths) paths.push(entry.sourcePath);
|
|
325
|
+
else exact.set(entry.route, [entry.sourcePath]);
|
|
326
|
+
}
|
|
327
|
+
for (const [route, paths] of [...exact].sort((a, b) => compareStrings(a[0], b[0]))) {
|
|
328
|
+
if (paths.length < 2) continue;
|
|
329
|
+
const sorted = [...paths].sort();
|
|
330
|
+
diagnostics.push({
|
|
331
|
+
code: "route.duplicate",
|
|
332
|
+
severity: "error",
|
|
333
|
+
sourcePath: sorted[0],
|
|
334
|
+
message: `route "${route}" is produced by ${sorted.length} files: ${sorted.join(", ")}`,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// lowercased route → exact route → first (sorted) sourcePath that produced it
|
|
339
|
+
const byCase = new Map<string, Map<string, string>>();
|
|
340
|
+
for (const entry of entries) {
|
|
341
|
+
const key = entry.route.toLowerCase();
|
|
342
|
+
let group = byCase.get(key);
|
|
343
|
+
if (!group) {
|
|
344
|
+
group = new Map();
|
|
345
|
+
byCase.set(key, group);
|
|
346
|
+
}
|
|
347
|
+
const seen = group.get(entry.route);
|
|
348
|
+
if (seen === undefined || entry.sourcePath < seen) group.set(entry.route, entry.sourcePath);
|
|
349
|
+
}
|
|
350
|
+
for (const [, routes] of [...byCase].sort((a, b) => compareStrings(a[0], b[0]))) {
|
|
351
|
+
if (routes.size < 2) continue;
|
|
352
|
+
const distinct = [...routes.keys()].sort();
|
|
353
|
+
const named = distinct.map((route) => `"${route}" (${routes.get(route)})`);
|
|
354
|
+
diagnostics.push({
|
|
355
|
+
code: "route.case-collision",
|
|
356
|
+
severity: "error",
|
|
357
|
+
sourcePath: routes.get(distinct[0]),
|
|
358
|
+
message: `routes ${named.join(" and ")} differ only by case and collide on case-insensitive filesystems`,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return diagnostics;
|
|
363
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// Public types: the config input shape (what a user's `site.config.ts`
|
|
2
|
+
// imports), the branded validated `Site`, the resolved view, the frontmatter
|
|
3
|
+
// contract, and the content-pipeline shapes (pages, headings, internal
|
|
4
|
+
// links). Input shapes are what defineSite TYPE-checkS against;
|
|
5
|
+
// runtime validation in config.ts is independent (JS users get the same
|
|
6
|
+
// diagnostics — crust rule 1, no silent pass).
|
|
7
|
+
|
|
8
|
+
import type { Diagnostic } from "./diagnostics";
|
|
9
|
+
import type { Html } from "./html";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Documented-open union (plan, "Locked decisions" 4): v1 accepts only
|
|
13
|
+
* `"never"`, but typing it open means a future `"always"`/`"html"` widens the
|
|
14
|
+
* enum without breaking anyone's compiled config. defineSite still REJECTS
|
|
15
|
+
* every value other than "never" at runtime.
|
|
16
|
+
*/
|
|
17
|
+
export type TrailingSlash = "never" | (string & {});
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A layout is any function; the real LayoutContext arrives with the render
|
|
21
|
+
* chunk. Typed `(...args: never[]) => unknown` so a user's
|
|
22
|
+
* `(ctx: LayoutContext) => string` is assignable while non-functions are not.
|
|
23
|
+
*/
|
|
24
|
+
export type LayoutFn = (...args: never[]) => unknown;
|
|
25
|
+
|
|
26
|
+
export interface ContentConfig {
|
|
27
|
+
/** Directory of .md sources, relative to the config file (absolute allowed). */
|
|
28
|
+
directory: string;
|
|
29
|
+
/** Globs to include. Default: every .md file, recursively. */
|
|
30
|
+
include?: string[];
|
|
31
|
+
/** Globs to exclude; exclude wins over include. Default `[]`. */
|
|
32
|
+
exclude?: string[];
|
|
33
|
+
/** Must be a key of `layouts`. */
|
|
34
|
+
defaultLayout: string;
|
|
35
|
+
/**
|
|
36
|
+
* Maps a source file to a route. Must return an ALREADY-normalized route
|
|
37
|
+
* (no auto-normalization, plan §2); a bad return is an error naming the page.
|
|
38
|
+
*/
|
|
39
|
+
route?: (arg: { relativePath: string }) => string;
|
|
40
|
+
/**
|
|
41
|
+
* Hook for the data layer. `parsePage` calls it with the page's RAW
|
|
42
|
+
* frontmatter `data` (or `{}` when absent) and the page's sourcePath; an
|
|
43
|
+
* async hook (a returned Promise) is awaited. It must RESOLVE to a plain
|
|
44
|
+
object — a throw or rejection becomes `frontmatter.data-invalid`, and a
|
|
45
|
+
non-object result is the same (never a silent pass). The resolved object
|
|
46
|
+
becomes `page.data`.
|
|
47
|
+
*/
|
|
48
|
+
parseData?: (value: unknown, ctx: { sourcePath: string }) => unknown;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface AssetsConfig {
|
|
52
|
+
/** name → entrypoint path. Names share ONE namespace with `styles`. */
|
|
53
|
+
scripts?: Record<string, string>;
|
|
54
|
+
styles?: Record<string, string>;
|
|
55
|
+
/** Copied to the output root unhashed (no cache-busting, documented). */
|
|
56
|
+
publicDirectory?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface OutputConfig {
|
|
60
|
+
directory: string;
|
|
61
|
+
/** v1 accepts only `"never"`. */
|
|
62
|
+
trailingSlash?: TrailingSlash;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface RobotsConfig {
|
|
66
|
+
indexable: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface DevConfig {
|
|
70
|
+
hostname?: string;
|
|
71
|
+
/** 1..65535. */
|
|
72
|
+
port?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The shape a user's `site.config.ts` passes to `defineSite`. */
|
|
76
|
+
export interface SiteConfig {
|
|
77
|
+
/** Absolute http(s) URL, may carry a path prefix, must NOT end in "/". */
|
|
78
|
+
site: string;
|
|
79
|
+
content: ContentConfig;
|
|
80
|
+
/** Non-empty registry name → layout function. */
|
|
81
|
+
layouts: Record<string, LayoutFn>;
|
|
82
|
+
assets?: AssetsConfig;
|
|
83
|
+
output: OutputConfig;
|
|
84
|
+
/** Default true. */
|
|
85
|
+
sitemap?: boolean;
|
|
86
|
+
robots?: RobotsConfig;
|
|
87
|
+
dev?: DevConfig;
|
|
88
|
+
/** BCP 47. Default "en". */
|
|
89
|
+
lang?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Phantom brand: `defineSite` is the only producer of a `Site`, and the
|
|
94
|
+
* property does not exist at runtime — a hand-made object literal cannot
|
|
95
|
+
* impersonate a validated config.
|
|
96
|
+
*/
|
|
97
|
+
declare const siteBrand: unique symbol;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* A validated config with RAW (unresolved) paths, as stored by `defineSite`.
|
|
101
|
+
* Resolve with `resolveConfigPaths(site, configDir)` before any I/O.
|
|
102
|
+
*/
|
|
103
|
+
export interface Site {
|
|
104
|
+
readonly [siteBrand]: never;
|
|
105
|
+
readonly site: string;
|
|
106
|
+
readonly content: {
|
|
107
|
+
readonly directory: string;
|
|
108
|
+
readonly include: readonly string[];
|
|
109
|
+
readonly exclude: readonly string[];
|
|
110
|
+
readonly defaultLayout: string;
|
|
111
|
+
readonly route?: (arg: { relativePath: string }) => string;
|
|
112
|
+
readonly parseData?: (value: unknown, ctx: { sourcePath: string }) => unknown;
|
|
113
|
+
};
|
|
114
|
+
readonly layouts: Readonly<Record<string, LayoutFn>>;
|
|
115
|
+
readonly assets: {
|
|
116
|
+
readonly scripts: Readonly<Record<string, string>>;
|
|
117
|
+
readonly styles: Readonly<Record<string, string>>;
|
|
118
|
+
readonly publicDirectory?: string;
|
|
119
|
+
};
|
|
120
|
+
readonly output: { readonly directory: string; readonly trailingSlash: TrailingSlash };
|
|
121
|
+
readonly sitemap: boolean;
|
|
122
|
+
readonly robots?: RobotsConfig;
|
|
123
|
+
readonly dev?: DevConfig;
|
|
124
|
+
readonly lang: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The view after `resolveConfigPaths`: every path is absolute (posix form),
|
|
129
|
+
* resolved against the CONFIG FILE's directory — never cwd.
|
|
130
|
+
*/
|
|
131
|
+
export interface ResolvedSite {
|
|
132
|
+
readonly configDir: string;
|
|
133
|
+
readonly site: string;
|
|
134
|
+
readonly content: Site["content"] & { readonly directory: string };
|
|
135
|
+
readonly layouts: Site["layouts"];
|
|
136
|
+
readonly assets: {
|
|
137
|
+
readonly scripts: Readonly<Record<string, string>>;
|
|
138
|
+
readonly styles: Readonly<Record<string, string>>;
|
|
139
|
+
readonly publicDirectory?: string;
|
|
140
|
+
};
|
|
141
|
+
readonly output: Site["output"];
|
|
142
|
+
readonly sitemap: boolean;
|
|
143
|
+
readonly robots?: RobotsConfig;
|
|
144
|
+
readonly dev?: DevConfig;
|
|
145
|
+
readonly lang: string;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The built-in frontmatter keys, everything else is frontmatter.unknown-key. */
|
|
149
|
+
export interface PageFrontmatter {
|
|
150
|
+
title: string;
|
|
151
|
+
description?: string;
|
|
152
|
+
layout?: string;
|
|
153
|
+
/** Always a normalized route (validated at parse time). */
|
|
154
|
+
route?: string;
|
|
155
|
+
draft?: boolean;
|
|
156
|
+
data?: Record<string, unknown>;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface FrontmatterResult {
|
|
160
|
+
frontmatter: PageFrontmatter;
|
|
161
|
+
/** Everything after the closing `---` line. */
|
|
162
|
+
body: string;
|
|
163
|
+
/** 1-based line of the first body line, in the FULL file's coordinates. */
|
|
164
|
+
bodyStartLine: number;
|
|
165
|
+
/**
|
|
166
|
+
* 1-based line (FULL-file coordinates) of the `data` key, when the
|
|
167
|
+
* frontmatter has one — so a content.parseData failure can point at it.
|
|
168
|
+
*/
|
|
169
|
+
dataLine?: number;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** One row of the route table handed to `findRouteCollisions`. */
|
|
173
|
+
export interface RouteEntry {
|
|
174
|
+
route: string;
|
|
175
|
+
sourcePath: string;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* A heading as collected during render: `id` is the FINAL slug (collision
|
|
180
|
+
* suffixes applied in document order), `text` the plain text of the heading
|
|
181
|
+
* with markdown syntax stripped and entities decoded.
|
|
182
|
+
*/
|
|
183
|
+
export interface Heading {
|
|
184
|
+
level: number;
|
|
185
|
+
id: string;
|
|
186
|
+
text: string;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* An internal link extracted during render — the input for chunk 7's link
|
|
191
|
+
* validation. `route` is the destination route (for a pure fragment link,
|
|
192
|
+
* the page's own route); `exists: false` means the destination looked like
|
|
193
|
+
* an internal page but was not in the route table (chunk 7 turns that into
|
|
194
|
+
* link.broken — chunk 4 only records). `line` is best-effort: block-level
|
|
195
|
+
* map info in FULL-file coordinates, `null` when no enclosing block carried
|
|
196
|
+
* a map.
|
|
197
|
+
*/
|
|
198
|
+
export interface InternalLink {
|
|
199
|
+
route: string;
|
|
200
|
+
fragment: string | null;
|
|
201
|
+
line: number | null;
|
|
202
|
+
exists: boolean;
|
|
203
|
+
isDraft: boolean;
|
|
204
|
+
kind: "page" | "fragment";
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* A route-table answer for an internal link candidate: the page entry,
|
|
209
|
+
* `"external"` (not an internal route at all — keep the destination and
|
|
210
|
+
* apply the URL policy), or `null` (looks internal, not found).
|
|
211
|
+
*/
|
|
212
|
+
export type RouteResolution =
|
|
213
|
+
| { route: string; exists: boolean; isDraft: boolean }
|
|
214
|
+
| "external"
|
|
215
|
+
| null;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Resolves an internal link candidate — the destination path already
|
|
219
|
+
* resolved against the PAGE'S ROUTE and stripped of `.md` (never the
|
|
220
|
+
* source path, plan §2). Chunk 6 wires this to the real route table;
|
|
221
|
+
* chunk 4 defines the interface and tests it with a fake.
|
|
222
|
+
*/
|
|
223
|
+
export type RouteResolver = (candidateRoute: string) => RouteResolution;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* One parsed .md page. `route`/`canonicalUrl` are filled by the route-table
|
|
227
|
+
* step (buildRouteTable) — a page straight from parsePage carries only the
|
|
228
|
+
* frontmatter `route` when it had one. `diagnostics` carries every
|
|
229
|
+
* page-level problem found during parse/render (errors AND warnings);
|
|
230
|
+
* `warnings` is the warning-severity subset, for callers that only surface
|
|
231
|
+
* those.
|
|
232
|
+
*/
|
|
233
|
+
export interface Page {
|
|
234
|
+
readonly sourcePath: string;
|
|
235
|
+
/** posix, relative to content.directory, extension stripped (plan §2). */
|
|
236
|
+
readonly relativePath: string;
|
|
237
|
+
/** frontmatter route only; content.route() runs in buildRouteTable. */
|
|
238
|
+
readonly route?: string;
|
|
239
|
+
/** frontmatter layout only; defaultLayout fallback in buildRouteTable. */
|
|
240
|
+
readonly layout?: string;
|
|
241
|
+
readonly title: string;
|
|
242
|
+
readonly description: string;
|
|
243
|
+
readonly headings: Heading[];
|
|
244
|
+
readonly data: Record<string, unknown>;
|
|
245
|
+
readonly draft: boolean;
|
|
246
|
+
readonly body: Html;
|
|
247
|
+
readonly links: InternalLink[];
|
|
248
|
+
readonly warnings: Diagnostic[];
|
|
249
|
+
readonly diagnostics: Diagnostic[];
|
|
250
|
+
}
|