2020-09-20 09:16:08 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const Handlebars = require('handlebars')
|
2021-04-06 05:36:44 +00:00
|
|
|
const slugify = require('slugify')
|
2020-09-20 09:16:08 +00:00
|
|
|
|
2020-09-13 19:28:38 +00:00
|
|
|
module.exports = ({ defaults, url }, templatesPath, destinationPath) => {
|
2020-09-20 09:16:08 +00:00
|
|
|
if (defaults.categories !== null) {
|
2020-11-27 12:45:16 +00:00
|
|
|
const pageTemplate = fs.readFileSync(
|
|
|
|
`${templatesPath}/page.md.handlebars`,
|
|
|
|
'utf8'
|
|
|
|
)
|
2021-04-04 10:49:28 +00:00
|
|
|
Handlebars.registerHelper('shellescape', function (value) {
|
|
|
|
return '"'+`${value}`.trim().replace(/(["'$`\\])/g,'\\$1')+'"'
|
|
|
|
});
|
2020-09-20 09:16:08 +00:00
|
|
|
const renderPage = Handlebars.compile(pageTemplate)
|
|
|
|
defaults.categories.forEach(({ folder, name, keys }) => {
|
2020-11-27 12:45:16 +00:00
|
|
|
if (keys === undefined) {
|
|
|
|
return
|
|
|
|
}
|
2020-09-20 09:16:08 +00:00
|
|
|
|
|
|
|
keys.forEach(({ domain, ...page }) => {
|
2021-04-04 10:49:28 +00:00
|
|
|
page.examples.forEach(example => {
|
|
|
|
if (!Array.isArray(example.value)) {
|
|
|
|
example.value = [example.value]
|
|
|
|
}
|
|
|
|
})
|
2020-11-27 12:45:16 +00:00
|
|
|
const pageReadmeContent = renderPage({
|
|
|
|
...page,
|
|
|
|
folder,
|
|
|
|
name,
|
|
|
|
domain,
|
|
|
|
url,
|
|
|
|
})
|
|
|
|
fs.writeFileSync(
|
2021-04-06 05:36:44 +00:00
|
|
|
`${destinationPath}/${folder}/${slugify(page.key)}.md`,
|
2020-11-27 12:45:16 +00:00
|
|
|
pageReadmeContent
|
|
|
|
)
|
2020-09-20 09:16:08 +00:00
|
|
|
|
2020-11-27 12:45:16 +00:00
|
|
|
page.examples.forEach((example) => {
|
2020-09-20 09:16:08 +00:00
|
|
|
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}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|