GenderDysphoria.fyi/build/page-writer.js

65 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-02-27 18:57:39 -08:00
const path = require('path');
const Promise = require('bluebird');
const fs = require('fs-extra');
const getEngines = require('./engines');
const { resolve, ROOT, ENGINE } = require('./resolve');
2020-02-27 18:57:39 -08:00
const { siteInfo } = require(resolve('package.json'));
module.exports = exports = async function writePageContent (pages, posts, prod) {
2020-02-27 18:57:39 -08:00
const engines = await getEngines(prod);
await processPages(engines, posts, null, prod);
await processPages(engines, pages, posts, prod);
};
2020-02-27 18:57:39 -08:00
function processPages (engines, pages, posts, prod) {
return Promise.map(pages, async (page) => {
2020-02-27 18:57:39 -08:00
// page = new Page(page);
var data = {
...page,
meta: { ...page.meta, ...page },
page: {
domain: siteInfo.domain,
title: page.meta.title
? (page.meta.title + (page.meta.subtitle ? ', ' + page.meta.subtitle : '') + ' :: ' + siteInfo.title)
: siteInfo.title,
description: page.meta.description || siteInfo.description,
},
site: siteInfo,
local: {
cwd: resolve(page.cwd),
root: ROOT,
basename: page.basename,
},
posts,
2020-02-27 18:57:39 -08:00
};
const json = {
2020-02-27 18:57:39 -08:00
url: page.fullurl,
title: page.meta.title,
subtitle: page.meta.subtitle,
description: page.meta.description,
tweets: page.tweets,
images: page.images,
dateCreated: page.dateCreated,
dateModified: page.dateModified,
titlecard: page.titlecard,
};
const html = String(engines[page.engine](data.source, data));
if (page.engine === ENGINE.MARKDOWN) {
json.preview = String(engines.MARKDOWN_PREVIEW(data.source, data));
page.content = String(engines.MARKDOWN_CONTENT(data.source, data));
}
2020-02-29 16:27:55 -08:00
const output = resolve('dist', page.out);
2020-02-27 18:57:39 -08:00
await fs.ensureDir(path.dirname(output));
await Promise.all([
fs.writeFile(output, Buffer.from(html)),
page.json && fs.writeFile(resolve('dist', page.json), Buffer.from(
2020-02-27 18:57:39 -08:00
prod ? JSON.stringify(json) : JSON.stringify(json, null, 2),
)),
]);
2020-02-28 09:15:32 -08:00
});
}