forked from minchinweb/markdown-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.js
76 lines (67 loc) · 2.34 KB
/
markdown.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
/* CREATE TOC */
function createTOC() {
var headers = $("h1, h2, h3, h4, h5, h6"),
toc = $("<ul></ul>");
if (!headers.length) {
return false;
}
headers.each(function (index) {
var $elem = $(this),
headerId = $elem.attr('id');
if (!headerId) {
headerId = "header-" + index;
$elem.before($("<a name=\"" + headerId + "\">"));
}
toc.append($("<li class=\"" + $elem.prop("tagName") + "\"><a href=\"#" + headerId + "\">" + $elem.html() + "</a></li>"));
});
return toc;
}
$(function () {
// Set page title to the first H1
$(this).attr("title", $("h1").first().text());
// If there is a contents div then create the TOC and associated divs
if ($("#contents").length) {
var toc = createTOC();
if (toc) {
$("#contents").html(toc);
}
$("#contents").wrap('<div id="contentsWrapper" />');
$('<div id="contentsTab">Contents</div>').appendTo("#contentsWrapper");
var offset = $("#contentsWrapper").offset().top - parseFloat($('#contentsWrapper').css('marginTop').replace(/auto/, 0));
$(window).scroll(function() {
if ($(this).scrollTop() > offset) {
$("#contentsWrapper").addClass('fixed');
} else {
$("#contentsWrapper").removeClass('fixed');
};
});
$("#contentsTab").click(function(event) {
event.stopPropagation();
if ($("#contentsWrapper").css("left") == '-319px') {
$("#contentsWrapper").animate({left: '0px'}, 500);
} else {
$("#contentsWrapper").animate({left: '-319px'}, 500);
}
});
};
});
// replace text node with predefined mapping, no regex needed
$(function () {
let mapping = {
":x:": "❌",
":heavy_check_mark:": "✔",
};
// found all the #text nodes under p
var p = document.querySelectorAll('p');
p.forEach(function (node) {
var textNode = node.childNodes[0];
if (textNode.nodeType === 3) {
var text = textNode.nodeValue;
Object.keys(mapping).forEach(function (key) {
if (text.includes(key)) {
textNode.nodeValue = text.replace(key, mapping[key]);
}
});
}
});
});