-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.js
More file actions
118 lines (99 loc) · 3.04 KB
/
page.js
File metadata and controls
118 lines (99 loc) · 3.04 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
$(document).ready(function() {
var lastURL = "/";
var anchor = "";
function isPreview() {
//If I'm debugging this locally
return /localhost|[^\.]127\./i.test(window.origin);
}
function fetchPage(url) {
if(!url) {
onLoaded();
return;
}
if(url.slice(-1) == "/")
url += "default.md";
$("#toolbar-logo").removeClass("toolbar-logo-error");
$("#toolbar-logo").addClass("toolbar-logo-loading");
$.ajax({
url: _Blog.contentPath + url,
cache: !isPreview(),
error: onLoadError,
success: function(text) {
try {
renderPage(text, url);
} catch(e) {
console.error(e);
onLoadError();
}
}
});
}
function renderPage(text, url) {
lastURL = url;
$("#contents").html(marked(text)).promise().then(onLoaded);
var title = $("main h1:first").text() || url;
document.title = title + _Blog.titleSuffix;
$("#toolbar-code").attr("href", _Blog.contentPath + url);
//$("main img").lazyload({effect : "fadeIn"});
_Blog.gitment.id = url;
_Blog.gitment.title = title;
new Gitment(_Blog.gitment).render("comments-gitment");
$("#toolbar-error").hide();
$("#toolbar-logo").removeClass("toolbar-logo-loading");
}
function onLoaded() {
// Anchoring
var elemAnchor = $("#" + unescape(anchor || "top")).get(0);
if(elemAnchor)
elemAnchor.scrollIntoView();
// Extension function "collapse"
$(".collapse>legend").each(function() {
$(this).click(function() {
$(this).parent(".collapse").toggleClass("collapse-hidden");
});
});
// Clear empty <pre>s
//$("pre>code:empty").parent().remove();
}
function onLoadError() {
$("#toolbar-logo").removeClass("toolbar-logo-loading");
$("#toolbar-logo").addClass("toolbar-logo-error");
$("#toolbar-error").show();
}
function onHighlight(code, lang) {
try {
return hljs.highlight(lang, code).value;
} catch(e) {
if(lang)
console.error(e);
return code;
}
}
function onHashChange() {
var hash = location.hash.slice(1).split("#");
if(hash[0][0] == "/") {
anchor = hash[1];
if(hash[0] == lastURL && hash[0] != "/")
hash[0] = undefined;
fetchPage(hash[0]);
} else {
history.replaceState(history.state, document.title, "#" + lastURL + location.hash);
onHashChange();
}
}
function onToggleComments() {
$("#toolbar-comments").toggleClass("toolbar-comments-shown");
$("#comments").slideToggle("fast", () => {
$("#comments").get(0).scrollIntoView();
});
}
_Blog.marked.highlight = onHighlight;
marked.setOptions(_Blog.marked);
$("#toolbar-comments").click(onToggleComments);
$("#toolbar-reload").click(onHashChange);
$(window).bind("hashchange", function(e) {
e.preventDefault();
onHashChange();
});
onHashChange();
}); //$(document).ready