diff --git a/astro.config.ts b/astro.config.ts index 704e41f..e1bc6cb 100644 --- a/astro.config.ts +++ b/astro.config.ts @@ -18,6 +18,7 @@ import { remarkReadingTime } from './src/plugins/remark-reading-time.mjs' const url = themeConfig.site.url const locale = themeConfig.global.locale +const isKatexEnabled = themeConfig.global.katex const linkPrefetch = themeConfig.preload.linkPrefetch const imageHostURL = themeConfig.preload.imageHostURL const imageConfig = imageHostURL @@ -31,6 +32,28 @@ const imageConfig = imageHostURL } : {} +const remarkPlugins = [ + remarkDirective, + ...(isKatexEnabled ? [remarkMath] : []), + remarkAdmonitions, + remarkGithubCard, + remarkReadingTime, +] + +const rehypePlugins = [ + rehypeSlug, + ...(isKatexEnabled ? [rehypeKatex] : []), + rehypeImgToFigure, + [ + rehypeExternalLinks, + { + target: '_blank', + rel: ['nofollow', 'noopener', 'noreferrer', 'external'], + protocols: ['http', 'https', 'mailto'], + }, + ], +] as any[] + export default defineConfig({ site: url, base: '/', @@ -61,26 +84,8 @@ export default defineConfig({ robotsTxt(), ], markdown: { - remarkPlugins: [ - remarkDirective, - remarkMath, - remarkAdmonitions, - remarkGithubCard, - remarkReadingTime, - ], - rehypePlugins: [ - rehypeSlug, - rehypeKatex, - rehypeImgToFigure, - [ - rehypeExternalLinks, - { - target: '_blank', - rel: ['nofollow', 'noopener', 'noreferrer', 'external'], - protocols: ['http', 'https', 'mailto'], - }, - ], - ], + remarkPlugins, + rehypePlugins, shikiConfig: { // Available themes: https://shiki.style/themes themes: { diff --git a/src/components/Widgets/BackToTop.astro b/src/components/Widgets/BackToTop.astro index baaf2f3..721cfe3 100644 --- a/src/components/Widgets/BackToTop.astro +++ b/src/components/Widgets/BackToTop.astro @@ -60,18 +60,6 @@ function initBackToTop() { }) } -function cleanup() { - // Cleanup observer - if (observer) { - observer.disconnect() - observer = null - } - - // Remove event listeners - backToTopButton = null -} - // Handle page transitions document.addEventListener('astro:page-load', initBackToTop) -document.addEventListener('astro:before-swap', cleanup) diff --git a/src/components/Widgets/GithubCard.astro b/src/components/Widgets/GithubCard.astro index e7ee6a2..bc80520 100644 --- a/src/components/Widgets/GithubCard.astro +++ b/src/components/Widgets/GithubCard.astro @@ -11,7 +11,7 @@ function setupGithubCards() { observer.unobserve(entry.target) } }) - }, { rootMargin: '200px' }) + }, { rootMargin: '400px' }) githubCards.forEach(card => observer.observe(card)) } diff --git a/src/components/Widgets/PhotoSwipe.astro b/src/components/Widgets/PhotoSwipe.astro index 2a2039e..a6202a0 100644 --- a/src/components/Widgets/PhotoSwipe.astro +++ b/src/components/Widgets/PhotoSwipe.astro @@ -53,11 +53,13 @@ function setupPhotoSwipe() { } function lazySetupPhotoSwipe() { - if ('requestIdleCallback' in window) { + if (typeof window.requestIdleCallback === 'function') { window.requestIdleCallback(() => setupPhotoSwipe(), { timeout: 1000 }) } else { - setTimeout(setupPhotoSwipe, 100) + requestAnimationFrame(() => { + setupPhotoSwipe() + }) } } diff --git a/src/components/Widgets/Scrollbar.astro b/src/components/Widgets/Scrollbar.astro index 0b62be5..70dd177 100644 --- a/src/components/Widgets/Scrollbar.astro +++ b/src/components/Widgets/Scrollbar.astro @@ -24,43 +24,54 @@ function setupScrollbar() { bodyElement.setAttribute('data-scrollbar-initialized', 'true') } - // Add scrollbar to code blocks - const preElements = document.querySelectorAll('pre') - preElements.forEach((pre) => { - if (!pre.hasAttribute('data-scrollbar-initialized')) { + const setupSecondaryScrollbars = () => { + // Add scrollbar to TOC content + const tocElement = document.getElementById('toc-content') + if (tocElement && !tocElement.hasAttribute('data-scrollbar-initialized')) { OverlayScrollbars({ - target: pre, + target: tocElement, }, { scrollbars: { theme: 'scrollbar-widget', - autoHide: 'leave', - autoHideDelay: 500, + autoHide: 'never', }, overflow: { - y: 'hidden', + x: 'hidden', }, }) - pre.setAttribute('data-scrollbar-initialized', 'true') + tocElement.setAttribute('data-scrollbar-initialized', 'true') } - }) - // Add scrollbar to TOC content - const tocElement = document.getElementById('toc-content') - if (tocElement && !tocElement.hasAttribute('data-scrollbar-initialized')) { - OverlayScrollbars({ - target: tocElement, - }, { - scrollbars: { - theme: 'scrollbar-widget', - autoHide: 'never', - }, - overflow: { - x: 'hidden', - }, + // Add scrollbar to code blocks + const preElements = document.querySelectorAll('pre') + preElements.forEach((pre) => { + if (!pre.hasAttribute('data-scrollbar-initialized')) { + OverlayScrollbars({ + target: pre, + }, { + scrollbars: { + theme: 'scrollbar-widget', + autoHide: 'leave', + autoHideDelay: 500, + }, + overflow: { + y: 'hidden', + }, + }) + + pre.setAttribute('data-scrollbar-initialized', 'true') + } }) + } - tocElement.setAttribute('data-scrollbar-initialized', 'true') + if (typeof window.requestIdleCallback === 'function') { + window.requestIdleCallback(setupSecondaryScrollbars, { timeout: 1000 }) + } + else { + requestAnimationFrame(() => { + setupSecondaryScrollbars() + }) } } diff --git a/src/layouts/Head.astro b/src/layouts/Head.astro index b7f6396..7c6c88a 100644 --- a/src/layouts/Head.astro +++ b/src/layouts/Head.astro @@ -54,7 +54,7 @@ const pageImage = postSlug -{katex && } +{katex && } {commentURL && } {commentURL && }