-
Notifications
You must be signed in to change notification settings - Fork 12
/
mod.ts
164 lines (140 loc) · 3.93 KB
/
mod.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
import HandlebarsJS from "https://esm.sh/[email protected]";
import { walk } from "https://deno.land/[email protected]/fs/mod.ts";
import {
globToRegExp,
join,
normalize,
} from "https://deno.land/[email protected]/path/mod.ts";
const { readFile } = Deno;
export { HandlebarsJS };
export interface HandlebarsConfig {
baseDir: string;
extname: string;
layoutsDir: string;
partialsDir: string;
cachePartials?: boolean;
defaultLayout: string;
// deno-lint-ignore no-explicit-any
helpers: any;
// deno-lint-ignore no-explicit-any
compilerOptions: any;
}
const DEFAULT_HANDLEBARS_CONFIG: HandlebarsConfig = {
baseDir: "views",
extname: ".hbs",
layoutsDir: "layouts/",
partialsDir: "partials/",
cachePartials: true,
defaultLayout: "main",
helpers: undefined,
compilerOptions: undefined,
};
function getNormalizePath(path: string) {
return normalize(path).replace(/\\/g, "/");
}
export class Handlebars {
#havePartialsBeenRegistered = false;
constructor(private config: HandlebarsConfig = DEFAULT_HANDLEBARS_CONFIG) {
this.config = { ...DEFAULT_HANDLEBARS_CONFIG, ...config };
if (this.config.helpers) {
const helperKeys = Object.keys(this.config.helpers);
for (let i = 0; i < helperKeys.length; i++) {
const helperKey = helperKeys[i];
// deno-lint-ignore no-explicit-any
(HandlebarsJS as any).registerHelper(
helperKey,
this.config.helpers[helperKey],
);
}
}
}
/**
* Processes of rendering view
*
* @param view
* @param context
* @param layout
*/
public async renderView(
view: string,
context?: Record<string, unknown>,
layout?: string,
): Promise<string> {
if (!view) {
console.warn("View is null");
return "";
}
const config: HandlebarsConfig = this.config as HandlebarsConfig;
if (!config.cachePartials || !this.#havePartialsBeenRegistered) {
await this.registerPartials();
}
const path = join(config.baseDir, view + config.extname);
const body: string = await this.render(path, context);
layout = (layout as string) || config.defaultLayout;
if (layout) {
const layoutPath: string = join(
config.baseDir,
config.layoutsDir,
layout + config.extname,
);
return this.render(layoutPath, { ...context, body });
}
return body;
}
/**
* Processes on render without partials and layouts
*/
public async render(
path: string,
context?: Record<string, unknown>,
): Promise<string> {
// TODO: use cashe
const source: string = new TextDecoder().decode(await readFile(path));
// deno-lint-ignore no-explicit-any
const template = (HandlebarsJS as any).compile(
source,
this.config!.compilerOptions,
);
return template(context);
}
/**
* Processes on register partials
*/
private async registerPartials() {
const paths = await this.getTemplatesPath(
join(this.config.baseDir, this.config.partialsDir),
);
if (paths) {
for (const path of paths) {
const templateName: string = path
.replace(
getNormalizePath(this.config.baseDir) + "/" +
this.config!.partialsDir,
"",
)
.replace(new RegExp(this.config!.extname + "$"), "");
const source: string = new TextDecoder().decode(await readFile(path));
// deno-lint-ignore no-explicit-any
(HandlebarsJS as any).registerPartial(templateName, source);
}
}
this.#havePartialsBeenRegistered = true;
}
/**
* Gets template pathes with glob match
*/
private async getTemplatesPath(path: string): Promise<string[]> {
const arr: string[] = [];
for await (
const w of walk(
path,
{ match: [globToRegExp("**/*" + this.config!.extname)] },
)
) {
if (w.isFile) {
arr.push(getNormalizePath(w.path));
}
}
return arr;
}
}