slug 兼容性优化,支持用任意前缀访问文章不限于article

This commit is contained in:
tangly1024.com
2024-05-08 15:13:12 +08:00
parent f803b7ad06
commit de0908ea94
7 changed files with 209 additions and 151 deletions

View File

@@ -26,9 +26,12 @@ const Equation = dynamic(
{ ssr: false }
)
const Pdf = dynamic(() => import('react-notion-x/build/third-party/pdf').then(m => m.Pdf), {
ssr: false
})
const Pdf = dynamic(
() => import('react-notion-x/build/third-party/pdf').then(m => m.Pdf),
{
ssr: false
}
)
// https://github.com/txs
// import PrismMac from '@/components/PrismMac'
@@ -46,13 +49,25 @@ const TweetEmbed = dynamic(() => import('react-tweet-embed'), {
/**
* 文内google广告
*/
const AdEmbed = dynamic(() => import('@/components/GoogleAdsense').then(m => m.AdEmbed), { ssr: true })
const AdEmbed = dynamic(
() => import('@/components/GoogleAdsense').then(m => m.AdEmbed),
{ ssr: true }
)
const Collection = dynamic(() => import('react-notion-x/build/third-party/collection').then(m => m.Collection), {
ssr: true
})
const Collection = dynamic(
() =>
import('react-notion-x/build/third-party/collection').then(
m => m.Collection
),
{
ssr: true
}
)
const Modal = dynamic(() => import('react-notion-x/build/third-party/modal').then(m => m.Modal), { ssr: false })
const Modal = dynamic(
() => import('react-notion-x/build/third-party/modal').then(m => m.Modal),
{ ssr: false }
)
const Tweet = ({ id }) => {
return <TweetEmbed tweetId={id} />
@@ -83,19 +98,22 @@ const NotionPage = ({ post, className }) => {
// 将相册gallery下的图片加入放大功能
if (siteConfig('POST_DISABLE_GALLERY_CLICK')) {
setTimeout(() => {
if (isBrowser) {
const imgList = document?.querySelectorAll('.notion-collection-card-cover img')
if (imgList && zoomRef.current) {
for (let i = 0; i < imgList.length; i++) {
zoomRef.current.attach(imgList[i])
}
}
const imgList = document?.querySelectorAll(
'.notion-asset-wrapper-image img'
)
const cards = document.getElementsByClassName('notion-collection-card')
for (const e of cards) {
e.removeAttribute('href')
console.log('放大', imgList)
if (imgList && zoomRef.current) {
for (let i = 0; i < imgList.length; i++) {
zoomRef.current.attach(imgList[i])
}
}
const cards = document.getElementsByClassName('notion-collection-card')
for (const e of cards) {
e.removeAttribute('href')
}
}, 800)
}
@@ -108,10 +126,16 @@ const NotionPage = ({ post, className }) => {
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]
const hrefWithoutQueryHash = anchorTag.href
.split('?')[0]
.split('#')[0]
const hrefWithRelativeHash =
currentURL.split('#')[0] + anchorTag.href.split('#')[1]
if (currentURL === hrefWithoutQueryHash || currentURL === hrefWithRelativeHash) {
if (
currentURL === hrefWithoutQueryHash ||
currentURL === hrefWithRelativeHash
) {
anchorTag.target = '_self'
}
}
@@ -121,14 +145,20 @@ const NotionPage = ({ post, className }) => {
// 放大图片:调整图片质量
const observer = new MutationObserver((mutationsList, observer) => {
mutationsList.forEach(mutation => {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
if (
mutation.type === 'attributes' &&
mutation.attributeName === 'class'
) {
if (mutation.target.classList.contains('medium-zoom-image--opened')) {
// 等待动画完成后替换为更高清的图像
setTimeout(() => {
// 获取该元素的 src 属性
const src = mutation?.target?.getAttribute('src')
// 替换为更高清的图像
mutation?.target?.setAttribute('src', compressImage(src, siteConfig('IMAGE_ZOOM_IN_WIDTH', 1200)))
mutation?.target?.setAttribute(
'src',
compressImage(src, siteConfig('IMAGE_ZOOM_IN_WIDTH', 1200))
)
}, 800)
}
}
@@ -136,21 +166,23 @@ const NotionPage = ({ post, className }) => {
})
// 监视整个文档中的元素和属性的变化
observer.observe(document.body, { attributes: true, subtree: true, attributeFilter: ['class'] })
observer.observe(document.body, {
attributes: true,
subtree: true,
attributeFilter: ['class']
})
return () => {
observer.disconnect()
}
}, [])
if (!post || !post.blockMap) {
return <>{post?.summary || ''}</>
}
}, [post])
return (
<div id='notion-article' className={`mx-auto overflow-hidden ${className || ''}`}>
<div
id='notion-article'
className={`mx-auto overflow-hidden ${className || ''}`}>
<NotionRenderer
recordMap={post.blockMap}
recordMap={post?.blockMap}
mapPageUrl={mapPageUrl}
mapImageUrl={mapImgUrl}
components={{