/
TemplateContent.js
564 lines (485 loc) · 15.3 KB
/
TemplateContent.js
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
const os = require("os");
const fs = require("graceful-fs");
const util = require("util");
const readFile = util.promisify(fs.readFile);
const normalize = require("normalize-path");
const matter = require("gray-matter");
const lodashSet = require("lodash/set");
const { TemplatePath } = require("@11ty/eleventy-utils");
const EleventyExtensionMap = require("./EleventyExtensionMap");
const TemplateData = require("./TemplateData");
const TemplateRender = require("./TemplateRender");
const TemplateConfig = require("./TemplateConfig");
const EleventyBaseError = require("./EleventyBaseError");
const EleventyErrorUtil = require("./EleventyErrorUtil");
const debug = require("debug")("Eleventy:TemplateContent");
const debugDev = require("debug")("Dev:Eleventy:TemplateContent");
const eventBus = require("./EventBus");
class TemplateContentConfigError extends EleventyBaseError {}
class TemplateContentFrontMatterError extends EleventyBaseError {}
class TemplateContentCompileError extends EleventyBaseError {}
class TemplateContentRenderError extends EleventyBaseError {}
class TemplateContent {
constructor(inputPath, inputDir, config) {
if (!config) {
throw new TemplateContentConfigError(
"Missing `config` argument to TemplateContent"
);
}
this.config = config;
this.inputPath = inputPath;
if (inputDir) {
this.inputDir = normalize(inputDir);
} else {
this.inputDir = false;
}
}
/* Used by tests */
get extensionMap() {
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap([], this.config);
}
return this._extensionMap;
}
set extensionMap(map) {
this._extensionMap = map;
}
set config(config) {
this._config = config;
}
get config() {
if (this._config instanceof TemplateConfig) {
return this._config.getConfig();
}
return this._config;
}
get bench() {
return this.config.benchmarkManager.get("Aggregate");
}
get eleventyConfig() {
if (this._config instanceof TemplateConfig) {
return this._config;
}
throw new TemplateContentConfigError(
"Tried to get an eleventyConfig but none was found."
);
}
get engine() {
return this.templateRender.engine;
}
get templateRender() {
if (!this._templateRender) {
this._templateRender = new TemplateRender(
this.inputPath,
this.inputDir,
this.config
);
this._templateRender.extensionMap = this.extensionMap;
}
return this._templateRender;
}
getInputPath() {
return this.inputPath;
}
getInputDir() {
return this.inputDir;
}
async read() {
if (this.inputContent) {
await this.inputContent;
} else {
this.inputContent = await this.getInputContent();
}
if (this.inputContent) {
let options = this.config.frontMatterParsingOptions || {};
let fm;
try {
fm = matter(this.inputContent, options);
} catch (e) {
throw new TemplateContentFrontMatterError(
`Having trouble reading front matter from template ${this.inputPath}`,
e
);
}
if (options.excerpt && fm.excerpt) {
let excerptString = fm.excerpt + (options.excerpt_separator || "---");
if (fm.content.startsWith(excerptString + os.EOL)) {
// with a newline after excerpt separator
fm.content =
fm.excerpt.trim() +
"\n" +
fm.content.slice((excerptString + os.EOL).length);
} else if (fm.content.startsWith(excerptString)) {
// no newline after excerpt separator
fm.content = fm.excerpt + fm.content.slice(excerptString.length);
}
// alias, defaults to page.excerpt
let alias = options.excerpt_alias || "page.excerpt";
lodashSet(fm.data, alias, fm.excerpt);
}
this.frontMatter = fm;
} else {
this.frontMatter = {
data: {},
content: "",
excerpt: "",
};
}
}
static cache(path, content) {
this._inputCache.set(TemplatePath.absolutePath(path), content);
}
static getCached(path) {
return this._inputCache.get(TemplatePath.absolutePath(path));
}
static deleteCached(path) {
this._inputCache.delete(TemplatePath.absolutePath(path));
}
// Used via clone
setInputContent(content) {
this.inputContent = content;
}
async getInputContent() {
if (!this.engine.needsToReadFileContents()) {
return "";
}
let templateBenchmark = this.bench.get("Template Read");
templateBenchmark.before();
let content;
if (this.config.useTemplateCache) {
content = TemplateContent.getCached(this.inputPath);
}
if (!content) {
content = await readFile(this.inputPath, "utf8");
if (this.config.useTemplateCache) {
TemplateContent.cache(this.inputPath, content);
}
}
templateBenchmark.after();
return content;
}
async getFrontMatter() {
if (!this.frontMatter) {
await this.read();
}
return this.frontMatter;
}
async getPreRender() {
if (!this.frontMatter) {
await this.read();
}
return this.frontMatter.content;
}
async getFrontMatterData() {
if (this._frontMatterDataCache) {
return this._frontMatterDataCache;
}
if (!this.frontMatter) {
await this.read();
}
let extraData = await this.engine.getExtraDataFromFile(this.inputPath);
let data = TemplateData.mergeDeep({}, this.frontMatter.data, extraData);
let cleanedData = TemplateData.cleanupData(data);
this._frontMatterDataCache = cleanedData;
return cleanedData;
}
async getEngineOverride() {
let frontMatterData = await this.getFrontMatterData();
return frontMatterData[this.config.keys.engineOverride];
}
async setupTemplateRender(engineOverride, bypassMarkdown) {
if (engineOverride !== undefined) {
debugDev(
"%o overriding template engine to use %o",
this.inputPath,
engineOverride
);
this.templateRender.setEngineOverride(engineOverride, bypassMarkdown);
} else {
this.templateRender.setUseMarkdown(!bypassMarkdown);
}
}
_getCompileCache(str, bypassMarkdown) {
let engineName = this.engine.getName() + "::" + !!bypassMarkdown;
let engineMap = TemplateContent._compileEngineCache.get(engineName);
if (!engineMap) {
engineMap = new Map();
TemplateContent._compileEngineCache.set(engineName, engineMap);
}
let cacheable = this.engine.cacheable;
let key = this.engine.getCompileCacheKey(str, this.inputPath);
return [cacheable, key, engineMap];
}
async compile(str, bypassMarkdown, engineOverride) {
await this.setupTemplateRender(engineOverride, bypassMarkdown);
if (bypassMarkdown && !this.engine.needsCompilation(str)) {
return async function () {
return str;
};
}
debugDev(
"%o compile() using engine: %o",
this.inputPath,
this.templateRender.engineName
);
try {
let res;
if (this.config.useTemplateCache) {
let [cacheable, key, cache] = this._getCompileCache(
str,
bypassMarkdown
);
if (cacheable && key) {
if (cache.has(key)) {
this.bench
.get("(count) Template Compile Cache Hit")
.incrementCount();
return cache.get(key);
}
this.bench
.get("(count) Template Compile Cache Miss")
.incrementCount();
// Compilation is async, so we eagerly cache a Promise that eventually
// resolves to the compiled function
cache.set(
key,
new Promise((resolve) => {
res = resolve;
})
);
}
}
let templateBenchmark = this.bench.get("Template Compile");
let inputPathBenchmark = this.bench.get(`> Compile > ${this.inputPath}`);
templateBenchmark.before();
inputPathBenchmark.before();
let fn = await this.templateRender.getCompiledTemplate(str);
inputPathBenchmark.after();
templateBenchmark.after();
debugDev("%o getCompiledTemplate function created", this.inputPath);
if (this.config.useTemplateCache && res) {
res(fn);
}
return fn;
} catch (e) {
let [cacheable, key, cache] = this._getCompileCache(str, bypassMarkdown);
if (cacheable && key) {
cache.delete(key);
}
debug(`Having trouble compiling template ${this.inputPath}: %O`, str);
throw new TemplateContentCompileError(
`Having trouble compiling template ${this.inputPath}`,
e
);
}
}
getParseForSymbolsFunction(str) {
let engine = this.engine;
// Don’t use markdown as the engine to parse for symbols
let preprocessorEngine = this.templateRender.getPreprocessorEngine(); // TODO pass in engineOverride here
if (preprocessorEngine && engine.getName() !== preprocessorEngine) {
let replacementEngine =
this.templateRender.getEngineByName(preprocessorEngine);
if (replacementEngine) {
engine = replacementEngine;
}
}
if ("parseForSymbols" in engine) {
return () => {
return engine.parseForSymbols(str);
};
}
}
// used by computed data or for permalink functions
async _renderFunction(fn, ...args) {
let mixins = Object.assign({}, this.config.javascriptFunctions);
let result = await fn.call(mixins, ...args);
// normalize Buffer away if returned from permalink
if (Buffer.isBuffer(result)) {
return result.toString();
}
return result;
}
async renderComputedData(str, data) {
if (typeof str === "function") {
return this._renderFunction(str, data);
}
return this._render(str, data, true);
}
async renderPermalink(permalink, data) {
this.bench.get("(count) Render Permalink").incrementCount();
this.bench
.get(
`(count) > Render Permalink > ${
this.inputPath
}${this._getPaginationLogSuffix(data)}`
)
.incrementCount();
let permalinkCompilation = this.engine.permalinkNeedsCompilation(permalink);
// No string compilation:
// ({ compileOptions: { permalink: "raw" }})
// These mean `permalink: false`, which is no file system writing:
// ({ compileOptions: { permalink: false }})
// ({ compileOptions: { permalink: () => false }})
// ({ compileOptions: { permalink: () => (() = > false) }})
if (permalinkCompilation === false) {
return permalink;
}
/* Custom `compile` function for permalinks, usage:
permalink: function(permalinkString, inputPath) {
return async function(data) {
return "THIS IS MY RENDERED PERMALINK";
}
}
*/
if (permalinkCompilation && typeof permalinkCompilation === "function") {
permalink = await this._renderFunction(
permalinkCompilation,
permalink,
this.inputPath
);
}
// Raw permalink function (in the app code data cascade)
if (typeof permalink === "function") {
return this._renderFunction(permalink, data);
}
return this._render(permalink, data, true);
}
async render(str, data, bypassMarkdown) {
return this._render(str, data, bypassMarkdown);
}
_getPaginationLogSuffix(data) {
let suffix = [];
if ("pagination" in data) {
suffix.push(" (");
if (data.pagination.pages) {
suffix.push(
`${data.pagination.pages.length} page${
data.pagination.pages.length !== 1 ? "s" : ""
}`
);
} else {
suffix.push("Pagination");
}
suffix.push(")");
}
return suffix.join("");
}
async _render(str, data, bypassMarkdown) {
try {
if (bypassMarkdown && !this.engine.needsCompilation(str)) {
return str;
}
let fn = await this.compile(
str,
bypassMarkdown,
data[this.config.keys.engineOverride]
);
if (fn === undefined) {
return;
} else if (typeof fn !== "function") {
throw new Error(
`The \`compile\` function did not return a function. Received ${fn}`
);
}
// Benchmark
let templateBenchmark = this.bench.get("Render");
// Skip benchmark for each individual pagination entry (very busy output)
let logRenderToOutputBenchmark = "pagination" in data;
let inputPathBenchmark = this.bench.get(
`> Render > ${this.inputPath}${this._getPaginationLogSuffix(data)}`
);
let outputPathBenchmark;
if (data.page && data.page.outputPath && logRenderToOutputBenchmark) {
outputPathBenchmark = this.bench.get(
`> Render to > ${data.page.outputPath}`
);
}
templateBenchmark.before();
if (inputPathBenchmark) {
inputPathBenchmark.before();
}
if (outputPathBenchmark) {
outputPathBenchmark.before();
}
let rendered = await fn(data);
if (outputPathBenchmark) {
outputPathBenchmark.after();
}
if (inputPathBenchmark) {
inputPathBenchmark.after();
}
templateBenchmark.after();
debugDev(
"%o getCompiledTemplate called, rendered content created",
this.inputPath
);
return rendered;
} catch (e) {
if (EleventyErrorUtil.isPrematureTemplateContentError(e)) {
throw e;
} else {
let engine = this.templateRender.getReadableEnginesList();
debug(
`Having trouble rendering ${engine} template ${this.inputPath}: %O`,
str
);
throw new TemplateContentRenderError(
`Having trouble rendering ${engine} template ${this.inputPath}`,
e
);
}
}
}
getExtensionEntries() {
let extensions = this.templateRender.engine.extensionEntries;
return extensions;
}
isFileRelevantToThisTemplate(incrementalFile, metadata = {}) {
// always relevant if incremental file not set (build everything)
if (!incrementalFile) {
return true;
}
let extensionEntries = this.getExtensionEntries().filter(
(entry) => !!entry.isIncrementalMatch
);
if (extensionEntries.length) {
for (let entry of extensionEntries) {
if (
entry.isIncrementalMatch.call(
{
inputPath: this.inputPath,
isFullTemplate: metadata.isFullTemplate,
},
incrementalFile
)
) {
return true;
}
}
return false;
} else {
// This is the fallback way of determining if something is incremental (no isIncrementalMatch available)
// Not great way of building all templates if this is a layout, include, JS dependency.
// TODO improve this for default langs
if (!metadata.isFullTemplate) {
return true;
}
// only build if this input path is the same as the file that was changed
if (this.inputPath === incrementalFile) {
return true;
}
}
return false;
}
}
TemplateContent._inputCache = new Map();
TemplateContent._compileEngineCache = new Map();
eventBus.on("eleventy.resourceModified", (path) => {
TemplateContent.deleteCached(path);
});
// Used when the configuration file reset https://github.com/11ty/eleventy/issues/2147
eventBus.on("eleventy.compileCacheReset", (path) => {
TemplateContent._compileEngineCache = new Map();
});
module.exports = TemplateContent;