-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
80 lines (67 loc) · 2.7 KB
/
Copy pathmain.ts
File metadata and controls
80 lines (67 loc) · 2.7 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
80
class Features {
public bold = true;
public breaks = true;
public code = true;
public headings = true;
public horizontalRule = true;
public italics = true;
public images = true;
public links = true;
public strikethrough = true;
}
interface Reference {
markdown: string;
}
function transformHeadings(reference: Reference): void {
reference.markdown = reference.markdown.replace(/^# ([^\n]+)/gm, '<h1>$1</h1>');
reference.markdown = reference.markdown.replace(/^## ([^\n]+)/gm, '<h2>$1</h2>');
reference.markdown = reference.markdown.replace(/^### ([^\n]+)/gm, '<h3>$1</h3>');
}
function transformHorizontalRule(reference: Reference): void {
reference.markdown = reference.markdown.replace(/^---$/gm, '<hr>');
}
function transformBold(reference: Reference): void {
reference.markdown = reference.markdown.replace(/\*([^\*]+)\*/gm, '<strong>$1</strong>');
}
function transformItalics(reference: Reference): void {
reference.markdown = reference.markdown.replace(/_([^_]+)_/gm, '<em>$1</em>');
}
function transformStrikethrough(reference: Reference): void {
reference.markdown = reference.markdown.replace(/~([^~]+)~/gm, '<s>$1</s>');
}
function transformCode(reference: Reference): void {
reference.markdown = reference.markdown.replace(/```\n([^`]+)\n```/gm, '<pre><code>$1</code></pre>');
reference.markdown = reference.markdown.replace(/```([^`]+)```/gm, '<code>$1</code>');
}
function transformImages(reference: Reference): void {
reference.markdown = reference.markdown.replace(/!\[([^\]]+)\]\(([^\)]+)\)/g, '<img src="$2" alt="$1">');
}
function transformLinks(reference: Reference): void {
reference.markdown = reference.markdown.replace(/\[([^\]]+)\]\(([^\)]+)\)/g, '<a href="$2">$1</a>');
}
function transformBreaks(reference: Reference): void {
reference.markdown = reference.markdown.replace(/\s\s\n/g, '<br>');
reference.markdown = reference.markdown.replace(/\n\n/g, '<br>');
}
/**
* Parse markdown into HTML.
* @param markdown The markdown string.
* @param features _(Optional)_ A record of features to enable/disable.
* @returns The parsed HTML string.
*/
export function parse(
markdown: string,
features: Partial<Features> = new Features(),
): string {
const reference: Reference = { markdown };
if (features.headings) transformHeadings(reference);
if (features.horizontalRule) transformHorizontalRule(reference);
if (features.bold) transformBold(reference);
if (features.italics) transformItalics(reference);
if (features.strikethrough) transformStrikethrough(reference);
if (features.code) transformCode(reference);
if (features.images) transformImages(reference);
if (features.links) transformLinks(reference);
if (features.breaks) transformBreaks(reference);
return reference.markdown;
}