Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/lxg/eleventy into lxg-master
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/UserConfig.js
#	test/TemplateConfigTest.js
  • Loading branch information
zachleat committed Nov 15, 2020
2 parents 7ee3e15 + b8a0bdd commit 84f8229
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Engines/Nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Nunjucks extends TemplateEngine {
this.config.nunjucksAsyncPairedShortcodes,
true
);
this.addGlobals(this.config.nunjucksGlobals);
}

addFilters(helpers, isAsync) {
Expand Down Expand Up @@ -67,6 +68,16 @@ class Nunjucks extends TemplateEngine {
this.njkEnv.addExtension(name, tagObj);
}

addGlobals(globals) {
for (let name in globals) {
this.addGlobal(name, globals[name]);
}
}

addGlobal(name, globalFn) {
this.njkEnv.addGlobal(name, globalFn);
}

addAllShortcodes(shortcodes, isAsync = false) {
for (let name in shortcodes) {
this.addShortcode(name, shortcodes[name], isAsync);
Expand Down
20 changes: 20 additions & 0 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class UserConfig {
this.nunjucksFilters = {};
this.nunjucksAsyncFilters = {};
this.nunjucksTags = {};
this.nunjucksGlobals = {};
this.nunjucksShortcodes = {};
this.nunjucksAsyncShortcodes = {};
this.nunjucksPairedShortcodes = {};
Expand Down Expand Up @@ -235,6 +236,24 @@ class UserConfig {
return this;
}

addNunjucksGlobal(name, globalFn) {
name = this.getNamespacedName(name);

if (this.nunjucksGlobals[name]) {
debug(
chalk.yellow(
"Warning, overwriting a Nunjucks global with `addNunjucksGlobal(%o)`"
),
name
);
}

this.nunjucksGlobals[name] = bench.add(
`"${name}" Nunjucks Global`,
globalFn
);
}

addTransform(name, callback) {
name = this.getNamespacedName(name);

Expand Down Expand Up @@ -665,6 +684,7 @@ class UserConfig {
nunjucksFilters: this.nunjucksFilters,
nunjucksAsyncFilters: this.nunjucksAsyncFilters,
nunjucksTags: this.nunjucksTags,
nunjucksGlobals: this.nunjucksGlobals,
nunjucksAsyncShortcodes: this.nunjucksAsyncShortcodes,
nunjucksShortcodes: this.nunjucksShortcodes,
nunjucksAsyncPairedShortcodes: this.nunjucksAsyncPairedShortcodes,
Expand Down
14 changes: 14 additions & 0 deletions test/TemplateConfigTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ test("Add nunjucks tag", (t) => {
t.not(Object.keys(cfg.nunjucksTags).indexOf("myNunjucksTag"), -1);
});

test("Add nunjucks global", (t) => {
eleventyConfig.reset();
eleventyConfig.addNunjucksGlobal("myNunjucksGlobal1", function () {});
eleventyConfig.addNunjucksGlobal("myNunjucksGlobal2", 42);

let templateCfg = new TemplateConfig(
require("../src/defaultConfig.js"),
"./test/stubs/config.js"
);
let cfg = templateCfg.getConfig();
t.not(Object.keys(cfg.nunjucksGlobals).indexOf("myNunjucksGlobal1"), -1);
t.not(Object.keys(cfg.nunjucksGlobals).indexOf("myNunjucksGlobal2"), -1);
});

test("Add liquid filter", (t) => {
eleventyConfig.reset();
eleventyConfig.addLiquidFilter("myFilterName", function (liquidEngine) {
Expand Down

0 comments on commit 84f8229

Please sign in to comment.