bajo-template 1.2.1 → 1.2.3
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 +25 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -50,6 +50,9 @@ async function factory (pkgName) {
|
|
|
50
50
|
},
|
|
51
51
|
_excerpt: (content, words) => {
|
|
52
52
|
return this.getExcerpt(content, words)
|
|
53
|
+
},
|
|
54
|
+
_dumo: (value, noPre) => {
|
|
55
|
+
return (noPre ? '' : '<pre>') + JSON.stringify(value, null, 2) + (noPre ? '' : '</pre>')
|
|
53
56
|
}
|
|
54
57
|
}
|
|
55
58
|
}
|
|
@@ -88,12 +91,16 @@ async function factory (pkgName) {
|
|
|
88
91
|
const { fs } = this.lib
|
|
89
92
|
const { breakNsPath } = this.app.bajo
|
|
90
93
|
|
|
91
|
-
const { subNs } = breakNsPath(tpl)
|
|
92
94
|
let resp
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
let subNs
|
|
96
|
+
if (path.isAbsolute(tpl)) resp = { file: tpl }
|
|
97
|
+
else {
|
|
98
|
+
subNs = breakNsPath(tpl).subNs
|
|
99
|
+
if (subNs === 'template') {
|
|
100
|
+
resp = this.resolveTemplate(tpl, opts)
|
|
101
|
+
} else if (subNs === 'partial') {
|
|
102
|
+
resp = this.resolvePartial(tpl, opts)
|
|
103
|
+
}
|
|
97
104
|
}
|
|
98
105
|
if (!resp) throw this.error('resourceNotFound%s', tpl)
|
|
99
106
|
const { file } = resp
|
|
@@ -247,21 +254,24 @@ async function factory (pkgName) {
|
|
|
247
254
|
const { upperFirst } = this.lib._
|
|
248
255
|
const cache = this.app.bajoCache
|
|
249
256
|
const key = crypto.createHash('md5').update(`${tpl}:${JSON.stringify(locals)}`).digest('hex')
|
|
250
|
-
|
|
251
|
-
const
|
|
257
|
+
let subNs
|
|
258
|
+
const isAbsolute = path.isAbsolute(tpl)
|
|
259
|
+
if (!isAbsolute) subNs = breakNsPath(tpl).subNs
|
|
260
|
+
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && cache && this.app.bajo.config.env !== 'dev'
|
|
252
261
|
if (canCache) {
|
|
253
262
|
const item = await cache.get({ key })
|
|
254
263
|
if (item) return item
|
|
255
264
|
}
|
|
256
|
-
await runHook(`${this.name}:beforeRender${upperFirst(subNs)}`, { tpl, locals, opts })
|
|
265
|
+
if (subNs) await runHook(`${this.name}:beforeRender${upperFirst(subNs)}`, { tpl, locals, opts })
|
|
257
266
|
let text = await this._render(tpl, locals, opts)
|
|
258
267
|
if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
|
|
259
|
-
await runHook(`${this.name}:afterRender${upperFirst(subNs)}`, { tpl, locals, opts, text })
|
|
268
|
+
if (subNs) await runHook(`${this.name}:afterRender${upperFirst(subNs)}`, { tpl, locals, opts, text })
|
|
260
269
|
if (canCache) await cache.set({ key, value: text, ttl: opts.cacheMaxAge ?? this.config.cache.maxAgeDur })
|
|
261
270
|
return text
|
|
262
271
|
}
|
|
263
272
|
|
|
264
273
|
resolveLayout = (item = '', opts = {}) => {
|
|
274
|
+
const { find } = this.lib._
|
|
265
275
|
const fallbackHandler = ({ file, exts, ns, subSubNs, type, theme }) => {
|
|
266
276
|
const dir = ''
|
|
267
277
|
const base = 'default'
|
|
@@ -271,11 +281,16 @@ async function factory (pkgName) {
|
|
|
271
281
|
const check = `${this.app.main.dir.pkg}/${this.name}/${type}/_${theme}`
|
|
272
282
|
file = filecheck.call(this, { dir, base, exts, check })
|
|
273
283
|
}
|
|
274
|
-
// check
|
|
284
|
+
// check main: common
|
|
275
285
|
if (!file) {
|
|
276
286
|
const check = `${this.app.main.dir.pkg}/${this.name}/${type}`
|
|
277
287
|
file = filecheck.call(this, { dir, base, exts, check })
|
|
278
288
|
}
|
|
289
|
+
if (theme && !file) {
|
|
290
|
+
const otheme = find(this.app.waibuMpa.themes, { name: theme })
|
|
291
|
+
const check = `${otheme.plugin.dir.pkg}/${this.name}/extend/${this.name}/${type}`
|
|
292
|
+
file = filecheck.call(this, { dir, base, exts, check })
|
|
293
|
+
}
|
|
279
294
|
// check fallback: common
|
|
280
295
|
if (!file) {
|
|
281
296
|
const check = `${this.app[ns].dir.pkg}/${this.name}/${subSubNs ? (subSubNs + '/') : ''}${type}`
|