fix 页内url替换

This commit is contained in:
tangly1024.com
2024-10-14 14:05:09 +08:00
parent 675def4936
commit b17be3024e
2 changed files with 44 additions and 42 deletions

View File

@@ -1,16 +1,15 @@
import { siteConfig } from '@/lib/config'
import dynamic from 'next/dynamic'
import { GlobalStyle } from './GlobalStyle'
import LA51 from './LA51'
import TianLiGPT from './TianliGPT'
import WebWhiz from './Webwhiz'
import { convertInnerUrl } from '@/lib/notion/convertInnerUrl'
import { isBrowser, loadExternalResource } from '@/lib/utils'
import dynamic from 'next/dynamic'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
import Coze from './Coze'
import { GlobalStyle } from './GlobalStyle'
import { initGoogleAdsense } from './GoogleAdsense'
import LA51 from './LA51'
import TianLiGPT from './TianliGPT'
import WebWhiz from './Webwhiz'
/**
* 各种插件脚本
@@ -105,7 +104,7 @@ const ExternalPlugin = props => {
if (ADSENSE_GOOGLE_ID) {
setTimeout(() => {
initGoogleAdsense(ADSENSE_GOOGLE_ID)
}, 1000)
}, 3000)
}
// 映射url

View File

@@ -7,45 +7,48 @@ import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
* 2.url是notion-id转成站内文章链接
*/
export const convertInnerUrl = allPages => {
if (isBrowser) {
const allAnchorTags = document
?.getElementById('notion-article')
?.getElementsByTagName('a')
if (!isBrowser) {
return
}
const allAnchorTags = document
?.getElementById('notion-article')
?.querySelectorAll('a.notion-link')
if (!allAnchorTags) {
return
}
const currentURL = window.location.origin + window.location.pathname
// url替换成slug
for (const anchorTag of allAnchorTags) {
// 检查url
if (anchorTag?.href) {
// 如果url是一个Notion_id尝试匹配成博客的文章内链
const slug = getLastPartOfUrl(anchorTag.href)
if (checkStrIsNotionId(slug)) {
const slugPage = allPages?.find(page => {
return idToUuid(slug).indexOf(page.short_id) === 0
})
if (slugPage) {
anchorTag.href = slugPage?.href
}
}
}
}
if (!allAnchorTags) {
return
}
// 链接在当前页面打开
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'
const currentURL = window.location.origin + window.location.pathname
// url替换成slug
for (const anchorTag of allAnchorTags) {
// 检查url
if (anchorTag?.href) {
// 如果url是一个Notion_id尝试匹配成博客的文章内链
const slug = getLastPartOfUrl(anchorTag.href)
if (checkStrIsNotionId(slug)) {
const slugPage = allPages?.find(page => {
const find = idToUuid(slug).indexOf(page.short_id) === 0
return find
})
if (slugPage) {
anchorTag.href = slugPage?.href
}
}
}
}
// 链接在当前页面打开
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'
}
}
}
}