deno.land / x / lume@v2.1.4 / plugins / vento.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
import { engine, FileLoader } from "../deps/vento.ts";import loader from "../core/loaders/text.ts";import { merge } from "../core/utils/object.ts";import { normalizePath } from "../core/utils/path.ts";
import type Site from "../core/site.ts";import type { Data } from "../core/file.ts";import type { Engine, Helper, HelperOptions } from "../core/renderer.ts";import type FS from "../core/fs.ts";import type { Environment, Plugin, Token } from "../deps/vento.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;
/** * Plugins to use by vento */ plugins?: Plugin[];
/** * The options for the Vento engine * @see https://vento.js.org/configuration/ */ options: { /** The name of the variable to access to the data in the templates */ dataVarname?: string;
/** Make data available on the global object instead of varName */ useWith?: boolean;
/** Whether or not to automatically XML-escape interpolations. */ autoescape?: boolean; };}
// Default optionsexport const defaults: Options = { extensions: [".vento", ".vto"], options: { dataVarname: "it", useWith: true, autoescape: false, },};
class LumeLoader extends FileLoader { fs: FS;
constructor(includes: string, fs: FS) { super(includes); this.fs = fs; }
async load(file: string) { const entry = this.fs.entries.get(normalizePath(file));
if (!entry) { throw new Error(`File not found: ${file}`); }
const data = await entry.getContent(loader);
return { source: data.content as string, data: data, }; }}
/** Template engine to render Vento files */export class VentoEngine implements Engine { engine: Environment; includes: string;
constructor(engine: Environment, includes: string) { this.engine = engine; this.includes = includes; }
deleteCache(file: string) { this.engine.cache.delete(file); }
async render( content: string, data?: Record<string, unknown>, filename?: string, ) { const result = await this.engine.runString(content, data, filename); return result.content; }
renderComponent(content: string, data?: Record<string, unknown>): string { const result = this.engine.runStringSync(content, data); return result.content; }
addHelper(name: string, fn: Helper, options: HelperOptions) { if (options.async) { this.engine.filters[name] = async function (...args: unknown[]) { return await fn.apply({ data: this.data as Data }, args); }; } else { this.engine.filters[name] = function (...args: unknown[]) { return fn.apply({ data: this.data as Data }, args); }; } }}
/** Register the plugin to support Vento files */export default function (userOptions?: Options) { return (site: Site) => { const options = merge( { ...defaults, includes: site.options.includes }, userOptions, );
const vento = engine({ includes: new LumeLoader(normalizePath(options.includes), site.fs), ...options.options, });
vento.tags.push(compTag); options.plugins?.forEach((plugin) => vento.use(plugin));
site.hooks.addVentoPlugin = (plugin: Plugin) => { vento.use(plugin); }; site.hooks.vento = (callback) => callback(vento);
const ventoEngine = new VentoEngine(vento, 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: ventoEngine, pageSubExtension: options.pageSubExtension, });
site.filter("vto", filter as Helper, true);
async function filter(string: string, data?: Record<string, unknown>) { const result = await vento.runString(string, { ...site.scopedData.get("/"), ...data, }); return result.content; } };}
/** Vento tag to render a component */function compTag( env: Environment, code: string, output: string, tokens: Token[],): string | undefined { if (!code.startsWith("comp ")) { return; }
const match = code.match( /^comp\s+([\w.]+)(?:\s+(\{.*\}))?(?:\s+(\/))?$/, );
if (!match) { throw new Error(`Invalid component tag: ${code}`); }
const [_, comp, args, closed] = match;
if (closed) { return `${output} += await comp.${comp}(${args || ""});`; }
const compiled: string[] = []; const tmpOutput = `__content_${tokens.length}`; compiled.push("{"); compiled.push(`let ${tmpOutput} = ""`); compiled.push(...env.compileTokens(tokens, tmpOutput, ["/comp"]));
if (tokens.length && (tokens[0][0] !== "tag" || tokens[0][1] !== "/comp")) { throw new Error(`Missing closing tag for component tag: ${code}`); }
tokens.shift(); compiled.push( `${output} += await comp.${comp}({...${ args || "{}" }, content: ${tmpOutput}});`, ); compiled.push("}");
return compiled.join("\n");}
lume

Version Info

Tagged at
5 months ago