-
Notifications
You must be signed in to change notification settings - Fork 5
/
render.js
94 lines (82 loc) · 2.47 KB
/
render.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
const path = require("path");
const fs = require("fs");
const os = require("os");
const http = require("http");
const url = require("url");
const mume = require("@shd101wyy/mume");
function readFile(file, options) {
return new Promise((resolve, reject) => {
fs.readFile(file, options, (error, text) => {
if (error) {
return reject(error.toString());
}
else {
return resolve(text.toString());
}
});
});
}
function writeFile(file, text, options) {
return new Promise((resolve, reject) => {
fs.writeFile(file, text, options, (error) => {
if (error) {
return reject(error.toString());
}
else {
return resolve();
}
});
});
}
async function render(inputFile, outputFile, darkMode) {
const configPath = path.resolve(os.homedir(), ".mume");
await mume.init(configPath);
const engine = new mume.MarkdownEngine({
filePath: inputFile,
config: {
configPath: configPath,
previewTheme: darkMode ? "atom-dark.css" : "atom-light.css",
mermaidTheme: darkMode ? "mermaid.dark.css" : "mermaid.css",
codeBlockTheme: darkMode ? "atom-dark.css" : "atom-light.css",
printBackground: true,
enableScriptExecution: true,
},
});
const inputString = await readFile(inputFile, {
encoding: "utf-8",
});
let html;
let yamlConfig;
({ html, yamlConfig } = await engine.parseMD(inputString, {
useRelativeFilePath: false,
hideFrontMatter: true,
isForPreview: false,
runAllCodeChunks: true,
}));
html = await engine.generateHTMLTemplateForExport(html, yamlConfig, {
isForPrint: false,
isForPrince: false,
offline: true,
embedLocalImages: false,
});
await writeFile(outputFile, html);
}
async function server_handler(req, resp) {
const q = url.parse(req.url, true).query;
const inputFile = q.input_file;
const outputFile = q.output_file;
const darkMode = q.dark_mode == "true";
resp.writeHead(200, {'Content-Type': 'text/plain'});
try {
await render(inputFile, outputFile, darkMode);
resp.end('ok');
} catch (err) {
resp.end(err.message);
}
}
async function main() {
const argv = process.argv;
const port = Number(argv[2]);
http.createServer(server_handler).listen(port);
}
main()