bajo-template 2.5.4 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +20 -12
- package/package.json +1 -1
- package/wiki/CHANGES.md +9 -0
package/index.js
CHANGED
|
@@ -31,16 +31,16 @@ async function factory (pkgName) {
|
|
|
31
31
|
this.loopDetector = {}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
buildCompileImports = (
|
|
34
|
+
buildCompileImports = (locals = {}, req = {}) => {
|
|
35
35
|
const _ = this.app.lib._
|
|
36
36
|
return {
|
|
37
37
|
_,
|
|
38
38
|
_t: (text, ...args) => {
|
|
39
|
-
const params = [...args, { lang }]
|
|
39
|
+
const params = [...args, { lang: req.lang }]
|
|
40
40
|
return this.t(text, ...params)
|
|
41
41
|
},
|
|
42
42
|
_format: (val, type, opts = {}) => {
|
|
43
|
-
opts.lang = opts.lang ?? lang
|
|
43
|
+
opts.lang = opts.lang ?? req.lang
|
|
44
44
|
return this.app.bajo.format(val, type, opts)
|
|
45
45
|
},
|
|
46
46
|
_findRoute: (input) => {
|
|
@@ -63,6 +63,14 @@ async function factory (pkgName) {
|
|
|
63
63
|
},
|
|
64
64
|
_dump: (value, noPre) => {
|
|
65
65
|
return (noPre ? '' : '<pre>') + JSON.stringify(value, null, 2) + (noPre ? '' : '</pre>')
|
|
66
|
+
},
|
|
67
|
+
_getSetting: (key, defValue) => {
|
|
68
|
+
if (!this.app.waibu) return _.get(locals, `schema.view.${key}`, defValue)
|
|
69
|
+
const cfg = this.app.waibu.getSetting(key, { defValue, req })
|
|
70
|
+
const { path } = this.app.bajo.breakNsPath(key)
|
|
71
|
+
const paths = path.replaceAll('/', '.').split('.')
|
|
72
|
+
if (paths[0] === '') paths.shift()
|
|
73
|
+
return _.get(locals, `schema.view.${paths.join('.')}`, cfg)
|
|
66
74
|
}
|
|
67
75
|
}
|
|
68
76
|
}
|
|
@@ -137,7 +145,7 @@ async function factory (pkgName) {
|
|
|
137
145
|
const { breakNsPath } = this.app.bajo
|
|
138
146
|
const start = '<!-- include '
|
|
139
147
|
const end = ' -->'
|
|
140
|
-
const imports = this.buildCompileImports(opts.
|
|
148
|
+
const imports = this.buildCompileImports(locals, opts.req)
|
|
141
149
|
while (content.includes(start) && content.includes(end)) {
|
|
142
150
|
const { pattern, result: rsc } = extractText(content, start, end)
|
|
143
151
|
if (!isEmpty(rsc)) {
|
|
@@ -170,7 +178,7 @@ async function factory (pkgName) {
|
|
|
170
178
|
_renderString = async (content, locals = {}, opts = {}) => {
|
|
171
179
|
const { merge, without, isString, omit, kebabCase, get } = this.app.lib._
|
|
172
180
|
if (opts.ext === '.md' && this.app.bajoMarkdown) {
|
|
173
|
-
content = await this.compile(content, locals, {
|
|
181
|
+
content = await this.compile(content, locals, { ttl: -1, req: opts.req }) // markdown can't process template tags, hence preprocess here
|
|
174
182
|
content = this.app.bajoMarkdown.parse(content)
|
|
175
183
|
}
|
|
176
184
|
let layout
|
|
@@ -207,7 +215,7 @@ async function factory (pkgName) {
|
|
|
207
215
|
const fullTitle = usePluginTitle ? `${locals.page.title} - ${this.app.waibuMpa.getPluginTitle(locals.page.ns, opts.lang)}` : locals.page.title
|
|
208
216
|
locals.page.fullTitle = locals.page.fullTitle ?? fullTitle
|
|
209
217
|
}
|
|
210
|
-
content = await this.compile(content, locals, {
|
|
218
|
+
content = await this.compile(content, locals, { ttl: this.config.cache.maxAgeDu, req: opts.req })
|
|
211
219
|
return await this._handleInclude(content, locals, opts)
|
|
212
220
|
}
|
|
213
221
|
|
|
@@ -238,12 +246,12 @@ async function factory (pkgName) {
|
|
|
238
246
|
return parseObject(success, { parseValue: false, lang }) ?? {}
|
|
239
247
|
}
|
|
240
248
|
|
|
241
|
-
compile = async (content, locals, {
|
|
249
|
+
compile = async (content, locals, { ttl = 0, req = {} } = {}) => {
|
|
242
250
|
const { get: getCache, set: setCache } = this.app.bajoCache ?? {}
|
|
243
251
|
const { template } = this.app.lib._
|
|
244
252
|
locals.attr = locals.attr ?? {}
|
|
245
253
|
const opts = {
|
|
246
|
-
imports: this.buildCompileImports(
|
|
254
|
+
imports: this.buildCompileImports(locals, req)
|
|
247
255
|
}
|
|
248
256
|
const key = 'fn:' + crypto.createHash('md5').update(content).digest('hex')
|
|
249
257
|
let item
|
|
@@ -265,21 +273,21 @@ async function factory (pkgName) {
|
|
|
265
273
|
render = async (tpl, locals = {}, opts = {}) => {
|
|
266
274
|
const { runHook, breakNsPath } = this.app.bajo
|
|
267
275
|
const { upperFirst } = this.app.lib._
|
|
268
|
-
const
|
|
276
|
+
const { get, set } = this.app.bajoCache ?? {}
|
|
269
277
|
const key = crypto.createHash('md5').update(`${tpl}:${JSON.stringify(locals)}`).digest('hex')
|
|
270
278
|
let subNs
|
|
271
279
|
const isAbsolute = path.isAbsolute(tpl)
|
|
272
280
|
if (!isAbsolute) subNs = breakNsPath(tpl).subNs
|
|
273
|
-
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false &&
|
|
281
|
+
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && get && set
|
|
274
282
|
if (canCache) {
|
|
275
|
-
const item = await
|
|
283
|
+
const item = await get({ key })
|
|
276
284
|
if (item) return item
|
|
277
285
|
}
|
|
278
286
|
if (subNs) await runHook(`${this.ns}:beforeRender${upperFirst(subNs)}`, { tpl, locals, opts })
|
|
279
287
|
let text = await this._render(tpl, locals, opts)
|
|
280
288
|
if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
|
|
281
289
|
if (subNs) await runHook(`${this.ns}:afterRender${upperFirst(subNs)}`, { tpl, locals, opts, text })
|
|
282
|
-
if (canCache) await
|
|
290
|
+
if (canCache) await set({ key, value: text, ttl: opts.ttlDur ?? this.config.cache.ttlDur })
|
|
283
291
|
return text
|
|
284
292
|
}
|
|
285
293
|
|
package/package.json
CHANGED
package/wiki/CHANGES.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 2026-04-25
|
|
4
|
+
|
|
5
|
+
- [2.6.1] Bug fix in ```render()```
|
|
6
|
+
|
|
7
|
+
## 2026-04-13
|
|
8
|
+
|
|
9
|
+
- [2.6.0] Add ```_getSetting()``` template function
|
|
10
|
+
- [2.6.0] ```buildCompileImports()``` now accept ```locals``` and ```req``` as its parameters
|
|
11
|
+
|
|
3
12
|
## 2026-03-25
|
|
4
13
|
|
|
5
14
|
- [2.5.3] Bug fix in cache handling for template function
|