-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
65 lines (58 loc) · 1.95 KB
/
parser.ts
File metadata and controls
65 lines (58 loc) · 1.95 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
const parser = (markdown: string): string => {
let html = "<!DOCTYPE html><html><head><meta charset='UTF-8'><link rel='stylesheet' href='/dist/output.css'></head><body class='prose'>";
const lines = markdown.split("\n");
let inOrderedList = false;
let inUnorderedList = false;
const parseInlineElements = (text: string): string => {
// Bold
text = text.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
// Italics
text = text.replace(/\*(.+?)\*/g, "<em>$1</em>");
// Inline code
text = text.replace(/`(.+?)`/g, "<code>$1</code>");
// Inline links
text = text.replace(/\[(.+?)\]\((.+?)\)/g, "<a href='$2'>$1</a>");
return text;
};
lines.forEach((line) => {
line = parseInlineElements(line.trim());
if (line.startsWith("# ")) {
html += `<h1 class='text-3xl font-bold underline'>${line.substring(2)}</h1>`;
} else if (line.startsWith("## ")) {
html += `<h2 class='text-2xl font-bold'>${line.substring(3)}</h2>`;
} else if (line.startsWith("### ")) {
html += `<h3 class='text-xl font-bold'>${line.substring(4)}</h3>`;
} else if (line.match(/^\d+\./)) {
if (!inOrderedList) {
html += "<ol class='list-decimal list-inside'>";
inOrderedList = true;
}
html += `<li>${line.substring(line.indexOf(".") + 1).trim()}</li>`;
} else if (line.startsWith("-")) {
if (!inUnorderedList) {
html += "<ul class='list-disc list-inside'>";
inUnorderedList = true;
}
html += `<li>${line.substring(1).trim()}</li>`;
} else if (line.trim() !== "") {
if (inOrderedList) {
html += `</ol>`;
inOrderedList = false;
}
if (inUnorderedList) {
html += `</ul>`;
inUnorderedList = false;
}
html += `<p>${line}</p>`;
}
});
if (inOrderedList) {
html += `</ol>`;
}
if (inUnorderedList) {
html += `</ul>`;
}
html += "</body></html>";
return html;
};
export default parser;