@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/content.ts
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// Content discovery + the route table (plan §2, §4.6). Discovery is
|
|
2
|
+
// deterministic by construction: lexicographic (code-unit) order, exclude
|
|
3
|
+
// wins over include, dotfiles excluded unless the include pattern starts
|
|
4
|
+
// with `.`, symlinks never followed, only .md files. The route table is the
|
|
5
|
+
// single place a page acquires its route: frontmatter `route` wins, else
|
|
6
|
+
// content.route(relativePath); every candidate goes through normalizeRoute
|
|
7
|
+
// (already-normalized-or-error, plan §2 — no auto-fixing), collisions run
|
|
8
|
+
// BOTH passes (exact + case-insensitive), and drafts are excluded from the
|
|
9
|
+
// production table while staying listed (with their routes) for the dev
|
|
10
|
+
// server and the link checker.
|
|
11
|
+
|
|
12
|
+
import { lstat } from "node:fs/promises";
|
|
13
|
+
import { compareStrings, type Diagnostic, isDiagnostic, sortDiagnostics } from "./diagnostics";
|
|
14
|
+
import { filesystemPath as posix } from "./fs-path";
|
|
15
|
+
import { canonicalUrl, findRouteCollisions, normalizeRoute } from "./routes";
|
|
16
|
+
import type { Page, ResolvedSite } from "./types";
|
|
17
|
+
|
|
18
|
+
export interface DiscoveredFile {
|
|
19
|
+
absolutePath: string;
|
|
20
|
+
/** posix, relative to content.directory, extension stripped (plan §2). */
|
|
21
|
+
relativePath: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function toPosix(p: string): string {
|
|
25
|
+
return p.replace(/\\/g, "/");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Discover the .md sources under `content.directory`. Globs run with
|
|
30
|
+
* `cwd = content.directory`; results are posix-relative. A symlink (to a
|
|
31
|
+
* file OR a directory) is skipped via lstat — the plan's containment story
|
|
32
|
+
* says symlinks never extend a boundary, and discovery is where that
|
|
33
|
+
* boundary is enforced for inputs.
|
|
34
|
+
*/
|
|
35
|
+
export async function discoverContent(resolvedSite: ResolvedSite): Promise<DiscoveredFile[]> {
|
|
36
|
+
const dir = resolvedSite.content.directory;
|
|
37
|
+
const found = new Set<string>();
|
|
38
|
+
for (const pattern of resolvedSite.content.include) {
|
|
39
|
+
const glob = new Bun.Glob(pattern);
|
|
40
|
+
for await (const rel of glob.scan({
|
|
41
|
+
cwd: dir,
|
|
42
|
+
onlyFiles: true,
|
|
43
|
+
// dotfiles are excluded unless the pattern itself reaches for them
|
|
44
|
+
dot: pattern.startsWith("."),
|
|
45
|
+
followSymlinks: false,
|
|
46
|
+
})) {
|
|
47
|
+
found.add(toPosix(rel));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const excludes = resolvedSite.content.exclude.map((p) => new Bun.Glob(p));
|
|
51
|
+
const kept = [...found]
|
|
52
|
+
.filter((rel) => !excludes.some((g) => g.match(rel)))
|
|
53
|
+
.filter((rel) => rel.endsWith(".md"))
|
|
54
|
+
.sort(compareStrings);
|
|
55
|
+
|
|
56
|
+
const out: DiscoveredFile[] = [];
|
|
57
|
+
for (const rel of kept) {
|
|
58
|
+
const absolutePath = posix.resolve(dir, rel);
|
|
59
|
+
let st: Awaited<ReturnType<typeof lstat>>;
|
|
60
|
+
try {
|
|
61
|
+
st = await lstat(absolutePath);
|
|
62
|
+
} catch {
|
|
63
|
+
continue; // vanished between scan and stat — not an error, it is simply not there
|
|
64
|
+
}
|
|
65
|
+
if (st.isSymbolicLink()) continue;
|
|
66
|
+
out.push({ absolutePath, relativePath: rel.replace(/\.md$/, "") });
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A page that has a final route, canonical URL, and resolved layout key. */
|
|
72
|
+
export interface RoutedPage extends Page {
|
|
73
|
+
route: string;
|
|
74
|
+
canonicalUrl: string;
|
|
75
|
+
layout: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* One page's route decision (chunk 8). A page's internal .md links must
|
|
80
|
+
* resolve against the FULL route table, but the table is built from parsed
|
|
81
|
+
* pages — so every page's route has to be known BEFORE the first markdown
|
|
82
|
+
* render. The decision needs only the frontmatter (`route`/`draft`) plus
|
|
83
|
+
* `content.route`, all of which the pre-pass has from the frontmatter parse
|
|
84
|
+
* alone: that is why this function takes them as plain values, not a Page.
|
|
85
|
+
* `buildRouteTable` reuses the same helper for callers that do not pre-pass
|
|
86
|
+
* decisions, so the derivation logic (and its diagnostics) has one home.
|
|
87
|
+
*/
|
|
88
|
+
export interface PrecomputedRoute {
|
|
89
|
+
/** The final normalized route; undefined when the derivation failed. */
|
|
90
|
+
readonly route?: string;
|
|
91
|
+
/** The frontmatter `draft` flag (the link resolver and the table need it). */
|
|
92
|
+
readonly draft: boolean;
|
|
93
|
+
/** The decision's diagnostics (route.missing / route.invalid / …). */
|
|
94
|
+
readonly diagnostics: Diagnostic[];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function derivePageRoute(args: {
|
|
98
|
+
/** The frontmatter `route` (already normalized at parse time), when present. */
|
|
99
|
+
frontmatterRoute?: string;
|
|
100
|
+
relativePath: string;
|
|
101
|
+
sourcePath: string;
|
|
102
|
+
routeFn?: (arg: { relativePath: string }) => string;
|
|
103
|
+
draft: boolean;
|
|
104
|
+
}): PrecomputedRoute {
|
|
105
|
+
const { frontmatterRoute, relativePath, sourcePath, routeFn, draft } = args;
|
|
106
|
+
let candidate: unknown = frontmatterRoute;
|
|
107
|
+
if (candidate === undefined) {
|
|
108
|
+
if (!routeFn) {
|
|
109
|
+
return {
|
|
110
|
+
draft,
|
|
111
|
+
diagnostics: [
|
|
112
|
+
{
|
|
113
|
+
code: "route.missing",
|
|
114
|
+
severity: "error",
|
|
115
|
+
sourcePath,
|
|
116
|
+
message: `page ${sourcePath} has no route: no frontmatter "route" and content.route is not configured`,
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
candidate = routeFn({ relativePath });
|
|
123
|
+
} catch (e) {
|
|
124
|
+
return {
|
|
125
|
+
draft,
|
|
126
|
+
diagnostics: [
|
|
127
|
+
{
|
|
128
|
+
code: "route.invalid",
|
|
129
|
+
severity: "error",
|
|
130
|
+
sourcePath,
|
|
131
|
+
message: `content.route threw for page ${sourcePath}: ${e instanceof Error ? e.message : String(e)}`,
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
return { route: normalizeRoute(candidate, { sourcePath }), draft, diagnostics: [] };
|
|
139
|
+
} catch (d) {
|
|
140
|
+
if (!isDiagnostic(d)) throw d;
|
|
141
|
+
return { draft, diagnostics: [d] };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface RouteTable {
|
|
146
|
+
/** Production pages (drafts excluded), in lexicographic route order. */
|
|
147
|
+
pages: RoutedPage[];
|
|
148
|
+
/** Draft pages WITH their routes — dev server + link checking only. */
|
|
149
|
+
drafts: RoutedPage[];
|
|
150
|
+
/** Page warnings + every route/layout/collision problem, sorted. */
|
|
151
|
+
diagnostics: Diagnostic[];
|
|
152
|
+
/** Production route → page. Drafts are NOT here (chunk 6/7 consult both). */
|
|
153
|
+
routeIndex: Map<string, RoutedPage>;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Turn parsed pages into the route table. A page whose route is missing,
|
|
158
|
+
* malformed, or whose layout is unknown is reported and DROPPED from the
|
|
159
|
+
* table (the build fails on the error — the table stays internally sound for
|
|
160
|
+
* everything else that consumes it in the same run).
|
|
161
|
+
*
|
|
162
|
+
* Decisions pinned here:
|
|
163
|
+
* - `content.route` throwing is a route.invalid diagnostic naming the page,
|
|
164
|
+
* never a raw exception escaping the table. A route() returning a
|
|
165
|
+
* non-string (number, undefined, …) is the SAME code: normalizeRoute
|
|
166
|
+
* rejects it with the received type named.
|
|
167
|
+
* - Collisions run over production pages AND drafts: a draft at /foo and a
|
|
168
|
+
* page at /foo is ambiguous for link resolution in every mode, so it is
|
|
169
|
+
* an error in every mode (crust rule 1 — no false pass in dev either).
|
|
170
|
+
* A page that already failed validation is dropped BEFORE the collision
|
|
171
|
+
* pass — it can never go live, so it cannot squat a route (its own
|
|
172
|
+
* errors are still all surfaced).
|
|
173
|
+
* - Zero production pages is a `content.no-pages` WARNING (never an error,
|
|
174
|
+
* never silence): the build would emit no content pages, only the
|
|
175
|
+
* generated 404 page. The message names the draft count in the
|
|
176
|
+
* drafts-only case.
|
|
177
|
+
*/
|
|
178
|
+
export function buildRouteTable(
|
|
179
|
+
pages: readonly Page[],
|
|
180
|
+
resolvedSite: ResolvedSite,
|
|
181
|
+
precomputed?: ReadonlyMap<string, PrecomputedRoute>,
|
|
182
|
+
): RouteTable {
|
|
183
|
+
const diagnostics: Diagnostic[] = [];
|
|
184
|
+
const routed: RoutedPage[] = [];
|
|
185
|
+
const routeFn = resolvedSite.content.route;
|
|
186
|
+
const layoutKeys = Object.keys(resolvedSite.layouts).sort(compareStrings);
|
|
187
|
+
|
|
188
|
+
for (const page of pages) {
|
|
189
|
+
diagnostics.push(...page.diagnostics);
|
|
190
|
+
|
|
191
|
+
// Route derivation. The build's pre-pass (chunk 8) computes every route
|
|
192
|
+
// BEFORE the first markdown render — a page's .md links resolve against
|
|
193
|
+
// the full table, so the table's contents must already exist when the
|
|
194
|
+
// links are written. Callers with a pre-pass hand the decisions over and
|
|
195
|
+
// the table REUSES them (replaying the same diagnostics, never
|
|
196
|
+
// re-deriving: a second derivation would double-report, or re-invoke a
|
|
197
|
+
// throwing content.route). Pre-pass decisions replay UNCONDITIONALLY —
|
|
198
|
+
// even for a page that already failed: the pre-pass already ran (it
|
|
199
|
+
// already called content.route), so dropping its diagnostics would
|
|
200
|
+
// silently swallow a real problem the run paid for. Without a pre-pass
|
|
201
|
+
// the same helper derives on the spot, and only for pages that have not
|
|
202
|
+
// failed (the historical behaviour: no spurious route.* follow-ups on a
|
|
203
|
+
// broken page, no user callback invoked for it).
|
|
204
|
+
const pre = precomputed?.get(page.sourcePath);
|
|
205
|
+
if (pre) diagnostics.push(...pre.diagnostics);
|
|
206
|
+
// a page that already failed validation is never routed
|
|
207
|
+
if (page.diagnostics.some((d) => d.severity === "error")) continue;
|
|
208
|
+
const decision =
|
|
209
|
+
pre ??
|
|
210
|
+
derivePageRoute({
|
|
211
|
+
frontmatterRoute: page.route,
|
|
212
|
+
relativePath: page.relativePath,
|
|
213
|
+
sourcePath: page.sourcePath,
|
|
214
|
+
routeFn,
|
|
215
|
+
draft: page.draft,
|
|
216
|
+
});
|
|
217
|
+
if (!pre) diagnostics.push(...decision.diagnostics);
|
|
218
|
+
if (decision.route === undefined) continue;
|
|
219
|
+
const route = decision.route;
|
|
220
|
+
|
|
221
|
+
const layout = page.layout ?? resolvedSite.content.defaultLayout;
|
|
222
|
+
if (!Object.hasOwn(resolvedSite.layouts, layout)) {
|
|
223
|
+
diagnostics.push({
|
|
224
|
+
code: "layout.unknown",
|
|
225
|
+
severity: "error",
|
|
226
|
+
sourcePath: page.sourcePath,
|
|
227
|
+
message: `layout "${layout}" for page ${page.sourcePath} is not a key of layouts (available: ${layoutKeys.join(", ")})`,
|
|
228
|
+
});
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
routed.push({ ...page, route, layout, canonicalUrl: canonicalUrl(resolvedSite.site, route) });
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Deterministic table order (plan §4.6): route first, sourcePath breaks ties.
|
|
235
|
+
routed.sort(
|
|
236
|
+
(a, b) => compareStrings(a.route, b.route) || compareStrings(a.sourcePath, b.sourcePath),
|
|
237
|
+
);
|
|
238
|
+
diagnostics.push(
|
|
239
|
+
...findRouteCollisions(routed.map((p) => ({ route: p.route, sourcePath: p.sourcePath }))),
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
const production = routed.filter((p) => !p.draft);
|
|
243
|
+
const drafts = routed.filter((p) => p.draft);
|
|
244
|
+
|
|
245
|
+
// A production build emits one file per PRODUCTION page; zero of those
|
|
246
|
+
// means the site renders only the generated 404 page. That is SUSPICIOUS
|
|
247
|
+
// (empty content dir, everything accidentally a draft) but LEGAL — pinned
|
|
248
|
+
// as a warning, never an error, and never silence.
|
|
249
|
+
if (production.length === 0) {
|
|
250
|
+
diagnostics.push({
|
|
251
|
+
code: "content.no-pages",
|
|
252
|
+
severity: "warning",
|
|
253
|
+
sourcePath: "",
|
|
254
|
+
message:
|
|
255
|
+
drafts.length === 0
|
|
256
|
+
? "the content directory produced zero pages; a production build would emit no content pages (only the generated 404 page)"
|
|
257
|
+
: `no non-draft pages found (${drafts.length} draft page${drafts.length === 1 ? "" : "s"}); a production build would emit no content pages (only the generated 404 page)`,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
pages: production,
|
|
263
|
+
drafts,
|
|
264
|
+
diagnostics: sortDiagnostics(diagnostics),
|
|
265
|
+
routeIndex: new Map(production.map((p) => [p.route, p])),
|
|
266
|
+
};
|
|
267
|
+
}
|