Merge pull request #3073 from qixing-jk/feat-article-multi-lang-link

feat(支持文章内站内链接多语言跳转并优化性能):
This commit is contained in:
tangly1024
2024-12-21 21:23:49 +08:00
committed by GitHub

View File

@@ -1,5 +1,6 @@
import { idToUuid } from 'notion-utils'
import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
import { loadLangFromLocalStorage } from '@/lib/lang'
/**
* 处理页面内连接跳转:
@@ -17,11 +18,13 @@ export const convertInnerUrl = allPages => {
if (!allAnchorTags) {
return
}
const currentURL = window.location.origin + window.location.pathname
// url替换成slug
const { origin, pathname } = window.location;
const currentURL = origin + pathname
const currentPathLang = pathname.split('/').filter(Boolean)[0]
const lang = loadLangFromLocalStorage().split(/[-_]/)[0]
const langPrefix = lang === currentPathLang ? '/' + lang : ''
for (const anchorTag of allAnchorTags) {
// 检查url
// url替换成slug
if (anchorTag?.href) {
// 如果url是一个Notion_id尝试匹配成博客的文章内链
const slug = getLastPartOfUrl(anchorTag.href)
@@ -31,14 +34,11 @@ export const convertInnerUrl = allPages => {
return find
})
if (slugPage) {
anchorTag.href = slugPage?.href
anchorTag.href = langPrefix + slugPage?.href
}
}
}
}
// 链接在当前页面打开
for (const anchorTag of allAnchorTags) {
// 链接在当前页面打开
if (anchorTag?.target === '_blank') {
const hrefWithoutQueryHash = anchorTag.href.split('?')[0].split('#')[0]
const hrefWithRelativeHash =