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 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 cache = this.app.bajoCache
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 && cache
281
+ const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && get && set
282
282
  if (canCache) {
283
- const item = await cache.get({ key })
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 cache.set({ key, value: text, ttl: opts.ttlDur ?? this.config.cache.ttlDur })
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
- const output = listOfWords.length > maxWords ? excerpt : content
340
- return output
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-template",
3
- "version": "2.6.0",
3
+ "version": "2.7.0",
4
4
  "description": "Template Renderer for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-05-22
4
+
5
+ - [2.7.0] Add ```getTruncated()```
6
+
7
+ ## 2026-04-25
8
+
9
+ - [2.6.1] Bug fix in ```render()```
10
+
3
11
  ## 2026-04-13
4
12
 
5
13
  - [2.6.0] Add ```_getSetting()``` template function