Files
NotionNext/lib/notion/convertInnerUrl.js
anime 45a6ec1eb4 feat(更换short_id实现方式): 避免因为缩短导致的潜在bug
(cherry picked from commit 6d73a4388c1613dc0a719d68c8b4b3432fefd314)
2024-12-20 18:17:15 +08:00

53 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { idToUuid } from 'notion-utils'
import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
/**
* 处理页面内连接跳转:
* 1.若是本站域名,则在当前窗口打开、不开新窗口
* 2.url是notion-id转成站内文章链接
*/
export const convertInnerUrl = allPages => {
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) === 14
})
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'
}
}
}
}