diff --git a/src/TemplateData.js b/src/TemplateData.js index b37615235..2c0c0aacd 100755 --- a/src/TemplateData.js +++ b/src/TemplateData.js @@ -256,7 +256,7 @@ class TemplateData { let folders = parsed.dir ? parsed.dir.split("/") : []; folders.push(parsed.name); - return folders.join("."); + return folders; } async getAllGlobalData() { @@ -271,21 +271,27 @@ class TemplateData { let dataFileConflicts = {}; for (let j = 0, k = files.length; j < k; j++) { - let objectPathTarget = await this.getObjectPathForDataFile(files[j]); let data = await this.getDataValue(files[j], rawImports); + let objectPathTarget = this.getObjectPathForDataFile(files[j]); + + // Since we're joining directory paths and an array is not useable as an objectkey since two identical arrays are not double equal, + // we can just join the array by a forbidden character ("/"" is chosen here, since it works on Linux, Mac and Windows). + // If at some point this isn't enough anymore, it would be possible to just use JSON.stringify(objectPathTarget) since that + // is guaranteed to work but is signifivcantly slower. + let objectPathTargetString = objectPathTarget.join("/"); // if two global files have the same path (but different extensions) // and conflict, let’s merge them. - if (dataFileConflicts[objectPathTarget]) { + if (dataFileConflicts[objectPathTargetString]) { debugWarn( - `merging global data from ${files[j]} with an already existing global data file (${dataFileConflicts[objectPathTarget]}). Overriding existing keys.` + `merging global data from ${files[j]} with an already existing global data file (${dataFileConflicts[objectPathTargetString]}). Overriding existing keys.` ); let oldData = lodashget(globalData, objectPathTarget); data = TemplateData.mergeDeep(this.config, oldData, data); } - dataFileConflicts[objectPathTarget] = files[j]; + dataFileConflicts[objectPathTargetString] = files[j]; debug( `Found global data file ${files[j]} and adding as: ${objectPathTarget}` ); diff --git a/test/TemplateDataTest.js b/test/TemplateDataTest.js index 3ba4ab8f9..62e424a7d 100644 --- a/test/TemplateDataTest.js +++ b/test/TemplateDataTest.js @@ -461,6 +461,29 @@ test("Parent directory for data (Issue #337)", async (t) => { }); }); +test("Dots in datafile path (Issue #1242)", async (t) => { + let eleventyConfig = new TemplateConfig({ + dataTemplateEngine: false, + dir: { + input: "./test/stubs-1242/", + data: "_data/", + }, + }); + let dataObj = new TemplateData("./test/stubs-1242/", eleventyConfig); + dataObj.setInputDir("./test/stubs-1242/"); + + let data = await dataObj.getData(); + + t.deepEqual(data, { + "xyz.dottest": { + hi: "bye", + test: { + abc: 42, + }, + }, + }); +}); + test("addGlobalData values", async (t) => { let eleventyConfig = new TemplateConfig(); eleventyConfig.userConfig.addGlobalData("myFunction", () => "fn-value"); diff --git a/test/stubs-1242/_data/xyz.dottest.json b/test/stubs-1242/_data/xyz.dottest.json new file mode 100644 index 000000000..6c820361c --- /dev/null +++ b/test/stubs-1242/_data/xyz.dottest.json @@ -0,0 +1,3 @@ +{ + "hi": "bye" +} diff --git a/test/stubs-1242/_data/xyz.dottest/test.json b/test/stubs-1242/_data/xyz.dottest/test.json new file mode 100644 index 000000000..d6f996357 --- /dev/null +++ b/test/stubs-1242/_data/xyz.dottest/test.json @@ -0,0 +1,3 @@ +{ + "abc": 42 +} diff --git a/test/stubs-1242/empty.md b/test/stubs-1242/empty.md new file mode 100644 index 000000000..e69de29bb