Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1242 Dot Pathing in data file paths #1912

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class TemplateData {
let folders = parsed.dir ? parsed.dir.split("/") : [];
folders.push(parsed.name);

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

async getAllGlobalData() {
Expand All @@ -265,21 +265,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]);
Snapstromegon marked this conversation as resolved.
Show resolved Hide resolved

// 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 @@ -448,6 +448,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.