deno.land / x / lume@v2.1.4 / plugins / liquid.ts

نووسراو ببینە
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { evalToken, Liquid, Tag, Tokenizer, TokenKind, toPromise, Value, ValueToken,} from "../deps/liquid.ts";import { posix } from "../deps/path.ts";import loader from "../core/loaders/text.ts";import { merge } from "../core/utils/object.ts";
import type { Data } from "../core/file.ts";import type Site from "../core/site.ts";import type { Engine, Helper, HelperOptions } from "../core/renderer.ts";import type { Context, Emitter, LiquidOptions, TagClass, TagToken, Template, TopLevelToken,} from "../deps/liquid.ts";
export interface Options { /** The list of extensions this plugin applies to */ extensions?: string[];
/** Optional sub-extension for page files */ pageSubExtension?: string;
/** * Custom includes path * @default `site.options.includes` */ includes?: string;
/** * Options passed to Liquidjs library * @see https://liquidjs.com/tutorials/options.html */ options?: LiquidOptions;}
// Default optionsexport const defaults: Options = { extensions: [".liquid"],};
/** Template engine to render Liquid files */export class LiquidEngine implements Engine { liquid: Liquid; cache = new Map<string, Template[]>(); basePath: string; includes: string;
constructor(liquid: Liquid, basePath: string, includes: string) { this.liquid = liquid; this.basePath = basePath; this.includes = includes; }
deleteCache(file: string): void { this.cache.delete(file); }
async render( content: string, data?: Record<string, unknown>, filename?: string, ) { if (!filename) { return this.liquid.parseAndRender(content, data); }
const template = this.getTemplate(content, filename); return await this.liquid.render(template, data); }
renderComponent( content: string, data?: Record<string, unknown>, filename?: string, ) { if (!filename) { return this.liquid.parseAndRenderSync(content, data); }
const template = this.getTemplate(content, filename); return this.liquid.renderSync(template, data); }
getTemplate(content: string, filename: string): Template[] { if (!this.cache.has(filename)) { this.cache.set( filename, this.liquid.parse(content, posix.join(this.basePath, filename)), ); } return this.cache.get(filename)!; }
addHelper(name: string, fn: Helper, options: HelperOptions) { switch (options.type) { case "filter": // deno-lint-ignore no-explicit-any this.liquid.registerFilter(name, function (this: any, ...args) { return fn.apply({ data: this.context.environments }, args); }); break;
case "tag": // Tag with body not supported yet if (!options.body) { this.liquid.registerTag(name, createCustomTag(fn)); } else { this.liquid.registerTag(name, createCustomTagWithBody(fn)); } break; } }}
/** Register the plugin to add support for Liquid files */export default function (userOptions?: Options) { return function (site: Site) { const options = merge( { ...defaults, includes: site.options.includes }, userOptions, );
const liquidOptions: LiquidOptions = { root: site.src(options.includes), ...options.options, };
const engine = new LiquidEngine( new Liquid(liquidOptions), site.src(), options.includes, );
// Ignore includes folder if (options.includes) { site.ignore(options.includes); }
// Load the pages and register the engine site.loadPages(options.extensions, { loader, engine, pageSubExtension: options.pageSubExtension, });
// Register the liquid filter site.filter("liquid", filter, true);
function filter( string: string, data?: Record<string, unknown>, ): Promise<string> { return engine.render(string, { ...site.scopedData.get("/"), ...data }); } };}
/** * Create a custom tag * https://liquidjs.com/tutorials/register-filters-tags.html#Register-Tags */function createCustomTag(fn: Helper): TagClass { return class extends Tag { #value: Value;
constructor( token: TagToken, remainTokens: TopLevelToken[], liquid: Liquid, ) { super(token, remainTokens, liquid); this.#value = new Value(token.args, liquid); }
async render(ctx: Context, emitter: Emitter) { const str = await toPromise(this.#value.value(ctx, false)); emitter.write(await fn.call({ data: ctx.environments as Data }, str)); } };}
/** * Create a custom tag with body * https://liquidjs.com/tutorials/register-filters-tags.html#Register-Tags */function createCustomTagWithBody(fn: Helper): TagClass { return class extends Tag { args: ValueToken[] = []; templates: Template[] = [];
constructor( token: TagToken, remainTokens: TopLevelToken[], liquid: Liquid, ) { super(token, remainTokens, liquid); const tokenizer = new Tokenizer( token.args, this.liquid.options.operators, ); const name = token.name;
while (!tokenizer.end()) { const value = tokenizer.readValue(); if (value) this.args.push(value); tokenizer.readTo(","); }
while (remainTokens.length) { const token = remainTokens.shift()!; if ( token.kind === TokenKind.Tag && (token as TagToken).name === `end${name}` ) { return; } this.templates.push(liquid.parser.parseToken(token, remainTokens)); } throw new Error(`tag ${token.getText()} not closed`); }
async render(ctx: Context, emitter: Emitter) { const r = this.liquid.renderer; const str = await toPromise(r.renderTemplates(this.templates, ctx)); const args = [str];
for (const arg of this.args) { args.push(await toPromise(evalToken(arg, ctx))); }
emitter.write(await fn.apply({ data: ctx.environments as Data }, args)); } };}
/** Extends Helpers interface */declare global { namespace Lume { export interface Helpers { /** @see https://lume.land/plugins/liquid/ */ liquid: ( string: string, data?: Record<string, unknown>, ) => Promise<string>; } }}
lume

Version Info

Tagged at
5 months ago