-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
42 lines (33 loc) · 1.04 KB
/
index.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
/**
* Pretty HTML.
*/
module.exports = function(html) {
// remove too many white space
html = html.replace(/ ?/g, ' ');
// transfer *** --- ——— to <hr>
html = html.replace(/^\s*(?:(?:\*|\-|—)\s*){3,}\s*$/gm, '<hr>');
html = html.replace(/<p>(?:(?:\*|\-|—)\s*){3,}</g, '<p><hr><');
html = html.replace(/<br\s*\/?>(?:(?:\*|\-|—)\s*){3,}</g, '<hr><');
// clean span wrapper for ---
html = html.replace(
/<(strong|b|i|em)>(?:(?:\*|\-|—)\s*){3,}<\/(\1)>/g,
'<hr>'
);
// merge <hr><br>
html = html.replace(
/(?:<br\s*\/?>\s*)*<hr\s*\/?>\s*(?:<br\s*\/?>\s*)+/g, '<hr>'
);
// merge <p><hr></p>
html = html.replace(/<p>*<hr\s*\/?>\s*<\/p>/g, '<hr>');
// transfer -- to ——
html = html.replace(/(?!<\w+\s+)([^-])(--)([^-])(?!.*>)/g, '$1——$3');
// transfer <br> to <p>
var snippets = html.split(/(?:<br\s*\/?>[\s|\n]*){2,}/);
if (snippets.length > 1) {
html = '';
for (var i = 0; i < snippets.length; i++) {
html += '<p>' + snippets[i] + '</p>';
}
}
return html;
};