Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Oct 15, 2024
2 parents a703162 + ab21e1f commit 084ae02
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
19 changes: 10 additions & 9 deletions components/ExternalPlugins.js
Original file line number Diff line number Diff line change
@@ -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'

/**
* 各种插件脚本
Expand Down Expand Up @@ -105,11 +104,13 @@ const ExternalPlugin = props => {
if (ADSENSE_GOOGLE_ID) {
setTimeout(() => {
initGoogleAdsense(ADSENSE_GOOGLE_ID)
}, 1000)
}, 3000)
}

// 映射url
convertInnerUrl(props?.allNavPages)
setTimeout(() => {
// 映射url
convertInnerUrl(props?.allNavPages)
}, 500)
}, [router])

useEffect(() => {
Expand Down
69 changes: 36 additions & 33 deletions lib/notion/convertInnerUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,47 @@ 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
}

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'
}
// 链接在当前页面打开
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'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion themes/magzine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const LayoutBase = props => {
<Style />

<div
id='theme-medium'
id='theme-magzine'
className={`${siteConfig('FONT_STYLE')} bg-white dark:bg-hexo-black-gray w-full h-full min-h-screen justify-center dark:text-gray-300 scroll-smooth`}>
<main
id='wrapper'
Expand Down

0 comments on commit 084ae02

Please sign in to comment.