deno.land / x / lume@v2.1.4 / core / renderer.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
import { resolveInclude } from "./utils/path.ts";import { isGenerator } from "./utils/generator.ts";import { concurrent } from "./utils/concurrent.ts";import { mergeData } from "./utils/merge_data.ts";import { getPageUrl } from "./utils/page_url.ts";import { getPageDate } from "./utils/page_date.ts";import { Page } from "./file.ts";import { posix } from "../deps/path.ts";
import type { Content, Data } from "./file.ts";import type Processors from "./processors.ts";import type Formats from "./formats.ts";import type FS from "./fs.ts";
export interface Options { includes: string; prettyUrls: boolean; preprocessors: Processors; formats: Formats; fs: FS;}
/** * The renderer is responsible for rendering the site pages * in the right order and using the right template engine. */export default class Renderer { /** The default folder to include the layouts */ includes: string;
/** The filesystem instance used to read the layouts */ fs: FS;
/** To convert the urls to pretty /example.html => /example/ */ prettyUrls: boolean;
/** All preprocessors */ preprocessors: Processors;
/** Available file formats */ formats: Formats;
/** The registered helpers */ helpers = new Map<string, [Helper, HelperOptions]>();
constructor(options: Options) { this.includes = options.includes; this.prettyUrls = options.prettyUrls; this.preprocessors = options.preprocessors; this.formats = options.formats; this.fs = options.fs; }
/** Register a new helper used by the template engines */ addHelper(name: string, fn: Helper, options: HelperOptions) { this.helpers.set(name, [fn, options]);
for (const format of this.formats.entries.values()) { format.engines?.forEach((engine) => engine.addHelper(name, fn, options)); }
return this; }
/** Render the provided pages */ async renderPages(from: Page[], to: Page[], onDemand: Page[]): Promise<void> { for (const group of this.#groupPages(from)) { const pages: Page[] = []; const generators: Page[] = [];
// Split regular pages and generators for (const page of group) { if (isGenerator(page.data.content)) { generators.push(page); continue; }
if (page.data.ondemand) { onDemand.push(page); continue; } pages.push(page); }
// Preprocess the pages and add them to site.pages await this.preprocessors.run(pages); to.push(...pages);
const generatedPages: Page[] = []; for (const page of generators) { const data = { ...page.data }; const { content } = data; delete data.content;
const generator = await this.render<Generator<Data, Data>>( content, data, page.src.path + page.src.ext, );
let index = 0; const basePath = posix.dirname(page.data.url);
for await (const data of generator) { if (!data.content) { data.content = undefined; } const newPage = page.duplicate( index++, mergeData(page.data, data) as Data, ); const url = getPageUrl(newPage, this.prettyUrls, basePath); if (!url) { continue; } newPage.data.url = url; newPage.data.date = getPageDate(newPage); newPage._data.layout = "layout" in data ? data.layout : page._data.layout; generatedPages.push(newPage); } }
// Preprocess the generators and add them to site.pages await this.preprocessors.run(generatedPages); to.push(...generatedPages);
// Render the pages content const renderedPages: Page[] = []; await concurrent( pages.concat(generatedPages), async (page) => { try { const content = await this.#renderPage(page);
// Save the children to render the layout later // (Only HTML pages and pages with the layout in the frontmatter) // This prevents to call the layout for every page (like css, js, etc) if (page.outputPath.endsWith(".html") || page._data.layout) { page.data.children = content; renderedPages.push(page); } else { page.content = content; } } catch (cause) { throw new Error(`Error rendering the page: ${page.sourcePath}`, { cause, }); } }, );
// Render the pages layouts await concurrent( renderedPages, async (page) => { try { page.content = await this.#renderLayout( page, page.data.children as Content, );
// Ensure all HTML pages have the DOCTYPE declaration if ( page.outputPath.endsWith(".html") && typeof page.content === "string" ) { const trim = page.content.trim();
if (trim && !trim.match(/^<!DOCTYPE\s/i)) { page.content = `<!DOCTYPE html>\n${page.content}`; } } } catch (cause) { throw new Error( `Error rendering the layout of the page ${page.sourcePath}`, { cause }, ); } }, ); } }
/** Render the provided pages */ async renderPageOnDemand(page: Page): Promise<void> { if (isGenerator(page.data.content)) { throw new Error( `Cannot render the generator page ${page.sourcePath} on demand.`, ); }
await this.preprocessors.run([page]);
// The page is type asset if (this.formats.get(page.src.ext)?.pageType === "asset") { page.content = page.data.content as Content; } else { const content = await this.#renderPage(page); page.content = await this.#renderLayout(page, content); } }
/** Render a template */ async render<T>( content: unknown, data: Record<string, unknown>, filename: string, isLayout = false, ): Promise<T> { const engines = this.#getEngine(filename, data, isLayout);
if (engines) { for (const engine of engines) { content = await engine.render(content, data, filename); } }
return content as T; }
/** Group the pages by renderOrder */ #groupPages(pages: Page[]): Page[][] { const renderOrder: Record<number | string, Page[]> = {};
for (const page of pages) { const order = page.data.renderOrder || 0; renderOrder[order] = renderOrder[order] || []; renderOrder[order].push(page); }
return Object.keys(renderOrder).sort().map((order) => renderOrder[order]); }
/** Render a page */ async #renderPage(page: Page): Promise<Content> { const data = { ...page.data }; const { content } = data; delete data.content;
return await this.render<Content>( content, data, page.src.path + page.src.ext, ); }
/** Render the page layout */ async #renderLayout(page: Page, content: Content): Promise<Content> { let data = { ...page.data }; let path = page.src.path + page.src.ext; let layout = data.layout;
// Render the layouts recursively while (layout) { const format = this.formats.search(layout);
if (!format || !format.loader) { throw new Error(`The layout format "${layout}" doesn't exist`); }
const includesPath = format.engines?.[0].includes;
if (!includesPath) { throw new Error( `The layout format "${layout}" doesn't support includes`, ); }
const layoutPath = resolveInclude( layout, includesPath, posix.dirname(path), ); const entry = this.fs.entries.get(layoutPath);
if (!entry) { throw new Error(`The layout file "${layoutPath}" doesn't exist`); }
const layoutData = await entry.getContent(format.loader);
delete data.layout; delete data.templateEngine;
data = { ...layoutData, ...data, content, };
content = await this.render<Content>( layoutData.content, data, layoutPath, true, ); layout = layoutData.layout; path = layoutPath; }
return content; }
/** Get the engines assigned to an extension or configured in the data */ #getEngine( path: string, data: Partial<Data>, isLayout: boolean, ): Engine[] | undefined { let { templateEngine } = data;
if (templateEngine) { templateEngine = Array.isArray(templateEngine) ? templateEngine : templateEngine.split(",");
return templateEngine.reduce((engines, name) => { const format = this.formats.get(`.${name.trim()}`);
if (format?.engines) { return engines.concat(format.engines); }
throw new Error(`The template engine "${name}" doesn't exist`); }, [] as Engine[]); }
const format = this.formats.search(path);
if (isLayout || format?.pageType === "page") { return format?.engines; } }}
/** An interface used by all template engines */export interface Engine<T = string | { toString(): string }> { /** The folder name of the includes */ includes?: string;
/** Delete a cached template */ deleteCache(file: string): void;
/** Render a template (used to render pages) */ render( content: unknown, data?: Record<string, unknown>, filename?: string, ): T | Promise<T>;
/** Render a component (it must be synchronous) */ renderComponent( content: unknown, data?: Record<string, unknown>, filename?: string, ): T;
/** Add a helper to the template engine */ addHelper( name: string, fn: Helper, options: HelperOptions, ): void;}
/** A generic helper to be used in template engines */export interface HelperThis { data: Data;}
// deno-lint-ignore no-explicit-anyexport type Helper = (this: HelperThis | void, ...args: any[]) => any;
/** The options for a template helper */export interface HelperOptions { /** The type of the helper (tag, filter, etc) */ type: string;
/** Whether the helper returns an instance or not */ async?: boolean;
/** Whether the helper has a body or not (used for tag types) */ body?: boolean;}
lume

Version Info

Tagged at
7 months ago