forked from ruanyf/es6tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ditto.js
415 lines (363 loc) · 11.9 KB
/
ditto.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
var ditto = {
// page element ids
content_id: "#content",
sidebar_id: "#sidebar",
edit_id: "#edit",
back_to_top_id: "#back_to_top",
loading_id: "#loading",
error_id: "#error",
// display elements
sidebar: true,
edit_button: true,
back_to_top_button: true,
save_progress: true, // 保存阅读进度
search_bar: true,
// initialize function
run: initialize
};
/**
* 获取当前hash
*
* @param {string} hash 要解析的hash,默认取当前页面的hash,如: nav#类目 => {nav:nav, anchor:类目}
* @description 分导航和页面锚点
* @return {Object} {nav:导航, anchor:页面锚点}
*/
var getHash = function (hash) {
hash = hash || window.location.hash.substr(1);
if (!hash) {
return {
nav: '',
anchor: ''
}
}
hash = hash.split('#');
return {
nav: hash[0],
anchor: decodeURIComponent(hash[1] || '')
}
};
var disqusCode = '<h3>留言</h3><div id="disqus_thread"></div>';
var menu = new Array();
function initialize() {
// initialize sidebar and buttons
if (ditto.sidebar) {
init_sidebar_section();
}
if (ditto.back_to_top_button) {
init_back_to_top_button();
}
if (ditto.edit_button) {
init_edit_button();
}
// page router
router();
$(window).on('hashchange', router);
}
function init_sidebar_section() {
$.get(ditto.sidebar_file, function (data) {
$(ditto.sidebar_id).html(marked(data));
if (ditto.search_bar) {
init_searchbar();
}
// 初始化内容数组
var menuOL = $(ditto.sidebar_id + ' ol');
menuOL.attr('start', 0);
menuOL.find('li a').map(function() {
menu.push(this.href.slice(this.href.indexOf('#')));
});
$('#pageup').on('click', function() {
var hash = getHash().nav;
for (var i = 0; i < menu.length; i++) {
if (hash === '') break;
if (menu[i] === '#' + hash) break;
}
location.hash = menu[i - 1]
});
$('#pagedown').on('click', function() {
var hash = getHash().nav;
for (var i = 0; i < menu.length; i++) {
if (hash === '') break;
if (menu[i] === '#' + hash) break;
}
location.hash = menu[i + 1];
});
}, "text").fail(function() {
alert("Opps! can't find the sidebar file to display!");
});
}
function init_searchbar() {
var search = '<form class="searchBox" onSubmit="return searchbar_listener()">' +
'<input name="search" type="search">' +
'<input type="image" class="searchButton" src="images/magnifier.jpg" alt="Search" />' +
// '<a class="searchLink" href="#" target="_blank"><img src="images/magnifier.jpg"></a>' +
'</form>';
$(ditto.sidebar_id).find('h2').first().before($(search));
// $('input.searchButton').click(searchbar_listener);
// $('input[name=search]').keydown(searchbar_listener);
}
function searchbar_listener(event) {
// event.preventDefault();
var q = $('input[name=search]').val();
if (q !== '') {
var url = 'https://github.com/ruanyf/es6tutorial/search?utf8=✓&q=' + encodeURIComponent(q);
window.open(url, '_blank');
win.focus();
}
return false;
/*
if (event.which === 13) {
var q = $('input[name=search]').val();
if (q !== '') {
var url = 'https://github.com/ruanyf/es6tutorial/search?utf8=✓&q=' + encodeURIComponent(q);
location.href = url;
}
}
*/
}
function init_back_to_top_button() {
$(ditto.back_to_top_id).show();
$(ditto.back_to_top_id).on('click', goTop);
}
function goTop(e) {
if(e) e.preventDefault();
$('html body').animate({
scrollTop: 0
}, 200);
history.pushState(null, null, '#' + location.hash.split('#')[1]);
}
function goSection(sectionId){
$('html, body').animate({
scrollTop: ($('#' + sectionId).offset().top)
}, 300);
}
function init_edit_button() {
if (ditto.base_url === null) {
alert("Error! You didn't set 'base_url' when calling ditto.run()!");
} else {
$(ditto.edit_id).show();
$(ditto.edit_id).on("click", function() {
var hash = location.hash.replace("#", "/");
if (/#.*$/.test(hash)) {
hash = hash.replace(/#.*$/, '');
}
if (hash === "") {
hash = "/" + ditto.index.replace(".md", "");
}
window.open(ditto.base_url + hash + ".md");
// open is better than redirecting, as the previous page history
// with redirect is a bit messed up
});
}
}
function replace_symbols(text) {
// replace symbols with underscore
return text
.replace(/, /g, ',')
.replace(/[&\/\\#,.+=$~%'":*?<>{}\ \]\[]/g, "-")
.replace(/[()]/g, '');
}
function li_create_linkage(li_tag, header_level) {
// add custom id and class attributes
html_safe_tag = replace_symbols(li_tag.text());
li_tag.attr('data-src', html_safe_tag);
li_tag.attr("class", "link");
// add click listener - on click scroll to relevant header section
li_tag.click(function(e) {
e.preventDefault();
// scroll to relevant section
var header = $(
ditto.content_id + " h" + header_level + "." + li_tag.attr('data-src')
);
$('html, body').animate({
scrollTop: header.offset().top
}, 200);
// highlight the relevant section
original_color = header.css("color");
header.animate({ color: "#ED1C24", }, 500, function() {
// revert back to orig color
$(this).animate({color: original_color}, 2500);
});
history.pushState(null, null, '#' + location.hash.split('#')[1] + '#' + li_tag.attr('data-src'));
});
}
function create_page_anchors() {
// create page anchors by matching li's to headers
// if there is a match, create click listeners
// and scroll to relevant sections
// go through header level 1 to 3
for (var i = 2; i <= 4; i++) {
// parse all headers
var headers = [];
$('#content h' + i).map(function() {
var content = $(this).text();
headers.push(content);
$(this).addClass(replace_symbols(content));
this.id = replace_symbols(content);
$(this).hover(function () {
$(this).html(content +
' <a href="#' + location.hash.split('#')[1] +
'#' +
replace_symbols(content) +
'" class="section-link">§</a> <a href="#' +
location.hash.split('#')[1] + '" onclick="goTop()">⇧</a>');
}, function () {
$(this).html(content);
});
$(this).on('click', 'a.section-link', function(event) {
event.preventDefault();
history.pushState(null, null, '#' + location.hash.split('#')[1] + '#' + replace_symbols(content));
goSection(replace_symbols(content));
});
});
if ((i === 2) && headers.length !== 0) {
var ul_tag = $('<ol></ol>')
.insertAfter('#content h1')
.addClass('content-toc')
.attr('id', 'content-toc');
for (var j = 0; j < headers.length; j++) {
var li_tag = $('<li></li>').html('<a href="#' + location.hash.split('#')[1] + '#' + headers[j] + '">' + headers[j] + '</a>');
ul_tag.append(li_tag);
li_create_linkage(li_tag, i);
}
}
}
}
function normalize_paths() {
// images
$(ditto.content_id + " img").map(function() {
var src = $(this).attr("src").replace("./", "");
if ($(this).attr("src").slice(0, 5) !== "http") {
var pathname = location.pathname.substr(0, location.pathname.length - 1);
var url = location.hash.replace("#", "");
// split and extract base dir
url = url.split("/");
var base_dir = url.slice(0, url.length - 1).toString();
// normalize the path (i.e. make it absolute)
$(this).attr("src", pathname + base_dir + "/" + src);
}
});
}
function show_error() {
console.log("SHOW ERORR!");
$(ditto.error_id).show();
}
function show_loading() {
$(ditto.loading_id).show();
$(ditto.content_id).html(''); // clear content
// infinite loop until clearInterval() is called on loading
var loading = setInterval(function() {
$(ditto.loading_id).fadeIn(1000).fadeOut(1000);
}, 2000);
return loading;
}
function router() {
var path = location.hash.replace(/#([^#]*)(#.*)?/, './$1');
var hashArr = location.hash.split('#');
var sectionId;
if (hashArr.length > 2 && !(/^comment-/.test(hashArr[2]))) {
sectionId = hashArr[2];
}
if (ditto.save_progress && store.get('menu-progress') !== location.hash) {
store.set('menu-progress', location.hash);
store.set('page-progress', 0);
}
// default page if hash is empty
if (location.pathname === "/index.html") {
path = location.pathname.replace("index.html", ditto.index);
normalize_paths();
} else if (path === "") {
path = location.pathname + ditto.index;
normalize_paths();
} else {
path = path + ".md";
}
// 取消scroll事件的监听函数
// 防止改变下面的变量perc的值
$(window).off('scroll');
// otherwise get the markdown and render it
var loading = show_loading();
$.get(path, function(data) {
$(ditto.error_id).hide();
$(ditto.content_id).html(marked(data) + disqusCode);
if ($(ditto.content_id + " h1").text() === ditto.document_title) {
document.title = ditto.document_title;
} else {
document.title = $(ditto.content_id + " h1").text() + " - " + ditto.document_title;
}
normalize_paths();
create_page_anchors();
// 完成代码高亮
$('#content code').map(function() {
Prism.highlightElement(this);
});
// 加载disqus
(function() {
// http://docs.disqus.com/help/2/
window.disqus_shortname = 'es6';
window.disqus_identifier = (location.hash ? location.hash.replace("#", "") : 'READEME');
window.disqus_title = $(ditto.content_id + " h1").text();
window.disqus_url = 'http://es6.ruanyifeng.com/' + (location.hash ? location.hash.replace("#", "") : 'README');
// http://docs.disqus.com/developers/universal/
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = 'http://' + window.disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
})();
var perc = ditto.save_progress ? store.get('page-progress') || 0 : 0;
if (sectionId) {
$('html, body').animate({
scrollTop: ($('#' + decodeURI(sectionId)).offset().top)
}, 300);
} else {
if (location.hash !== '' || Boolean(perc)) {
if (!Boolean(perc)) {
$('html, body').animate({
scrollTop: ($('#content').offset().top + 10)
}, 300);
$('html, body').animate({
scrollTop: ($('#content').offset().top)
}, 300);
} else {
$('html, body').animate({
scrollTop: ($('body').height()-$(window).height())*perc
}, 200);
}
}
}
if (location.hash === '' || '#' + getHash().nav === menu[0]) {
$('#pageup').css('display', 'none');
} else {
$('#pageup').css('display', 'inline-block');
}
if ('#' + getHash().nav === menu[(menu.length - 1)]) {
$('#pagedown').css('display', 'none');
} else {
$('#pagedown').css('display', 'inline-block');
}
(function() {
var $w = $(window);
var $prog2 = $('.progress-indicator-2');
var wh = $w.height();
var h = $('body').height();
var sHeight = h - wh;
$w.on('scroll', function() {
window.requestAnimationFrame(function(){
var perc = Math.max(0, Math.min(1, $w.scrollTop() / sHeight));
updateProgress(perc);
});
});
function updateProgress(perc) {
$prog2.css({width: perc * 100 + '%'});
ditto.save_progress && store.set('page-progress', perc);
}
}());
}).fail(function() {
show_error();
}).always(function() {
clearInterval(loading);
$(ditto.loading_id).hide();
});
}