Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
115 lines (87 loc) · 3.61 KB
/
Copy pathmain.js
File metadata and controls
115 lines (87 loc) · 3.61 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
105
106
107
108
109
110
111
112
113
114
115
/*
if you encounter any issues or have feature recommendations,
write an issue & feature on : https://github.com/cagan-elden/markdjs
or send me a mail on : caganeldenk@gmail.com
this package is owned by : https://github.com/cagan-elden
*/
// this function sanitizes url's, I only allowed http, https & mailto for now
const sanitizeUrl = (url) => {
const trimmedUrl = url.trim();
if (/^(https?:\/\/|mailto:)/i.test(trimmedUrl)) {
return trimmedUrl;
}
return '#';
}
// regex for html-md rendering
const inlineRegexParser = (line) => {
return (line
.replace(/`([^`]+)`/g, '<code>$1</code>')
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
.replace(/\*([^\*]+)\*/g, '<em>$1</em>')
.replace(/\[([^\]]+)\]\(([^\)]+)\)/g, (match, str, url) => {
return `<a href="${ sanitizeUrl(url) }">${str}</a>`
})
);
};
// escapes html tags to avoid malicious script & html tags
const escapeHTML = (str) => {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
};
// I've added this so adding new tags is going to be much more easier, this is where the tags such as <h1>, <h2> etc. would go
// It is important to define rows of the map as a function, otherwise things would to break
const defaultTags = [
{
tag: (line) => line.startsWith('# '),
parse: (line) => `<h1>${ inlineRegexParser(line.slice(2)) }</h1>`
},
{
tag: (line) => line.startsWith('## '),
parse: (line) => `<h2>${ inlineRegexParser(line.slice(3)) }</h2>`
},
{
tag: (line) => line.startsWith('### '),
parse: (line) => `<h3>${ inlineRegexParser(line.slice(4)) }</h3>`
},
{
tag: (line) => line.startsWith('---'),
parse: () => '<div class="md-line"></div>'
}
];
// this is the function which does the job
const rendMD = (mdString) => {
const lineArr = escapeHTML(mdString).split(/\r?\n/);
const renderedArr = [];
let listChecker = false;
let inOrderedList = false;
for (let i=0; i < lineArr.length; i++) {
const line = lineArr[i];
const lineNoSpaces = line.trim();
// check for list tags
let isUnorderedList = (lineNoSpaces.startsWith('- ') || lineNoSpaces.startsWith('+ ') || lineNoSpaces.startsWith('* '));
let isOrderedList = /^\d+\.\s/.test(lineNoSpaces);
// I'm gonna make these much more readable & easier to add new tags in newer versions
if (isOrderedList) {
if (!inOrderedList) { renderedArr.push('<ol>'); inOrderedList=true; }
renderedArr.push(`<li>${inlineRegexParser(lineNoSpaces.slice(lineNoSpaces.indexOf('.')+1))}</li>`);
} else if (isUnorderedList) {
if (inOrderedList) { renderedArr.push('</ol>'); }
if (!listChecker) { renderedArr.push('<ul>'); listChecker = true; }
renderedArr.push(`<li>${ inlineRegexParser(lineNoSpaces.slice(2)) }</li>`);
} else {
if (inOrderedList) { renderedArr.push('</ol>'); }
if (listChecker) { renderedArr.push('</ul>'); listChecker = false; }
// tag-checking made easier to add new tags much easier
const checkForTags = defaultTags.find(rule => rule.tag(lineNoSpaces));
if (checkForTags) { renderedArr.push(checkForTags.parse(lineNoSpaces)); }
else { renderedArr.push(`<p>${ inlineRegexParser(lineNoSpaces) }</p>`) }
}
}
if (listChecker) { renderedArr.push('</ul>'); }
return renderedArr.join('');
}
export default rendMD;