-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marked.js
55 lines (44 loc) · 1.44 KB
/
marked.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
51
52
53
54
55
window.onload = function() {
const input = document.getElementById('markdown-input');
const preview = document.getElementById('markdown-preview');
let lastInput = '';
function updatePreview() {
const markdownText = input.value;
if (markdownText !== lastInput) {
preview.innerHTML = marked.parse(markdownText);
lastInput = markdownText;
}
}
setInterval(updatePreview, 1000);
};
function addBold() {
insertText('**bold text**');
}
function addItalic() {
insertText('_italic text_');
}
function addHeading() {
insertText('# Heading 1');
}
function addImage() {
insertText('![alt text](image-url)');
}
function addLink() {
insertText('[link text](url)');
}
function addcode() {
insertText('```md\nCode Block\n```');
}
function insertText(markdown) {
const input = document.getElementById('markdown-input');
const start = input.selectionStart;
const end = input.selectionEnd;
const text = input.value;
const before = text.substring(0, start);
const after = text.substring(end, text.length);
const newLine = (before && !before.endsWith('\n')) ? '\n\n' : '';
input.value = before + newLine + markdown + after;
input.focus();
input.setSelectionRange(start + newLine.length + markdown.length, start + newLine.length + markdown.length);
input.dispatchEvent(new Event('input'));
}