-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·92 lines (68 loc) · 2.47 KB
/
index.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
#! /usr/bin/env node
'use strict';
var fs = require('fs');
var path = require('path');
var ajv = require('ajv')({ "useDefaults": true, "allErrors": true });
var doT = require('dot');
var config = JSON.parse(fs.readFileSync(process.argv[2] || 'site.json'));
var schema = require('./site_config_schema.json');
var valid = ajv.validate(schema, config);
config.repository = escapePath(config.repository);
if (!valid) {
console.log('site configuration file is invalid')
console.log(ajv.errorsText(ajv.errors, { separator: '\n' }));
process.exit(1);
}
var frontMatter, navTemplate, repoPattern;
prepareTemplates();
config.pages.forEach(createPage);
if (config.navigation) generateNavigation();
function prepareTemplates() {
doT.templateSettings.strip = false;
frontMatter = fs.readFileSync(path.join(__dirname, 'front_matter.dot'));
frontMatter = doT.compile(frontMatter);
navTemplate = fs.readFileSync(path.join(__dirname, 'navigation.dot'));
navTemplate = doT.compile(navTemplate);
}
function createPage(page) {
readFile(page);
replaceLinks(page);
addFrontMatter(page);
writePage(page);
}
function generateNavigation() {
try { fs.mkdirSync(config.folders.includes); } catch (e) {}
var navHtml = navTemplate({ navigation: config.navigation, site: config.folders.site });
var navPath = path.join(config.folders.includes, 'nav.html');
fs.writeFileSync(navPath, navHtml);
}
function readFile(page) {
var sourcePath = path.join(config.folders.source, page.file);
page.content = fs.readFileSync(sourcePath, 'utf8');
}
var rootRegex;
function replaceLinks(page) {
config.pages.forEach(replaceLinksToPage);
replaceLinksToRoot();
function replaceLinksToPage(toPage) {
if (!toPage.linkRegex) {
var pagePathPattern = config.repository + escapePath('/blob/master/' + toPage.file);
toPage.linkRegex = new RegExp(pagePathPattern, 'g');
}
page.content = page.content.replace(toPage.linkRegex, toPage.page + '.html');
}
function replaceLinksToRoot() {
rootRegex = rootRegex || new RegExp(config.repository + '(?:\\/)?#', 'g');
page.content = page.content.replace(rootRegex, '/' + config.folders.site + '#');
}
}
function addFrontMatter(page) {
page.content = frontMatter({ config: config, page: page }) + page.content;
}
function writePage(page) {
var targetPath = path.join(config.folders.target, page.page + '.md');
fs.writeFileSync(targetPath, page.content);
}
function escapePath(str) {
return str.replace(/(\/|\.)/g, '\\$1');
}