-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
eleventyConfig.augmentFunctionContext
for plugins to use on sh…
…ortcodes and filters to establish this.page and this.eleventy (and future props too) Related to #3310 and noelforte/eleventy-plugin-vento#9
- Loading branch information
Showing
6 changed files
with
210 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const DATA_KEYS = ["page", "eleventy"]; | ||
|
||
function augmentFunction(fn, options = {}) { | ||
let t = typeof fn; | ||
if (t !== "function") { | ||
throw new Error( | ||
"Invalid type passed to `augmentFunction`. A function was expected and received: " + t, | ||
); | ||
} | ||
|
||
return function (...args) { | ||
let context = augmentObject(this || {}, options); | ||
return fn.call(context, ...args); | ||
}; | ||
} | ||
|
||
function augmentObject(targetObject, options = {}) { | ||
options = Object.assign( | ||
{ | ||
source: undefined, // where to copy from | ||
overwrite: true, | ||
lazy: false, // lazily fetch the property | ||
// getter: function() {}, | ||
}, | ||
options, | ||
); | ||
|
||
for (let key of DATA_KEYS) { | ||
// Skip if overwrite: false and prop already exists on target | ||
if (!options.overwrite && targetObject[key]) { | ||
continue; | ||
} | ||
|
||
if (options.lazy) { | ||
let value; | ||
if (typeof options.getter == "function") { | ||
value = () => options.getter(key, options.source); | ||
} else { | ||
value = () => options.source?.[key]; | ||
} | ||
|
||
// lazy getter important for Liquid strictVariables support | ||
Object.defineProperty(targetObject, key, { | ||
writeable: true, | ||
configurable: true, | ||
enumerable: true, | ||
value, | ||
}); | ||
} else { | ||
let value; | ||
if (typeof options.getter == "function") { | ||
value = options.getter(key, options.source); | ||
} else { | ||
value = options.source?.[key]; | ||
} | ||
|
||
if (value) { | ||
targetObject[key] = value; | ||
} | ||
} | ||
} | ||
|
||
return targetObject; | ||
} | ||
|
||
export { DATA_KEYS as augmentKeys, augmentFunction, augmentObject }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters