bajo-template 2.6.0 → 2.7.0
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 +11 -7
- package/package.json +1 -1
- package/wiki/CHANGES.md +8 -0
package/index.js
CHANGED
|
@@ -273,21 +273,21 @@ async function factory (pkgName) {
|
|
|
273
273
|
render = async (tpl, locals = {}, opts = {}) => {
|
|
274
274
|
const { runHook, breakNsPath } = this.app.bajo
|
|
275
275
|
const { upperFirst } = this.app.lib._
|
|
276
|
-
const
|
|
276
|
+
const { get, set } = this.app.bajoCache ?? {}
|
|
277
277
|
const key = crypto.createHash('md5').update(`${tpl}:${JSON.stringify(locals)}`).digest('hex')
|
|
278
278
|
let subNs
|
|
279
279
|
const isAbsolute = path.isAbsolute(tpl)
|
|
280
280
|
if (!isAbsolute) subNs = breakNsPath(tpl).subNs
|
|
281
|
-
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false &&
|
|
281
|
+
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && get && set
|
|
282
282
|
if (canCache) {
|
|
283
|
-
const item = await
|
|
283
|
+
const item = await get({ key })
|
|
284
284
|
if (item) return item
|
|
285
285
|
}
|
|
286
286
|
if (subNs) await runHook(`${this.ns}:beforeRender${upperFirst(subNs)}`, { tpl, locals, opts })
|
|
287
287
|
let text = await this._render(tpl, locals, opts)
|
|
288
288
|
if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
|
|
289
289
|
if (subNs) await runHook(`${this.ns}:afterRender${upperFirst(subNs)}`, { tpl, locals, opts, text })
|
|
290
|
-
if (canCache) await
|
|
290
|
+
if (canCache) await set({ key, value: text, ttl: opts.ttlDur ?? this.config.cache.ttlDur })
|
|
291
291
|
return text
|
|
292
292
|
}
|
|
293
293
|
|
|
@@ -332,12 +332,16 @@ async function factory (pkgName) {
|
|
|
332
332
|
}
|
|
333
333
|
|
|
334
334
|
// based on: https://medium.com/@paulohfev/problem-solving-how-to-create-an-excerpt-fdb048687928
|
|
335
|
-
getExcerpt = (content, maxWords = 50, trailChars = '...') => {
|
|
335
|
+
getExcerpt = (content = '', maxWords = 50, trailChars = '...') => {
|
|
336
336
|
const listOfWords = content.trim().split(' ')
|
|
337
337
|
const truncatedContent = listOfWords.slice(0, maxWords).join(' ')
|
|
338
338
|
const excerpt = truncatedContent + trailChars
|
|
339
|
-
|
|
340
|
-
|
|
339
|
+
return listOfWords.length > maxWords ? excerpt : content
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
getTruncated = (content = '', maxChars = 50, trailChars = '...') => {
|
|
343
|
+
const truncated = content.slice(0, maxChars)
|
|
344
|
+
return truncated.length !== content.length ? (truncated + trailChars) : content
|
|
341
345
|
}
|
|
342
346
|
}
|
|
343
347
|
|
package/package.json
CHANGED