Skip to content

Commit

Permalink
Merge pull request #1912 from Snapstromegon/fix-1242-dot-pathing
Browse files Browse the repository at this point in the history
Fix #1242 Dot Pathing in data file paths
  • Loading branch information
zachleat committed May 10, 2022
2 parents 13e7a52 + 69c37b4 commit fe1036d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class TemplateData {
let folders = parsed.dir ? parsed.dir.split("/") : [];
folders.push(parsed.name);

return folders.join(".");
return folders;
}

async getAllGlobalData() {
Expand All @@ -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}`
);
Expand Down
23 changes: 23 additions & 0 deletions test/TemplateDataTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 3 additions & 0 deletions test/stubs-1242/_data/xyz.dottest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hi": "bye"
}
3 changes: 3 additions & 0 deletions test/stubs-1242/_data/xyz.dottest/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"abc": 42
}
Empty file added test/stubs-1242/empty.md
Empty file.

0 comments on commit fe1036d

Please sign in to comment.