-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-slides.js
More file actions
executable file
·107 lines (80 loc) · 3.01 KB
/
build-slides.js
File metadata and controls
executable file
·107 lines (80 loc) · 3.01 KB
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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const {execSync} = require('child_process');
require('dotenv').config();
const slidesConfig = {
"src": process.env.REMARK_SLIDES_SRC || "src/md",
"output": process.env.REMARK_SLIDES_OUTPUT || "dist/",
"templateSrc": process.env.REMARK_SLIDES_TEMPLATES_SRC || "src",
"contentTemplateFile": process.env.REMARK_SLIDES_TEMPLATES_SRC + "/" + process.env.REMARK_SLIDES_CONTENT_TEMPLATE || "src/index.dynamic.template.html",
"staticDir": process.env.REMARK_STATIC_DIR || "."
};
let cwd = process.cwd();
const argv = require('optimist').argv;
let isWatchModeEnabled = !!argv.watch;
const srcDirPath = path.resolve(cwd, slidesConfig.src);
const templatesSrc = path.resolve(cwd, slidesConfig.templateSrc);
const outDirPath = path.resolve(cwd, slidesConfig.output);
createDir(outDirPath);
const customTemplateFile = path.resolve(cwd, slidesConfig.contentTemplateFile);
const distTemplateFile = path.resolve(cwd, slidesConfig.output + "/" + process.env.REMARK_SLIDES_CONTENT_TEMPLATE);
const staticDir = slidesConfig.staticDir;
let templatesConfigJson = {
"header": "",
"footer": ""
};
function init() {
initHeaderFooterTemplates();
createDistTemplateFile();
let files = fs.readdirSync(srcDirPath);
files.forEach(function (file) {
let destFile = getDestFile(file), srcFile = getSrcFile(file);
let command = 'npx markdown-to-slides -l ' + distTemplateFile + ' ' + srcFile + ' -o ' + destFile;
var convertToSlides = function() {
execSync(command);
updateHeaderFooter(destFile);
};
if (isWatchModeEnabled) {
convertToSlides();
fs.watchFile(srcFile, { interval: 100 }, function () {
convertToSlides();
});
} else {
convertToSlides();
}
});
}
function updateHeaderFooter(file) {
replacePlaceHolders(file, {
"{{header}}": templatesConfigJson.header,
"{{footer}}": templatesConfigJson.footer
});
}
function getSrcFile(mdFileName) {
return path.resolve(srcDirPath, mdFileName);
}
function getDestFile(mdFileName) {
return path.resolve(outDirPath, path.parse(mdFileName).name + ".html");
}
function createDistTemplateFile() {
fs.copyFileSync(customTemplateFile, distTemplateFile);
replacePlaceHolders(distTemplateFile, {"{{staticPath}}": staticDir});
}
function replacePlaceHolders(file, placeHolderObj) {
let data = fs.readFileSync(file, 'utf8');
for (const plcHldr in placeHolderObj) {
data = data.replace(new RegExp(plcHldr, "ig"), placeHolderObj[plcHldr]);
}
fs.writeFileSync(file, data, 'utf8');
}
function createDir(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
function initHeaderFooterTemplates() {
templatesConfigJson["header"] = fs.readFileSync(templatesSrc + "/header.template.html", 'utf8');
templatesConfigJson["footer"] = fs.readFileSync(templatesSrc + "/footer.template.html", 'utf8');
}
init();