-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMdToHtml.js
40 lines (40 loc) · 952 Bytes
/
MdToHtml.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
function renderKatex(str){
katex.render(str,document.getElementById("katex-render-temp"),{
throwOnError:false
});
let res=document.getElementById("katex-render-temp").innerHTML;
document.getElementById("katex-render-temp").innerHTML="";
return res;
}
function findChar(str,pos,ch){
while(pos<str.length){
if(str[pos]==ch)
return pos;
}
return -1;
}
function mdToHtml(str){
let res="";
let pos=0;
while(pos<str.length){
if(str[pos]=="$"){
if(pos<str.length-1&&str[pos+1]=="$"){
let lst=findChar(str,pos+2,"$");
if(lst!=-1){
res+="<div class=\"katex katex-between-line\">"
+renderKatex(str.substr(pos+2,(lst-1)-(pos+2)+1))
+"</div>";
}
pos=lst+2;
}
else{
let lst=findChar(str,pos+1,"$");
if(lst!=-1){
res+="<span class=\"katex katex-in-line\">"
+renderKatex(str.substr(pos+1,(lst-1)-(pos+1)+1))
+"</div>"
}
}
}
}
}