File size: 2,019 Bytes
af9a3da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d763513
 
af9a3da
 
 
 
d88bc0c
 
af9a3da
 
 
d88bc0c
 
 
af9a3da
 
d88bc0c
af9a3da
d88bc0c
af9a3da
 
 
 
 
d88bc0c
 
 
 
 
 
 
 
 
 
 
 
af9a3da
 
1daa44a
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
class RenderSymbols{
    constructor(){

    }
    renderAll(elem){
        this.renderText(elem);
        this.renderCode(elem);
        this.renderMath(elem);
        elem.scrollIntoView({behavior: "smooth", block: "end"});
    }
    renderMath(elem) {
        let content = elem.innerHTML;
        MathJax.typesetPromise([elem])
            .then(() => {
                
            })
            .catch((err) => {

            });
    }
    renderText(elem){
        elem.innerHTML = elem.innerHTML.replace(/<br\s*\/?>/g, '\n');
        elem.innerHTML = elem.innerHTML.replace(/\#\#\#(.*?)\#\#\#/g, '<span class="heading">$1</span>');
        elem.innerHTML = elem.innerHTML.replace(/\*\*\*(.*?)\*\*\*/g, '<span class="boldText">$1</span>');
        elem.innerHTML = elem.innerHTML.replace(/\*\*(.*?)\*\*/g, '<span class="subHeading">$1</span>');
        elem.innerHTML = elem.innerHTML.replace(/\n/g, '<br>');
    }
    renderCode(element) {
        let content = element.innerHTML;
    
        if (content.includes("```") && content.split("```").length >= 3) {
            content = content.replace(/```(\w*)<br>([\s\S]*?)```/g, (match, language, code) => {
                code = code.replace(/<br>/g, '\n').trim();
                language = language.trim() || 'text';    
                code = this.escapeHTML(code);
    
                return `<pre class="code-block"><code class="language-${language}">${code}</code></pre>`;
            });
    
            element.innerHTML = content;
    
            element.querySelectorAll('code:not(.hljs)').forEach(block => {
                hljs.highlightElement(block);
            });
        }
    }
    escapeHTML(str) {
        return str.replace(/[&<>"']/g, function (char) {
            const map = {
                '&': '&amp;',
                '<': '&lt;',
                '>': '&gt;',
                '"': '&quot;',
                "'": '&#39;',
            };
            return map[char] || char;
        });
    }
  
}
export default RenderSymbols