Skip to content

Commit

Permalink
A bit more shortcode normalization cleanup for #1522
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 14, 2022
1 parent 44a48cb commit 0b549b0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
49 changes: 25 additions & 24 deletions src/Engines/Liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ class Liquid extends TemplateEngine {
return options;
}

static wrapFilter(fn) {
return function (...args) {
if (this.context && "get" in this.context) {
this.page = this.context.get(["page"]);
this.eleventy = this.context.get(["eleventy"]);
}

return fn.call(this, ...args);
};
}

// Shortcodes
static normalizeScope(context) {
let obj = {};
if (context) {
obj.ctx = context;
obj.page = context.get(["page"]);
obj.eleventy = context.get(["eleventy"]);
}
return obj;
}

addCustomTags(tags) {
for (let name in tags) {
this.addTag(name, tags[name]);
Expand All @@ -69,17 +91,6 @@ class Liquid extends TemplateEngine {
this.liquidLib.registerFilter(name, Liquid.wrapFilter(filter));
}

static wrapFilter(fn) {
return function (...args) {
if (this.context && "get" in this.context) {
this.page = this.context.get(["page"]);
this.eleventy = this.context.get(["eleventy"]);
}

return fn.call(this, ...args);
};
}

addTag(name, tagFn) {
let tagObj;
if (typeof tagFn === "function") {
Expand Down Expand Up @@ -139,15 +150,6 @@ class Liquid extends TemplateEngine {
return argArray;
}

static _normalizeShortcodeScope(ctx) {
let obj = {};
if (ctx) {
obj.page = ctx.get(["page"]);
obj.eleventy = ctx.get(["eleventy"]);
}
return obj;
}

addShortcode(shortcodeName, shortcodeFn) {
let _t = this;
this.addTag(shortcodeName, function (liquidEngine) {
Expand All @@ -156,7 +158,7 @@ class Liquid extends TemplateEngine {
this.name = tagToken.name;
this.args = tagToken.args;
},
render: function* (ctx, emitter) {
render: function* (ctx) {
let rawArgs = Liquid.parseArguments(_t.argLexer, this.args);
let argArray = [];
let contextScope = ctx.getAll();
Expand All @@ -166,10 +168,9 @@ class Liquid extends TemplateEngine {
}

let ret = yield shortcodeFn.call(
Liquid._normalizeShortcodeScope(ctx),
Liquid.normalizeScope(ctx),
...argArray
);
// emitter.write(ret);
return ret;
},
};
Expand Down Expand Up @@ -210,7 +211,7 @@ class Liquid extends TemplateEngine {
);

let ret = yield shortcodeFn.call(
Liquid._normalizeShortcodeScope(ctx),
Liquid.normalizeScope(ctx),
html,
...argArray
);
Expand Down
39 changes: 21 additions & 18 deletions src/Engines/Nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ class Nunjucks extends TemplateEngine {
};
}

// Shortcodes
static normalizeContext(context) {
let obj = {};
if (context.ctx) {
obj.ctx = context.ctx;

if (context.ctx.page) {
obj.page = context.ctx.page;
}

if (context.ctx.eleventy) {
obj.eleventy = context.ctx.eleventy;
}
}
return obj;
}

addCustomTags(tags) {
for (let name in tags) {
this.addTag(name, tags[name]);
Expand Down Expand Up @@ -155,20 +172,6 @@ class Nunjucks extends TemplateEngine {
}
}

static _normalizeShortcodeContext(context) {
let obj = {};
if (context.ctx) {
obj.ctx = context.ctx;
if (context.ctx.page) {
obj.page = context.ctx.page;
}
if (context.ctx.eleventy) {
obj.eleventy = context.ctx.eleventy;
}
}
return obj;
}

_getShortcodeFn(shortcodeName, shortcodeFn, isAsync = false) {
return function ShortcodeFunction() {
this.tags = [shortcodeName];
Expand Down Expand Up @@ -203,7 +206,7 @@ class Nunjucks extends TemplateEngine {

if (isAsync) {
shortcodeFn
.call(Nunjucks._normalizeShortcodeContext(context), ...argArray)
.call(Nunjucks.normalizeContext(context), ...argArray)
.then(function (returnValue) {
resolve(
null,
Expand All @@ -223,7 +226,7 @@ class Nunjucks extends TemplateEngine {
} else {
try {
let ret = shortcodeFn.call(
Nunjucks._normalizeShortcodeContext(context),
Nunjucks.normalizeContext(context),
...argArray
);
return new NunjucksLib.runtime.SafeString("" + ret);
Expand Down Expand Up @@ -274,7 +277,7 @@ class Nunjucks extends TemplateEngine {
if (isAsync) {
shortcodeFn
.call(
Nunjucks._normalizeShortcodeContext(context),
Nunjucks.normalizeContext(context),
bodyContent,
...argArray
)
Expand All @@ -297,7 +300,7 @@ class Nunjucks extends TemplateEngine {
null,
new NunjucksLib.runtime.SafeString(
shortcodeFn.call(
Nunjucks._normalizeShortcodeContext(context),
Nunjucks.normalizeContext(context),
bodyContent,
...argArray
)
Expand Down

0 comments on commit 0b549b0

Please sign in to comment.