diff --git a/src/app/posts/[slug]/page.jsx b/src/app/posts/[slug]/page.jsx index 4350354..7e2fdcf 100644 --- a/src/app/posts/[slug]/page.jsx +++ b/src/app/posts/[slug]/page.jsx @@ -21,6 +21,25 @@ md.use( }), ); +// 处理 h1 到 h6 标签 +md.renderer.rules.heading_open = function (tokens, idx, options, env, self) { + const level = tokens[idx].tag.slice(1); + const title = tokens[idx + 1].content; + const slug = title.replace(/\s+/g, '-').toLowerCase(); + return ``; +}; + +md.renderer.rules.heading_close = function (tokens, idx) { + const level = tokens[idx].tag.slice(1); + return ``; +}; + +md.renderer.rules.image = function (tokens, idx, options, env, self) { + const src = tokens[idx].attrGet('src'); + const alt = tokens[idx].content; + return `
${alt}${alt}
`; +}; + let title; export async function generateStaticParams() { @@ -110,7 +129,7 @@ export default async function Post(params) { {createCategory(post.category)} {' • '} {' '} {post.content.length}字 - {/* {' • '} --- */} + {' • '} ---

{createTag(post.tag)}
diff --git a/src/assets/js/Global.jsx b/src/assets/js/Global.jsx index 42530ff..74ebf97 100644 --- a/src/assets/js/Global.jsx +++ b/src/assets/js/Global.jsx @@ -81,7 +81,12 @@ function loadPage() { zoomPics(); document.addEventListener('click', function (event) { let target = event.target; - if (target.href) { + if (target.href && target.href !== window.location.href) { + const targetUrl = new URL(target.href); + const currentUrl = new URL(window.location.href); + if (targetUrl.pathname === currentUrl.pathname && targetUrl.hash !== currentUrl.hash) { + return; + } event.preventDefault(); let url = target.href; fadeOutPage('#viewmap'); @@ -414,6 +419,90 @@ function getTime(formats, startTime = '') { } } +// 目录重组 +function updateMenu() { + var menuStructure = "
"; + let titleSet = document.querySelectorAll( + '#articles-header h1 , #articles-body h2 , #articles-body h3 , #articles-body h4 , #articles-body h5 , #articles-body h6', + ); + titleSet.forEach((element) => { + switch (element.outerHTML.substring(0, 3)) { + case '${element.innerHTML}
`; + break; + case '${ + ' '.repeat(2) + '' + element.innerHTML + '' + }
`; + break; + case '${ + ' '.repeat(4) + '' + element.innerHTML + '' + }
`; + break; + case '${ + ' '.repeat(6) + '' + element.innerHTML + '' + }
`; + break; + case '${ + ' '.repeat(8) + '' + element.innerHTML + '' + }
`; + break; + case '${ + ' '.repeat(10) + '' + element.innerHTML + '' + }
`; + break; + } + }); + menuStructure += '
'; + return menuStructure; +} + +// 目录高亮 +function highlightMenu() { + if (cookie.getItem('settingEnableMenuHighlight') == 'false') { + return false; + } + document.querySelectorAll('#articles-menu *.active').forEach((element) => { + element.classList.remove('active'); + }); + const titleList = document.querySelectorAll( + '#articles-body h2 , #articles-body h3 , #articles-body h4 , #articles-body h5 , #articles-body h6', + ); + for (let i = 0; i < titleList.length; i++) { + let heights = getHeightDifferent(titleList[i]); + if (heights == 0) { + document + .querySelector(`#articles-menu #${titleList[i].firstChild.id}`) + .classList.add('active'); + return titleList[i]; + } + if (heights > 0) { + document + .querySelector(`#articles-menu #${titleList[i - 1].firstChild.id}`) + .classList.add('active'); + return titleList[i - 1]; + } + } + return false; +} + +// 相对高度差 +function getHeightDifferent(element) { + const rect = element.getBoundingClientRect(); + const vWidth = document.querySelector('#viewmap article').clientWidth; + const vHeight = document.querySelector('#viewmap article').clientHeight; + + if (rect.right < 0 || rect.bottom < 0 || rect.left > vWidth || rect.top > vHeight) { + return rect.top; + } + + return 0; +} + // InfoBar功能分发 function openInfoBar(mode) { infoBarMode = mode || ''; @@ -429,9 +518,8 @@ function openInfoBar(mode) { switchElementContent('#infobar-left', updateMenu(), 0); setTimeout(() => { highlightMenu(); - document - .querySelector('#articles-menu') - .setAttribute('onclick', 'setTimeout(()=>highlightMenu(),1000)'); + document.querySelector('#articles-menu').onclick = () => + setTimeout(() => highlightMenu(), 1000); }, 10); break; case 'setting': @@ -1230,6 +1318,21 @@ function loadPageType() { 20, ); } + if (window.location.pathname.split('/')[2]) { + // 显示目录 + i18n.originMessageBar = ( + openInfoBar('menu')}> + 目录  + + ); + message.switch(i18n.originMessageBar); + if (cookie.getItem('settingEnableUmamiAnalytics') !== 'false') { + analysis.getPageVisitors().then((data) => { + switchElementContent('#pageVisitors', data['pageviews'].value); + }); + } + zoomPics(); + } break; case '404page': // code @@ -1251,45 +1354,6 @@ function loadPageType() { break; case 'articles-index': break; - case 'articles-context': - highlightNav('articles'); - resetImage(); - switchElementContent( - '#textLength', - document.querySelector('#articles-body').innerText.length + '字', - ); - loadComment(); - codeHighlight(); - updateTitle(); - resetFilter(); - i18n.originMessageBar = `目录 `; - message.add(i18n.originMessageBar, 0); - if (cookie.getItem('settingEnableUmamiAnalytics') !== 'false') { - analysis.getPageVisitors().then((data) => { - switchElementContent('#pageVisitors', data['pageviews'].value); - }); - } - document.querySelectorAll('time').forEach((element) => { - element.setAttribute('onclick', 'switchTimeDisplay(this)'); - }); - loadBox(); - zoomPics(); - prefetchImg(); - getSearchData().then(() => - switchElementContent( - '#more-articles', - loadMoreArticles( - document.querySelector('#articles-header h1 a').getAttribute('href'), - ), - ), - ); - switchElementContent( - '#blockchain-data', - ` 此数据所有权由区块链加密技术(区块链标识:#57514)和智能合约保障仅归创作者所有。

${base.encryption( - window.location.pathname, - )}
`, - ); - break; } checkPageHash(); } @@ -1337,4 +1401,4 @@ const globalModule = { toggleLayoutUserbar, }; -export default globalModule; \ No newline at end of file +export default globalModule; diff --git a/src/assets/js/display.js b/src/assets/js/display.js index cd72e4f..c587c44 100644 --- a/src/assets/js/display.js +++ b/src/assets/js/display.js @@ -618,48 +618,6 @@ function updateTitle() { }); } -// 目录重组 -function updateMenu() { - var menuStructure = "
"; - let titleSet = document.querySelectorAll( - '#articles-header h1 , #articles-body h2 , #articles-body h3 , #articles-body h4 , #articles-body h5 , #articles-body h6', - ); - titleSet.forEach((element) => { - switch (element.outerHTML.substring(0, 3)) { - case '${element.innerHTML}
`; - break; - case '${ - ' '.repeat(2) + '' + element.innerHTML + '' - }
`; - break; - case '${ - ' '.repeat(4) + '' + element.innerHTML + '' - }
`; - break; - case '${ - ' '.repeat(6) + '' + element.innerHTML + '' - }
`; - break; - case '${ - ' '.repeat(8) + '' + element.innerHTML + '' - }
`; - break; - case '${ - ' '.repeat(10) + '' + element.innerHTML + '' - }
`; - break; - } - }); - menuStructure += '
'; - return menuStructure; -} - // 预加载图片 function prefetchImg() { if (cookie.getItem('settingEnableImgPrefetch') == 'false') { @@ -670,58 +628,9 @@ function prefetchImg() { }); } -// 检测元素是否可见 -function isElementVisible(element) { - const rect = element.getBoundingClientRect(); - const vWidth = window.innerWidth || document.documentElement.clientWidth; - const vHeight = window.innerHeight || document.documentElement.clientHeight; - if (rect.right < 0 || rect.bottom < 0 || rect.left > vWidth || rect.top > vHeight) { - return false; - } - return true; -} - -// 相对高度差 -function getHeightDifferent(element) { - const rect = element.getBoundingClientRect(); - const vWidth = document.querySelector('#viewmap article').clientWidth; - const vHeight = document.querySelector('#viewmap article').clientHeight; - if (rect.right < 0 || rect.bottom < 0 || rect.left > vWidth || rect.top > vHeight) { - return rect.top; - } - return 0; -} -// 目录高亮 -function highlightMenu() { - if (cookie.getItem('settingEnableMenuHighlight') == 'false') { - return false; - } - document.querySelectorAll('#articles-menu *.active').forEach((element) => { - element.classList.remove('active'); - }); - const titleList = document.querySelectorAll( - '#articles-body h2 , #articles-body h3 , #articles-body h4 , #articles-body h5 , #articles-body h6', - ); - for (let i = 0; i < titleList.length; i++) { - let heights = getHeightDifferent(titleList[i]); - if (heights == 0) { - document - .querySelector(`#articles-menu #${titleList[i].firstChild.id}`) - .classList.add('active'); - return titleList[i]; - } - if (heights > 0) { - document - .querySelector(`#articles-menu #${titleList[i - 1].firstChild.id}`) - .classList.add('active'); - return titleList[i - 1]; - } - } - return false; -} // 重置筛选 function resetFilter() {