-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-markdown.js
78 lines (69 loc) · 1.91 KB
/
compile-markdown.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
// Define the skeleton for index.html
let indexHtml = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Book wiki</title>
<link rel="stylesheet" href="css/wpln.css">
</head>
<body>
<h1>Book wiki</h1>
<ul>`
// Function to check if pandoc is installed by trying to get its version
async function checkPandocInstalled() {
try {
await new Deno.Command("pandoc", { args: ["--version"] }).output()
} catch (error) {
console.error("Error: pandoc is not installed.")
Deno.exit(1)
}
}
// Function to convert markdown files to HTML
async function convertMarkdownToHtml() {
for await (const entry of Deno.readDir("markdown/")) {
if (entry.isFile && entry.name.endsWith(".md")) {
const markdownFilePath = `markdown/${entry.name}`
const htmlFileName = markdownFilePath
.replace(".md", ".html")
.replace("markdown", "html")
console.log(`${entry.name} → ${htmlFileName}`)
try {
let args = [
"-o",
htmlFileName,
"-f",
"markdown",
"--include-before-body",
"header.template.html",
"--standalone",
"--css",
"css/wpln.css",
markdownFilePath,
]
await new Deno.Command("pandoc", {
args,
}).output()
// console.log(args.join` `)
indexHtml += `<li><a href="${htmlFileName.split('/').pop()}">${
entry.name.replace(".md", "")
}</a></li>`
} catch (error) {
console.error(`Error converting file ${entry.name}: ${error}`)
}
}
}
}
// Function to write the index.html file
async function writeIndexHtml() {
indexHtml = `${indexHtml}\n</ul>
</body>
</html>`
await Deno.writeTextFile("html/index.html", indexHtml)
}
// Main function to orchestrate the conversion process
async function main() {
await checkPandocInstalled()
await convertMarkdownToHtml()
await writeIndexHtml()
}
main()