Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyword arguments for Liquid shortcodes. #1269

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@
"hamljs": "^0.6.2",
"handlebars": "^4.7.6",
"javascript-stringify": "^2.0.1",
"liquid-args": "^0.3.1",
"liquidjs": "^6.4.3",
"lodash": "^4.17.15",
"luxon": "^1.24.1",
"markdown-it": "^10.0.0",
"minimist": "^1.2.5",
"moo": "^0.5.1",
"multimatch": "^4.0.0",
"mustache": "^2.3.2",
"normalize-path": "^3.0.0",
Expand Down
69 changes: 20 additions & 49 deletions src/Engines/Liquid.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const moo = require("moo");
const argParser = require("liquid-args");
const LiquidLib = require("liquidjs");
const TemplateEngine = require("./TemplateEngine");
const TemplatePath = require("../TemplatePath");
Expand All @@ -13,13 +13,7 @@ class Liquid extends TemplateEngine {
this.setLibrary(this.config.libraryOverrides.liquid);
this.setLiquidOptions(this.config.liquidOptions);

this.argLexer = moo.compile({
number: /[0-9]+\.*[0-9]*/,
doubleQuoteString: /"(?:\\["\\]|[^\n"\\])*"/,
singleQuoteString: /'(?:\\['\\]|[^\n'\\])*'/,
keyword: /[a-zA-Z0-9\.\-\_]+/,
"ignore:whitespace": /[, \t]+/ // includes comma separator
});
this.argLexer = argParser;
}

setLibrary(lib) {
Expand Down Expand Up @@ -48,7 +42,7 @@ class Liquid extends TemplateEngine {
root: [super.getIncludesDir()], // overrides in compile with inputPath below
extname: ".liquid",
dynamicPartials: false,
strict_filters: false
strict_filters: false,
};

let options = Object.assign(defaults, this.liquidOptions || {});
Expand Down Expand Up @@ -97,32 +91,9 @@ class Liquid extends TemplateEngine {
}
}

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

if (typeof str === "string") {
// TODO key=value key2=value
// TODO JSON?
lexer.reset(str);
let arg = lexer.next();
while (arg) {
/*{
type: 'doubleQuoteString',
value: '"test 2"',
text: '"test 2"',
toString: [Function: tokenToString],
offset: 0,
lineBreaks: 0,
line: 1,
col: 1 }*/
if (arg.type.indexOf("ignore:") === -1) {
argArray.push(LiquidLib.evalExp(arg.value, scope)); // or evalValue
}
arg = lexer.next();
}
}

return argArray;
static parseArguments(lexer, args, scope) {
const parse = (arg) => LiquidLib.evalExp(arg, scope);
return lexer(args, parse);
}

static _normalizeShortcodeScope(scope) {
Expand All @@ -135,51 +106,51 @@ class Liquid extends TemplateEngine {

addShortcode(shortcodeName, shortcodeFn) {
let _t = this;
this.addTag(shortcodeName, function(liquidEngine) {
this.addTag(shortcodeName, function (liquidEngine) {
return {
parse: function(tagToken, remainTokens) {
parse: function (tagToken, remainTokens) {
this.name = tagToken.name;
this.args = tagToken.args;
},
render: function(scope, hash) {
render: function (scope, hash) {
let argArray = Liquid.parseArguments(_t.argLexer, this.args, scope);
return Promise.resolve(
shortcodeFn.call(
Liquid._normalizeShortcodeScope(scope),
...argArray
)
);
}
},
};
});
}

addPairedShortcode(shortcodeName, shortcodeFn) {
let _t = this;
this.addTag(shortcodeName, function(liquidEngine) {
this.addTag(shortcodeName, function (liquidEngine) {
return {
parse: function(tagToken, remainTokens) {
parse: function (tagToken, remainTokens) {
this.name = tagToken.name;
this.args = tagToken.args;
this.templates = [];

var stream = liquidEngine.parser
.parseStream(remainTokens)
.on("template", tpl => this.templates.push(tpl))
.on("tag:end" + shortcodeName, token => stream.stop())
.on("end", x => {
.on("template", (tpl) => this.templates.push(tpl))
.on("tag:end" + shortcodeName, (token) => stream.stop())
.on("end", (x) => {
throw new Error(`tag ${tagToken.raw} not closed`);
});

stream.start();
},
render: function(scope, hash) {
render: function (scope, hash) {
let argArray = Liquid.parseArguments(_t.argLexer, this.args, scope);

return new Promise((resolve, reject) => {
liquidEngine.renderer
.renderTemplates(this.templates, scope)
.then(function(html) {
.then(function (html) {
resolve(
shortcodeFn.call(
Liquid._normalizeShortcodeScope(scope),
Expand All @@ -189,7 +160,7 @@ class Liquid extends TemplateEngine {
);
});
});
}
},
};
});
}
Expand All @@ -205,10 +176,10 @@ class Liquid extends TemplateEngine {
} else {
options.root = [
super.getIncludesDir(),
TemplatePath.getDirFromFilePath(inputPath)
TemplatePath.getDirFromFilePath(inputPath),
];
}
return async function(data) {
return async function (data) {
return engine.render(tmpl, data, options);
};
}
Expand Down
Loading