bajo-template 1.0.0 → 1.0.2

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.
@@ -31,7 +31,8 @@ async function compile (content, locals, { lang, ttl = 0 } = {}) {
31
31
  const _ = this.app.bajo.lib._
32
32
  const { template } = _
33
33
  const cache = this.app.bajoCache
34
- const canCache = this.config.cache !== false && cache && this.app.bajo.config.env !== 'dev'
34
+ let canCache = this.config.cache !== false && cache && this.app.bajo.config.env !== 'dev'
35
+ if (ttl === -1) canCache = false
35
36
  const opts = {
36
37
  imports: buildCompileImports.call(this, lang)
37
38
  }
@@ -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,18 @@ async function handleInclude (content, locals, opts) {
36
36
  return content
37
37
  }
38
38
 
39
- export async function _renderString (input, locals = {}, opts = {}) {
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._
41
+ if (opts.ext === '.md' && this.app.bajoMarkdown) {
42
+ content = await this.compile(content, locals, { lang: opts.lang, ttl: -1 }) // markdown can't process template tags, hence preprocess here
43
+ content = this.app.bajoMarkdown.parse(content)
44
+ }
42
45
  let layout
43
46
  if (!opts.partial) {
44
- const pageFm = await this.parseFrontMatter(frontMatter, opts.lang)
45
- locals.page = merge(locals.page, pageFm)
46
- layout = locals.page.layout ?? opts.layout ?? (locals.page.ns ? `${locals.page.ns}.layout:/default.html` : 'main.layout:/default.html')
47
+ const pageFm = await this.parseFrontMatter(opts.frontMatter, opts.lang)
48
+ if (pageFm.layout) opts.layout = pageFm.layout
49
+ locals.page = merge(locals.page, omit(pageFm, ['layout']))
50
+ layout = opts.layout ?? (locals.page.ns ? `${locals.page.ns}.layout:/default.html` : 'main.layout:/default.html')
47
51
  const ext = path.extname(layout)
48
52
  const { file } = this.resolveLayout(layout, opts)
49
53
  let { content: layoutContent, frontMatter: layoutFm } = this.splitContent(file, true)
@@ -1,5 +1,6 @@
1
1
  import crypto from 'crypto'
2
2
  import { _renderString } from './render-string.js'
3
+ import path from 'path'
3
4
 
4
5
  let loopDetector = {}
5
6
 
@@ -44,10 +45,11 @@ async function _render (tpl, locals = {}, opts = {}) {
44
45
  }
45
46
  const { file } = resp
46
47
  _detectLoop.call(this, tpl, file, opts)
47
- let content = trim(fs.readFileSync(file, 'utf8'))
48
- if (isEmpty(content) && subNs === 'template') {
49
- content = '<!-- include ' + tpl.replace('.template', '.partial') + ' -->'
50
- }
48
+ const fileContent = trim(fs.readFileSync(file, 'utf8'))
49
+ let { content, frontMatter } = this.splitContent(fileContent)
50
+ if (isEmpty(content) && (subNs === 'template')) content = '<!-- include ' + tpl.replace('.template', '.partial') + ' -->'
51
+ opts.ext = path.extname(file)
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
 
@@ -15,7 +15,7 @@ function splitContent (input, readFile) {
15
15
  content = text.slice(close + end.length)
16
16
  }
17
17
  }
18
- frontMatter = frontMatter ?? {}
18
+ frontMatter = frontMatter ?? ''
19
19
  content = content ?? ''
20
20
  return { frontMatter, content }
21
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-template",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Template Renderer for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {