Skip to content

Commit

Permalink
Fix 11ty#1242 Dot-Pathing
Browse files Browse the repository at this point in the history
Dots in the global datafile path leads to data beeing incorrectly nested.
This commits changes the behavior so a datafile a.b.json will create an entry with data["a.b"] instead of data.a.b.
The old behavior was a bug according to 11ty#1242.

The implementation makes use of lodashs set and get functions which also take object paths in array form instead of string. That way we can implement this safely.

Signed-off-by: Raphael Höser <raphael@hoeser.info>
  • Loading branch information
Snapstromegon committed Jul 29, 2021
1 parent a11a8ba commit 05468df
Showing 1 changed file with 11 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 @@ -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]);

// 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

0 comments on commit 05468df

Please sign in to comment.