From cb1b15e9eeba0f18a129ca226963f08f72be79b4 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Tue, 1 Aug 2023 16:51:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E9=A1=B5=E5=86=85=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/NotionPage.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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) {