summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/TemplateLayout.js
blob: f8526f23e02b3425531bc7fd0f2d129b7b41dbb4 (plain)
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
import { TemplatePath } from "@11ty/eleventy-utils";
import debugUtil from "debug";

import TemplateLayoutPathResolver from "./TemplateLayoutPathResolver.js";
import TemplateContent from "./TemplateContent.js";
import TemplateData from "./Data/TemplateData.js";
import layoutCache from "./LayoutCache.js";

// const debug = debugUtil("Eleventy:TemplateLayout");
const debugDev = debugUtil("Dev:Eleventy:TemplateLayout");

class TemplateLayout extends TemplateContent {
	constructor(key, extensionMap, eleventyConfig) {
		if (!eleventyConfig || eleventyConfig.constructor.name !== "TemplateConfig") {
			throw new Error("Expected `eleventyConfig` in TemplateLayout constructor.");
		}

		let resolver = new TemplateLayoutPathResolver(key, extensionMap, eleventyConfig);
		let resolvedPath = resolver.getFullPath();

		super(resolvedPath, eleventyConfig);

		if (!extensionMap) {
			throw new Error("Expected `extensionMap` in TemplateLayout constructor.");
		}

		this.extensionMap = extensionMap;
		this.key = resolver.getNormalizedLayoutKey();
		this.dataKeyLayoutPath = key;
		this.inputPath = resolvedPath;
	}

	getKey() {
		return this.key;
	}

	getFullKey() {
		return TemplateLayout.resolveFullKey(this.dataKeyLayoutPath, this.inputDir);
	}

	getCacheKeys() {
		return new Set([this.dataKeyLayoutPath, this.getFullKey(), this.key]);
	}

	static resolveFullKey(key, inputDir) {
		return TemplatePath.join(inputDir, key);
	}

	static getTemplate(key, eleventyConfig, extensionMap) {
		let config = eleventyConfig.getConfig();
		if (!config.useTemplateCache) {
			return new TemplateLayout(key, extensionMap, eleventyConfig);
		}

		let inputDir = eleventyConfig.directories.input;
		let fullKey = TemplateLayout.resolveFullKey(key, inputDir);
		if (!layoutCache.has(fullKey)) {
			let layout = new TemplateLayout(key, extensionMap, eleventyConfig);

			layoutCache.add(layout);
			debugDev("Added %o to LayoutCache", key);

			return layout;
		}

		return layoutCache.get(fullKey);
	}

	async getTemplateLayoutMapEntry() {
		let { data: frontMatterData } = await this.getFrontMatterData();
		return {
			// Used by `TemplateLayout.getTemplate()`
			key: this.dataKeyLayoutPath,

			// used by `this.getData()`
			frontMatterData,
		};
	}

	async #getTemplateLayoutMap() {
		// For both the eleventy.layouts event and cyclical layout chain checking  (e.g., a => b => c => a)
		let layoutChain = new Set();
		layoutChain.add(this.inputPath);

		let cfgKey = this.config.keys.layout;
		let map = [];
		let mapEntry = await this.getTemplateLayoutMapEntry();

		map.push(mapEntry);

		while (mapEntry.frontMatterData && cfgKey in mapEntry.frontMatterData) {
			// Layout of the current layout
			let parentLayoutKey = mapEntry.frontMatterData[cfgKey];

			let layout = TemplateLayout.getTemplate(
				parentLayoutKey,
				this.eleventyConfig,
				this.extensionMap,
			);

			// Abort if a circular layout chain is detected. Otherwise, we'll time out and run out of memory.
			if (layoutChain.has(layout.inputPath)) {
				throw new Error(
					`Your layouts have a circular reference, starting at ${map[0].key}! The layout at ${layout.inputPath} was specified twice in this layout chain.`,
				);
			}

			// Keep track of this layout so we can detect duplicates in subsequent iterations
			layoutChain.add(layout.inputPath);

			// reassign for next loop
			mapEntry = await layout.getTemplateLayoutMapEntry();

			map.push(mapEntry);
		}

		this.layoutChain = Array.from(layoutChain);

		return map;
	}

	async getTemplateLayoutMap() {
		if (!this.cachedLayoutMap) {
			this.cachedLayoutMap = this.#getTemplateLayoutMap();
		}

		return this.cachedLayoutMap;
	}

	async getLayoutChain() {
		if (!Array.isArray(this.layoutChain)) {
			await this.getTemplateLayoutMap();
		}

		return this.layoutChain;
	}

	async #getData() {
		let map = await this.getTemplateLayoutMap();
		let dataToMerge = [];
		for (let j = map.length - 1; j >= 0; j--) {
			dataToMerge.push(map[j].frontMatterData);
		}

		// Deep merge of layout front matter
		let data = TemplateData.mergeDeep(this.config.dataDeepMerge, {}, ...dataToMerge);
		delete data[this.config.keys.layout];

		return data;
	}

	async getData() {
		if (!this.dataCache) {
			this.dataCache = this.#getData();
		}

		return this.dataCache;
	}

	async #getCachedCompiledLayoutFunction() {
		let rawInput = await this.getPreRender();
		return this.compile(rawInput);
	}

	// Do only cache this layout’s render function and delegate the rest to the other templates.
	async getCachedCompiledLayoutFunction() {
		if (!this.cachedCompiledLayoutFunction) {
			this.cachedCompiledLayoutFunction = this.#getCachedCompiledLayoutFunction();
		}

		return this.cachedCompiledLayoutFunction;
	}

	async getCompiledLayoutFunctions() {
		let layoutMap = await this.getTemplateLayoutMap();
		let fns = [];

		try {
			fns.push({
				render: await this.getCachedCompiledLayoutFunction(),
			});

			if (layoutMap.length > 1) {
				let [, /*currentLayout*/ parentLayout] = layoutMap;
				let { key } = parentLayout;

				let layoutTemplate = TemplateLayout.getTemplate(
					key,
					this.eleventyConfig,
					this.extensionMap,
				);

				// The parent already includes the rest of the layout chain
				let upstreamFns = await layoutTemplate.getCompiledLayoutFunctions();
				for (let j = 0, k = upstreamFns.length; j < k; j++) {
					fns.push(upstreamFns[j]);
				}
			}

			return fns;
		} catch (e) {
			debugDev("Clearing LayoutCache after error.");
			layoutCache.clear();
			throw e;
		}
	}

	async render() {
		throw new Error("Internal error: `render` was removed from TemplateLayout.js in Eleventy 3.0.");
	}

	// Inefficient? We want to compile all the templatelayouts into a single reusable callback?
	// Trouble: layouts may need data variables present downstream/upstream
	// This is called from Template->renderPageEntry
	async renderPageEntry(pageEntry) {
		let templateContent = pageEntry.templateContent;
		let compiledFunctions = await this.getCompiledLayoutFunctions();
		for (let { render } of compiledFunctions) {
			let data = {
				content: templateContent,
				...pageEntry.data,
			};

			templateContent = await render(data);
		}

		// Don’t set `templateContent` on pageEntry because collection items should not have layout markup
		return templateContent;
	}

	resetCaches(types) {
		super.resetCaches(types);
		delete this.dataCache;
		delete this.layoutChain;
		delete this.cachedLayoutMap;
		delete this.cachedCompiledLayoutFunction;
	}
}

export default TemplateLayout;