Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/EleventyExtensionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class EleventyExtensionMap {
// on paths found from the file system glob search.
// TODO: Method name might just need to be renamed to something more accurate.
isFullTemplateFilePath(path) {
path = (path || "").toLowerCase();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking: it's an anti-pattern to modify function arguments.
Can you use a new variable like the normalizedPath below?


for (let extension of this.validTemplateLanguageKeys) {
if (path.endsWith(`.${extension}`)) {
return true;
Expand All @@ -97,6 +99,8 @@ class EleventyExtensionMap {
}

getValidExtensionsForPath(path) {
path = (path || "").toLowerCase();

let extensions = new Set();
for (let extension in this.extensionToKeyMap) {
if (path.endsWith(`.${extension}`)) {
Expand Down Expand Up @@ -242,8 +246,10 @@ class EleventyExtensionMap {
}

removeTemplateExtension(path) {
let normalizedPath = (path || "").toLowerCase();

for (let extension in this.extensionToKeyMap) {
if (path === extension || path.endsWith("." + extension)) {
if (normalizedPath === extension || normalizedPath.endsWith("." + extension)) {
return path.slice(
0,
path.length - 1 - extension.length < 0 ? 0 : path.length - 1 - extension.length,
Expand Down
4 changes: 4 additions & 0 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,8 @@ class UserConfig {
}

for (let extension of extensions) {
extension = extension.trim().toLowerCase();

if (this.extensionConflictMap[extension]) {
throw new Error(
`An attempt was made to override the "${extension}" template syntax twice (via the \`addExtension\` configuration API). A maximum of one override is currently supported.`,
Expand All @@ -944,6 +946,8 @@ class UserConfig {
},
options,
);
extensionOptions.key = extensionOptions.key.toLowerCase();
extensionOptions.extension = extensionOptions.extension.toLowerCase();

if (extensionOptions.key !== extensionOptions.extension) {
extensionOptions.aliasKey = extensionOptions.extension;
Expand Down
32 changes: 32 additions & 0 deletions test/EleventyExtensionMapTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,38 @@ test("Keys are mapped to lower case", async (t) => {
t.deepEqual(map.getGlobs("."), ["./**/*.{liquid,pug,njk}"]);
});

test("Custom addExtension keys are mapped to lower case", async (t) => {
let cfg = new TemplateConfig();
cfg.userConfig.addExtension("vZome", {
compile: function(content) {
return () => content;
}
});

let map = await getExtensionMap(["vZome"], cfg);

t.deepEqual(map.getValidGlobs("."), ["./**/*.vzome"]);
t.true(map.hasEngine("component.vZome"));
t.is(map.getKey("component.vZome"), "vzome");
t.true(map.isFullTemplateFilePath("component.vZome"));
t.deepEqual(map.getValidExtensionsForPath("component.vZome"), ["vzome"]);
t.is(map.removeTemplateExtension("component.vZome"), "component");
});

test("Custom addExtension keys detect mixed-case duplicates", (t) => {
let cfg = new TemplateConfig();
cfg.userConfig.addExtension("vZome", {});

let e = t.throws(() => {
cfg.userConfig.addExtension("VZOME", {});
});

t.is(
e.message,
'An attempt was made to override the "vzome" template syntax twice (via the `addExtension` configuration API). A maximum of one override is currently supported.',
);
});

test("Pruned globs", async (t) => {
let map = await getExtensionMap(["liquid", "njk", "png"]);
t.deepEqual(map.getPassthroughCopyGlobs("."), ["./**/*.png"]);
Expand Down
20 changes: 20 additions & 0 deletions test/EleventyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,26 @@ test("permalink on custom template lang, issue #3619", async (t) => {
}`);
});

test("Mixed-case custom template extensions are discovered, issue #4224", async (t) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay, so the content is less of importance. Gotcha.

let elev = new Eleventy("./test/stubs-4224/", undefined, {
config: function(eleventyConfig) {
eleventyConfig.addTemplateFormats("vZome");
eleventyConfig.addExtension("vZome", {
compile: function(str) {
return function() {
return `Rendered ${str.trim()}`;
};
},
});
},
});
elev.disableLogger();

let results = await elev.toJSON();
t.is(results.length, 1);
t.is(results[0].content, "Rendered mixed-case custom extension");
});

test("Template data throws error when tags is not an Array or String #1791", async (t) => {
let elev = new Eleventy("./test/noop/", "./test/noop/_site", {
config: function (eleventyConfig) {
Expand Down
1 change: 1 addition & 0 deletions test/stubs-4224/index.vZome
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mixed-case custom extension

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this file?