bajo-template 2.5.4 → 2.6.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 +16 -8
- package/package.json +1 -1
- package/wiki/CHANGES.md +5 -0
package/index.js
CHANGED
|
@@ -31,16 +31,16 @@ async function factory (pkgName) {
|
|
|
31
31
|
this.loopDetector = {}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
buildCompileImports = (
|
|
34
|
+
buildCompileImports = (locals = {}, req = {}) => {
|
|
35
35
|
const _ = this.app.lib._
|
|
36
36
|
return {
|
|
37
37
|
_,
|
|
38
38
|
_t: (text, ...args) => {
|
|
39
|
-
const params = [...args, { lang }]
|
|
39
|
+
const params = [...args, { lang: req.lang }]
|
|
40
40
|
return this.t(text, ...params)
|
|
41
41
|
},
|
|
42
42
|
_format: (val, type, opts = {}) => {
|
|
43
|
-
opts.lang = opts.lang ?? lang
|
|
43
|
+
opts.lang = opts.lang ?? req.lang
|
|
44
44
|
return this.app.bajo.format(val, type, opts)
|
|
45
45
|
},
|
|
46
46
|
_findRoute: (input) => {
|
|
@@ -63,6 +63,14 @@ async function factory (pkgName) {
|
|
|
63
63
|
},
|
|
64
64
|
_dump: (value, noPre) => {
|
|
65
65
|
return (noPre ? '' : '<pre>') + JSON.stringify(value, null, 2) + (noPre ? '' : '</pre>')
|
|
66
|
+
},
|
|
67
|
+
_getSetting: (key, defValue) => {
|
|
68
|
+
if (!this.app.waibu) return _.get(locals, `schema.view.${key}`, defValue)
|
|
69
|
+
const cfg = this.app.waibu.getSetting(key, { defValue, req })
|
|
70
|
+
const { path } = this.app.bajo.breakNsPath(key)
|
|
71
|
+
const paths = path.replaceAll('/', '.').split('.')
|
|
72
|
+
if (paths[0] === '') paths.shift()
|
|
73
|
+
return _.get(locals, `schema.view.${paths.join('.')}`, cfg)
|
|
66
74
|
}
|
|
67
75
|
}
|
|
68
76
|
}
|
|
@@ -137,7 +145,7 @@ async function factory (pkgName) {
|
|
|
137
145
|
const { breakNsPath } = this.app.bajo
|
|
138
146
|
const start = '<!-- include '
|
|
139
147
|
const end = ' -->'
|
|
140
|
-
const imports = this.buildCompileImports(opts.
|
|
148
|
+
const imports = this.buildCompileImports(locals, opts.req)
|
|
141
149
|
while (content.includes(start) && content.includes(end)) {
|
|
142
150
|
const { pattern, result: rsc } = extractText(content, start, end)
|
|
143
151
|
if (!isEmpty(rsc)) {
|
|
@@ -170,7 +178,7 @@ async function factory (pkgName) {
|
|
|
170
178
|
_renderString = async (content, locals = {}, opts = {}) => {
|
|
171
179
|
const { merge, without, isString, omit, kebabCase, get } = this.app.lib._
|
|
172
180
|
if (opts.ext === '.md' && this.app.bajoMarkdown) {
|
|
173
|
-
content = await this.compile(content, locals, {
|
|
181
|
+
content = await this.compile(content, locals, { ttl: -1, req: opts.req }) // markdown can't process template tags, hence preprocess here
|
|
174
182
|
content = this.app.bajoMarkdown.parse(content)
|
|
175
183
|
}
|
|
176
184
|
let layout
|
|
@@ -207,7 +215,7 @@ async function factory (pkgName) {
|
|
|
207
215
|
const fullTitle = usePluginTitle ? `${locals.page.title} - ${this.app.waibuMpa.getPluginTitle(locals.page.ns, opts.lang)}` : locals.page.title
|
|
208
216
|
locals.page.fullTitle = locals.page.fullTitle ?? fullTitle
|
|
209
217
|
}
|
|
210
|
-
content = await this.compile(content, locals, {
|
|
218
|
+
content = await this.compile(content, locals, { ttl: this.config.cache.maxAgeDu, req: opts.req })
|
|
211
219
|
return await this._handleInclude(content, locals, opts)
|
|
212
220
|
}
|
|
213
221
|
|
|
@@ -238,12 +246,12 @@ async function factory (pkgName) {
|
|
|
238
246
|
return parseObject(success, { parseValue: false, lang }) ?? {}
|
|
239
247
|
}
|
|
240
248
|
|
|
241
|
-
compile = async (content, locals, {
|
|
249
|
+
compile = async (content, locals, { ttl = 0, req = {} } = {}) => {
|
|
242
250
|
const { get: getCache, set: setCache } = this.app.bajoCache ?? {}
|
|
243
251
|
const { template } = this.app.lib._
|
|
244
252
|
locals.attr = locals.attr ?? {}
|
|
245
253
|
const opts = {
|
|
246
|
-
imports: this.buildCompileImports(
|
|
254
|
+
imports: this.buildCompileImports(locals, req)
|
|
247
255
|
}
|
|
248
256
|
const key = 'fn:' + crypto.createHash('md5').update(content).digest('hex')
|
|
249
257
|
let item
|
package/package.json
CHANGED
package/wiki/CHANGES.md
CHANGED