-
Notifications
You must be signed in to change notification settings - Fork 3
/
tistory-markdown-plugin.js
58 lines (52 loc) · 2.07 KB
/
tistory-markdown-plugin.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
// Tistory Markdown Plugin by makerj([email protected])
document.addEventListener('DOMContentLoaded', function() {
'use strict';
console.log('Tistory Markdown Plugin by makerj([email protected])');
var CONTAINER_SELECTOR = "";
var CONTENTS_SELECTOR = "";
function _find_container() {
var tryAt = ['div.tt_article_useless_p_margin', 'div.area_view', 'div.article'];
var elem = null;
for (var i = 0; i < tryAt.length; ++i) {
elem = document.querySelector(tryAt[i]);
if (elem) break;
}
return elem;
}
function _find_contents() {
var tryAt = ['div.tt_article_useless_p_margin > p', 'div.area_view > p', 'div.article > p'];
var elem = null;
for (var i = 0; i < tryAt.length; ++i) {
elem = document.querySelectorAll(tryAt[i]);
if (elem) break;
}
return elem;
}
// Extract markdown lines
var container = CONTAINER_SELECTOR ? document.querySelector(CONTAINER_SELECTOR) : _find_container();
var contents = CONTENTS_SELECTOR ? document.querySelectorAll(CONTENTS_SELECTOR) : _find_contents();
contents = Array.from(contents);
var firstline = contents[0].innerText.trim();
if (firstline != '@markdown' && firstline != '@마크다운') {
console.log("Contents must be starts with '@markdown' or '@마크다운' but was '" + firstline + "'");
console.log("Skip converting");
return;
}
container.removeChild(contents.shift())
// Preprocess extracted lines
var lines = [];
contents.forEach(function(e) {
if (e.children && e.children.length) lines.push(e.innerText + '\n');
else if (e.innerText == '_') lines.push('<br>\n');
else lines.push(e.innerText + '\n\n');
e.remove();
});
lines = lines.join('');
// Convert markdown to html
var converter = new showdown.Converter();
var doc = converter.makeHtml(lines);
// Render converted html
console.log(lines);
console.log(doc);
container.innerHTML = doc + container.innerHTML;
});