-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTMLshl.js
34 lines (34 loc) · 1.8 KB
/
HTMLshl.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
export class HTMLshl{
static highlight(htmlString){
var c = false;
return "<pre>" + htmlString.replaceAll(/(<(.*?)>)/gmis, function(a, m, p1){
if(p1?.startsWith("!--") && !c){
if(p1?.length > 5 && p1?.endsWith("--")){
return "<span class=\"comment\"><"+p1+"></span>";
}else{
c = true;
return "<span class=\"comment\"><"+p1+">";
}
}else if(p1?.endsWith("--")){
c = false;
return "<"+p1+"></span>";
}else if(!c){
return "<span class=\"tag\"><" + p1.replaceAll(/(\r\n|\r|\n|\s| )([^\=\s]+)(\=\"[^\"]*\"|\=\'[^\']*\'|\=[^\"\'][\w\"\'\-\*\,\.;\:\!\?\^\°\#\+\$\§\%\&\/\(\)\{\}\[\]\=\~\<\>\|\@]*(?=[\r\n|\r|\n|\s| |$])|)/gmis, function(a, m, p1, p2){
if(p2){
if(p2?.startsWith("=\"") && p2?.endsWith("\"")){
return m + "<span class=\"attribute\">" + p1 + "=\"<span class=\"property\">" + p2.substr(2).slice(0, -1) + "</span>\"</span>";
}else if(p2?.startsWith("=\'") && p2?.endsWith("\'")){
return m + "<span class=\"attribute\">" + p1 + "=\'<span class=\"property\">" + p2.substr(2).slice(0, -1) + "</span>\'</span>";
}else{
return m + "<span class=\"attribute\">" + p1 + "=<span class=\"property\">" + p2 .substr(1)+ "</span></span>";
}
}else{
return m + "<span class=\"attribute\">" + p1 + "</span>";
}
}) + "></span>";
}else{
return "<"+p1+">";
}
}) + "</pre>";
}
}