-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDEditor.js
134 lines (95 loc) · 3.76 KB
/
MDEditor.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const MDEditor = function(options) {
this.options = options;
this.EditorDivs = document.querySelectorAll(this.options.div);
const render = () => {
this.EditorDivs.forEach((el) => {
let html = el.innerHTML;
let htmlcpy = html;
html = html.replace(/(\*\*\*(.+)\*\*\*)/g , "<strong><i>$2</i></strong>");
html = html.replace(/(\*\*(.+)\*\*)/g , "<strong>$2</strong>");
html = html.replace(/(\*(.+)\*)/g , "<i>$2</i>");
html = html.replace(/(\_\_\_(.+)\_\_\_)/g , "<strong><i>$2</i></strong>");
html = html.replace(/(\_\_(.+)\_\_)/g , "<strong>$2</strong>");
html = html.replace(/(\_(.+)\_)/g , "<i>$2</i>");
html = functions.headingTags(html);
html = functions.backQuote(html);
html = functions.codeTag(html);
html = functions.horizontalRule(html);
html = functions.imageTag(html);
html = functions.linkTag(html);
html = functions.ulList(html);
html = functions.markTag(html);
if(html === htmlcpy) return;
el.innerHTML = html
});
}
let getHTML = () => {
let htmlArray = [];
this.EditorDivs.forEach((el) => {
render();
let html = el.innerHTML;
htmlArray.push(html);
});
return htmlArray;
}
const functions = {
makeEditable : () => {
this.EditorDivs.forEach((el) => {
el.contentEditable = true;
});
},
headingTags : (html) => {
if(this.options.headings === false) return html;
html = html.replace(/\#\#\#\#\#\#\#\# (.+)/g , "<h8>$1</h8>");
html = html.replace(/\#\#\#\#\#\#\# (.+)/g , "<h7>$1</h7>");
html = html.replace(/\#\#\#\#\#\# (.+)/g , "<h6>$1</h6>");
html = html.replace(/\#\#\#\#\# (.+)/g , "<h5>$1</h5>");
html = html.replace(/\#\#\#\# (.+)/g , "<h4>$1</h4>");
html = html.replace(/\#\#\# (.+)/g , "<h3>$1</h3>");
html = html.replace(/\#\# (.+)/g , "<h2>$1</h2>");
html = html.replace(/\# (.+)/g , "<h1>$1</h1>");
html = html.replace(/\<br\>/, "");
return html;
},
backQuote : (html) => {
if(this.options.quote === false) return html
html = html.replace(/\;/, "")
html = html.replace(/\> (.+)/g, "<blockquote>$1</blockquote>");
html = html.replace(/\<br\>/, "")
return html;
},
codeTag : (html) => {
if(this.options.code === false) return html;
html = html.replace(/\`(.+)\`/g, "<code>$1</code>");
return html
},
horizontalRule : (html) => {
if(this.options.horizontalRule === false) return html;
html = html.replace(/\-\-\-/g, "<hr>");
return html;
},
linkTag : (html) => {
if(this.options.links === false) return html;
html = html.replace(/\[(.+)\]\((.+)\)/g, `<a href = "$2">$1</a>`);
return html;
},
imageTag : (html) => {
if(this.options.images === false) return html;
html = html.replace(/\!\[(.+)\]\((.+)\)/g, `<img src="$2" alt="$1"></img>`);
return html;
},
ulList : (html) => {
if(this.options.ulList === false) return html;
html = html.replace(/(\- )/mg, "• ");
return html;
},
markTag : (html) => {
if(this.options.mark === false) return html;
html = html.replace(/\=\=(.+)\=\=/ , "<mark>$1</mark>");
return html;
},
}
this.options.editable ? functions.makeEditable() : "";
this.render = render;
this.getHTML = getHTML;
}