-
Notifications
You must be signed in to change notification settings - Fork 3
/
htmlAssembler.js
46 lines (41 loc) · 1.06 KB
/
htmlAssembler.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
/* eslint-disable no-undef */
const path = require("path");
var md = require("markdown-it")();
module.exports = htmlAssembler = (lines, txtInput, ss = "", argv_l) => {
let paragraphs = "";
try {
lines = lines.toString().split(/\r?\n\r?\n/);
} catch (err) {
throw "Error:" + err;
}
let title = lines.shift();
lines.forEach((string) => {
if (path.extname(txtInput) == ".md") {
string = md.render(string);
}
if (path.extname(txtInput) == ".txt") {
paragraphs +=
string.match(/<h1>/) || string.match(/<h2>/) || string.match(/<hr>/)
? `${string}\n`
: `<p>${string}</p>\n`;
} else {
paragraphs += string;
}
});
let styleSheet = ss ? `<link rel="stylesheet" href="${ss}"></link>` : "";
let h1 = title ? `<h1>${title}</h1>` : "";
const content = `<!doctype html>
<html lang="${argv_l}">
<head>
<meta charset="utf-8">
<title>${title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${styleSheet}
</head>
<body>
${h1}
${paragraphs}
</body>
</html>`;
return content;
};