@tschk/moonshine-astro 0.4.4 → 0.4.80
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 +176 -7
- package/package.json +18 -5
- package/src/actions.ts +556 -0
- package/src/assets.ts +710 -0
- package/src/config.ts +269 -0
- package/src/container.ts +560 -0
- package/src/content.ts +1523 -0
- package/src/context.ts +756 -0
- package/src/env.ts +361 -0
- package/src/i18n.ts +420 -0
- package/src/index.ts +264 -0
- package/src/loaders.ts +888 -0
- package/src/middleware.ts +206 -0
- package/src/routing.ts +239 -0
- package/src/ssr.ts +439 -0
- package/src/transitions.ts +496 -0
package/src/loaders.ts
ADDED
|
@@ -0,0 +1,888 @@
|
|
|
1
|
+
/** Independent host-free Astro Content Layer loaders (`astro/loaders`): `glob`, `file`, and structured frontmatter/data parsers. */
|
|
2
|
+
import {
|
|
3
|
+
extractMarkdownHeadings,
|
|
4
|
+
renderSimpleMarkdownToHtml,
|
|
5
|
+
type ContentLoader,
|
|
6
|
+
type LoaderContext,
|
|
7
|
+
type RenderedContent,
|
|
8
|
+
} from "./content";
|
|
9
|
+
|
|
10
|
+
export interface GenerateIdOptions {
|
|
11
|
+
/** Path to the entry file, relative to the base directory (POSIX forward-slash normalized). */
|
|
12
|
+
entry: string;
|
|
13
|
+
/** The base directory URL. */
|
|
14
|
+
base: URL;
|
|
15
|
+
/** The parsed, unvalidated data of the entry. */
|
|
16
|
+
data: Record<string, any>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface GlobLoaderOptions {
|
|
20
|
+
/** One or more glob patterns matched relative to `base` (supports `*`, `**`, `?`, `{a,b}`, and `!negated`). */
|
|
21
|
+
pattern: string | readonly string[];
|
|
22
|
+
/** Base directory path or `file:` URL (defaults to `"."`). */
|
|
23
|
+
base?: string | URL;
|
|
24
|
+
/** Optional custom ID generator. Defaults to stripping known content/data extensions. */
|
|
25
|
+
generateId?: (options: GenerateIdOptions) => string;
|
|
26
|
+
/** Optional custom parser for file contents. */
|
|
27
|
+
parser?: (
|
|
28
|
+
text: string,
|
|
29
|
+
filePath: string,
|
|
30
|
+
) => Record<string, any> | Promise<Record<string, any>>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface FileLoaderOptions {
|
|
34
|
+
/** Optional custom parser returning either a keyed map of entries or an array of `{ id, ... }` objects. */
|
|
35
|
+
parser?: (
|
|
36
|
+
text: string,
|
|
37
|
+
) =>
|
|
38
|
+
| Record<string, Record<string, any>>
|
|
39
|
+
| Array<Record<string, any>>
|
|
40
|
+
| Promise<Record<string, Record<string, any>> | Array<Record<string, any>>>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface FrontmatterParseResult {
|
|
44
|
+
data: Record<string, any>;
|
|
45
|
+
body: string;
|
|
46
|
+
rawFrontmatter: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const importBuiltin = new Function("specifier", "return import(specifier)") as <
|
|
50
|
+
T = any,
|
|
51
|
+
>(
|
|
52
|
+
specifier: string,
|
|
53
|
+
) => Promise<T>;
|
|
54
|
+
|
|
55
|
+
function unquoteScalar(raw: string): string {
|
|
56
|
+
const trimmed = raw.trim();
|
|
57
|
+
if (
|
|
58
|
+
trimmed.length >= 2 &&
|
|
59
|
+
((trimmed.startsWith('"') && trimmed.endsWith('"')) ||
|
|
60
|
+
(trimmed.startsWith("'") && trimmed.endsWith("'")))
|
|
61
|
+
) {
|
|
62
|
+
const inner = trimmed.slice(1, -1);
|
|
63
|
+
if (trimmed.startsWith('"')) {
|
|
64
|
+
return inner
|
|
65
|
+
.replace(/\\n/g, "\n")
|
|
66
|
+
.replace(/\\t/g, "\t")
|
|
67
|
+
.replace(/\\"/g, '"')
|
|
68
|
+
.replace(/\\\\/g, "\\");
|
|
69
|
+
}
|
|
70
|
+
return inner.replace(/''/g, "'");
|
|
71
|
+
}
|
|
72
|
+
return trimmed;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function parseScalarValue(raw: string): any {
|
|
76
|
+
const trimmed = raw.trim();
|
|
77
|
+
if (trimmed === "") return "";
|
|
78
|
+
if (trimmed === "true") return true;
|
|
79
|
+
if (trimmed === "false") return false;
|
|
80
|
+
if (trimmed === "null" || trimmed === "~") return null;
|
|
81
|
+
if (
|
|
82
|
+
(trimmed.startsWith('"') && trimmed.endsWith('"')) ||
|
|
83
|
+
(trimmed.startsWith("'") && trimmed.endsWith("'"))
|
|
84
|
+
) {
|
|
85
|
+
return unquoteScalar(trimmed);
|
|
86
|
+
}
|
|
87
|
+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
|
88
|
+
const inner = trimmed.slice(1, -1).trim();
|
|
89
|
+
if (!inner) return [];
|
|
90
|
+
return splitTopLevel(inner, ",").map((item) => parseScalarValue(item));
|
|
91
|
+
}
|
|
92
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
|
|
93
|
+
const inner = trimmed.slice(1, -1).trim();
|
|
94
|
+
if (!inner) return {};
|
|
95
|
+
const obj: Record<string, any> = {};
|
|
96
|
+
for (const pair of splitTopLevel(inner, ",")) {
|
|
97
|
+
const colonIdx = pair.indexOf(":");
|
|
98
|
+
if (colonIdx === -1) continue;
|
|
99
|
+
const k = unquoteScalar(pair.slice(0, colonIdx));
|
|
100
|
+
const v = parseScalarValue(pair.slice(colonIdx + 1));
|
|
101
|
+
if (k) obj[k] = v;
|
|
102
|
+
}
|
|
103
|
+
return obj;
|
|
104
|
+
}
|
|
105
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
|
|
106
|
+
const num = Number(trimmed);
|
|
107
|
+
if (!Number.isNaN(num)) return num;
|
|
108
|
+
}
|
|
109
|
+
return trimmed;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function splitTopLevel(input: string, delimiter: string): string[] {
|
|
113
|
+
const parts: string[] = [];
|
|
114
|
+
let current = "";
|
|
115
|
+
let depth = 0;
|
|
116
|
+
let quote: '"' | "'" | null = null;
|
|
117
|
+
for (let i = 0; i < input.length; i++) {
|
|
118
|
+
const ch = input[i]!;
|
|
119
|
+
if (quote) {
|
|
120
|
+
current += ch;
|
|
121
|
+
if (ch === "\\" && quote === '"' && i + 1 < input.length) {
|
|
122
|
+
current += input[++i]!;
|
|
123
|
+
} else if (ch === quote) {
|
|
124
|
+
quote = null;
|
|
125
|
+
}
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (ch === '"' || ch === "'") {
|
|
129
|
+
quote = ch;
|
|
130
|
+
current += ch;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (ch === "[" || ch === "{") {
|
|
134
|
+
depth++;
|
|
135
|
+
current += ch;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (ch === "]" || ch === "}") {
|
|
139
|
+
depth = Math.max(0, depth - 1);
|
|
140
|
+
current += ch;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (depth === 0 && input.startsWith(delimiter, i)) {
|
|
144
|
+
parts.push(current.trim());
|
|
145
|
+
current = "";
|
|
146
|
+
i += delimiter.length - 1;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
current += ch;
|
|
150
|
+
}
|
|
151
|
+
if (current.trim().length > 0) {
|
|
152
|
+
parts.push(current.trim());
|
|
153
|
+
}
|
|
154
|
+
return parts;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function stripInlineComment(line: string): string {
|
|
158
|
+
let quote: '"' | "'" | null = null;
|
|
159
|
+
for (let i = 0; i < line.length; i++) {
|
|
160
|
+
const ch = line[i]!;
|
|
161
|
+
if (quote) {
|
|
162
|
+
if (ch === "\\" && quote === '"') {
|
|
163
|
+
i++;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (ch === quote) quote = null;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (ch === '"' || ch === "'") {
|
|
170
|
+
quote = ch;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (ch === "#" && (i === 0 || /\s/.test(line[i - 1]!))) {
|
|
174
|
+
return line.slice(0, i).trimEnd();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return line;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Lightweight host-free YAML parser supporting objects, nested maps, arrays, and scalars. */
|
|
181
|
+
export function parseSimpleYaml(raw: string): any {
|
|
182
|
+
const rawLines = raw.split(/\r?\n/);
|
|
183
|
+
const lines: Array<{ indent: number; text: string }> = [];
|
|
184
|
+
for (const rawLine of rawLines) {
|
|
185
|
+
const cleaned = stripInlineComment(rawLine);
|
|
186
|
+
if (!cleaned.trim() || cleaned.trim() === "---") continue;
|
|
187
|
+
const indentMatch = /^(\s*)/.exec(cleaned);
|
|
188
|
+
const indent = indentMatch ? indentMatch[1]!.length : 0;
|
|
189
|
+
lines.push({ indent, text: cleaned.trim() });
|
|
190
|
+
}
|
|
191
|
+
if (lines.length === 0) return {};
|
|
192
|
+
|
|
193
|
+
let idx = 0;
|
|
194
|
+
|
|
195
|
+
function parseBlock(minIndent: number): any {
|
|
196
|
+
if (idx >= lines.length) return {};
|
|
197
|
+
const first = lines[idx]!;
|
|
198
|
+
if (first.indent < minIndent) return {};
|
|
199
|
+
|
|
200
|
+
if (first.text.startsWith("- ") || first.text === "-") {
|
|
201
|
+
const arr: any[] = [];
|
|
202
|
+
const listIndent = first.indent;
|
|
203
|
+
while (idx < lines.length && lines[idx]!.indent === listIndent) {
|
|
204
|
+
const line = lines[idx]!;
|
|
205
|
+
if (!line.text.startsWith("-")) break;
|
|
206
|
+
const afterDash = line.text.slice(1).trim();
|
|
207
|
+
idx++;
|
|
208
|
+
if (!afterDash) {
|
|
209
|
+
if (idx < lines.length && lines[idx]!.indent > listIndent) {
|
|
210
|
+
arr.push(parseBlock(lines[idx]!.indent));
|
|
211
|
+
} else {
|
|
212
|
+
arr.push(null);
|
|
213
|
+
}
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
const colonIdx = findKeyValueColon(afterDash);
|
|
217
|
+
if (colonIdx !== -1) {
|
|
218
|
+
const itemObj: Record<string, any> = {};
|
|
219
|
+
const k = unquoteScalar(afterDash.slice(0, colonIdx));
|
|
220
|
+
const vRaw = afterDash.slice(colonIdx + 1).trim();
|
|
221
|
+
if (vRaw) {
|
|
222
|
+
itemObj[k] = parseScalarValue(vRaw);
|
|
223
|
+
} else if (idx < lines.length && lines[idx]!.indent > listIndent) {
|
|
224
|
+
itemObj[k] = parseBlock(lines[idx]!.indent);
|
|
225
|
+
} else {
|
|
226
|
+
itemObj[k] = null;
|
|
227
|
+
}
|
|
228
|
+
while (idx < lines.length && lines[idx]!.indent > listIndent) {
|
|
229
|
+
const subLine = lines[idx]!;
|
|
230
|
+
if (subLine.text.startsWith("-")) break;
|
|
231
|
+
const subColon = findKeyValueColon(subLine.text);
|
|
232
|
+
if (subColon === -1) {
|
|
233
|
+
idx++;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const subKey = unquoteScalar(subLine.text.slice(0, subColon));
|
|
237
|
+
const subValRaw = subLine.text.slice(subColon + 1).trim();
|
|
238
|
+
const propIndent = subLine.indent;
|
|
239
|
+
idx++;
|
|
240
|
+
if (subValRaw) {
|
|
241
|
+
itemObj[subKey] = parseScalarValue(subValRaw);
|
|
242
|
+
} else if (idx < lines.length && lines[idx]!.indent > propIndent) {
|
|
243
|
+
itemObj[subKey] = parseBlock(lines[idx]!.indent);
|
|
244
|
+
} else {
|
|
245
|
+
itemObj[subKey] = null;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
arr.push(itemObj);
|
|
249
|
+
} else {
|
|
250
|
+
arr.push(parseScalarValue(afterDash));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return arr;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const obj: Record<string, any> = {};
|
|
257
|
+
const mapIndent = first.indent;
|
|
258
|
+
while (idx < lines.length && lines[idx]!.indent >= mapIndent) {
|
|
259
|
+
const line = lines[idx]!;
|
|
260
|
+
if (line.indent > mapIndent) {
|
|
261
|
+
idx++;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (line.text.startsWith("-")) break;
|
|
265
|
+
const colonIdx = findKeyValueColon(line.text);
|
|
266
|
+
if (colonIdx === -1) {
|
|
267
|
+
idx++;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
const key = unquoteScalar(line.text.slice(0, colonIdx));
|
|
271
|
+
const valRaw = line.text.slice(colonIdx + 1).trim();
|
|
272
|
+
idx++;
|
|
273
|
+
if (valRaw === "|" || valRaw === ">") {
|
|
274
|
+
const folded = valRaw === ">";
|
|
275
|
+
const blockLines: string[] = [];
|
|
276
|
+
const contentIndent =
|
|
277
|
+
idx < lines.length && lines[idx]!.indent > mapIndent
|
|
278
|
+
? lines[idx]!.indent
|
|
279
|
+
: mapIndent + 2;
|
|
280
|
+
while (idx < lines.length && lines[idx]!.indent >= contentIndent) {
|
|
281
|
+
blockLines.push(lines[idx]!.text);
|
|
282
|
+
idx++;
|
|
283
|
+
}
|
|
284
|
+
obj[key] = blockLines.join(folded ? " " : "\n");
|
|
285
|
+
} else if (valRaw !== "") {
|
|
286
|
+
obj[key] = parseScalarValue(valRaw);
|
|
287
|
+
} else if (idx < lines.length && lines[idx]!.indent > mapIndent) {
|
|
288
|
+
obj[key] = parseBlock(lines[idx]!.indent);
|
|
289
|
+
} else {
|
|
290
|
+
obj[key] = null;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return obj;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return parseBlock(lines[0]!.indent);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function findKeyValueColon(text: string): number {
|
|
300
|
+
let quote: '"' | "'" | null = null;
|
|
301
|
+
for (let i = 0; i < text.length; i++) {
|
|
302
|
+
const ch = text[i]!;
|
|
303
|
+
if (quote) {
|
|
304
|
+
if (ch === "\\" && quote === '"') {
|
|
305
|
+
i++;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (ch === quote) quote = null;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (ch === '"' || ch === "'") {
|
|
312
|
+
quote = ch;
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
if (ch === ":" && (i + 1 === text.length || /\s/.test(text[i + 1]!))) {
|
|
316
|
+
return i;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return -1;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Lightweight host-free TOML parser supporting key-value pairs, `[table]` sections, and `[[array]]` tables. */
|
|
323
|
+
export function parseSimpleToml(raw: string): Record<string, any> {
|
|
324
|
+
const root: Record<string, any> = {};
|
|
325
|
+
let currentTarget: Record<string, any> = root;
|
|
326
|
+
const lines = raw.split(/\r?\n/);
|
|
327
|
+
|
|
328
|
+
for (const rawLine of lines) {
|
|
329
|
+
const line = stripInlineComment(rawLine).trim();
|
|
330
|
+
if (!line) continue;
|
|
331
|
+
|
|
332
|
+
if (line.startsWith("[[") && line.endsWith("]]")) {
|
|
333
|
+
const path = line
|
|
334
|
+
.slice(2, -2)
|
|
335
|
+
.trim()
|
|
336
|
+
.split(".")
|
|
337
|
+
.map((part) => unquoteScalar(part));
|
|
338
|
+
let cursor = root;
|
|
339
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
340
|
+
const segment = path[i]!;
|
|
341
|
+
if (typeof cursor[segment] !== "object" || cursor[segment] === null) {
|
|
342
|
+
cursor[segment] = {};
|
|
343
|
+
}
|
|
344
|
+
cursor = cursor[segment];
|
|
345
|
+
}
|
|
346
|
+
const last = path[path.length - 1]!;
|
|
347
|
+
if (!Array.isArray(cursor[last])) {
|
|
348
|
+
cursor[last] = [];
|
|
349
|
+
}
|
|
350
|
+
const item: Record<string, any> = {};
|
|
351
|
+
cursor[last].push(item);
|
|
352
|
+
currentTarget = item;
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (line.startsWith("[") && line.endsWith("]")) {
|
|
357
|
+
const path = line
|
|
358
|
+
.slice(1, -1)
|
|
359
|
+
.trim()
|
|
360
|
+
.split(".")
|
|
361
|
+
.map((part) => unquoteScalar(part));
|
|
362
|
+
let cursor = root;
|
|
363
|
+
for (const segment of path) {
|
|
364
|
+
if (typeof cursor[segment] !== "object" || cursor[segment] === null) {
|
|
365
|
+
cursor[segment] = {};
|
|
366
|
+
}
|
|
367
|
+
cursor = cursor[segment];
|
|
368
|
+
}
|
|
369
|
+
currentTarget = cursor;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const eqIdx = line.indexOf("=");
|
|
374
|
+
if (eqIdx === -1) continue;
|
|
375
|
+
const key = unquoteScalar(line.slice(0, eqIdx));
|
|
376
|
+
const valRaw = line.slice(eqIdx + 1).trim();
|
|
377
|
+
if (key) {
|
|
378
|
+
currentTarget[key] = parseScalarValue(valRaw);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return root;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Lightweight host-free CSV parser returning an array of header-keyed objects. */
|
|
386
|
+
export function parseSimpleCsv(raw: string): Array<Record<string, string>> {
|
|
387
|
+
const lines = raw
|
|
388
|
+
.split(/\r?\n/)
|
|
389
|
+
.map((l) => l.trim())
|
|
390
|
+
.filter(Boolean);
|
|
391
|
+
if (lines.length === 0) return [];
|
|
392
|
+
|
|
393
|
+
function parseRow(line: string): string[] {
|
|
394
|
+
const cells: string[] = [];
|
|
395
|
+
let current = "";
|
|
396
|
+
let inQuotes = false;
|
|
397
|
+
for (let i = 0; i < line.length; i++) {
|
|
398
|
+
const ch = line[i]!;
|
|
399
|
+
if (inQuotes) {
|
|
400
|
+
if (ch === '"') {
|
|
401
|
+
if (line[i + 1] === '"') {
|
|
402
|
+
current += '"';
|
|
403
|
+
i++;
|
|
404
|
+
} else {
|
|
405
|
+
inQuotes = false;
|
|
406
|
+
}
|
|
407
|
+
} else {
|
|
408
|
+
current += ch;
|
|
409
|
+
}
|
|
410
|
+
} else if (ch === '"') {
|
|
411
|
+
inQuotes = true;
|
|
412
|
+
} else if (ch === ",") {
|
|
413
|
+
cells.push(current.trim());
|
|
414
|
+
current = "";
|
|
415
|
+
} else {
|
|
416
|
+
current += ch;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
cells.push(current.trim());
|
|
420
|
+
return cells;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const headers = parseRow(lines[0]!);
|
|
424
|
+
const rows: Array<Record<string, string>> = [];
|
|
425
|
+
for (let i = 1; i < lines.length; i++) {
|
|
426
|
+
const values = parseRow(lines[i]!);
|
|
427
|
+
const record: Record<string, string> = {};
|
|
428
|
+
for (let j = 0; j < headers.length; j++) {
|
|
429
|
+
const header = headers[j]!;
|
|
430
|
+
if (header) {
|
|
431
|
+
record[header] = values[j] ?? "";
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
rows.push(record);
|
|
435
|
+
}
|
|
436
|
+
return rows;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** Parses YAML (`---`) or TOML (`+++`) frontmatter and markdown body from a raw document. */
|
|
440
|
+
export function parseFrontmatter(raw: string): FrontmatterParseResult {
|
|
441
|
+
const normalized = raw.replace(/^\uFEFF/, "");
|
|
442
|
+
const yamlMatch = /^---[ \t]*\r?\n([\s\S]*?\r?\n)?---[ \t]*(?:\r?\n|$)/.exec(
|
|
443
|
+
normalized,
|
|
444
|
+
);
|
|
445
|
+
if (yamlMatch) {
|
|
446
|
+
const rawFrontmatter = (yamlMatch[1] ?? "").trimEnd();
|
|
447
|
+
const body = normalized.slice(yamlMatch[0].length);
|
|
448
|
+
const parsed = rawFrontmatter ? parseSimpleYaml(rawFrontmatter) : {};
|
|
449
|
+
return {
|
|
450
|
+
data:
|
|
451
|
+
typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)
|
|
452
|
+
? parsed
|
|
453
|
+
: {},
|
|
454
|
+
body,
|
|
455
|
+
rawFrontmatter,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const tomlMatch =
|
|
460
|
+
/^\+\+\+[ \t]*\r?\n([\s\S]*?\r?\n)?\+\+\+[ \t]*(?:\r?\n|$)/.exec(
|
|
461
|
+
normalized,
|
|
462
|
+
);
|
|
463
|
+
if (tomlMatch) {
|
|
464
|
+
const rawFrontmatter = (tomlMatch[1] ?? "").trimEnd();
|
|
465
|
+
const body = normalized.slice(tomlMatch[0].length);
|
|
466
|
+
const parsed = rawFrontmatter ? parseSimpleToml(rawFrontmatter) : {};
|
|
467
|
+
return {
|
|
468
|
+
data: parsed,
|
|
469
|
+
body,
|
|
470
|
+
rawFrontmatter,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return {
|
|
475
|
+
data: {},
|
|
476
|
+
body: normalized,
|
|
477
|
+
rawFrontmatter: "",
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function globToRegExp(globPattern: string): RegExp {
|
|
482
|
+
const normalized = globPattern.replace(/\\/g, "/").replace(/^\.\//, "");
|
|
483
|
+
let regexStr = "^";
|
|
484
|
+
let i = 0;
|
|
485
|
+
|
|
486
|
+
while (i < normalized.length) {
|
|
487
|
+
const ch = normalized[i]!;
|
|
488
|
+
if (ch === "*") {
|
|
489
|
+
if (normalized[i + 1] === "*") {
|
|
490
|
+
if (normalized[i + 2] === "/") {
|
|
491
|
+
regexStr += "(?:.*/)?";
|
|
492
|
+
i += 3;
|
|
493
|
+
} else {
|
|
494
|
+
regexStr += ".*";
|
|
495
|
+
i += 2;
|
|
496
|
+
}
|
|
497
|
+
} else {
|
|
498
|
+
regexStr += "[^/]*";
|
|
499
|
+
i++;
|
|
500
|
+
}
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
if (ch === "?") {
|
|
504
|
+
regexStr += "[^/]";
|
|
505
|
+
i++;
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
if (ch === "{") {
|
|
509
|
+
const closeIdx = normalized.indexOf("}", i + 1);
|
|
510
|
+
if (closeIdx !== -1) {
|
|
511
|
+
const alternatives = normalized
|
|
512
|
+
.slice(i + 1, closeIdx)
|
|
513
|
+
.split(",")
|
|
514
|
+
.map((alt) =>
|
|
515
|
+
alt.replace(/[.+^$|()[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*"),
|
|
516
|
+
);
|
|
517
|
+
regexStr += `(?:${alternatives.join("|")})`;
|
|
518
|
+
i = closeIdx + 1;
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (ch === "[") {
|
|
523
|
+
const closeIdx = normalized.indexOf("]", i + 1);
|
|
524
|
+
if (closeIdx !== -1) {
|
|
525
|
+
regexStr += normalized.slice(i, closeIdx + 1);
|
|
526
|
+
i = closeIdx + 1;
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (/[.+^$|()\\]/.test(ch)) {
|
|
531
|
+
regexStr += `\\${ch}`;
|
|
532
|
+
} else {
|
|
533
|
+
regexStr += ch;
|
|
534
|
+
}
|
|
535
|
+
i++;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
regexStr += "$";
|
|
539
|
+
return new RegExp(regexStr);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** Matches a POSIX-normalized relative path against one or more glob patterns (including `!` negations). */
|
|
543
|
+
export function matchGlobPattern(
|
|
544
|
+
entryPath: string,
|
|
545
|
+
pattern: string | readonly string[],
|
|
546
|
+
): boolean {
|
|
547
|
+
const normalizedPath = entryPath.replace(/\\/g, "/").replace(/^\.\//, "");
|
|
548
|
+
const patterns = typeof pattern === "string" ? [pattern] : pattern;
|
|
549
|
+
let matched = false;
|
|
550
|
+
let hasPositive = false;
|
|
551
|
+
|
|
552
|
+
for (const rawPat of patterns) {
|
|
553
|
+
const trimmed = rawPat.trim();
|
|
554
|
+
if (!trimmed) continue;
|
|
555
|
+
if (trimmed.startsWith("!")) {
|
|
556
|
+
const negRegex = globToRegExp(trimmed.slice(1));
|
|
557
|
+
if (negRegex.test(normalizedPath)) {
|
|
558
|
+
matched = false;
|
|
559
|
+
}
|
|
560
|
+
} else {
|
|
561
|
+
hasPositive = true;
|
|
562
|
+
const posRegex = globToRegExp(trimmed);
|
|
563
|
+
if (posRegex.test(normalizedPath)) {
|
|
564
|
+
matched = true;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
return hasPositive ? matched : false;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
function defaultGenerateId({ entry }: GenerateIdOptions): string {
|
|
573
|
+
return entry
|
|
574
|
+
.replace(/\\/g, "/")
|
|
575
|
+
.replace(/^\.\//, "")
|
|
576
|
+
.replace(/\.(md|mdx|markdown|json|yaml|yml|toml|csv)$/i, "");
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function resolveBasePathAndUrl(
|
|
580
|
+
base: string | URL | undefined,
|
|
581
|
+
pathMod: { resolve: (...paths: string[]) => string; sep: string },
|
|
582
|
+
urlMod: {
|
|
583
|
+
fileURLToPath: (url: URL | string) => string;
|
|
584
|
+
pathToFileURL: (p: string) => URL;
|
|
585
|
+
},
|
|
586
|
+
): { baseDir: string; baseUrl: URL } {
|
|
587
|
+
if (base instanceof URL) {
|
|
588
|
+
const baseDir =
|
|
589
|
+
base.protocol === "file:"
|
|
590
|
+
? urlMod.fileURLToPath(base)
|
|
591
|
+
: pathMod.resolve(base.pathname);
|
|
592
|
+
const baseUrl = urlMod.pathToFileURL(
|
|
593
|
+
baseDir.endsWith(pathMod.sep) ? baseDir : baseDir + pathMod.sep,
|
|
594
|
+
);
|
|
595
|
+
return { baseDir, baseUrl };
|
|
596
|
+
}
|
|
597
|
+
if (typeof base === "string" && base.startsWith("file://")) {
|
|
598
|
+
const baseDir = urlMod.fileURLToPath(base);
|
|
599
|
+
const baseUrl = urlMod.pathToFileURL(
|
|
600
|
+
baseDir.endsWith(pathMod.sep) ? baseDir : baseDir + pathMod.sep,
|
|
601
|
+
);
|
|
602
|
+
return { baseDir, baseUrl };
|
|
603
|
+
}
|
|
604
|
+
const baseDir = pathMod.resolve(base ?? ".");
|
|
605
|
+
const baseUrl = urlMod.pathToFileURL(
|
|
606
|
+
baseDir.endsWith(pathMod.sep) ? baseDir : baseDir + pathMod.sep,
|
|
607
|
+
);
|
|
608
|
+
return { baseDir, baseUrl };
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
async function walkFilesRecursively(
|
|
612
|
+
dir: string,
|
|
613
|
+
fsMod: {
|
|
614
|
+
readdir: (
|
|
615
|
+
p: string,
|
|
616
|
+
opts: { withFileTypes: true },
|
|
617
|
+
) => Promise<
|
|
618
|
+
Array<{ name: string; isDirectory: () => boolean; isFile: () => boolean }>
|
|
619
|
+
>;
|
|
620
|
+
},
|
|
621
|
+
pathMod: { join: (...paths: string[]) => string },
|
|
622
|
+
prefix = "",
|
|
623
|
+
): Promise<Array<{ relPath: string; fullPath: string }>> {
|
|
624
|
+
let entries: Array<{
|
|
625
|
+
name: string;
|
|
626
|
+
isDirectory: () => boolean;
|
|
627
|
+
isFile: () => boolean;
|
|
628
|
+
}>;
|
|
629
|
+
try {
|
|
630
|
+
entries = await fsMod.readdir(dir, { withFileTypes: true });
|
|
631
|
+
} catch {
|
|
632
|
+
return [];
|
|
633
|
+
}
|
|
634
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
635
|
+
const results: Array<{ relPath: string; fullPath: string }> = [];
|
|
636
|
+
for (const dirent of entries) {
|
|
637
|
+
if (dirent.name === "node_modules" || dirent.name === ".git") continue;
|
|
638
|
+
const relPath = prefix ? `${prefix}/${dirent.name}` : dirent.name;
|
|
639
|
+
const fullPath = pathMod.join(dir, dirent.name);
|
|
640
|
+
if (dirent.isDirectory()) {
|
|
641
|
+
const nested = await walkFilesRecursively(
|
|
642
|
+
fullPath,
|
|
643
|
+
fsMod,
|
|
644
|
+
pathMod,
|
|
645
|
+
relPath,
|
|
646
|
+
);
|
|
647
|
+
results.push(...nested);
|
|
648
|
+
} else if (dirent.isFile()) {
|
|
649
|
+
results.push({ relPath, fullPath });
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return results;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Built-in Astro Content Layer `glob()` loader (`astro/loaders`).
|
|
657
|
+
* Discovers files matching `pattern` under `base`, parses Markdown/MDX/JSON/YAML/TOML,
|
|
658
|
+
* validates against the collection schema, and populates the Content Layer store.
|
|
659
|
+
*/
|
|
660
|
+
export function glob(options: GlobLoaderOptions): ContentLoader {
|
|
661
|
+
return {
|
|
662
|
+
name: "glob-loader",
|
|
663
|
+
async load(context: LoaderContext): Promise<void> {
|
|
664
|
+
const [fsMod, pathMod, urlMod] = await Promise.all([
|
|
665
|
+
importBuiltin<{
|
|
666
|
+
readdir: any;
|
|
667
|
+
readFile: (p: string, enc: string) => Promise<string>;
|
|
668
|
+
}>("node:fs/promises"),
|
|
669
|
+
importBuiltin<{
|
|
670
|
+
resolve: (...paths: string[]) => string;
|
|
671
|
+
join: (...paths: string[]) => string;
|
|
672
|
+
extname: (p: string) => string;
|
|
673
|
+
sep: string;
|
|
674
|
+
}>("node:path"),
|
|
675
|
+
importBuiltin<{
|
|
676
|
+
fileURLToPath: (u: URL | string) => string;
|
|
677
|
+
pathToFileURL: (p: string) => URL;
|
|
678
|
+
}>("node:url"),
|
|
679
|
+
]);
|
|
680
|
+
|
|
681
|
+
const { baseDir, baseUrl } = resolveBasePathAndUrl(
|
|
682
|
+
options.base,
|
|
683
|
+
pathMod,
|
|
684
|
+
urlMod,
|
|
685
|
+
);
|
|
686
|
+
const files = await walkFilesRecursively(baseDir, fsMod, pathMod);
|
|
687
|
+
const seenIds = new Set<string>();
|
|
688
|
+
|
|
689
|
+
for (const { relPath, fullPath } of files) {
|
|
690
|
+
if (!matchGlobPattern(relPath, options.pattern)) continue;
|
|
691
|
+
const rawText = await fsMod.readFile(fullPath, "utf8");
|
|
692
|
+
const digest = context.generateDigest(rawText);
|
|
693
|
+
const ext = pathMod.extname(relPath).toLowerCase();
|
|
694
|
+
|
|
695
|
+
let rawData: Record<string, any> = {};
|
|
696
|
+
let body: string | undefined;
|
|
697
|
+
let rendered: RenderedContent | undefined;
|
|
698
|
+
|
|
699
|
+
if (options.parser) {
|
|
700
|
+
rawData = await options.parser(rawText, fullPath);
|
|
701
|
+
} else if (ext === ".md" || ext === ".mdx" || ext === ".markdown") {
|
|
702
|
+
const parsed = parseFrontmatter(rawText);
|
|
703
|
+
rawData = parsed.data;
|
|
704
|
+
body = parsed.body;
|
|
705
|
+
const headings = extractMarkdownHeadings(body);
|
|
706
|
+
rendered = {
|
|
707
|
+
html: body.trim() ? renderSimpleMarkdownToHtml(body) : "",
|
|
708
|
+
metadata: {
|
|
709
|
+
headings,
|
|
710
|
+
frontmatter: { ...rawData },
|
|
711
|
+
},
|
|
712
|
+
};
|
|
713
|
+
} else if (ext === ".json") {
|
|
714
|
+
const parsed = JSON.parse(rawText);
|
|
715
|
+
rawData =
|
|
716
|
+
typeof parsed === "object" && parsed !== null
|
|
717
|
+
? parsed
|
|
718
|
+
: { value: parsed };
|
|
719
|
+
} else if (ext === ".yaml" || ext === ".yml") {
|
|
720
|
+
const parsed = parseSimpleYaml(rawText);
|
|
721
|
+
rawData =
|
|
722
|
+
typeof parsed === "object" &&
|
|
723
|
+
parsed !== null &&
|
|
724
|
+
!Array.isArray(parsed)
|
|
725
|
+
? parsed
|
|
726
|
+
: { value: parsed };
|
|
727
|
+
} else if (ext === ".toml") {
|
|
728
|
+
rawData = parseSimpleToml(rawText);
|
|
729
|
+
} else {
|
|
730
|
+
const parsed = parseFrontmatter(rawText);
|
|
731
|
+
rawData = parsed.data;
|
|
732
|
+
body = parsed.body;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
const generateId = options.generateId ?? defaultGenerateId;
|
|
736
|
+
const id = generateId({
|
|
737
|
+
entry: relPath,
|
|
738
|
+
base: baseUrl,
|
|
739
|
+
data: rawData,
|
|
740
|
+
});
|
|
741
|
+
seenIds.add(id);
|
|
742
|
+
|
|
743
|
+
const existing = context.store.get(id);
|
|
744
|
+
if (existing && existing.digest === digest) {
|
|
745
|
+
continue;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
const validatedData = await context.parseData({
|
|
749
|
+
id,
|
|
750
|
+
data: rawData,
|
|
751
|
+
filePath: fullPath,
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
context.store.set({
|
|
755
|
+
id,
|
|
756
|
+
data: validatedData,
|
|
757
|
+
...(body !== undefined ? { body } : {}),
|
|
758
|
+
filePath: fullPath,
|
|
759
|
+
digest,
|
|
760
|
+
...(rendered !== undefined ? { rendered } : {}),
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
for (const existingId of context.store.keys()) {
|
|
765
|
+
if (!seenIds.has(existingId)) {
|
|
766
|
+
context.store.delete(existingId);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
},
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* Built-in Astro Content Layer `file()` loader (`astro/loaders`).
|
|
775
|
+
* Loads multiple collection entries from a single JSON, YAML, TOML, or CSV file
|
|
776
|
+
* (or via a custom `parser` function).
|
|
777
|
+
*/
|
|
778
|
+
export function file(
|
|
779
|
+
filePath: string | URL,
|
|
780
|
+
options?: FileLoaderOptions,
|
|
781
|
+
): ContentLoader {
|
|
782
|
+
return {
|
|
783
|
+
name: "file-loader",
|
|
784
|
+
async load(context: LoaderContext): Promise<void> {
|
|
785
|
+
const [fsMod, pathMod, urlMod] = await Promise.all([
|
|
786
|
+
importBuiltin<{
|
|
787
|
+
readFile: (p: string, enc: string) => Promise<string>;
|
|
788
|
+
}>("node:fs/promises"),
|
|
789
|
+
importBuiltin<{
|
|
790
|
+
resolve: (...paths: string[]) => string;
|
|
791
|
+
extname: (p: string) => string;
|
|
792
|
+
}>("node:path"),
|
|
793
|
+
importBuiltin<{
|
|
794
|
+
fileURLToPath: (u: URL | string) => string;
|
|
795
|
+
}>("node:url"),
|
|
796
|
+
]);
|
|
797
|
+
|
|
798
|
+
const resolvedPath =
|
|
799
|
+
filePath instanceof URL
|
|
800
|
+
? filePath.protocol === "file:"
|
|
801
|
+
? urlMod.fileURLToPath(filePath)
|
|
802
|
+
: pathMod.resolve(filePath.pathname)
|
|
803
|
+
: typeof filePath === "string" && filePath.startsWith("file://")
|
|
804
|
+
? urlMod.fileURLToPath(filePath)
|
|
805
|
+
: pathMod.resolve(filePath);
|
|
806
|
+
|
|
807
|
+
const rawText = await fsMod.readFile(resolvedPath, "utf8");
|
|
808
|
+
const ext = pathMod.extname(resolvedPath).toLowerCase();
|
|
809
|
+
|
|
810
|
+
let parsedEntries:
|
|
811
|
+
Record<string, Record<string, any>> | Array<Record<string, any>>;
|
|
812
|
+
|
|
813
|
+
if (options?.parser) {
|
|
814
|
+
parsedEntries = await options.parser(rawText);
|
|
815
|
+
} else if (ext === ".json") {
|
|
816
|
+
parsedEntries = JSON.parse(rawText);
|
|
817
|
+
} else if (ext === ".yaml" || ext === ".yml") {
|
|
818
|
+
parsedEntries = parseSimpleYaml(rawText);
|
|
819
|
+
} else if (ext === ".toml") {
|
|
820
|
+
parsedEntries = parseSimpleToml(rawText);
|
|
821
|
+
} else if (ext === ".csv") {
|
|
822
|
+
parsedEntries = parseSimpleCsv(rawText);
|
|
823
|
+
} else {
|
|
824
|
+
parsedEntries = JSON.parse(rawText);
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
context.store.clear();
|
|
828
|
+
|
|
829
|
+
if (Array.isArray(parsedEntries)) {
|
|
830
|
+
for (const item of parsedEntries) {
|
|
831
|
+
if (typeof item !== "object" || item === null) continue;
|
|
832
|
+
const rawId = item.id ?? item.slug;
|
|
833
|
+
if (rawId === undefined || rawId === null || String(rawId) === "") {
|
|
834
|
+
throw new Error(
|
|
835
|
+
`[${context.collection}] file() loader array entry is missing an "id" or "slug" property in ${resolvedPath}`,
|
|
836
|
+
);
|
|
837
|
+
}
|
|
838
|
+
const id = String(rawId);
|
|
839
|
+
const validatedData = await context.parseData({
|
|
840
|
+
id,
|
|
841
|
+
data: item,
|
|
842
|
+
filePath: resolvedPath,
|
|
843
|
+
});
|
|
844
|
+
context.store.set({
|
|
845
|
+
id,
|
|
846
|
+
data: validatedData,
|
|
847
|
+
filePath: resolvedPath,
|
|
848
|
+
digest: context.generateDigest(item),
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
} else if (typeof parsedEntries === "object" && parsedEntries !== null) {
|
|
852
|
+
for (const [id, rawItem] of Object.entries(parsedEntries)) {
|
|
853
|
+
if (typeof rawItem !== "object" || rawItem === null) continue;
|
|
854
|
+
if (Array.isArray(rawItem)) {
|
|
855
|
+
for (const subItem of rawItem) {
|
|
856
|
+
if (typeof subItem !== "object" || subItem === null) continue;
|
|
857
|
+
const subId = String(subItem.id ?? subItem.slug ?? "");
|
|
858
|
+
if (!subId) continue;
|
|
859
|
+
const validatedData = await context.parseData({
|
|
860
|
+
id: subId,
|
|
861
|
+
data: subItem,
|
|
862
|
+
filePath: resolvedPath,
|
|
863
|
+
});
|
|
864
|
+
context.store.set({
|
|
865
|
+
id: subId,
|
|
866
|
+
data: validatedData,
|
|
867
|
+
filePath: resolvedPath,
|
|
868
|
+
digest: context.generateDigest(subItem),
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
continue;
|
|
872
|
+
}
|
|
873
|
+
const validatedData = await context.parseData({
|
|
874
|
+
id,
|
|
875
|
+
data: rawItem,
|
|
876
|
+
filePath: resolvedPath,
|
|
877
|
+
});
|
|
878
|
+
context.store.set({
|
|
879
|
+
id,
|
|
880
|
+
data: validatedData,
|
|
881
|
+
filePath: resolvedPath,
|
|
882
|
+
digest: context.generateDigest(rawItem),
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
},
|
|
887
|
+
};
|
|
888
|
+
}
|