Files
Guangcong Luo 2105dc8e57 Add website to client repository
Closes #1567

The main reason I'm not simply merging Annika's PR is because this
way makes it clearer that I'm taking responsibility for all this
code, that it's mostly code I wrote, and also because it makes it
easier to ensure that none of the files have been changed.

(Not that I don't personally trust Annika, but I have something
resembling an obligation to users not to expose them to risks based
on personal trust.)
2020-07-27 19:37:04 -04:00

46 lines
1.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
'use strict';
const fs = require('fs');
const showdown = require('showdown');
const converter = new showdown.Converter({
tables: true,
simpleLineBreaks: true,
requireSpaceBeforeHeadingText: true,
simplifiedAutoLink: true,
literalMidWordUnderscores: true,
literalMidWordAsterisks: true,
openLinksInNewWindow: true,
});
process.chdir(__dirname);
const files = fs.readdirSync('../pages/');
const template = '' + fs.readFileSync('template.html');
for (const file of files) {
if (!file.endsWith('.md')) continue;
if (/[A-Z]/.test(file)) continue;
let buf = '' + fs.readFileSync('../pages/' + file);
const name = file.slice(0, -3);
const nlIndex = buf.indexOf('\n');
const title = buf.slice(2, nlIndex);
const secondHashIndex = buf.indexOf('\n# ');
if (secondHashIndex > 0 && buf.slice(nlIndex, secondHashIndex).trim() === '') {
buf = buf.slice(secondHashIndex + 1);
}
buf = converter.makeHtml(buf);
buf = buf.replace(/<a href="http/g, `<a target="_blank" href="http`);
buf = buf.replace(/\n/g, '\n\t\t').replace(/\n\t\t\n/g, '\n\n');
buf = template.replace('{title}', title).replace('{content}', buf);
buf = buf.replace(`<a href="/${name}">`, `<a href="/${name}" class="cur">`);
// showdown doesn't do this automatically
buf = buf.replace(/é/g, '&eacute;');
fs.writeFileSync('../.pages-cached/' + name + '.html', buf);
}
console.log("Done.");