-
Notifications
You must be signed in to change notification settings - Fork 2
/
createLink.html
53 lines (44 loc) · 1.54 KB
/
createLink.html
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
<script>
;(function() {
var tags = document.querySelectorAll('.post-tag');
if(tags == null || tags.length < 1) {
return;
}
for (var i = 0; i < tags.length; i++) {
var item = tags[i];
var tagList = item.innerHTML.trim();
if(/^\s*$/.test(tagList)) {
continue;
}
tagList = tagList.split(/\s+/)
.map(function(tag) {
return `<a href="/tag/#${tag}">#${tag}</a>`;
})
.join(' ');
tags[i].innerHTML = tagList;
}
return;
})();
;(function() {
var post = document.querySelector('article.post-content');
if(post == null) {
return;
}
(function iterate_node(node) {
if (/^(?:p|ul|h\d|table)$/i.test(node.tagName)) {
node.innerHTML = link(node.innerHTML);
} else { // Node.ELEMENT_NODE
for (var i = 0; i < node.childNodes.length; i++) {
iterate_node(node.childNodes[i]);
}
}
})(post);
function link(content) {
content = content.replace(/\\\[\[(.+?)\]\]/g, '\\[\\[$1\\]\\]');
content = content.replace(/\[\[(.+?)\]\]\{(.+?)\}/g, '<a href="../$1">$2</a>');
content = content.replace(/\[\[(.+?)\]\]/g, '<a href="../$1">$1</a>');
content = content.replace(/\\\[\\\[(.+?)\\\]\\\]/g, '[[$1]]');
return content;
}
})();
</script>