bajo-template 2.8.0 → 2.9.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/docs/BajoTemplate.html +1 -1
- package/docs/data/search.json +1 -1
- package/docs/global.html +1 -1
- package/docs/index.html +1 -1
- package/docs/index.js.html +353 -86
- package/docs/lib_imports.js.html +173 -0
- package/docs/lib_index.js.html +163 -0
- package/docs/module-Imports.html +6 -0
- package/docs/module-Lib.html +3 -0
- package/docs/module-LibImports.html +3 -0
- package/docs/module-Lib_Imports.html +3 -0
- package/docs/scripts/core.js +477 -476
- package/docs/scripts/resize.js +36 -36
- package/docs/scripts/search.js +105 -105
- package/docs/scripts/third-party/fuse.js +1 -1
- package/docs/scripts/third-party/hljs-line-num-original.js +285 -282
- package/docs/scripts/third-party/hljs-line-num.js +1 -1
- package/docs/scripts/third-party/hljs-original.js +1202 -1195
- package/docs/scripts/third-party/hljs.js +1 -1
- package/docs/scripts/third-party/popper.js +1 -1
- package/docs/scripts/third-party/tippy.js +1 -1
- package/docs/scripts/third-party/tocbot.js +509 -508
- package/index.js +305 -17
- package/lib/imports.js +170 -0
- package/package.json +4 -2
- package/test/bajo-template.test.js +275 -0
- package/wiki/CHANGES.md +5 -0
- package/lib/resolve-resource.js +0 -90
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
|
|
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 &&
|
|
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 (
|
|
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 (
|
|
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 =
|
|
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 =
|
|
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,6 +386,15 @@ 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
399
|
const { isEmpty, isPlainObject, isArray, filter, map } = this.app.lib._
|
|
234
400
|
const { parseObject } = this.app.lib
|
|
@@ -246,6 +412,18 @@ async function factory (pkgName) {
|
|
|
246
412
|
return parseObject(success, { parseValue: false, lang }) ?? {}
|
|
247
413
|
}
|
|
248
414
|
|
|
415
|
+
/**
|
|
416
|
+
* Compile a template with the given content and locals.
|
|
417
|
+
*
|
|
418
|
+
* @async
|
|
419
|
+
* @method
|
|
420
|
+
* @param {string} content - Template content
|
|
421
|
+
* @param {object} locals - Locals object
|
|
422
|
+
* @param {object} options - Options object
|
|
423
|
+
* @param {number} options.ttl - Time to live for cache
|
|
424
|
+
* @param {object} options.req - Request object
|
|
425
|
+
* @returns {Promise<Function>} Compiled template function
|
|
426
|
+
*/
|
|
249
427
|
compile = async (content, locals, { ttl = 0, req = {} } = {}) => {
|
|
250
428
|
const { get: getCache, set: setCache } = this.app.bajoCache ?? {}
|
|
251
429
|
const { template } = this.app.lib._
|
|
@@ -264,19 +442,39 @@ async function factory (pkgName) {
|
|
|
264
442
|
return item(locals)
|
|
265
443
|
}
|
|
266
444
|
|
|
445
|
+
/**
|
|
446
|
+
* Render a string with the given locals and options.
|
|
447
|
+
*
|
|
448
|
+
* @async
|
|
449
|
+
* @method
|
|
450
|
+
* @param {string} content - Content string
|
|
451
|
+
* @param {object} locals - Locals object
|
|
452
|
+
* @param {object} opts - Options object
|
|
453
|
+
* @returns {Promise<string>} Rendered content
|
|
454
|
+
*/
|
|
267
455
|
renderString = async (content, locals = {}, opts = {}) => {
|
|
268
456
|
let text = await this._renderString(content, locals, opts)
|
|
269
457
|
if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
|
|
270
458
|
return text
|
|
271
459
|
}
|
|
272
460
|
|
|
461
|
+
/**
|
|
462
|
+
* Render a template with the given locals and options.
|
|
463
|
+
*
|
|
464
|
+
* @async
|
|
465
|
+
* @method
|
|
466
|
+
* @param {string} tpl - Template name
|
|
467
|
+
* @param {object} locals - Locals object
|
|
468
|
+
* @param {object} opts - Options object
|
|
469
|
+
* @returns {Promise<string>} Rendered template
|
|
470
|
+
*/
|
|
273
471
|
render = async (tpl, locals = {}, opts = {}) => {
|
|
274
472
|
const { runHook, breakNsPath } = this.app.bajo
|
|
275
473
|
const { upperFirst } = this.app.lib._
|
|
276
474
|
const { get, set } = this.app.bajoCache ?? {}
|
|
277
475
|
const key = crypto.createHash('md5').update(`${tpl}:${JSON.stringify(locals)}`).digest('hex')
|
|
278
476
|
let subNs
|
|
279
|
-
const isAbsolute =
|
|
477
|
+
const isAbsolute = _path.isAbsolute(tpl)
|
|
280
478
|
if (!isAbsolute) subNs = breakNsPath(tpl).subNs
|
|
281
479
|
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && get && set
|
|
282
480
|
if (canCache) {
|
|
@@ -291,24 +489,96 @@ async function factory (pkgName) {
|
|
|
291
489
|
return text
|
|
292
490
|
}
|
|
293
491
|
|
|
492
|
+
/**
|
|
493
|
+
* Resolve a resource by type and name.
|
|
494
|
+
*
|
|
495
|
+
* @method
|
|
496
|
+
* @param {string} type - Resource type
|
|
497
|
+
* @param {string} item - Resource name
|
|
498
|
+
* @param {object} opts - Options object
|
|
499
|
+
* @param {Function} fallbackHandler - Fallback handler function
|
|
500
|
+
* @returns {object} Resolved resource
|
|
501
|
+
*/
|
|
502
|
+
resolveResource = (type, item = '', opts = {}, fallbackHandler) => {
|
|
503
|
+
const { trim, find, uniq } = this.app.lib._
|
|
504
|
+
const { fs } = this.app.lib
|
|
505
|
+
const env = this.app.bajo.config.env
|
|
506
|
+
if (env !== 'dev' && cache[item]) return cache[item]
|
|
507
|
+
|
|
508
|
+
let { ns, subSubNs, path } = this.getResource(item)
|
|
509
|
+
const ext = _path.extname(path)
|
|
510
|
+
path = trim(path, '/')
|
|
511
|
+
const dir = _path.dirname(path)
|
|
512
|
+
const base = _path.basename(path, ext)
|
|
513
|
+
const fallbackLang = this.app.bajo.config.intl.fallback
|
|
514
|
+
const exts = uniq([`.${fallbackLang}${ext}`, ext])
|
|
515
|
+
if (opts.lang) exts.unshift(`.${opts.lang}${ext}`)
|
|
516
|
+
let theme
|
|
517
|
+
if (opts.theme && this.app.waibuMpa && opts.req) theme = find(this.app.waibuMpa.themes, { name: opts.theme })
|
|
518
|
+
let file = this._findFile({ type, ns, subSubNs, dir, base, exts, theme, req: opts.req })
|
|
519
|
+
if (!file) {
|
|
520
|
+
if (fallbackHandler) file = fallbackHandler.call(this, { dir, base, exts, ns, subSubNs, type, theme, req: opts.req })
|
|
521
|
+
if (opts.default) {
|
|
522
|
+
const fname = this.getResource(opts.default).fullPath
|
|
523
|
+
if (fs.existsSync(fname)) file = fname
|
|
524
|
+
}
|
|
525
|
+
if (!file) throw this.error('cantFind%s%s', type, item)
|
|
526
|
+
}
|
|
527
|
+
const result = { file, ns, layout: opts.layout }
|
|
528
|
+
if (env !== 'dev') cache[item] = result
|
|
529
|
+
return result
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Resolve a layout by name.
|
|
534
|
+
*
|
|
535
|
+
* @method
|
|
536
|
+
* @param {string} item - Layout name
|
|
537
|
+
* @param {object} opts - Options object
|
|
538
|
+
* @returns {object} Resolved layout
|
|
539
|
+
*/
|
|
294
540
|
resolveLayout = (item = '', opts = {}) => {
|
|
295
541
|
const fallbackHandler = ({ type, ns, subSubNs, dir, exts, theme, req }) => {
|
|
296
542
|
if (!this.config.layout.fallback) return false
|
|
297
543
|
const base = 'default'
|
|
298
|
-
return
|
|
544
|
+
return this._findFile({ type, ns, subSubNs, dir, base, exts, theme, req })
|
|
299
545
|
}
|
|
300
546
|
|
|
301
|
-
return resolveResource
|
|
547
|
+
return this.resolveResource('layout', item, opts, fallbackHandler)
|
|
302
548
|
}
|
|
303
549
|
|
|
550
|
+
/**
|
|
551
|
+
* Resolve a partial by name.
|
|
552
|
+
*
|
|
553
|
+
* @method
|
|
554
|
+
* @param {string} item - Partial name
|
|
555
|
+
* @param {object} opts - Options object
|
|
556
|
+
* @returns {object} Resolved partial
|
|
557
|
+
*/
|
|
304
558
|
resolvePartial = (item = '', opts = {}) => {
|
|
305
|
-
return resolveResource
|
|
559
|
+
return this.resolveResource('partial', item, opts)
|
|
306
560
|
}
|
|
307
561
|
|
|
562
|
+
/**
|
|
563
|
+
* Resolve a template by name.
|
|
564
|
+
*
|
|
565
|
+
* @method
|
|
566
|
+
* @param {string} item - Template name
|
|
567
|
+
* @param {object} opts - Options object
|
|
568
|
+
* @returns {object} Resolved template
|
|
569
|
+
*/
|
|
308
570
|
resolveTemplate = (item = '', opts = {}) => {
|
|
309
|
-
return resolveResource
|
|
571
|
+
return this.resolveResource('template', item, opts)
|
|
310
572
|
}
|
|
311
573
|
|
|
574
|
+
/**
|
|
575
|
+
* Split content into front matter and body.
|
|
576
|
+
*
|
|
577
|
+
* @method
|
|
578
|
+
* @param {string} input - Input string or file path
|
|
579
|
+
* @param {boolean} readFile - Whether to read the file
|
|
580
|
+
* @returns {object} Object containing frontMatter and content
|
|
581
|
+
*/
|
|
312
582
|
splitContent = (input, readFile) => {
|
|
313
583
|
const { fs } = this.app.lib
|
|
314
584
|
const start = '---\n'
|
|
@@ -331,7 +601,16 @@ async function factory (pkgName) {
|
|
|
331
601
|
return { frontMatter, content }
|
|
332
602
|
}
|
|
333
603
|
|
|
334
|
-
|
|
604
|
+
/**
|
|
605
|
+
* Get an excerpt from the content.
|
|
606
|
+
*
|
|
607
|
+
* @method
|
|
608
|
+
* @param {string} content - Content string
|
|
609
|
+
* @param {number} maxWords - Maximum number of words
|
|
610
|
+
* @param {string} trailChars - Trailing characters
|
|
611
|
+
* @returns {string} Excerpt
|
|
612
|
+
* @see {@link https://medium.com/@paulohfev/problem-solving-how-to-create-an-excerpt-fdb048687928|Problem Solving: How to Create an Excerpt}
|
|
613
|
+
*/
|
|
335
614
|
getExcerpt = (content = '', maxWords = 50, trailChars = '...') => {
|
|
336
615
|
const listOfWords = content.trim().split(' ')
|
|
337
616
|
const truncatedContent = listOfWords.slice(0, maxWords).join(' ')
|
|
@@ -339,6 +618,15 @@ async function factory (pkgName) {
|
|
|
339
618
|
return listOfWords.length > maxWords ? excerpt : content
|
|
340
619
|
}
|
|
341
620
|
|
|
621
|
+
/**
|
|
622
|
+
* Get a truncated version of the content.
|
|
623
|
+
*
|
|
624
|
+
* @method
|
|
625
|
+
* @param {string} content - Content string
|
|
626
|
+
* @param {number} maxChars - Maximum number of characters
|
|
627
|
+
* @param {string} trailChars - Trailing characters
|
|
628
|
+
* @returns {string} Truncated content
|
|
629
|
+
*/
|
|
342
630
|
getTruncated = (content = '', maxChars = 50, trailChars = '...') => {
|
|
343
631
|
const truncated = content.slice(0, maxChars)
|
|
344
632
|
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.
|
|
3
|
+
"version": "2.9.0",
|
|
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
|
}
|