Decode TOC link

* See: https://github.com/hexojs/hexo/pull/4183
This commit is contained in:
Mimi 2020-05-29 20:51:37 +08:00
parent 4013450403
commit 0d2b3af50b

View File

@ -237,10 +237,10 @@ NexT.utils = {
const navItems = document.querySelectorAll('.post-toc li'); const navItems = document.querySelectorAll('.post-toc li');
const sections = [...navItems].map(element => { const sections = [...navItems].map(element => {
const link = element.querySelector('a.nav-link'); const link = element.querySelector('a.nav-link');
const target = document.getElementById(decodeURI(link.getAttribute('href')).replace('#', ''));
// TOC item animation navigate. // TOC item animation navigate.
link.addEventListener('click', event => { link.addEventListener('click', event => {
event.preventDefault(); event.preventDefault();
const target = document.getElementById(event.currentTarget.getAttribute('href').replace('#', ''));
const offset = target.getBoundingClientRect().top + window.scrollY; const offset = target.getBoundingClientRect().top + window.scrollY;
window.anime({ window.anime({
targets : document.scrollingElement, targets : document.scrollingElement,
@ -249,7 +249,7 @@ NexT.utils = {
scrollTop: offset + 10 scrollTop: offset + 10
}); });
}); });
return document.getElementById(link.getAttribute('href').replace('#', '')); return target;
}); });
const tocElement = document.querySelector('.post-toc-wrap'); const tocElement = document.querySelector('.post-toc-wrap');