deno.land / x / lume@v2.1.4 / plugins / pug.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
import { compile } from "../deps/pug.ts";import { join } from "../deps/path.ts";import loader from "../core/loaders/text.ts";import { merge } from "../core/utils/object.ts";
import type Site from "../core/site.ts";import type { Engine, Helper, HelperOptions } from "../core/renderer.ts";import type { Options as PugOptions } from "../deps/pug.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 Pug * @see https://pugjs.org/api/reference.html#options */ options?: PugOptions;}
// Default optionsexport const defaults: Options = { extensions: [".pug"], options: {},};
type Compiler = typeof compile;
/** Template engine to render Pug files */export class PugEngine implements Engine { options: PugOptions; compiler: Compiler; filters: Record<string, Helper> = {}; cache = new Map<string, (data?: Record<string, unknown>) => string>(); basePath: string; includes: string;
constructor( compiler: Compiler, basePath: string, includes: string, options: PugOptions = {}, ) { this.compiler = compiler; this.options = options; this.basePath = basePath; this.includes = includes; }
deleteCache(): void { this.cache.clear(); }
render( content: string, data?: Record<string, unknown>, filename?: string, ): string { return this.renderComponent(content, data, filename); }
renderComponent( content: string, data?: Record<string, unknown>, filename?: string, ): string { const dataWithFilters = { ...data, filters: { ...data?.filters || {}, ...this.filters, }, };
if (!filename) { return this.compiler(content, this.options)(dataWithFilters); } if (!this.cache.has(filename)) { this.cache.set( filename, this.compiler(content, { ...this.options, filename: join(this.basePath, filename), }), ); }
return this.cache.get(filename)!(dataWithFilters); }
addHelper(name: string, fn: Helper, options: HelperOptions) { switch (options.type) { case "filter": { this.options.filters ||= {};
const filter: Helper = function ( text: string, opt: Record<string, unknown>, ) { delete opt.filename; const args = Object.values(opt); return fn(text, ...args); };
this.filters[name] = fn; this.options.filters[name] = filter; return; } } }}
/** Register the plugin to use Pug as a template engine */export default function (userOptions?: Options) { return (site: Site) => { const options = merge( { ...defaults, includes: site.options.includes }, userOptions, ); options.options.basedir = site.src(options.includes);
const engine = new PugEngine( compile, site.src(), options.includes, options.options, );
// Ignore includes folder if (options.includes) { site.ignore(options.includes); }
// Load the pages and register the engine site.loadPages(options.extensions, { loader, engine, });
// Register the pug filter site.filter("pug", filter);
function filter(string: string, data?: Record<string, unknown>): string { return engine.render(string, { ...site.scopedData.get("/"), ...data }); } };}
/** Extends Helpers interface */declare global { namespace Lume { export interface Helpers { /** @see https://lume.land/plugins/pug/ */ pug: (string: string, data?: Record<string, unknown>) => string; } }}
lume

Version Info

Tagged at
6 months ago