This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md.js
90 lines (81 loc) · 3.13 KB
/
md.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
const marked = require("marked");
const path = require("path");
const fs = require("fs");
const cheerio = require("cheerio");
if (!fs.existsSync("./data")) {
// Data folder does not exist, create it
fs.mkdirSync("./data");
}
if (!fs.existsSync("./data/markdown-docs.json")) {
fs.writeFileSync("./data/markdown-docs.json", JSON.stringify([]), { encoding: "utf8" });
}
marked.use({
mangle: false,
headerIds: false
});
fs.writeFileSync("./data/markdown-docs.json", JSON.stringify([], null, 4), function (err) {
if (err) {
console.error("Error writing to log file:", err);
}
});
function readFilesInDirectory(directoryPath) {
const files = fs.readdirSync(directoryPath);
files.forEach((file) => {
const filePath = path.join(directoryPath, file);
const stat = fs.statSync(filePath);
if (stat.isFile()) {
const folder = path.basename(directoryPath);
const markdownContent = fs.readFileSync(filePath, "utf8");
const html = marked.parse(markdownContent);
const $ = cheerio.load(html);
const h1Regex = /<h1\b[^>]*>(.*?)<\/h1>/;
const h1Match = html.match(h1Regex);
const h2Element = $("h2").first();
const heading = h2Element.text().trim();
let text = "";
const nextElements = h2Element.nextAll();
nextElements.each((index, element) => {
if ($(element).is("h2")) {
return false; // Stop iteration if another h2 element is encountered
}
text += $(element).text().trim() + " ";
});
text = text.trim();
let truncatedText;
if (text) {
const words = text.split(" ");
truncatedText = words.slice(0, 20).join(" ");
if (words.length > 20) {
truncatedText += "...";
}
} else {
truncatedText = "No text available.";
}
if (h1Match && h1Match[1]) {
const h1Text = h1Match[1];
const markdown_json = JSON.parse(fs.readFileSync("./data/markdown-docs.json", { encoding: "utf8" }), null, 2);
if (folder == "docs") {
markdown_json.push({
title: h1Text,
url: `${file.replace(".md", "")}`,
description: truncatedText
});
} else {
markdown_json.push({
title: h1Text,
url: `${folder}/${file.replace(".md", "")}`,
description: truncatedText
});
}
fs.writeFileSync("./data/markdown-docs.json", JSON.stringify(markdown_json, null, 4), (err) => {
if (err) {
console.error("Error writing to log file:", err);
}
});
}
} else if (stat.isDirectory()) {
readFilesInDirectory(filePath);
}
});
}
readFilesInDirectory(path.resolve("assets/docs"));