Skip to content

Commit

Permalink
Normalize to generators
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 6, 2022
1 parent c1e49b5 commit 078b675
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
62 changes: 33 additions & 29 deletions src/Engines/Liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,16 @@ class Liquid extends TemplateEngine {
}
}

static async parseArguments(lexer, str, scope, engine) {
static parseArguments(lexer, str) {
let argArray = [];

if (!lexer) {
lexer = moo.compile(Liquid.argumentLexerOptions);
}

if (typeof str === "string") {
// TODO key=value key2=value
// TODO JSON?
lexer.reset(str);

let arg = lexer.next();
while (arg) {
/*{
Expand All @@ -119,13 +118,14 @@ class Liquid extends TemplateEngine {
// Push the promise into an array instead of awaiting it here.
// This forces the promises to run in order with the correct scope value for each arg.
// Otherwise they run out of order and can lead to undefined values for arguments in layout template shortcodes.
argArray.push(engine.evalValue(arg.value, scope));
// console.log( arg.value, scope, engine );
argArray.push(arg.value);
}
arg = lexer.next();
}
}

return await Promise.all(argArray);
return argArray;
}

static _normalizeShortcodeScope(ctx) {
Expand All @@ -139,26 +139,26 @@ class Liquid extends TemplateEngine {

addShortcode(shortcodeName, shortcodeFn) {
let _t = this;
this.addTag(shortcodeName, function () {
this.addTag(shortcodeName, function (liquidEngine) {
return {
parse: function (tagToken) {
parse(tagToken) {
this.name = tagToken.name;
this.args = tagToken.args;
},
render: async function (scope) {
let argArray = await Liquid.parseArguments(
_t.argLexer,
this.args,
scope,
this.liquid
);

return Promise.resolve(
shortcodeFn.call(
Liquid._normalizeShortcodeScope(scope),
...argArray
)
render: function* (ctx) {
let rawArgs = Liquid.parseArguments(_t.argLexer, this.args);
let argArray = [];
for (let arg of rawArgs) {
let b = yield liquidEngine.evalValue(arg, ctx);
argArray.push(b);
}

let ret = shortcodeFn.call(
Liquid._normalizeShortcodeScope(ctx),
...argArray
);
yield ret;
return ret;
},
};
});
Expand All @@ -168,7 +168,7 @@ class Liquid extends TemplateEngine {
let _t = this;
this.addTag(shortcodeName, function (liquidEngine) {
return {
parse: function (tagToken, remainTokens) {
parse(tagToken, remainTokens) {
this.name = tagToken.name;
this.args = tagToken.args;
this.templates = [];
Expand All @@ -184,21 +184,25 @@ class Liquid extends TemplateEngine {
stream.start();
},
render: function* (ctx) {
let argArray = yield Liquid.parseArguments(
_t.argLexer,
this.args,
ctx,
this.liquid
);
const html = yield this.liquid.renderer.renderTemplates(
let rawArgs = Liquid.parseArguments(_t.argLexer, this.args);
let argArray = [];
for (let arg of rawArgs) {
let b = yield liquidEngine.evalValue(arg, ctx);
argArray.push(b);
}

const html = yield liquidEngine.renderer.renderTemplates(
this.templates,
ctx
);
return shortcodeFn.call(

let ret = shortcodeFn.call(
Liquid._normalizeShortcodeScope(ctx),
html,
...argArray
);
yield ret;
return ret;
},
};
});
Expand Down
19 changes: 11 additions & 8 deletions src/Plugins/RenderPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function EleventyPlugin(eleventyConfig, options = {}) {

stream.start();
},
render: async function (ctx) {
render: function* (ctx) {
let normalizedContext = {};
if (ctx) {
if (opts.accessGlobalData) {
Expand All @@ -163,21 +163,24 @@ function EleventyPlugin(eleventyConfig, options = {}) {
normalizedContext.eleventy = ctx.get(["eleventy"]);
}

let argArray = await Liquid.parseArguments(
null,
this.args,
ctx,
this.liquid
);
let rawArgs = Liquid.parseArguments(null, this.args);
let argArray = [];
for (let arg of rawArgs) {
let b = yield liquidEngine.evalValue(arg, ctx);
argArray.push(b);
}

// plaintext paired shortcode content
let body = this.tokens.map((token) => token.getText()).join("");

return _renderStringShortcodeFn.call(
let ret = _renderStringShortcodeFn.call(
normalizedContext,
body,
// templateLang, data
...argArray
);
yield ret;
return ret;
},
};
}
Expand Down

0 comments on commit 078b675

Please sign in to comment.