shared-hugo-scripts/wiki/app.js

112 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-01-13 06:20:33 +01:00
require('checkenv').check();
const util = require('util');
const logger = require('winston');
const sanitizeHtml = require('sanitize-html');
const fs = require('fs-extra');
const exec = require('sync-exec');
const inputDirectory = './wiki/';
const outputDirectory = '../../../site/content/wiki/';
// The URL
function url(title) {
return '/wiki/' + title.replace(/\s+/g, '-').toLowerCase();
}
if (fs.existsSync(inputDirectory)) {
logger.info(`Purging input directory: ${inputDirectory}`);
fs.removeSync(inputDirectory);
}
const gitCloneCommand = `git clone ${process.env.GITHUB_WIKI_URL} wiki`;
logger.info(`exec ${gitCloneCommand}`);
exec(gitCloneCommand);
if (fs.existsSync(outputDirectory) === false) {
logger.info(`Creating missing output directory: ${outputDirectory}`);
fs.mkdirSync(outputDirectory);
}
fs.readdir(inputDirectory, (err, items) => {
if (err) {
logger.error(err);
return;
}
try {
2020-07-06 00:24:18 +02:00
// Look for all .md files within the wiki directory.
2018-01-13 06:20:33 +01:00
items.filter(file => file.substr(-3) === '.md').forEach(item => {
2020-07-06 00:24:18 +02:00
// Generate the title from the filename.
2018-01-13 06:20:33 +01:00
const title = item.replace(/-/g, ' ').slice(0, -3);
const stats = fs.statSync(`${inputDirectory}${item}`);
const modified = new Date(util.inspect(stats.mtime));
2020-07-06 00:24:18 +02:00
// Read the .md file.
2018-01-13 06:20:33 +01:00
fs.readFile(`${inputDirectory}${item}`, 'utf8', (err, data) => {
if (err) {
logger.error(err);
return;
}
try {
2020-07-06 00:24:18 +02:00
// Convert various data inside of the markdown language.
2018-01-13 06:20:33 +01:00
let cleanData = sanitizeHtml(data);
2020-07-06 00:24:18 +02:00
// Blackfriday Markdown Rendering requires a blank line before lists.
2018-01-13 06:20:33 +01:00
try {
const lines = cleanData.split(/\r?\n/);
for (let i = 0; i < lines.length; i++) {
2020-07-06 00:24:18 +02:00
// If it's the start of the file, ignore to prevent an index issue.
2018-01-13 06:20:33 +01:00
if (i > lines.length) {
return;
}
if (i === 0 || lines[i] === '\n') {
continue;
}
2020-07-06 00:24:18 +02:00
// Search for the start of a list designated by the * character.
2018-01-13 06:20:33 +01:00
if (lines[i].startsWith('* ') && lines[i - 1].startsWith('* ') === false) {
i += 1;
lines.splice(i - 1, 0, '');
}
}
cleanData = lines.join('\n');
} catch (err) {
logger.error(err);
}
2020-07-06 00:24:18 +02:00
// Replacing tags like [[Common Issues on Windows|Common Issues]]
2018-01-13 06:20:33 +01:00
cleanData = cleanData.replace(/\[\[(.*)\|(.*)\]\]/g, (match, p1, p2) => {
return `[${p1}](${url(p2)})`;
});
2020-07-06 00:24:18 +02:00
// Replacing tags like [[Common Issues]]
2018-01-13 06:20:33 +01:00
cleanData = cleanData.replace(/\[\[(.*)\]\]/g, (match, p1) => {
return `[${p1}](${url(p1)})`;
});
2021-06-28 18:35:49 +02:00
// Replace decoded &amp; -> &
cleanData = cleanData.replace(/&amp;/g, "&");
2018-01-13 06:20:33 +01:00
2020-07-06 00:24:18 +02:00
// Create the new markdown header for Hugo.
2018-01-13 06:20:33 +01:00
const newFileContents = `+++\r\ntitle = "${title}"\r\ndate = "${modified.toISOString()}"\r\n+++\r\n\r\n${cleanData}\r\n`;
const itemOutput = item.toLowerCase();
fs.writeFile(`${outputDirectory}${itemOutput}`, newFileContents, err => {
if (err) {
return logger.error(err);
}
logger.info(`Wrote file ${itemOutput} to filesystem.`);
});
} catch (err) {
logger.error(err);
}
});
});
} catch (err) {
logger.error(err);
}
});