-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (44 loc) · 1.59 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
38
39
40
41
42
43
44
45
46
47
48
49
50
document.addEventListener("DOMContentLoaded", function () {
var editor = CodeMirror(document.getElementById("editor"), {
mode: "markdown",
lineNumbers: true,
theme: "base16-dark",
autoCloseBrackets: true,
});
// Ensure CodeMirror expands to full height
editor.setSize("100%", "100%");
// Function to insert markdown text at cursor position
function insertMarkdown(markdown) {
const doc = editor.getDoc();
const cursor = doc.getCursor();
doc.replaceRange(markdown, cursor);
}
// Function to toggle between editor and preview mode
function togglePreview() {
const preview = document.getElementById("preview");
const editorContainer = document.getElementById("editor");
if (!preview.classList.contains("active")) {
preview.innerHTML = marked(editor.getValue());
preview.classList.add("active");
editorContainer.style.opacity = "0";
} else {
preview.classList.remove("active");
editorContainer.style.opacity = "1";
}
}
// Function to download markdown content
function downloadMarkdown() {
const text = editor.getValue();
const filename =
document.getElementById("downloadmd").value || "document.md";
const blob = new Blob([text], { type: "text/markdown" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = filename;
a.click();
}
// Exposing functions to global scope
window.insertMarkdown = insertMarkdown;
window.togglePreview = togglePreview;
window.downloadMarkdown = downloadMarkdown;
});