summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/TemplateLayoutPathResolver.js
diff options
context:
space:
mode:
authorShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
committerShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
commit7a52ddeba2a68388b544f529d2d92104420f77b0 (patch)
tree15ddd47457a2cb4a96060747437d36474e4f6b4e /node_modules/@11ty/eleventy/src/TemplateLayoutPathResolver.js
parent53d6ae2b5568437afa5e4995580a3fb679b7b91b (diff)
Changed from static to 11ty!
Diffstat (limited to 'node_modules/@11ty/eleventy/src/TemplateLayoutPathResolver.js')
-rw-r--r--node_modules/@11ty/eleventy/src/TemplateLayoutPathResolver.js136
1 files changed, 136 insertions, 0 deletions
diff --git a/node_modules/@11ty/eleventy/src/TemplateLayoutPathResolver.js b/node_modules/@11ty/eleventy/src/TemplateLayoutPathResolver.js
new file mode 100644
index 0000000..9f5a8ee
--- /dev/null
+++ b/node_modules/@11ty/eleventy/src/TemplateLayoutPathResolver.js
@@ -0,0 +1,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;