跳转页内链接处理

This commit is contained in:
tangly1024.com
2023-08-01 16:51:39 +08:00
parent 4b7564e018
commit cb1b15e9ee

View File

@@ -80,6 +80,25 @@ const NotionPage = ({ post, className }) => {
}
}, 800)
}
/**
* 处理页面内连接跳转
* 如果链接就是当前网站,则不打开新窗口访问
*/
if (isBrowser) {
const currentURL = window.location.origin + window.location.pathname
const allAnchorTags = document.getElementsByTagName('a') // 或者使用 document.querySelectorAll('a') 获取 NodeList
for (const anchorTag of allAnchorTags) {
if (anchorTag?.target === '_blank') {
const hrefWithoutQueryHash = anchorTag.href.split('?')[0].split('#')[0]
const hrefWithRelativeHash = currentURL.split('#')[0] + anchorTag.href.split('#')[1]
if (currentURL === hrefWithoutQueryHash || currentURL === hrefWithRelativeHash) {
anchorTag.target = '_self'
}
}
}
}
}, [])
if (!post || !post.blockMap) {