Skip to content

Commit 0f75135

Browse files
committed
improved version
1 parent 9571f05 commit 0f75135

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

_includes/toc.html

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<script>
22
$(document).ready(function () {
33
// Ensure that the TOC is generated after the document is fully loaded
4-
$('#toc-contents').toc({
5-
minimumHeaders: {{site.theme_variables.toc.min_headings | default: 1 }},
4+
$('#toc-contents').toc({
5+
minimumHeaders: {{ site.theme_variables.toc.min_headings | default: 1 }},
66
listType: 'ul',
77
classes: { list: 'list-unstyled' },
8-
noBackToTopLinks: true,
98
showSpeed: 0,
109
headers: '{{site.theme_variables.toc.headings | default: "main h2" }}',
1110
title: '<strong class="my-2">On this page</strong><hr class="my-2">'
1211
});
13-
1412
// After generating the TOC, check the hash in the URL and scroll to the anchor if present
1513
if (window.location.hash) {
1614
var target = $(window.location.hash);
@@ -20,6 +18,19 @@
2018
}, 0);
2119
}
2220
}
21+
// Allow the browser to handle the scrolling to the anchor
22+
$('#toc-contents a').on('click', function (e) {
23+
e.preventDefault(); // Prevent the default link click behavior
24+
25+
var target = this.hash; // Get the hash (e.g., #heading-id)
26+
var $target = $(target); // Find the element with the corresponding ID
27+
28+
if ($target.length) {
29+
// Change the URL hash
30+
window.location.hash = target;
31+
}
32+
});
33+
2334
});
2435
</script>
2536
<button id="btn-toc-hide" class="btn bg-light d-xl-none hover-primary mb-3" type="button" data-bs-toggle="collapse" data-bs-target="#toc-contents" aria-expanded="true" aria-controls="toc-contents">

assets/js/toc.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
(function ($) {
33
$.fn.toc = function (options) {
44
var defaults = {
5-
noBackToTopLinks: false,
65
title: '<i>Jump to...</i>',
76
minimumHeaders: 3,
87
headers: 'h1, h2, h3, h4, h5, h6',
@@ -35,7 +34,9 @@
3534
this.id = $(this).text().trim().replace(/\s+/g, '-').toLowerCase();
3635
}
3736
return this.id;
38-
}), output = $(this);
37+
});
38+
39+
var output = $(this);
3940

4041
// Check if there are any headers
4142
if (!headers.length || headers.length < settings.minimumHeaders || !output.length) {
@@ -59,16 +60,11 @@
5960

6061
var get_level = function (ele) { return parseInt(ele.nodeName.replace("H", ""), 10); };
6162
var highest_level = headers.map(function (_, ele) { return get_level(ele); }).get().sort()[0];
62-
var return_to_top = '<i class="icon-arrow-up back-to-top"> </i>';
63+
var level = get_level(headers[0]), this_level;
64+
var html = settings.title + " <" + settings.listType + " class=\"" + settings.classes.list + "\">";
6365

64-
var level = get_level(headers[0]),
65-
this_level,
66-
html = settings.title + " <" + settings.listType + " class=\"" + settings.classes.list + "\">";
6766
headers.each(function (_, header) {
6867
this_level = get_level(header);
69-
if (!settings.noBackToTopLinks && this_level === highest_level) {
70-
$(header).addClass('top-level-header').after(return_to_top);
71-
}
7268
if (this_level === level) { // same level as before; same indenting
7369
html += "<li class=\"" + settings.classes.item + "\">" + createLink(header);
7470
} else if (this_level <= level) { // higher level than before; end parent ol
@@ -86,14 +82,6 @@
8682
level = this_level; // update for the next one
8783
});
8884
html += "</" + settings.listType + ">";
89-
90-
if (!settings.noBackToTopLinks) {
91-
$(document).on('click', '.back-to-top', function () {
92-
$(window).scrollTop(0);
93-
window.location.hash = '';
94-
});
95-
}
96-
9785
render[settings.showEffect]();
9886
};
9987
})(jQuery);

0 commit comments

Comments
 (0)