-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
36 lines (29 loc) · 1.14 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
const editor = CodeMirror(document.getElementById('editor'), {
mode: 'markdown',
lineNumbers: true,
theme: 'monokai',
lineWrapping: true,
direction: 'rtl',
});
const preview = document.getElementById('preview');
function detectDirection(text) {
const rtlChars = /[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]/;
return rtlChars.test(text) ? 'rtl' : 'ltr';
}
function updatePreview() {
const markdownContent = editor.getValue();
const renderedHTML = marked.parse(markdownContent);
const direction = detectDirection(markdownContent);
preview.style.direction = direction;
preview.innerHTML = renderedHTML;
}
function syncScroll() {
const editorScroll = editor.getScrollInfo();
const previewScrollHeight = preview.scrollHeight - preview.clientHeight;
const scrollRatio = editorScroll.top / (editorScroll.height - editorScroll.clientHeight);
preview.scrollTop = scrollRatio * previewScrollHeight;
}
editor.on('change', updatePreview);
editor.on('scroll', syncScroll);
editor.setValue('# Welcome to the Markdown Editor\n\nType your markdown on the left, and see the rendered result here!');
updatePreview();