/
eta.ts
118 lines (97 loc) 路 3.04 KB
/
eta.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
import { Site } from "../core.ts";
import * as eta from "../deps/eta.ts";
import { posix } from "../deps/path.ts";
import loader from "../core/loaders/text.ts";
import { merge } from "../core/utils.ts";
import type { Data, Engine, Helper, HelperOptions } from "../core.ts";
import type { EtaConfig } from "../deps/eta.ts";
export interface Options {
/** The list of extensions this plugin applies to */
extensions: string[] | {
pages: string[];
components: string[];
};
/** Custom includes path */
includes: string;
/** Configuration to pass to Eta */
options: Partial<EtaConfig>;
}
// Default options
export const defaults: Options = {
extensions: [".eta"],
includes: "",
options: {
useWith: true,
},
};
/** Template engine to render Eta files */
export class EtaEngine implements Engine {
engine: typeof eta;
filters: Record<string, Helper> = {};
basePath: string;
constructor(engine: typeof eta, basePath: string) {
this.engine = engine;
this.basePath = basePath;
}
deleteCache(file: string): void {
this.engine.templates.remove(posix.join(this.basePath, file));
}
render(content: string, data: Data, filename: string) {
const template = this.getTemplate(content, filename);
data.filters = this.filters;
return template(data, this.engine.config);
}
renderSync(content: string, data: Data, filename: string): string {
const template = this.getTemplate(content, filename, { async: false });
data.filters = this.filters;
return template(data, this.engine.config);
}
getTemplate(content: string, filename: string, options?: Partial<EtaConfig>) {
filename = posix.join(this.basePath, filename);
if (!this.engine.templates.get(filename)) {
this.engine.templates.define(
filename,
this.engine.compile(
content,
this.engine.getConfig({ filename, ...options }),
),
);
}
return this.engine.templates.get(filename)!;
}
addHelper(name: string, fn: Helper, options: HelperOptions) {
switch (options.type) {
case "filter":
this.filters[name] = fn;
if (options.async) {
this.engine.configure({ async: true });
}
return;
}
}
}
/** Register the plugin to use Eta as a template engine */
export default function (userOptions?: Partial<Options>) {
return (site: Site) => {
const options = merge(
{ ...defaults, includes: site.options.includes },
userOptions,
);
// Configure Eta
eta.configure({
...options.options,
views: [
site.src(options.includes),
site.src(),
],
});
const extensions = Array.isArray(options.extensions)
? { pages: options.extensions, components: options.extensions }
: options.extensions;
const engine = new EtaEngine(eta, site.src());
site.loadPages(extensions.pages, loader, engine);
site.includes(extensions.pages, options.includes);
site.includes(extensions.components, options.includes);
site.loadComponents(extensions.components, loader, engine);
};
}