Skip to content

Commit

Permalink
跳转页内链接处理
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Aug 1, 2023
1 parent 4b7564e commit cb1b15e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions components/NotionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cb1b15e

Please sign in to comment.