bajo-template 2.8.0 → 2.9.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 CHANGED
@@ -1,36 +1,67 @@
1
- import resolveResource, { checking } from './lib/resolve-resource.js'
2
1
  import crypto from 'crypto'
3
- import path from 'path'
2
+ import _path from 'path'
3
+
4
+ const cache = {}
4
5
 
5
6
  /**
6
- * Plugin factory
7
+ * Plugin factory.
8
+ *
9
+ * **Never** call this function directly!!! It's only-meant to be called by the {@link https://ardhi.github.io/bajo|Bajo framework} during plugin initialization.
7
10
  *
11
+ * @async
8
12
  * @param {string} pkgName - NPM package name
9
- * @returns {class}
13
+ * @returns {Promise<class>} Promise that resolves to the BajoTemplate class
10
14
  */
11
15
  async function factory (pkgName) {
12
16
  const me = this
13
17
 
14
18
  /**
15
- * BajoTemplate class
19
+ * BajoTemplate class definition.
16
20
  *
17
- * @class
21
+ * @class BajoTemplate
18
22
  */
19
23
  class BajoTemplate extends this.app.baseClass.Base {
24
+ /**
25
+ * Constructor
26
+ */
20
27
  constructor () {
21
28
  super(pkgName, me.app)
29
+ /**
30
+ * @property {object} config - Configuration object
31
+ * @property {object} [config.layout={}] - Layout configuration
32
+ * @property {boolean} [config.layout.fallback=true] - Whether to fallback to default layout if not found
33
+ * @property {Number|string} [config.loopDetectorDur='1m'] - Duration for loop detection
34
+ * @property {object} [config.log={}] - Logging configuration
35
+ * @property {boolean} [config.log.resolver=false] - Whether to log resolver actions
36
+ * @property {object} [config.cache={}] - Cache configuration
37
+ * @property {Number|string} [config.cache.ttlDur='1s'] - Time-to-live duration for cache
38
+ */
22
39
  this.config = {
23
40
  layout: {
24
41
  fallback: true
25
42
  },
26
43
  loopDetectorDur: '1m',
44
+ log: {
45
+ resolver: false
46
+ },
27
47
  cache: {
28
48
  ttlDur: '1s'
29
49
  }
30
50
  }
51
+ /**
52
+ * @property {object} loopDetector - Loop detection object
53
+ */
31
54
  this.loopDetector = {}
32
55
  }
33
56
 
57
+ /**
58
+ * Build compile imports. Used to provide utility functions to templates.
59
+ *
60
+ * @method
61
+ * @param {object} locals - Locals object
62
+ * @param {object} req - Request object
63
+ * @returns {TImportsResult} Imports object
64
+ */
34
65
  buildCompileImports = (locals = {}, req = {}) => {
35
66
  const _ = this.app.lib._
36
67
  return {
@@ -75,6 +106,12 @@ async function factory (pkgName) {
75
106
  }
76
107
  }
77
108
 
109
+ /**
110
+ * Clear loop detector by removing entries that have expired based on the configured duration.
111
+ *
112
+ * @method
113
+ * @private
114
+ */
78
115
  _clearLoopDetector = () => {
79
116
  const { omit } = this.app.lib._
80
117
  const now = Date.now()
@@ -86,11 +123,20 @@ async function factory (pkgName) {
86
123
  this.loopDetector = omit(this.loopDetector, omitted)
87
124
  }
88
125
 
126
+ /**
127
+ * Detect loop in template rendering.
128
+ *
129
+ * @method
130
+ * @private
131
+ * @param {string} tpl - Template name
132
+ * @param {string} file - File path
133
+ * @param {object} opts - Options object
134
+ */
89
135
  _detectLoop = (tpl, file, opts) => {
90
136
  const { last } = this.app.lib._
91
137
  if (opts.groupId) {
92
138
  if (this.loopDetector[opts.groupId]) {
93
- if (last(this.loopDetector[opts.groupId].file) === file && path.basename(file)[0] !== '~') {
139
+ if (last(this.loopDetector[opts.groupId].file) === file && _path.basename(file)[0] !== '~') {
94
140
  throw this.error('loopDetected%s%s', tpl, file)
95
141
  }
96
142
  this.loopDetector[opts.groupId].file.push(file)
@@ -103,6 +149,17 @@ async function factory (pkgName) {
103
149
  }
104
150
  }
105
151
 
152
+ /**
153
+ * Render a template.
154
+ *
155
+ * @async
156
+ * @method
157
+ * @private
158
+ * @param {string} tpl - Template name
159
+ * @param {object} locals - Locals object
160
+ * @param {object} opts - Options object
161
+ * @returns {Promise<string>} Rendered content
162
+ */
106
163
  _render = async (tpl, locals = {}, opts = {}) => {
107
164
  this._clearLoopDetector()
108
165
  const { trim, isEmpty, last } = this.app.lib._
@@ -111,7 +168,7 @@ async function factory (pkgName) {
111
168
 
112
169
  let resp
113
170
  let subNs
114
- if (path.isAbsolute(tpl)) resp = { file: tpl }
171
+ if (_path.isAbsolute(tpl)) resp = { file: tpl }
115
172
  else {
116
173
  subNs = breakNsPath(tpl).subNs
117
174
  if (subNs === 'template') {
@@ -127,18 +184,29 @@ async function factory (pkgName) {
127
184
  let { content, frontMatter } = this.splitContent(fileContent)
128
185
  if (isEmpty(content)) {
129
186
  const sep = '/waibuMpa/route/'
130
- if (path.isAbsolute(tpl) && tpl.includes(sep)) { // for direct waibuMpa's route
187
+ if (_path.isAbsolute(tpl) && tpl.includes(sep)) { // for direct waibuMpa's route
131
188
  const parts = tpl.split(sep)
132
189
  const ns = last(parts[0].split('/'))
133
190
  content = `<!-- include ${ns}.partial:/${parts[1]} -->`
134
191
  } else content = '<!-- include ' + tpl.replace('.template', '.partial') + ' -->'
135
192
  }
136
- opts.ext = path.extname(file)
193
+ opts.ext = _path.extname(file)
137
194
  opts.frontMatter = frontMatter
138
195
  opts.partial = opts.partial ?? subNs === 'partial'
139
196
  return await this._renderString(content, locals, opts)
140
197
  }
141
198
 
199
+ /**
200
+ * Handle include directives in the content.
201
+ *
202
+ * @async
203
+ * @method
204
+ * @private
205
+ * @param {string} content - Content string
206
+ * @param {object} locals - Locals object
207
+ * @param {object} opts - Options object
208
+ * @returns {Promise<string>} Processed content
209
+ */
142
210
  _handleInclude = async (content, locals = {}, opts = {}) => {
143
211
  const { isEmpty, omit, template, merge } = this.app.lib._
144
212
  const { extractText } = this.app.lib.aneka
@@ -175,6 +243,17 @@ async function factory (pkgName) {
175
243
  return content
176
244
  }
177
245
 
246
+ /**
247
+ * Render a string with the given locals and options.
248
+ *
249
+ * @async
250
+ * @method
251
+ * @private
252
+ * @param {string} content - Content string
253
+ * @param {object} locals - Locals object
254
+ * @param {object} opts - Options object
255
+ * @returns {Promise<string>} Rendered content
256
+ */
178
257
  _renderString = async (content, locals = {}, opts = {}) => {
179
258
  const { merge, without, isString, omit, kebabCase, get } = this.app.lib._
180
259
  if (opts.ext === '.md' && this.app.bajoMarkdown) {
@@ -192,7 +271,7 @@ async function factory (pkgName) {
192
271
  for (const b of ['scriptBlock', 'styleBlock']) {
193
272
  locals.page[b] = pageFm[b] ?? opts[b] ?? (locals.page.ns ? `${locals.page.ns}.partial:/${kebabCase(b)}.html` : `bajoTemplate.partial:/${kebabCase(b)}.html`)
194
273
  }
195
- const ext = path.extname(layout)
274
+ const ext = _path.extname(layout)
196
275
  const { file } = this.resolveLayout(layout, opts)
197
276
  let { content: layoutContent, frontMatter: layoutFm } = this.splitContent(file, true)
198
277
  layoutFm = await this.parseFrontMatter(layoutFm, opts.lang)
@@ -219,6 +298,84 @@ async function factory (pkgName) {
219
298
  return await this._handleInclude(content, locals, opts)
220
299
  }
221
300
 
301
+ /**
302
+ * Find a file based on the given options.
303
+ *
304
+ * @method
305
+ * @private
306
+ * @param {object} options - Options object
307
+ * @param {string} options.type - Type of the file
308
+ * @param {string} options.ns - Namespace
309
+ * @param {string} options.subSubNs - Sub-sub namespace
310
+ * @param {string} options.dir - Directory
311
+ * @param {string} options.base - Base name
312
+ * @param {string[]} options.exts - Array of extensions
313
+ * @param {object} options.theme - Theme object
314
+ * @param {object} options.req - Request object
315
+ * @returns {string|null} - Found file path or null
316
+ */
317
+ _findFile = (options = {}) => {
318
+ const { type, ns, subSubNs, dir, base, exts, theme, req } = options
319
+ const checking = ({ check, info }) => {
320
+ const { fs } = this.app.lib
321
+ let file
322
+ for (const ext of exts) {
323
+ let path = `${check}/${dir}/${base}${ext}`
324
+ if (['', '.'].includes(dir)) path = `${check}/${base}${ext}`
325
+ if (fs.existsSync(path)) {
326
+ file = path
327
+ break
328
+ }
329
+ }
330
+ if (this.config.log.resolver) {
331
+ this.log.trace('Checking %s for "%s" -> %s', info, base, check)
332
+ if (file) this.log.trace('OK: %s', file)
333
+ else this.log.trace('Failed')
334
+ }
335
+ return file
336
+ }
337
+
338
+ const extension = subSubNs ? `${subSubNs}/` : ''
339
+ let file
340
+ if (this.app.waibu && req) {
341
+ const hostname = this.app.waibu.getHostname(req)
342
+ const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${hostname}/${ns}/${extension}${type}`
343
+ file = checking({ check, info: '1. main override: domain specific' })
344
+ }
345
+ if (theme && !file) {
346
+ const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${theme.name}/${ns}/${extension}${type}`
347
+ file = checking({ check, info: '2. main override: theme specific' })
348
+ }
349
+ if (!file) {
350
+ const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${ns}/${extension}${type}`
351
+ file = checking({ check, info: '3. main override: common' })
352
+ }
353
+ if (theme && !file) {
354
+ const check = `${theme.plugin.dir.pkg}/extend/${this.ns}/${theme.name}/${ns}/${extension}${type}`
355
+ file = checking({ check, info: '4. override: theme specific' })
356
+ }
357
+ if (theme && !file) {
358
+ const check = `${theme.plugin.dir.pkg}/extend/${this.ns}/_common/${ns}/${extension}${type}`
359
+ file = checking({ check, info: '5. override: theme specific (common)' })
360
+ }
361
+ if (theme && !file) {
362
+ const check = `${this.app[ns].dir.pkg}/extend/${this.ns}/${theme.name}/${extension}${type}`
363
+ file = checking({ check, info: '6. real: theme specific' })
364
+ }
365
+ if (!file) {
366
+ const check = `${this.app[ns].dir.pkg}/extend/${this.ns}/${extension}${type}`
367
+ file = checking({ check, info: '7. real: common' })
368
+ }
369
+ return file
370
+ }
371
+
372
+ /**
373
+ * Get a resource by name.
374
+ *
375
+ * @method
376
+ * @param {string} name - Resource name
377
+ * @returns {object} Resource object
378
+ */
222
379
  getResource = (name) => {
223
380
  const subNses = ['layout', 'template', 'partial']
224
381
  const { ns, path, subNs, subSubNs, qs } = this.app.bajo.breakNsPath(name)
@@ -229,23 +386,36 @@ async function factory (pkgName) {
229
386
  return { ns, subNs, subSubNs, path, qs, fullPath }
230
387
  }
231
388
 
389
+ /**
390
+ * Parse front matter from the input string.
391
+ *
392
+ * @async
393
+ * @method
394
+ * @param {string} input - Input string
395
+ * @param {string} lang - Language
396
+ * @returns {Promise<object>} Parsed front matter
397
+ */
232
398
  parseFrontMatter = async (input = '', lang) => {
233
- const { isEmpty, isPlainObject, isArray, filter, map } = this.app.lib._
399
+ const { isEmpty } = this.app.lib._
400
+ const { readAsConfig } = this.app.bajo
234
401
  const { parseObject } = this.app.lib
235
- const handlers = map(filter(this.app.configHandlers, h => !['.js'].includes(h.ext)), h => h.readHandler)
236
- let success
237
- for (const handler of handlers) {
238
- if (success) break
239
- try {
240
- const result = await handler(input, true)
241
- if (isPlainObject(result) || isArray(result)) success = result
242
- } catch (err) {
243
- }
244
- }
245
- if (isEmpty(success)) return {}
246
- return parseObject(success, { parseValue: false, lang }) ?? {}
402
+ const result = await readAsConfig(input)
403
+ if (isEmpty(result)) return {}
404
+ return parseObject(result, { parseValue: false, lang }) ?? {}
247
405
  }
248
406
 
407
+ /**
408
+ * Compile a template with the given content and locals.
409
+ *
410
+ * @async
411
+ * @method
412
+ * @param {string} content - Template content
413
+ * @param {object} locals - Locals object
414
+ * @param {object} options - Options object
415
+ * @param {number} options.ttl - Time to live for cache
416
+ * @param {object} options.req - Request object
417
+ * @returns {Promise<Function>} Compiled template function
418
+ */
249
419
  compile = async (content, locals, { ttl = 0, req = {} } = {}) => {
250
420
  const { get: getCache, set: setCache } = this.app.bajoCache ?? {}
251
421
  const { template } = this.app.lib._
@@ -264,19 +434,39 @@ async function factory (pkgName) {
264
434
  return item(locals)
265
435
  }
266
436
 
437
+ /**
438
+ * Render a string with the given locals and options.
439
+ *
440
+ * @async
441
+ * @method
442
+ * @param {string} content - Content string
443
+ * @param {object} locals - Locals object
444
+ * @param {object} opts - Options object
445
+ * @returns {Promise<string>} Rendered content
446
+ */
267
447
  renderString = async (content, locals = {}, opts = {}) => {
268
448
  let text = await this._renderString(content, locals, opts)
269
449
  if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
270
450
  return text
271
451
  }
272
452
 
453
+ /**
454
+ * Render a template with the given locals and options.
455
+ *
456
+ * @async
457
+ * @method
458
+ * @param {string} tpl - Template name
459
+ * @param {object} locals - Locals object
460
+ * @param {object} opts - Options object
461
+ * @returns {Promise<string>} Rendered template
462
+ */
273
463
  render = async (tpl, locals = {}, opts = {}) => {
274
464
  const { runHook, breakNsPath } = this.app.bajo
275
465
  const { upperFirst } = this.app.lib._
276
466
  const { get, set } = this.app.bajoCache ?? {}
277
467
  const key = crypto.createHash('md5').update(`${tpl}:${JSON.stringify(locals)}`).digest('hex')
278
468
  let subNs
279
- const isAbsolute = path.isAbsolute(tpl)
469
+ const isAbsolute = _path.isAbsolute(tpl)
280
470
  if (!isAbsolute) subNs = breakNsPath(tpl).subNs
281
471
  const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && get && set
282
472
  if (canCache) {
@@ -291,24 +481,96 @@ async function factory (pkgName) {
291
481
  return text
292
482
  }
293
483
 
484
+ /**
485
+ * Resolve a resource by type and name.
486
+ *
487
+ * @method
488
+ * @param {string} type - Resource type
489
+ * @param {string} item - Resource name
490
+ * @param {object} opts - Options object
491
+ * @param {Function} fallbackHandler - Fallback handler function
492
+ * @returns {object} Resolved resource
493
+ */
494
+ resolveResource = (type, item = '', opts = {}, fallbackHandler) => {
495
+ const { trim, find, uniq } = this.app.lib._
496
+ const { fs } = this.app.lib
497
+ const env = this.app.bajo.config.env
498
+ if (env !== 'dev' && cache[item]) return cache[item]
499
+
500
+ let { ns, subSubNs, path } = this.getResource(item)
501
+ const ext = _path.extname(path)
502
+ path = trim(path, '/')
503
+ const dir = _path.dirname(path)
504
+ const base = _path.basename(path, ext)
505
+ const fallbackLang = this.app.bajo.config.intl.fallback
506
+ const exts = uniq([`.${fallbackLang}${ext}`, ext])
507
+ if (opts.lang) exts.unshift(`.${opts.lang}${ext}`)
508
+ let theme
509
+ if (opts.theme && this.app.waibuMpa && opts.req) theme = find(this.app.waibuMpa.themes, { name: opts.theme })
510
+ let file = this._findFile({ type, ns, subSubNs, dir, base, exts, theme, req: opts.req })
511
+ if (!file) {
512
+ if (fallbackHandler) file = fallbackHandler.call(this, { dir, base, exts, ns, subSubNs, type, theme, req: opts.req })
513
+ if (opts.default) {
514
+ const fname = this.getResource(opts.default).fullPath
515
+ if (fs.existsSync(fname)) file = fname
516
+ }
517
+ if (!file) throw this.error('cantFind%s%s', type, item)
518
+ }
519
+ const result = { file, ns, layout: opts.layout }
520
+ if (env !== 'dev') cache[item] = result
521
+ return result
522
+ }
523
+
524
+ /**
525
+ * Resolve a layout by name.
526
+ *
527
+ * @method
528
+ * @param {string} item - Layout name
529
+ * @param {object} opts - Options object
530
+ * @returns {object} Resolved layout
531
+ */
294
532
  resolveLayout = (item = '', opts = {}) => {
295
533
  const fallbackHandler = ({ type, ns, subSubNs, dir, exts, theme, req }) => {
296
534
  if (!this.config.layout.fallback) return false
297
535
  const base = 'default'
298
- return checking.call(this, { type, ns, subSubNs, dir, base, exts, theme, req })
536
+ return this._findFile({ type, ns, subSubNs, dir, base, exts, theme, req })
299
537
  }
300
538
 
301
- return resolveResource.call(this, 'layout', item, opts, fallbackHandler)
539
+ return this.resolveResource('layout', item, opts, fallbackHandler)
302
540
  }
303
541
 
542
+ /**
543
+ * Resolve a partial by name.
544
+ *
545
+ * @method
546
+ * @param {string} item - Partial name
547
+ * @param {object} opts - Options object
548
+ * @returns {object} Resolved partial
549
+ */
304
550
  resolvePartial = (item = '', opts = {}) => {
305
- return resolveResource.call(this, 'partial', item, opts)
551
+ return this.resolveResource('partial', item, opts)
306
552
  }
307
553
 
554
+ /**
555
+ * Resolve a template by name.
556
+ *
557
+ * @method
558
+ * @param {string} item - Template name
559
+ * @param {object} opts - Options object
560
+ * @returns {object} Resolved template
561
+ */
308
562
  resolveTemplate = (item = '', opts = {}) => {
309
- return resolveResource.call(this, 'template', item, opts)
563
+ return this.resolveResource('template', item, opts)
310
564
  }
311
565
 
566
+ /**
567
+ * Split content into front matter and body.
568
+ *
569
+ * @method
570
+ * @param {string} input - Input string or file path
571
+ * @param {boolean} readFile - Whether to read the file
572
+ * @returns {object} Object containing frontMatter and content
573
+ */
312
574
  splitContent = (input, readFile) => {
313
575
  const { fs } = this.app.lib
314
576
  const start = '---\n'
@@ -331,7 +593,16 @@ async function factory (pkgName) {
331
593
  return { frontMatter, content }
332
594
  }
333
595
 
334
- // based on: https://medium.com/@paulohfev/problem-solving-how-to-create-an-excerpt-fdb048687928
596
+ /**
597
+ * Get an excerpt from the content.
598
+ *
599
+ * @method
600
+ * @param {string} content - Content string
601
+ * @param {number} maxWords - Maximum number of words
602
+ * @param {string} trailChars - Trailing characters
603
+ * @returns {string} Excerpt
604
+ * @see {@link https://medium.com/@paulohfev/problem-solving-how-to-create-an-excerpt-fdb048687928|Problem Solving: How to Create an Excerpt}
605
+ */
335
606
  getExcerpt = (content = '', maxWords = 50, trailChars = '...') => {
336
607
  const listOfWords = content.trim().split(' ')
337
608
  const truncatedContent = listOfWords.slice(0, maxWords).join(' ')
@@ -339,6 +610,15 @@ async function factory (pkgName) {
339
610
  return listOfWords.length > maxWords ? excerpt : content
340
611
  }
341
612
 
613
+ /**
614
+ * Get a truncated version of the content.
615
+ *
616
+ * @method
617
+ * @param {string} content - Content string
618
+ * @param {number} maxChars - Maximum number of characters
619
+ * @param {string} trailChars - Trailing characters
620
+ * @returns {string} Truncated content
621
+ */
342
622
  getTruncated = (content = '', maxChars = 50, trailChars = '...') => {
343
623
  const truncated = content.slice(0, maxChars)
344
624
  return truncated.length !== content.length ? (truncated + trailChars) : content
package/lib/imports.js ADDED
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Functions and types for {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
3
+ *
4
+ * With these functions now your templates can use all listed functions below.
5
+ * The imports are automatically injected into the template's scope during compilation.
6
+ *
7
+ * @module Imports
8
+ * @example
9
+ * // In your template file (e.g., template.html)
10
+ * <p><%= _t('Hello, %s!', user.name) %></p>
11
+ * <p><%= _format(user.birthDate, 'date') %></p>
12
+ * <p><%= _routePath('home:/user/{id}/profile', { id: user.id }) %></p>
13
+ */
14
+
15
+ /**
16
+ * Translate text with optional parameters.
17
+ *
18
+ * @callback FnTranslate
19
+ * @function
20
+ * @param {string} text - Text to be translated.
21
+ * @param {...*} args - Optional parameters for translation.
22
+ * @returns {string} Translated text.
23
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
24
+ */
25
+
26
+ /**
27
+ * Format text with optional parameters.
28
+ *
29
+ * See {@link https://ardhi.github.io/bajo/Bajo.html#format|Bajo.format} for more details.
30
+ *
31
+ * @callback FnFormat
32
+ * @function
33
+ * @param {*} val - Value to be formatted.
34
+ * @param {string} type - Value type (e.g., 'string', 'number', 'date').
35
+ * @param {object} [opts] - Optional formatting options.
36
+ * @returns {string} Formatted text.
37
+ * @see {@link https://ardhi.github.io/bajo/Bajo.html#format|Bajo.format}
38
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
39
+ */
40
+
41
+ /**
42
+ * Find route by name. Only available when Waibu plugin is installed & loaded.
43
+ *
44
+ * See {@link https://ardhi.github.io/waibu/Waibu.html#findRoute|Waibu.findRoute} for more details.
45
+ *
46
+ * @callback FnFindRoute
47
+ * @function
48
+ * @param {string} name - Route name.
49
+ * @param {string} [method='GET'] - HTTP method (e.g., 'GET', 'POST').
50
+ * @returns {object|null} Route object or null if not found.
51
+ * @see {@link https://ardhi.github.io/waibu/Waibu.html#findRoute|Waibu.findRoute}
52
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
53
+ */
54
+
55
+ /**
56
+ * Get route path by name. Only available when Waibu plugin is installed & loaded.
57
+ *
58
+ * See {@link https://ardhi.github.io/waibu/Waibu.html#routePath|Waibu.routePath} for more details.
59
+ *
60
+ * @callback FnRoutePath
61
+ * @function
62
+ * @param {string} name - Route name.
63
+ * @param {object} [options={}] - Options object.
64
+ * @returns {string|null} Route path or null if not found.
65
+ * @see {@link https://ardhi.github.io/waibu/Waibu.html#routePath|Waibu.routePath}
66
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
67
+ */
68
+
69
+ /**
70
+ * Get titleized version of a string.
71
+ *
72
+ * @callback FnTitleize
73
+ * @function
74
+ * @param {string} str - String to be titleized.
75
+ * @returns {string} Titleized string.
76
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
77
+ */
78
+
79
+ /**
80
+ * Check if a plugin is loaded by its name or alias.
81
+ *
82
+ * @callback FnHasPlugin
83
+ * @function
84
+ * @param {string} nameOrAlias - Plugin name or alias.
85
+ * @returns {boolean} True if the plugin is loaded, false otherwise.
86
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
87
+ */
88
+
89
+ /**
90
+ * JSON stringify a value. Only available when WaibuMpa plugin is installed & loaded.
91
+ *
92
+ * See {@link https://ardhi.github.io/waibu/WaibuMpa.html#jsonStringify|WaibuMpa.jsonStringify} for more details.
93
+ *
94
+ * @callback FnJsonStringify
95
+ * @function
96
+ * @param {*} value - Value to be stringified.
97
+ * @param {object} [replacer] - Optional replacer function or array.
98
+ * @param {string|number} [space] - Optional space for indentation.
99
+ * @returns {string} JSON stringified value.
100
+ * @see {@link https://ardhi.github.io/waibu/WaibuMpa.html#jsonStringify|WaibuMpa.jsonStringify}
101
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
102
+ */
103
+
104
+ /**
105
+ * Parse markdown text. Only available when BajoMarkdown plugin is installed & loaded.
106
+ * See {@link https://ardhi.github.io/bajo/BajoMarkdown.html#parse|BajoMarkdown.parse} for more details.
107
+ *
108
+ * @callback FnParseMarkdown
109
+ * @function
110
+ * @param {string} value - Markdown text to be parsed.
111
+ * @returns {string} Parsed HTML string.
112
+ * @see {@link https://ardhi.github.io/bajo/BajoMarkdown.html#parse|BajoMarkdown.parse}
113
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
114
+ */
115
+
116
+ /**
117
+ * Get excerpt of a string.
118
+ *
119
+ * @callback FnExcerpt
120
+ * @function
121
+ * @param {string} text - String to be excerpted.
122
+ * @param {number} [maxWords=50] - Max number of words.
123
+ * @param {string} [trailChars='...'] - Characters to append if truncated.
124
+ * @returns {string} Excerpted string.
125
+ * @see {@link BajoTemplate#getExcerpt|BajoTemplate.getExcerpt}
126
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
127
+ */
128
+
129
+ /**
130
+ * Dump a value to console.
131
+ *
132
+ * @callback FnDump
133
+ * @function
134
+ * @param {*} value - Value to be dumped.
135
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
136
+ */
137
+
138
+ /**
139
+ * Get a setting value by key. Only available when Waibu plugin is installed & loaded.
140
+ *
141
+ * See {@link https://ardhi.github.io/waibu/Waibu.html#getSetting|Waibu.getSetting} for more details.
142
+ *
143
+ * @callback FnGetSetting
144
+ * @function
145
+ * @param {string} key - Setting key.
146
+ * @returns {*} Setting value.
147
+ * @see {@link https://ardhi.github.io/waibu/Waibu.html#getSetting|Waibu.getSetting}
148
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
149
+ */
150
+
151
+ /**
152
+ * Result of {@link BajoTemplate#buildCompileImports} method.
153
+ *
154
+ * @typedef {object} TImportsResult
155
+ * @type {object}
156
+ * @global
157
+ * @property {object} _ - Utility library (lodash)
158
+ * @property {module:Imports~FnTranslate} _t - Translation function
159
+ * @property {module:Imports~FnFormat} _format - Format function
160
+ * @property {module:Imports~FnFindRoute} _findRoute - Find route function
161
+ * @property {module:Imports~FnRoutePath} _routePath - Route path function
162
+ * @property {module:Imports~FnTitleize} _titleize - Titleize function
163
+ * @property {module:Imports~FnHasPlugin} _hasPlugin - Check if plugin exists function
164
+ * @property {module:Imports~FnJsonStringify} _jsonStringify - JSON stringify function
165
+ * @property {module:Imports~FnParseMarkdown} _parseMarkdown - Parse markdown function
166
+ * @property {module:Imports~FnExcerpt} _excerpt - Excerpt function
167
+ * @property {module:Imports~FnDump} _dump - Dump function
168
+ * @property {module:Imports~FnGetSetting} _getSetting - Get setting function
169
+ * @see {@link BajoTemplate#buildCompileImports|BajoTemplate.buildCompileImports}
170
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-template",
3
- "version": "2.8.0",
3
+ "version": "2.9.1",
4
4
  "description": "Template Renderer for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -29,7 +29,9 @@
29
29
  },
30
30
  "homepage": "https://github.com/ardhi/bajo-template#readme",
31
31
  "devDependencies": {
32
+ "chai": "^6.2.2",
32
33
  "clean-jsdoc-theme": "^4.3.0",
33
- "jsdoc-plugin-intersection": "^1.0.4"
34
+ "jsdoc-plugin-intersection": "^1.0.4",
35
+ "mocha": "^11.7.6"
34
36
  }
35
37
  }