macos-defaults/build/production/write-pages.js
Cameron Little 2bc15fe0de 🩹 Improve markdown support
This improves markdown support within descriptions. Currently, adding multiline text blocks in the defaults.yml file breaks, because the generated head meta tags content includes that whitespace as is and breaks the generated yaml contents. Now, meta tag content is stringified
as JSON, which is yaml compliant. The final html meta tag contains the markdown as is. Also currently, markdown descriptions inserted into the
content can interact with subsequent content (if you end with a list it'll cause funky interaction with the following content). That's fixed by inserting a "break" between (done on categores and pages).
2021-04-05 20:10:03 +02:00

54 lines
1.7 KiB
JavaScript

const fs = require('fs')
const Handlebars = require('handlebars')
module.exports = ({ defaults, url }, templatesPath, destinationPath) => {
if (defaults.categories !== null) {
const pageTemplate = fs.readFileSync(
`${templatesPath}/page.md.handlebars`,
'utf8'
)
Handlebars.registerHelper('shellescape', function (value) {
return '"'+`${value}`.trim().replace(/(["'$`\\])/g,'\\$1')+'"'
});
const renderPage = Handlebars.compile(pageTemplate)
defaults.categories.forEach(({ folder, name, keys }) => {
if (keys === undefined) {
return
}
keys.forEach(({ domain, ...page }) => {
page.examples.forEach(example => {
if (!Array.isArray(example.value)) {
example.value = [example.value]
}
})
const pageReadmeContent = renderPage({
...page,
folder,
name,
domain,
url,
})
fs.writeFileSync(
`${destinationPath}/${folder}/${page.key.toLowerCase()}.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}`
)
}
})
})
})
}
}