bajo-template 1.1.2 → 1.1.4

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.
@@ -2,7 +2,7 @@ import _path from 'path'
2
2
  const cache = {}
3
3
 
4
4
  export function filecheck ({ check, dir, base, exts }) {
5
- const { fs } = this.app.bajo.lib
5
+ const { fs } = this.lib
6
6
  let file
7
7
  for (const ext of exts) {
8
8
  let path = `${check}/${dir}/${base}${ext}`
@@ -17,7 +17,7 @@ export function filecheck ({ check, dir, base, exts }) {
17
17
 
18
18
  function resolveResource (type, item = '', opts = {}, fallbackHandler) {
19
19
  const { getPluginDataDir } = this.app.bajo
20
- const { trim, uniq } = this.app.bajo.lib._
20
+ const { trim, uniq } = this.lib._
21
21
  const env = this.app.bajo.config.env
22
22
  if (env !== 'dev' && cache[item]) return cache[item]
23
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-template",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Template Renderer for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/plugin/factory.js CHANGED
@@ -22,7 +22,7 @@ async function factory (pkgName) {
22
22
  }
23
23
 
24
24
  buildCompileImports = (lang) => {
25
- const _ = this.app.bajo.lib._
25
+ const _ = this.lib._
26
26
  return {
27
27
  _,
28
28
  _t: (text, ...args) => {
@@ -48,7 +48,7 @@ async function factory (pkgName) {
48
48
  }
49
49
 
50
50
  _clearLoopDetector = () => {
51
- const { omit } = this.app.bajo.lib._
51
+ const { omit } = this.lib._
52
52
  const now = Date.now()
53
53
  const omitted = []
54
54
  for (const groupId in this.loopDetector) {
@@ -59,7 +59,7 @@ async function factory (pkgName) {
59
59
  }
60
60
 
61
61
  _detectLoop = (tpl, file, opts) => {
62
- const { last } = this.app.bajo.lib._
62
+ const { last } = this.lib._
63
63
  if (opts.groupId) {
64
64
  if (this.loopDetector[opts.groupId]) {
65
65
  if (last(this.loopDetector[opts.groupId].file) === file && path.basename(file)[0] !== '~') throw this.error('loopDetected%s%s', tpl, file)
@@ -75,8 +75,8 @@ async function factory (pkgName) {
75
75
 
76
76
  _render = async (tpl, locals = {}, opts = {}) => {
77
77
  this._clearLoopDetector()
78
- const { trim, isEmpty } = this.app.bajo.lib._
79
- const { fs } = this.app.bajo.lib
78
+ const { trim, isEmpty } = this.lib._
79
+ const { fs } = this.lib
80
80
  const { breakNsPath } = this.app.bajo
81
81
 
82
82
  const { subNs } = breakNsPath(tpl)
@@ -99,7 +99,7 @@ async function factory (pkgName) {
99
99
  }
100
100
 
101
101
  _handleInclude = async (content, locals = {}, opts = {}) => {
102
- const { isEmpty, omit, template, merge } = this.app.bajo.lib._
102
+ const { isEmpty, omit, template, merge } = this.lib._
103
103
  const { extractText, breakNsPath } = this.app.bajo
104
104
  const start = '<!-- include '
105
105
  const end = ' -->'
@@ -134,7 +134,7 @@ async function factory (pkgName) {
134
134
  }
135
135
 
136
136
  _renderString = async (content, locals = {}, opts = {}) => {
137
- const { merge, without, isString, omit } = this.app.bajo.lib._
137
+ const { merge, without, isString, omit } = this.lib._
138
138
  if (opts.ext === '.md' && this.app.bajoMarkdown) {
139
139
  content = await this.compile(content, locals, { lang: opts.lang, ttl: -1 }) // markdown can't process template tags, hence preprocess here
140
140
  content = this.app.bajoMarkdown.parse(content)
@@ -143,8 +143,8 @@ async function factory (pkgName) {
143
143
  if (!opts.partial) {
144
144
  const pageFm = await this.parseFrontMatter(opts.frontMatter, opts.lang)
145
145
  if (pageFm.layout) opts.layout = pageFm.layout
146
- locals.page = merge(locals.page, omit(pageFm, ['layout']))
147
- layout = opts.layout ?? (locals.page.ns ? `${locals.page.ns}.layout:/default.html` : 'main.layout:/default.html')
146
+ locals.page = merge({}, locals.page, omit(pageFm, ['layout']))
147
+ layout = opts.layout ?? locals.page.layout ?? (locals.page.ns ? `${locals.page.ns}.layout:/default.html` : 'main.layout:/default.html')
148
148
  const ext = path.extname(layout)
149
149
  const { file } = this.resolveLayout(layout, opts)
150
150
  let { content: layoutContent, frontMatter: layoutFm } = this.splitContent(file, true)
@@ -183,7 +183,7 @@ async function factory (pkgName) {
183
183
  }
184
184
 
185
185
  parseFrontMatter = async (input = '', lang) => {
186
- const { isEmpty, isPlainObject, isArray, filter, map } = this.app.bajo.lib._
186
+ const { isEmpty, isPlainObject, isArray, filter, map } = this.lib._
187
187
  const { parseObject } = this.app.bajo
188
188
  const handlers = map(filter(this.app.bajo.configHandlers, h => !['.js'].includes(h.ext)), h => h.readHandler)
189
189
  let success
@@ -201,7 +201,7 @@ async function factory (pkgName) {
201
201
 
202
202
  compile = async (content, locals, { lang, ttl = 0 } = {}) => {
203
203
  locals.attr = locals.attr ?? {}
204
- const _ = this.app.bajo.lib._
204
+ const _ = this.lib._
205
205
  const { template } = _
206
206
  const cache = this.app.bajoCache
207
207
  let canCache = this.config.cache !== false && cache && this.app.bajo.config.env !== 'dev'
@@ -229,7 +229,7 @@ async function factory (pkgName) {
229
229
 
230
230
  render = async (tpl, locals = {}, opts = {}) => {
231
231
  const { runHook, breakNsPath } = this.app.bajo
232
- const { upperFirst } = this.app.bajo.lib._
232
+ const { upperFirst } = this.lib._
233
233
  const cache = this.app.bajoCache
234
234
  const key = crypto.createHash('md5').update(`${tpl}:${JSON.stringify(locals)}`).digest('hex')
235
235
  const { subNs } = breakNsPath(tpl)
@@ -286,7 +286,7 @@ async function factory (pkgName) {
286
286
  }
287
287
 
288
288
  splitContent = (input, readFile) => {
289
- const { fs } = this.app.bajo.lib
289
+ const { fs } = this.lib
290
290
  const start = '---\n'
291
291
  const end = '\n---'
292
292
 
@@ -306,6 +306,15 @@ async function factory (pkgName) {
306
306
  content = content ?? ''
307
307
  return { frontMatter, content }
308
308
  }
309
+
310
+ // based on: https://medium.com/@paulohfev/problem-solving-how-to-create-an-excerpt-fdb048687928
311
+ getExcerpt = (content, maxWords = 50, trailChars = '...') => {
312
+ const listOfWords = content.trim().split(' ')
313
+ const truncatedContent = listOfWords.slice(0, maxWords).join(' ')
314
+ const excerpt = truncatedContent + trailChars
315
+ const output = listOfWords.length > maxWords ? excerpt : content
316
+ return output
317
+ }
309
318
  }
310
319
  }
311
320