forked from yaniswang/markdownReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdownreader.js
98 lines (84 loc) · 2.76 KB
/
markdownreader.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
(function(document) {
//忽略HTML代码
if (document.doctype) return;
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = chrome.extension.getURL('markdownreader.css');
document.head.appendChild(link);
link = document.createElement('link');
link.rel = 'stylesheet';
link.href = chrome.extension.getURL('prettify.css');
document.head.appendChild(link);
document.body.innerHTML = '<div id="markdown-container"></div><div id="markdown-outline"></div><div id="markdown-backTop" onclick="window.scrollTo(0,0);"></div>';
window.onresize = showOutline;
var markdownConverter = new Showdown.converter();
var lastText = null;
function updateMarkdown(text) {
if (text !== lastText) {
lastText = text;
document.getElementById('markdown-container').innerHTML = markdownConverter.makeHtml(lastText);
prettyPrint();
updateOutline();
}
}
function updateOutline() {
var arrAllHeader = document.querySelectorAll("h1,h2,h3,h4,h5,h6");
var arrOutline = ['<ul>'];
var header, headerText;
var id = 0;
var level = 0,
lastLevel = 1;
var levelCount = 0;
for (var i = 0, c = arrAllHeader.length; i < c; i++) {
header = arrAllHeader[i];
headerText = header.innerText;
header.setAttribute('id', id);
level = header.tagName.match(/^h(\d)$/i)[1];
levelCount = level - lastLevel;
if (levelCount > 0) {
for (var j = 0; j < levelCount; j++) {
arrOutline.push('<ul>');
}
} else if (levelCount < 0) {
levelCount *= -1;
for (var j = 0; j < levelCount; j++) {
arrOutline.push('</ul>');
}
};
arrOutline.push('<li>');
arrOutline.push('<a href="#' + id + '">' + headerText + '</a>');
arrOutline.push('</li>');
lastLevel = level;
id++;
}
arrOutline.push('</ul>')
var outline = document.getElementById('markdown-outline');
if(arrOutline.length > 2){
outline.innerHTML = arrOutline.join('');
showOutline();
}
else outline.style.display = 'none';
}
function showOutline() {
var outline = document.getElementById('markdown-outline');
var markdownContainer = document.getElementById('markdown-container');
outline.style.left = markdownContainer.offsetLeft + markdownContainer.offsetWidth + 10 + 'px';
outline.style.maxHeight = document.body.clientHeight - 30;
outline.style.display = 'block';
}
var xmlhttp = new XMLHttpRequest();
var fileurl = location.href,
bLocalFile = /^file:\/\//i.test(fileurl);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status != 404) {
updateMarkdown(xmlhttp.responseText);
}
};
function checkUpdate() {
xmlhttp.abort();
xmlhttp.open("GET", fileurl + '?rnd=' + new Date().getTime(), true);
xmlhttp.send(null);
if (bLocalFile) setTimeout(checkUpdate, 500);
}
checkUpdate();
}(document));