diff --git a/components/NotionPage.js b/components/NotionPage.js index 1cbfad36..16e8716e 100644 --- a/components/NotionPage.js +++ b/components/NotionPage.js @@ -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) {