forked from hirentimbadiya/Markdown-Previewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
37 lines (29 loc) · 1.12 KB
/
script.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
const markdownInput = document.getElementById("markdow-input");
const markdownPreview = document.getElementById("markdown-preview");
function toggleTheme() {
const body = document.body;
const slider = document.getElementById('slider');
if (body.classList.contains('light-mode')) {
body.classList.remove('light-mode');
body.classList.add('dark-mode');
slider.classList.remove('active-sun');
slider.classList.add('active-moon');
} else {
body.classList.remove('dark-mode');
body.classList.add('light-mode');
slider.classList.remove('active-moon');
slider.classList.add('active-sun');
}
}
function updatePreview() {
let inputText = markdownInput.value;
inputText = inputText.replace(/```([^`]+)```/g, (_, code) => {
return `<pre class="code-block"><code>${code.replace(/\n/, '')}</code></pre>`;
}).replace(/`([^`]+)`/g, (_, code) => {
return `<code class="code-block">${code.replace(/\n/, '')}</code>`;
});
const htmlOutput = marked.parse(inputText);
markdownPreview.innerHTML = htmlOutput;
}
markdownInput.addEventListener("input", updatePreview);
updatePreview();