Skip to content

Commit ab44276

Browse files
committed
refactor: switch to mustache for simpler render
1 parent de07a7e commit ab44276

File tree

3 files changed

+340
-39
lines changed

3 files changed

+340
-39
lines changed

lib/publish.mjs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,23 @@ import postcssrc from 'postcss-load-config'
99
import posthtml from 'posthtml'
1010
import posthtmlBaseURL from 'posthtml-base-url'
1111
import { transform as sucraseTransform } from 'sucrase'
12-
import { compile as templateCompile } from 'tempura'
12+
import Mustache from 'mustache'
1313
import glob from 'tiny-glob'
1414

15+
const compileTemplate = (layout) => (vars) => Mustache.render(layout, vars)
16+
17+
const htmlProcessor = async (baseHTML, options) => {
18+
return (
19+
await posthtml([
20+
posthtmlBaseURL({
21+
url: options.baseUrl,
22+
allTags: true,
23+
}),
24+
htmlnano(),
25+
]).process(baseHTML)
26+
).html
27+
}
28+
1529
const markdownTransfomer = (options) => ({
1630
transform: async (code, file) => {
1731
const { frontmatter, content } = getFrontmatter(code)
@@ -33,11 +47,11 @@ const markdownTransfomer = (options) => ({
3347
vars,
3448
)
3549
: options.layoutTemplate
36-
3750
baseHTML = await layoutTemplate(vars)
3851
} else if (options.layoutTemplate) {
3952
baseHTML = await options.layoutTemplate({
4053
content: sourceHTML,
54+
...vars,
4155
})
4256
}
4357
} else {
@@ -46,14 +60,7 @@ const markdownTransfomer = (options) => ({
4660
})
4761
}
4862

49-
const output = await posthtml([
50-
posthtmlBaseURL({
51-
url: options.baseUrl,
52-
allTags: true,
53-
}),
54-
htmlnano(),
55-
]).process(baseHTML)
56-
return output.html
63+
return htmlProcessor(baseHTML, options)
5764
},
5865
ext: '.html',
5966
})
@@ -66,14 +73,7 @@ const htmlTranformer = (options) => ({
6673
content: code,
6774
})
6875
}
69-
const output = await posthtml([
70-
posthtmlBaseURL({
71-
url: options.baseUrl,
72-
allTags: true,
73-
}),
74-
htmlnano(),
75-
]).process(toCompile)
76-
return output.html
76+
return htmlProcessor(toCompile, options)
7777
},
7878
ext: '.html',
7979
})
@@ -130,17 +130,15 @@ export async function compile(basePath, distPath, options) {
130130
baseUrl: options.baseUrl,
131131
jsxImportSource: options.jsxImportSource,
132132
}
133-
const layoutSource = options.layoutFile ?? join(basePath, '_layout.hbs')
133+
const layoutSource =
134+
options.layoutFile ?? join(basePath, '_layout.mustache')
134135
const layoutExists = await fs.promises
135136
.access(layoutSource)
136137
.then((_) => true)
137138
.catch((_) => false)
138139
if (layoutExists) {
139-
const templateContent = `
140-
{{#expect content}}
141-
${await fs.promises.readFile(layoutSource, 'utf8')}
142-
`
143-
transformerOptions.layoutTemplate = templateCompile(templateContent)
140+
const templateContent = `${await fs.promises.readFile(layoutSource, 'utf8')}`
141+
transformerOptions.layoutTemplate = compileTemplate(templateContent)
144142
}
145143

146144
const files = (
@@ -152,7 +150,7 @@ ${await fs.promises.readFile(layoutSource, 'utf8')}
152150
).filter(
153151
(d) =>
154152
d != resolve(layoutSource) &&
155-
!d.endsWith('.hbs') &&
153+
!d.endsWith('.mustache') &&
156154
!d.startsWith(resolve('node_modules')) &&
157155
(distPath ? !d.startsWith(resolve(distPath)) : true),
158156
)
@@ -227,10 +225,7 @@ async function prepareFrontmatterTemplate(pathToFile, file, vars) {
227225
throw new Error(`Invalid layout provided for file:${file}`)
228226
}
229227
const layout = await fs.promises.readFile(pathToFile, 'utf8')
230-
const withExpects = `{{#expect ${Object.keys(vars).filter(Boolean).join(', ')}}}
231-
${layout}`
232-
233-
return templateCompile(withExpects)
228+
return compileTemplate(layout)
234229
}
235230

236231
function getFrontmatter(code) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"kleur": "^4.1.5",
4141
"marked": "^15.0.2",
4242
"mrmime": "^2.0.0",
43+
"mustache": "^4.2.0",
4344
"postcss": "^8.4.49",
4445
"postcss-import": "^16.1.0",
4546
"postcss-load-config": "^6.0.1",
@@ -50,7 +51,6 @@
5051
"sirv": "^3.0.0",
5152
"sucrase": "^3.35.0",
5253
"svgo": "^3.3.2",
53-
"tempura": "^0.4.1",
5454
"terser": "^5.36.0",
5555
"tiny-glob": "^0.2.9"
5656
},

0 commit comments

Comments
 (0)