summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/TemplateLayoutPathResolver.js
blob: 9f5a8ee6792186c4ae990e3b4a2f5467da533b2e (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
import fs from "node:fs";
import { TemplatePath } from "@11ty/eleventy-utils";
// import debugUtil from "debug";
// const debug = debugUtil("Eleventy:TemplateLayoutPathResolver");

class TemplateLayoutPathResolver {
	constructor(path, extensionMap, templateConfig) {
		if (!templateConfig) {
			throw new Error("Expected `templateConfig` in TemplateLayoutPathResolver constructor");
		}

		this.templateConfig = templateConfig;
		this.originalPath = path;
		this.originalDisplayPath =
			TemplatePath.join(this.layoutsDir, this.originalPath) +
			` (via \`layout: ${this.originalPath}\`)`; // for error messaging

		this.path = path;
		this.aliases = {};
		this.extensionMap = extensionMap;
		if (!extensionMap) {
			throw new Error("Expected `extensionMap` in TemplateLayoutPathResolver constructor.");
		}

		this.init();
	}

	getVirtualTemplate(layoutPath) {
		let inputDirRelativePath =
			this.templateConfig.directories.getLayoutPathRelativeToInputDirectory(layoutPath);
		return this.config.virtualTemplates[inputDirRelativePath];
	}

	get dirs() {
		return this.templateConfig.directories;
	}

	get inputDir() {
		return this.dirs.input;
	}

	get layoutsDir() {
		return this.dirs.layouts || this.dirs.includes;
	}

	/* Backwards compat */
	getLayoutsDir() {
		return this.layoutsDir;
	}

	setAliases() {
		this.aliases = Object.assign({}, this.config.layoutAliases, this.aliases);
	}

	// for testing
	set config(cfg) {
		this._config = cfg;
		this.init();
	}

	get config() {
		if (!this.templateConfig) {
			throw new Error("Internal error: Missing this.templateConfig");
		}

		return this.templateConfig.getConfig();
	}

	exists(layoutPath) {
		if (this.getVirtualTemplate(layoutPath)) {
			return true;
		}
		let fullPath = this.templateConfig.directories.getLayoutPath(layoutPath);
		if (this.templateConfig.existsCache.exists(fullPath)) {
			return true;
		}
		return false;
	}

	init() {
		// we might be able to move this into the constructor?
		this.aliases = Object.assign({}, this.config.layoutAliases, this.aliases);

		if (this.aliases[this.path]) {
			this.path = this.aliases[this.path];
		}

		let useLayoutResolution = this.config.layoutResolution;

		if (this.path.split(".").length > 0 && this.exists(this.path)) {
			this.filename = this.path;
			this.fullPath = this.templateConfig.directories.getLayoutPath(this.path);
		} else if (useLayoutResolution) {
			this.filename = this.findFileName();
			this.fullPath = this.templateConfig.directories.getLayoutPath(this.filename || "");
		}
	}

	addLayoutAlias(from, to) {
		this.aliases[from] = to;
	}

	getFileName() {
		if (!this.filename) {
			throw new Error(
				`You’re trying to use a layout that does not exist: ${this.originalDisplayPath}`,
			);
		}

		return this.filename;
	}

	getFullPath() {
		if (!this.filename) {
			throw new Error(
				`You’re trying to use a layout that does not exist: ${this.originalDisplayPath}`,
			);
		}

		return this.fullPath;
	}

	findFileName() {
		for (let filename of this.extensionMap.getFileList(this.path)) {
			if (this.exists(filename)) {
				return filename;
			}
		}
	}

	getNormalizedLayoutKey() {
		return TemplatePath.stripLeadingSubPath(this.fullPath, this.layoutsDir);
	}
}

export default TemplateLayoutPathResolver;