-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-bookdown.js.patch
More file actions
81 lines (78 loc) · 3.95 KB
/
plugin-bookdown.js.patch
File metadata and controls
81 lines (78 loc) · 3.95 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
--- a/gitbook/js/plugin-bookdown.js
+++ b/gitbook/js/plugin-bookdown.js
@@ -78,29 +78,24 @@ gitbook.require(["gitbook", "lodash", "jQuery"], function(gitbook, _, $) {
}
// add the Information button
- var info = ['Keyboard shortcuts (<> indicates arrow keys):',
- '<left>/<right>: navigate to previous/next page',
- 's: Toggle sidebar'];
- if (config.search !== false) info.push('f: Toggle search input ' +
- '(use <up>/<down>/Enter in the search input to navigate through search matches; ' +
- 'press Esc to cancel search)');
+ var info = ['<h1>Keyboard shortcuts</h1><dl>',
+ '<dt><kbd>Left</kbd>/<kbd>Right</kbd></dt><dd>navigate to previous/next page</dd>',
+ '<dt><kbd>s</kbd></dt><dd>Toggle sidebar</dd>'];
+ if (config.search !== false) info.push('<dt><kbd>f</kbd></dt><dd>Toggle search input ' +
+ '(use <kbd>Up</kbd>/<kbd>Down</kbd>/<kbd>Enter</kbd> in the search input to navigate through search matches; ' +
+ 'press <kbd>Esc</kbd> to cancel search)</dd>');
+ info.push('</dl>');
if (config.info !== false) gitbook.toolbar.createButton({
icon: 'fa fa-info',
label: 'Information about the toolbar',
position: 'left',
- onClick: function(e) {
- e.preventDefault();
- window.alert(info.join('\n\n'));
- }
+ className: 'info',
+ dropdown: info.join('\n\n')
});
// highlight the current section in TOC
- var href = window.location.pathname;
- href = href.substr(href.lastIndexOf('/') + 1);
- // accentuated characters need to be decoded (#819)
- href = decodeURIComponent(href);
- if (href === '') href = 'index.html';
- var li = $('a[href^="' + href + location.hash + '"]').parent('li.chapter').first();
+ var href = location.hash ? '' : location.hash;
+ var li = $('a[href="' + href + '"]').parent('li.chapter').first();
var summary = $('ul.summary'), chaps = summary.find('li.chapter');
if (li.length === 0) li = chaps.first();
li.addClass('active');
@@ -162,22 +157,22 @@ gitbook.require(["gitbook", "lodash", "jQuery"], function(gitbook, _, $) {
}
if (m === 0) return;
// all section titles on current page
- var hs = bookInner.find('.page-inner').find('h1,h2,h3'), n = hs.length,
- ts = hs.map(function(i, el) { return $(el).text(); });
+ var hs = bookInner.find('.page-inner').find('h1,h2,h3,h4,h5').children('span[class^="ltx_tag"]'), n = hs.length,
+ ts = hs.map(function(i, el) { return ($(el).closest('section').attr('id') ? '#' + $(el).closest('section').attr('id') : ''); });
if (n === 0) return;
var scrollHandler = function(e) {
var ht = $(window).height();
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
- // find the first visible title in the viewport
+ // find the last section starting by the middle of the viewport
for (var i = 0; i < n; i++) {
var rect = hs[i].getBoundingClientRect();
- if (rect.top >= 0 && rect.bottom <= ht) break;
+ if (rect.top > ht / 2) break;
}
- if (i === n) return;
+ if (i > 0) i--;
items.removeClass('active');
for (var j = 0; j < m; j++) {
- if (items.eq(j).children('a').first().text() === ts[i]) break;
+ if (items.eq(j).children('a').attr('href') === ts[i]) break;
}
if (j === m) j = 0; // highlight the chapter title
// search bottom-up for a visible TOC item to highlight; if an item is
@@ -191,7 +186,7 @@ gitbook.require(["gitbook", "lodash", "jQuery"], function(gitbook, _, $) {
})();
// do not refresh the page if the TOC item points to the current page
- $('a[href="' + href + '"]').parent('li.chapter').children('a')
+ $('a[href=""]').parent('li.chapter').children('a')
.on('click', function(e) {
bookInner.scrollTop(0);
bookBody.scrollTop(0);