-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdownToHTML.js
More file actions
104 lines (90 loc) · 3.99 KB
/
markdownToHTML.js
File metadata and controls
104 lines (90 loc) · 3.99 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function MDtoHTML(markdown) {
// Reference Library
// ---------------------------------------
// ---------------------------------------
var notp = ['####### ', '###### ', '##### ', '#### ', '### ', '## ', '# ', '---', '- ']
var headings = [
['####### ', ['<h7>', '</h7>']],
['###### ', ['<h6>', '</h6>']],
['##### ', ['<h5>', '</h5>']],
['#### ', ['<h4>', '</h4>']],
['### ', ['<h3>', '</h3>']],
['## ', ['<h2>', '</h2>']],
['# ', ['<h1>', '</h1>']]
]
var b1 = true;
var bold = ['**', ['<b>', '</b>']]
var i1 = true;
var italics = ['*', ['<i>', '</i>']]
var hr = ['---', '<hr>']
var ul = ['- ', ['<ul>', '<li>', '</li>', '</ul>']]
listLineIndex = []
// ---------------------------------------
// ---------------------------------------
let lines = markdown.split(/\r\n|\n/);
// Set heading and bold tags
// ---------------------------------------
// ---------------------------------------
for (line in lines) {
if (!(lines[line].includes(notp[0])) && !(lines[line].includes(notp[1])) && !(lines[line].includes(notp[2])) && !(lines[line].includes(notp[3])) && !(lines[line].includes(notp[4])) && !(lines[line].includes(notp[5])) && !(lines[line].includes(notp[6])) && !(lines[line].includes(notp[7])) && !(lines[line].includes(notp[8]))) {
lines[line] = '<p>' + lines[line] + '</p>';
}
}
// Set heading and bold tags
// ---------------------------------------
// ---------------------------------------
for (i=0; i<headings.length; i++) {
for (line in lines) {
if (lines[line].includes(headings[i][0])) {
lines[line] = lines[line].replace(headings[i][0], headings[i][1][0]) + headings[i][1][1];
}
if (lines[line].includes(hr[0])) {
lines[line] = lines[line].replace(hr[0], hr[1]);
}
if (lines[line].includes(bold[0]) && b1) {
b1 = false;
lines[line] = lines[line].replace(bold[0], bold[1][0]);
} else if (lines[line].includes(bold[0]) && !b1) {
b1 = true;
lines[line] = lines[line].replace(bold[0], bold[1][1]);
}
}
}
// Set Italics
// ---------------------------------------
// ---------------------------------------
for (line in lines) {
if (lines[line].includes(italics[0]) && i1) {
i1 = false;
lines[line] = lines[line].replace(italics[0], italics[1][0]);
}
if (lines[line].includes(italics[0]) && !i1) {
i1 = true;
lines[line] = lines[line].replace(italics[0], italics[1][1]);
}
}
// Set UL
// ---------------------------------------
// ---------------------------------------
for (line in lines) { // make index of lines making a list
if (lines[line].includes(ul[0])) {
listLineIndex.push(line);
}
}
for (x in listLineIndex) {
if (x == 0 && lines[listLineIndex[x]].includes(ul[0])) {
// console.log(lines[listLineIndex[x]])
lines[listLineIndex[x]] = lines[listLineIndex[x]].replace(ul[0], ul[1][0] + ul[1][1]) + ul[1][2];
listLineIndex[x] = -1;
// console.log(lines[listLineIndex[x]])
} else if (x == listLineIndex.length && lines[listLineIndex[x]].includes(ul[0])) {
lines[listLineIndex[x]] = lines[listLineIndex[x]].replace(ul[0], ul[1][1]) + ul[1][2] + ul[1][3];
listLineIndex[x] = -1;
} else if (lines[listLineIndex[x]].includes(ul[0])) {
lines[listLineIndex[x]] = lines[listLineIndex[x]].replace(ul[0], ul[1][1]) + ul[1][2];
listLineIndex[x] = -1;
}
}
console.log(lines)
return lines.join('') // Finally returns html for of the markdown text!
}