This repository has been archived by the owner on Oct 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
54 lines (51 loc) · 1.44 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
51
52
53
54
'use strict';
var editor, preview;
var saved = true;
document.addEventListener("DOMContentLoaded", function () {
editor = document.getElementById('editor');
preview = document.getElementById('preview');
editor.onchange = editor.onkeyup = convert;
if (storageAvailable) {
editor.value = localStorage.draft || '';
convert();
autosave();
}
new Clipboard('#copy', {
text: function () {
return preview.innerHTML;
}
})
})
function convert() {
saved = false;
var out = editor.value;
out = out.replace(/( (?= ))/g, ' ');
out = out.replace(/(\*{3})(.*?)\1/g, '<strong><em>$2</em></strong>');
out = out.replace(/(\*{3})(.+?)\*{2}(.+?)\*/g, '<em><strong>$2</strong>$3</em>');
out = out.replace(/(\*{2})(.*?)\1/g, '<strong>$2</strong>');
out = out.replace(/(\*)(.*?)\1/g, '<em>$2</em>');
out = out.replace(/\[([^\[]+)\]\(([^\)]+)\)/g, '<a href=\'$2\'>$1</a>');
out = out.replace(/\n/g, '\n<br>');
preview.innerHTML = out;
}
function autosave() {
if (!saved) {
localStorage.draft = editor.value;
saved = true;
}
setTimeout(function () {
autosave();
}, 5000);
}
function storageAvailable() {
try {
var storage = window['localStorage']
, x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch (e) {
return false;
}
}