bajo-template 1.0.0 → 1.0.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.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
import { buildCompileImports } from './compile.js'
|
|
3
3
|
|
|
4
|
-
async function handleInclude (content, locals, opts) {
|
|
4
|
+
async function handleInclude (content, locals = {}, opts = {}) {
|
|
5
5
|
const { isEmpty, omit, template, merge } = this.app.bajo.lib._
|
|
6
6
|
const { extractText, breakNsPath } = this.app.bajo
|
|
7
7
|
const start = '<!-- include '
|
|
@@ -36,14 +36,14 @@ async function handleInclude (content, locals, opts) {
|
|
|
36
36
|
return content
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
export async function _renderString (
|
|
40
|
-
const { merge, without, isString } = this.app.bajo.lib._
|
|
41
|
-
let { content, frontMatter } = this.splitContent(input)
|
|
39
|
+
export async function _renderString (content, locals = {}, opts = {}) {
|
|
40
|
+
const { merge, without, isString, omit } = this.app.bajo.lib._
|
|
42
41
|
let layout
|
|
43
42
|
if (!opts.partial) {
|
|
44
|
-
const pageFm = await this.parseFrontMatter(frontMatter, opts.lang)
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
const pageFm = await this.parseFrontMatter(opts.frontMatter, opts.lang)
|
|
44
|
+
if (pageFm.layout) opts.layout = pageFm.layout
|
|
45
|
+
locals.page = merge(locals.page, omit(pageFm, ['layout']))
|
|
46
|
+
layout = opts.layout ?? (locals.page.ns ? `${locals.page.ns}.layout:/default.html` : 'main.layout:/default.html')
|
|
47
47
|
const ext = path.extname(layout)
|
|
48
48
|
const { file } = this.resolveLayout(layout, opts)
|
|
49
49
|
let { content: layoutContent, frontMatter: layoutFm } = this.splitContent(file, true)
|
package/bajo/method/render.js
CHANGED
|
@@ -44,10 +44,12 @@ async function _render (tpl, locals = {}, opts = {}) {
|
|
|
44
44
|
}
|
|
45
45
|
const { file } = resp
|
|
46
46
|
_detectLoop.call(this, tpl, file, opts)
|
|
47
|
-
|
|
47
|
+
const fileContent = trim(fs.readFileSync(file, 'utf8'))
|
|
48
|
+
let { content, frontMatter } = this.splitContent(fileContent)
|
|
48
49
|
if (isEmpty(content) && subNs === 'template') {
|
|
49
50
|
content = '<!-- include ' + tpl.replace('.template', '.partial') + ' -->'
|
|
50
51
|
}
|
|
52
|
+
opts.frontMatter = frontMatter
|
|
51
53
|
opts.partial = opts.partial ?? subNs === 'partial'
|
|
52
54
|
return await _renderString.call(this, content, locals, opts)
|
|
53
55
|
}
|
|
@@ -67,7 +69,7 @@ async function render (tpl, locals = {}, opts = {}) {
|
|
|
67
69
|
let text = await _render.call(this, tpl, locals, opts)
|
|
68
70
|
if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
|
|
69
71
|
await runHook(`${this.name}:afterRender${upperFirst(subNs)}`, { tpl, locals, opts, text })
|
|
70
|
-
if (canCache) await cache.set({ key, value: text, ttl: this.config.cache.maxAgeDur })
|
|
72
|
+
if (canCache) await cache.set({ key, value: text, ttl: opts.cacheMaxAge ?? this.config.cache.maxAgeDur })
|
|
71
73
|
return text
|
|
72
74
|
}
|
|
73
75
|
|