-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_4.js
More file actions
74 lines (69 loc) · 2.97 KB
/
script_4.js
File metadata and controls
74 lines (69 loc) · 2.97 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
if (typeof marked !== 'undefined') {
marked.setOptions({
gfm: true,
breaks: true,
highlight: function(code, lang) {
if (typeof hljs !== 'undefined') {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(code, { language: lang }).value;
} catch (__) {}
}
return hljs.highlightAuto(code).value;
}
return code;
}
});
}
function parseSimpleMarkdown(md) {
let html = md;
// Escape HTML
html = html.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
// Headers
html = html.replace(/^# (.*)$/gm, "<h1>$1</h1>");
html = html.replace(/^## (.*)$/gm, "<h2>$1</h2>");
html = html.replace(/^### (.*)$/gm, "<h3>$1</h3>");
html = html.replace(/^#### (.*)$/gm, "<h4>$1</h4>");
html = html.replace(/^##### (.*)$/gm, "<h5>$1</h5>");
html = html.replace(/^###### (.*)$/gm, "<h6>$1</h6>");
// Bold / Italic
html = html.replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>");
html = html.replace(/\*(.*?)\*/g, "<em>$1</em>");
// Inline code
html = html.replace(/`(.*?)`/g, "<code>$1</code>");
// Code blocks
html = html.replace(/```(.*?)\n([\s\S]*?)```/g, "<pre><code>$2</code></pre>");
// Links
html = html.replace(/\[(.*?)\]\((.*?)\)/g, "<a href='$2'>$1</a>");
// Paragraphs / Newlines
html = html.replace(/\n/g, "<br>");
return html;
}
function updatePreview(md) {
if (typeof marked !== 'undefined' && typeof marked.parse === 'function') {
document.getElementById('content').innerHTML = marked.parse(md);
} else {
document.getElementById('content').innerHTML = parseSimpleMarkdown(md);
}
if (window.renderMathInElement) {
renderMathInElement(document.getElementById('content'), {
delimiters: [
{left: '$$', right: '$$', display: true},
{left: '$', right: '$', display: false},
{left: '\(', right: '\)', display: false},
{left: '\[', right: '\]', display: true}
],
throwOnError: false
});
}
}
function setTheme(themeName) {
document.body.className = themeName;
// Switch highlight.js theme based on markdown theme dark/light mode
let hljsTheme = document.getElementById('hljs-theme');
if (themeName.includes('dark') || themeName === 'dracula' || themeName === 'monokai') {
hljsTheme.href = "https://cdn.jsdelivr.net/npm/highlight.js@11.8.0/styles/github-dark.min.css";
} else {
hljsTheme.href = "https://cdn.jsdelivr.net/npm/highlight.js@11.8.0/styles/github.min.css";
}
}