-
Notifications
You must be signed in to change notification settings - Fork 218
/
make-html.js
126 lines (105 loc) · 4.03 KB
/
make-html.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import showdown from 'showdown';
import fm from 'front-matter';
import mustache from 'mustache';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const defaultPageData = {
author: 'Jez Swanson',
translatorMarkdown: '',
textDirection: '',
};
const contentDir = 'content/'
const markdownConverter = new showdown.Converter();
const template = fs.readFileSync('template.html').toString();
export function exportAllLanguages({outputDir}) {
// Read the metadata from all the markdown files.
const pageDatas = [];
// Filter out the README file. All the rest are contents of the page for different languages and such.
const markdownFiles = fs.readdirSync(contentDir)
.filter(f => !f.startsWith('README'));
for (const file of markdownFiles) {
const content = fs.readFileSync(path.join(contentDir, file), 'utf-8');
// Read the metadata from the top of the file.
const frontMatterParsed = fm(content);
// Skip if the attributes are empty.
if (Object.keys(frontMatterParsed.attributes).length === 0) {
continue;
}
const pageData = Object.assign(
{},
defaultPageData,
frontMatterParsed.attributes,
{markdown: frontMatterParsed.body, markdownFileName: file}
);
// Some extra data calculated from the other data:
// We might have a string or an array of strings. Convert it so we always have an array
let outFileNames = pageData.outFileName;
if (!(outFileNames instanceof Array)) {
// Wrap in Array
outFileNames = [outFileNames];
}
pageData.outFileNames = outFileNames;
// The URL of the page is just calculated from the (first) output filename.
// index.html is exceptional, as we we want to link to the base path.
let url = '/' + pageData.outFileNames[0];
if (pageData.outFileNames[0] == 'index.html') {
url = '';
}
pageData.url = url;
// Replace the textDirection with a string that can be inserted directly into the HTML.
if (pageData.textDirection.includes('rtl')) {
pageData.textDirection = 'dir=rtl'
}
else {
pageData.textDirection = '';
}
pageDatas.push(pageData);
}
const languages = [];
for (const pageData of pageDatas) {
if (!pageData.hasOwnProperty('languageName')) {
continue;
}
languages.push({
name: pageData.languageName,
url: `/fourier${pageData.url}`,
});
}
languages.sort((a, b) => a.name.localeCompare(b.name));
// And then put English at the front.
const english = languages.splice(languages.findIndex(l => l.name == "English"), 1)[0];
languages.unshift(english);
for (const pageData of pageDatas) {
console.log(`Processing ${pageData.markdownFileName}`);
const html = createHtml(pageData, languages);
// Output to build directory.
for (const outFileName of pageData.outFileNames) {
console.log(`Writing to ${outFileName}`)
const outFilePath = path.join(outputDir, outFileName);
fs.writeFileSync(outFilePath, html)
}
}
}
function createHtml(pageData, languages) {
// Read in content
const markdown = pageData.markdown;
// Convert to html
const htmlContent = markdownConverter.makeHtml(markdown);
const translator = markdownConverter.makeHtml(pageData.translatorMarkdown);
// Fill into template
const view = Object.assign({}, pageData);
view.content = htmlContent;
view.translator = translator;
view.languages = languages;
const html = mustache.render(template, view);
return html;
}
function isMain() {
const filename = fileURLToPath(import.meta.url); // Includes the .js
const processArg = process.argv[1]; // Sometimes doesn't include the .js ?
return filename.startsWith(processArg)
}
if (isMain()) {
exportAllLanguages({outputDir: 'build/'});
}