-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
LiquidEdge.js
48 lines (41 loc) · 1.41 KB
/
LiquidEdge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { Tokenizer, evalToken } = require("liquidjs");
function rawContentLiquidTag(liquidEngine, renderFn, tagName) {
// via https://github.com/harttle/liquidjs/blob/b5a22fa0910c708fe7881ef170ed44d3594e18f3/src/builtin/tags/raw.ts
return {
parse: function (tagToken, remainTokens) {
this.name = tagToken.name;
this.args = [];
this.tokens = [];
const tokenizer = new Tokenizer(tagToken.args);
this.args = [];
while (!tokenizer.end()) {
let value = tokenizer.readValue();
if (!value) {
break;
}
this.args.push(value);
}
var stream = liquidEngine.parser
.parseStream(remainTokens)
.on("token", (token) => {
if (token.name === "end" + tagName) stream.stop();
else this.tokens.push(token);
})
.on("end", () => {
throw new Error(`tag ${tagToken.getText()} not closed`);
});
stream.start();
},
render: async function (ctx) {
let normalizedContext = {};
if (ctx) {
normalizedContext.page = ctx.get(["page"]);
normalizedContext.eleventy = ctx.get(["eleventy"]);
}
let argArray = this.args.map((token) => evalToken(token, ctx));
let body = this.tokens.map((token) => token.getText()).join("");
return renderFn.call(normalizedContext, tagName, body, ...argArray);
},
};
}
module.exports = rawContentLiquidTag;