macos-defaults/build/production/write-pages.js
Yann Bertrand 3eb3fa5605
♻️ Normalize templates
- Allow special chars in category name and page title
- Improve HTML image attributes readibility
- Prod: allow double quotes in category and page descriptions
2020-09-26 20:24:54 +02:00

36 lines
1.3 KiB
JavaScript

const fs = require('fs')
const Handlebars = require('handlebars')
Handlebars.registerHelper('escapeDoubleQuote', (string) => {
return string.replace(/"/g, '"')
})
module.exports = ({ defaults, url }, templatesPath, destinationPath) => {
if (defaults.categories !== null) {
const pageTemplate = fs.readFileSync(`${templatesPath}/page.md.handlebars`, 'utf8')
const renderPage = Handlebars.compile(pageTemplate)
defaults.categories.forEach(({ folder, name, keys }) => {
if (keys === undefined) { return }
keys.forEach(({ domain, ...page }) => {
const pageReadmeContent = renderPage({ ...page, folder, name, domain, url })
fs.writeFileSync(`${destinationPath}/${folder}/${page.key}.md`, pageReadmeContent)
page.examples.forEach(example => {
if (example.image !== undefined) {
fs.copyFileSync(
`../../images/${folder}/${page.key}/${example.image.filename}`,
`${destinationPath}/${folder}/${folder}-${page.key}-${example.image.filename}`
)
}
if (example.video !== undefined) {
fs.copyFileSync(
`../../images/${folder}/${page.key}/${example.video.filename}`,
`${destinationPath}/${folder}/${folder}-${page.key}-${example.video.filename}`
)
}
})
})
})
}
}