-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgist_markdown_preview.js
98 lines (86 loc) · 2.79 KB
/
gist_markdown_preview.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const d = document;
const qs = d.querySelector.bind(d);
const qsa = d.querySelectorAll.bind(d);
const byId = d.getElementById.bind(d);
const togglePreview = cqs => {
const article = cqs('.js-gist-preview');
const editor = cqs('.CodeMirror');
const previewBtn = cqs('.js-gist-preview-btn');
if (editor.style.display === 'none') {
editor.style.display = "block";
article.style.display = "none";
previewBtn.innerHTML = "Preview";
} else {
editor.style.display = "none";
article.style.display = "inline-block";
previewBtn.innerHTML = "Edit";
}
article.innerHTML = marked(cqs(".commit-create > textarea").value, { sanitize: false, gfm: true, tables: true, breaks:true });
};
const renderPreviewArea = (value, cqs) => {
const placeHolder = cqs(".commit-create");
if (!isMarkdown(value)) {
const previewArea = cqs('.js-gist-preview');
previewArea && previewArea.remove();
placeHolder.style.display = "visible";
return;
}
if(cqs('.js-gist-preview')) {
return;
}
const previewArea = d.createElement('div');
previewArea.classList.add('js-gist-preview', 'markdown-body', 'entry-content');
previewArea.setAttribute("style", "padding: 45px;width: 100%;min-height: 362px;display:none");
placeHolder.appendChild(previewArea);
}
const isMarkdown = filename => {
return /.*(\.md|\.markdown)$/.test(filename);
}
const renderPreviewButton = (value, cqs) => {
if (!isMarkdown(value)) {
const previewBtn = cqs('.js-gist-preview-btn');
previewBtn && previewBtn.remove();
return;
}
if(cqs('.js-gist-preview-btn')) {
return;
}
const previewBtn = createPreviewBtn();
cqs('.file-actions').appendChild(previewBtn);
previewBtn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
togglePreview(cqs);
});
}
const createPreviewBtn = () => {
const previewBtn = d.createElement('button');
previewBtn.type = "button";
previewBtn.innerHTML = "Preview";
previewBtn.classList.add('btn', 'js-gist-preview-btn');
return previewBtn;
}
const setup = gistHolder => {
const cqs = gistHolder.querySelector.bind(gistHolder);
const fileNameInput = cqs('.js-gist-filename')
fileNameInput.addEventListener('change', e => {
renderPreviewButton(e.target.value, cqs);
renderPreviewArea(e.target.value, cqs);
});
if(isMarkdown(fileNameInput.value)) {
const event = d.createEvent("HTMLEvents");
event.initEvent("change", false, true);
event.synthetic = true;
fileNameInput.dispatchEvent(event, true);
}
}
d.addEventListener('DOMContentLoaded', evt => {
qsa(".js-gist-file").forEach(setup);
qs('.js-add-gist-file')
.addEventListener('click', e => {
setTimeout(() => {
const fileList = qsa(".js-gist-file");
setup(fileList[fileList.length -1]);
}, 100);
});
});