优化toc

This commit is contained in:
1152958806@qq.com 2023-08-30 15:42:24 +08:00
parent efba4f0d86
commit f483f3f7f2
3 changed files with 24 additions and 54 deletions

View File

@ -147,6 +147,29 @@ document.addEventListener('DOMContentLoaded', function () {
}
}
/**
* toc
*/
const tocFn = function () {
const postContent = document.querySelector('.post-content');
if (postContent == null) return;
const headers = postContent.querySelectorAll('h1,h2,h3,h4,h5,h6');
// 没有 toc 目录,则直接移除
if (headers.length === 0) {
document.getElementById("card-toc").remove();
} else {
tocbot.init({
tocSelector: '.toc-content',
contentSelector: '.post-content',
headingSelector: 'h1,h2,h3,h4,h5,h6',
hasInnerContainers: true
});
}
}
/**
* 滾動處理
*/
@ -446,6 +469,7 @@ document.addEventListener('DOMContentLoaded', function () {
if (GLOBAL_CONFIG.isPost) {
addRuntime();
tocFn();
} else {
addLastPushDate()
toggleCardCategory()

View File

@ -452,13 +452,6 @@ function removeLoading() {
}, 3000)
}
//移除pwa
navigator.serviceWorker.getRegistrations().then(function (registrations) {
for (let registration of registrations) {
registration.unregister()
}
})
function addFriendLink() {
var input = document.getElementsByClassName('el-textarea__inner')[0];
let evt = document.createEvent('HTMLEvents');
@ -672,7 +665,6 @@ function initBlog() {
//隐藏加载动画
GLOBAL_CONFIG.loadingBox && heo.hideLoading(),
checkUrlAndAddHideBanner()
}
// 如果当前页有评论就执行函数

View File

@ -4,51 +4,5 @@
<th:block th:fragment="toc-bot">
<script th:src="${assets_link + '/libs/tocbot/4.18.2/tocbot.min.js'}"></script>
<link th:href="${assets_link + '/libs/tocbot/4.18.2/tocbot.css'}" rel="stylesheet">
<script>
document.addEventListener("DOMContentLoaded", function() {
const postContent = document.querySelector('.post-content');
if (postContent == null) return;
const headers = postContent.querySelectorAll('h1,h2,h3,h4,h5,h6');
// 没有 toc 目录,则直接移除
if (headers.length === 0) {
document.getElementById("card-toc").remove();
} else {
tocbot.init({
tocSelector: '.toc-content',
contentSelector: '.post-content',
headingSelector: 'h1,h2,h3,h4,h5,h6',
hasInnerContainers: true
});
}
})
document.addEventListener('pjax:complete', function () {
const postContent = document.querySelector('.post-content');
if (postContent == null) return;
const headers = postContent.querySelectorAll('h1,h2,h3,h4,h5,h6');
// 没有 toc 目录,则直接移除
if (headers.length === 0) {
document.getElementById("card-toc").remove();
} else {
tocbot.init({
tocSelector: '.toc-content',
contentSelector: '.post-content',
headingSelector: 'h1,h2,h3,h4,h5,h6',
hasInnerContainers: true
});
}
})
</script>
</th:block>
</html>