Repository navigation
Expand file tree
/
Copy pathconvert.ts
More file actions
79 lines (61 loc) · 1.83 KB
/
Copy pathconvert.ts
File metadata and controls
79 lines (61 loc) · 1.83 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
import { convert2img } from "mdimg";
import fs from "fs";
const INPUT_DIR = "contents";
const OUTPUT_DIR = "public";
const contents: { [key: string]: string[] } = {};
async function convertSingleDir(dir: string) {
const pageMap: { [key: string]: string } = {};
const promises = fs.readdirSync(dir).map(async (file) => {
if (file.endsWith(".md")) {
const fileName = `${dir}/${file.replace(".md", ".png")}`;
const convertRes = await convert2img({
mdFile: `${dir}/${file}`,
outputFilename: `public/${fileName}`,
width: 600,
cssTemplate: "github",
});
if (convertRes.error) {
console.error(convertRes.error);
}
pageMap[file] = `/${fileName}`;
}
});
await Promise.all(promises);
const pages: string[] = [];
fs.readdirSync(dir).forEach((file) => {
if (file.endsWith(".md")) {
pages.push(pageMap[file]);
}
});
if (pages.length) {
contents[dir] = pages;
}
}
async function createIndexPage(slides: string[]) {
const indexMD = `
# Slides
${slides.map((slide) => `- ${slide.replace("contents/", "")}`).join("\n")}
Input a slide name for browsing.
`;
const convertRes = await convert2img({
mdText: indexMD,
outputFilename: `public/contents/index.png`,
width: 600,
cssTemplate: "github",
});
if (convertRes.error) {
console.error(convertRes.error);
}
}
fs.existsSync(OUTPUT_DIR) && fs.rmSync(OUTPUT_DIR, { recursive: true });
const promises = fs
.readdirSync(INPUT_DIR, { withFileTypes: true })
.map(async (dir) => {
if (dir.isDirectory()) {
await convertSingleDir(`${INPUT_DIR}/${dir.name}`);
}
});
await Promise.all(promises);
fs.writeFileSync("public/contents.json", JSON.stringify({ contents }, null, 2));
await createIndexPage(Object.keys(contents));
console.log("Conversion complete!");