deno.land / x / lume@v2.1.4 / core / source.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import { posix } from "../deps/path.ts";import { normalizePath } from "./utils/path.ts";import { mergeData } from "./utils/merge_data.ts";import { parseDateFromFilename } from "./utils/date.ts";import { getPageUrl } from "./utils/page_url.ts";import { getPageDate } from "./utils/page_date.ts";import { Page, StaticFile } from "./file.ts";
import type { Data, RawData } from "./file.ts";import type { default as FS, Entry } from "./fs.ts";import type Formats from "./formats.ts";import type DataLoader from "./data_loader.ts";import type { ScopeFilter } from "./scopes.ts";import type { Components, default as ComponentLoader,} from "./component_loader.ts";
export interface Options { formats: Formats; dataLoader: DataLoader; componentLoader: ComponentLoader; scopedData: Map<string, RawData>; scopedPages: Map<string, RawData[]>; scopedComponents: Map<string, Components>; fs: FS; prettyUrls: boolean; components: { variable: string; cssFile: string; jsFile: string; };}
/** * Scan and load files from the source folder * with the data, pages, assets and static files */export default class Source { /** Filesystem reader to scan folders */ fs: FS;
/** To load all _data files */ dataLoader: DataLoader;
/** To load all components */ componentLoader: ComponentLoader;
/** Info about how to handle different file formats */ formats: Formats;
/** The list of paths to ignore */ ignored = new Set<string>();
/** The path filters to ignore */ filters: ScopeFilter[] = [];
/** The data assigned per path */ scopedData: Map<string, RawData>;
/** The pages assigned per path */ scopedPages: Map<string, RawData[]>;
/** The components assigned per path */ scopedComponents: Map<string, Components>;
/** Use pretty URLs */ prettyUrls: boolean;
/** List of static files and folders to copy */ staticPaths = new Map< string, { dest: string | ((path: string) => string) | undefined; dirOnly: boolean } >();
/** List of static files and folders to copy */ copyRemainingFiles?: (path: string) => string | boolean;
/** Extra code generated by the components */ extraCode = new Map<string, Map<string, string>>();
components: { /** File name used to output the extra CSS code generated by the components */ cssFile: string;
/** File name used to output the extra JavaScript code generated by the components */ jsFile: string;
/** Variable name used to access to the components */ variable: string; };
/** The data assigned per path */ data = new Map<string, Partial<Data>>();
constructor(options: Options) { this.dataLoader = options.dataLoader; this.componentLoader = options.componentLoader; this.fs = options.fs; this.formats = options.formats; this.components = options.components; this.scopedData = options.scopedData; this.scopedPages = options.scopedPages; this.scopedComponents = options.scopedComponents; this.prettyUrls = options.prettyUrls; }
addIgnoredPath(path: string) { this.ignored.add(normalizePath(path)); }
addIgnoreFilter(filter: ScopeFilter) { this.filters.push(filter); }
addStaticPath(from: string, to?: string | ((path: string) => string)) { this.staticPaths.set( normalizePath(from.replace(/\/$/, "")), { dest: typeof to === "string" ? normalizePath(to) : to, dirOnly: from.endsWith("/"), }, ); }
async build(...buildFilters: BuildFilter[]): Promise<[Page[], StaticFile[]]> { const pages: Page[] = []; const staticFiles: StaticFile[] = [];
await this.#build( buildFilters, this.fs.entries.get("/")!, "/", new Map(), {}, pages, staticFiles, );
return [ pages, staticFiles, ]; }
async #build( buildFilters: BuildFilter[], dir: Entry, path: string, parentComponents: Components, parentData: RawData, pages: Page[], staticFiles: StaticFile[], ) { if (buildFilters.some((filter) => !filter(dir))) { return; }
// Parse the date/time in the folder name const [basename, date] = parseDateFromFilename(dir.name);
// Load the _data files const dirDatas: RawData[] = [];
for (const entry of dir.children.values()) { if ( (entry.type === "file" && entry.name.startsWith("_data.")) || (entry.type === "directory" && entry.name === "_data") ) { const loaded = await this.dataLoader.load(entry); if (loaded) { dirDatas.push(loaded); } } }
if (date) { dirDatas.push({ date }); }
// Merge directory data const dirData = mergeData( parentData, { basename }, this.scopedData.get(dir.path) || {}, ...dirDatas, ) as Partial<Data>;
path = posix.join(path, dirData.basename!);
// Directory components const scopedComponents = this.scopedComponents.get(dir.path); let loadedComponents: Components | undefined;
// Load _components files for (const entry of dir.children.values()) { if (entry.type === "directory" && entry.name === "_components") { loadedComponents = await this.componentLoader.load(entry, dirData); break; } }
// Merge the components if (scopedComponents || loadedComponents) { parentComponents = mergeComponents( parentComponents, scopedComponents || new Map(), loadedComponents || new Map(), );
dirData[this.components.variable] = toProxy( parentComponents, this.extraCode, ); }
// Store the root data to be used by other plugins this.data.set(path, dirData);
// Load the pages assigned to the current path if (this.scopedPages.has(dir.path)) { for (const data of this.scopedPages.get(dir.path)!) { const basename = posix.basename(data.url as string).replace( /\.[\w.]+$/, "", ); const page = new Page(); page.data = mergeData( dirData, { basename, date: new Date() }, data, ) as Data;
const url = getPageUrl(page, this.prettyUrls, path); if (!url) { continue; } page.data.url = url; page.data.date = getPageDate(page); page.data.page = page; pages.push(page); } }
// Load the pages and static files for (const entry of dir.children.values()) { if (buildFilters.some((filter) => !filter(entry))) { continue; }
// Static files if (this.staticPaths.has(entry.path)) { const { dest, dirOnly } = this.staticPaths.get(entry.path)!;
staticFiles.push(...this.#getStaticFiles(path, entry, dest, dirOnly)); continue; }
// Check if the entry should be ignored if ( (entry.name.startsWith(".") && !isWellKnownDir(entry)) || entry.name.startsWith("_") || this.ignored.has(entry.path) ) { for (const [staticSrc, { dest, dirOnly }] of this.staticPaths) { if (staticSrc.startsWith(entry.path)) { const staticEntry = this.fs.entries.get(staticSrc)!; const staticPath = posix.dirname( posix.join(path, staticEntry.path.slice(entry.path.length)), ); staticFiles.push( ...this.#getStaticFiles(staticPath, staticEntry, dest, dirOnly), ); } } continue; }
if (this.filters.some((filter) => filter(entry.path))) { continue; }
if (entry.type === "file") { const format = this.formats.search(entry.path);
// Unknown file format if (!format) { // Remaining files if (this.copyRemainingFiles) { const dest = this.copyRemainingFiles(entry.path);
if (dest) { staticFiles.push( ...this.#getStaticFiles( path, entry, typeof dest === "string" ? dest : undefined, ), ); } } continue; }
// The file is a static file if (format.copy) { staticFiles.push( ...this.#getStaticFiles( path, entry, typeof format.copy === "function" ? format.copy : undefined, ), ); continue; }
// The file is a page if (format.pageType) { const loader = format.pageType === "asset" ? format.assetLoader : format.loader;
if (!loader) { throw new Error( `Missing loader for ${format.pageType} page type (${entry.path}))`, ); }
const { ext } = format; const [basename, date] = parseDateFromFilename(entry.name);
// Create the page const page = new Page({ path: entry.path.slice(0, -ext.length), ext, asset: format.pageType === "asset", entry, });
// Load and merge the page data const pageData = await entry.getContent(loader); page.data = mergeData( dirData, { basename: basename.slice(0, -ext.length) }, date ? { date } : {}, this.scopedData.get(entry.path) || {}, pageData, ) as Data;
const url = getPageUrl(page, this.prettyUrls, path); if (!url) { continue; } page.data.url = url; page.data.date = getPageDate(page); page.data.page = page; page._data.layout = pageData.layout;
if (buildFilters.some((filter) => !filter(entry, page))) { continue; }
pages.push(page); continue; } }
// Load recursively the directory if (entry.type === "directory") { await this.#build( buildFilters, entry, path, parentComponents, dirData, pages, staticFiles, ); } }
return [pages, staticFiles]; }
/** Returns the pages with extra code generated by the components */ getComponentsExtraCode(): Page[] { const files = { css: this.components.cssFile, js: this.components.jsFile, }; const pages: Page[] = [];
for (const [type, path] of Object.entries(files)) { const code = this.extraCode.get(type);
if (code && code.size) { pages.push( Page.create({ url: path, content: Array.from(code.values()).join("\n"), }), ); } }
return pages; }
/** Scan the static files in a directory */ *#scanStaticFiles( dirEntry: Entry, destPath: string, destFn?: (file: string) => string, ): Generator<StaticFile> { for (const entry of dirEntry.children.values()) { if (entry.type === "file") { if (entry.name.startsWith(".") || entry.name.startsWith("_")) { continue; }
// Check if the file should be ignored if (this.ignored.has(entry.path)) { continue; }
if (this.filters.some((filter) => filter(entry.path))) { continue; }
const outputPath = getOutputPath(entry, destPath, destFn); yield { entry, outputPath }; }
if (entry.type === "directory") { yield* this.#scanStaticFiles( entry, posix.join(destPath, entry.name), destFn, ); } } }
#getStaticFiles( path: string, entry: Entry, dest: string | ((path: string) => string) | undefined, dirOnly = false, ): StaticFile[] { if (entry.type === "file") { if (!dirOnly) { return [{ entry, outputPath: getOutputPath(entry, path, dest), }]; } return []; }
return Array.from(this.#scanStaticFiles( entry, typeof dest === "string" ? dest : posix.join(path, entry.name), typeof dest === "function" ? dest : undefined, )); }}
/** * Create and returns a proxy to use the components * as comp.name() instead of components.get("name").render() */function toProxy( components: Components, extraCode?: Map<string, Map<string, string>>,): ProxyComponents { const node = { _components: components, _proxies: new Map(), }; return new Proxy(node, { get: (target, name) => { if (typeof name !== "string" || name in target) { return; }
const key = name.toLowerCase();
if (target._proxies.has(key)) { return target._proxies.get(key); }
const component = target._components.get(key);
if (!component) { throw new Error(`Component "${name}" not found`); }
if (component instanceof Map) { const proxy = toProxy(component, extraCode); target._proxies.set(key, proxy); return proxy; }
// Save CSS & JS code for the component if (extraCode) { if (component.css) { const code = extraCode.get("css") ?? new Map(); code.set(key, component.css); extraCode.set("css", code); }
if (component.js) { const code = extraCode.get("js") ?? new Map(); code.set(key, component.js); extraCode.set("js", code); } }
// Return the function to render the component return (props: Record<string, unknown>) => component.render(props); }, }) as unknown as ProxyComponents;}
export type BuildFilter = (entry: Entry, page?: Page) => boolean;
export interface ProxyComponents { // deno-lint-ignore no-explicit-any (props?: Record<string, unknown>): any; [key: string]: ProxyComponents;}
/** Merge the cascade components */function mergeComponents(...components: Components[]): Components { return components.reduce((previous, current) => { const components = new Map(previous);
for (const [key, value] of current) { if (components.has(key)) { const previousValue = components.get(key);
if (previousValue instanceof Map && value instanceof Map) { components.set(key, mergeComponents(value, previousValue)); } else { components.set(key, value); } } else { components.set(key, value); } } return components; });}
function getOutputPath( entry: Entry, path: string, dest?: string | ((path: string) => string),): string { if (typeof dest === "function") { return dest(posix.join(path, entry.name)); }
if (typeof dest === "string") { return dest; }
return posix.join(path, entry.name);}
function isWellKnownDir(entry: Entry) { return entry.type === "directory" && entry.path === "/.well-known";}
lume

Version Info

Tagged at
7 months ago