-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin-bookdown.js.patch
55 lines (53 loc) · 2.53 KB
/
plugin-bookdown.js.patch
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
--- a/gitbook/js/plugin-bookdown.js
+++ b/gitbook/js/plugin-bookdown.js
@@ -95,12 +95,8 @@
});
// 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 +158,22 @@
}
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 +187,7 @@
})();
// 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);