diff --git a/components/CommonHead.js b/components/CommonHead.js index b496255b..43e56fe7 100644 --- a/components/CommonHead.js +++ b/components/CommonHead.js @@ -1,7 +1,7 @@ import BLOG from '@/blog.config' import Head from 'next/head' -const CommonHead = ({ meta }) => { +const CommonHead = ({ meta, children }) => { let url = BLOG?.PATH?.length ? `${BLOG.LINK}/${BLOG.PATH}` : BLOG.LINK if (meta) { url = `${url}/${meta.slug}` @@ -42,6 +42,7 @@ const CommonHead = ({ meta }) => { )} {/* 谷歌字体镜像 */} + {children} } diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index dc978145..90b697f5 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -1,5 +1,5 @@ -import { getCacheFromMemory, setCacheToMemory } from '@/lib/cache/memory_cache' -// import { getCacheFromFile, setCacheToFile } from './local_file_cache' +import { getCacheFromMemory, setCacheToMemory, delCacheFromMemory } from '@/lib/cache/memory_cache' +// import { getCacheFromFile, setCacheToFile, delCacheFromFile } from './local_file_cache' const enableCache = true // 生产环境禁用 /** @@ -24,3 +24,10 @@ export async function setDataToCache (key, data) { } await setCacheToMemory(key, data) } + +export async function delCacheData (key) { + if (!enableCache) { + return + } + await delCacheFromMemory(key) +} diff --git a/lib/cache/local_file_cache.js b/lib/cache/local_file_cache.js index 75ee4f7d..390c3c66 100644 --- a/lib/cache/local_file_cache.js +++ b/lib/cache/local_file_cache.js @@ -40,3 +40,11 @@ export async function setCacheToFile (key, data) { json[key + '_expire_time'] = new Date().getTime() fs.writeFileSync(jsonFile, JSON.stringify(json)) } + +export async function delCacheFromFile (key, data) { + const exist = await fs.existsSync(jsonFile) + const json = exist ? JSON.parse(await fs.readFileSync(jsonFile)) : {} + delete json.key + json[key + '_expire_time'] = new Date().getTime() + fs.writeFileSync(jsonFile, JSON.stringify(json)) +} diff --git a/lib/cache/memory_cache.js b/lib/cache/memory_cache.js index 6965dbd7..f0ff0eec 100644 --- a/lib/cache/memory_cache.js +++ b/lib/cache/memory_cache.js @@ -10,3 +10,7 @@ export async function getCacheFromMemory (key, options) { export async function setCacheToMemory (key, data) { await cache.put(key, data, cacheTime * 1000) } + +export async function delCacheFromMemory (key) { + await cache.del(key) +} diff --git a/lib/notion/getAllPosts.js b/lib/notion/getAllPosts.js index 7473a4d8..246c5c3f 100644 --- a/lib/notion/getAllPosts.js +++ b/lib/notion/getAllPosts.js @@ -3,6 +3,7 @@ import getAllPageIds from './getAllPageIds' import getPageProperties from './getPageProperties' import { defaultMapImageUrl } from 'react-notion-x' import { getNotionPageData } from '@/lib/notion/getNotionData' +import { delCacheData } from '@/lib/cache/cache_manager' /** * 获取所有文章列表 @@ -61,6 +62,8 @@ export async function getAllPosts ({ notionPageData, from, includePage = false } if (!posts || posts.length === 0) { console.warn('文章列表为空') + const cacheKey = 'page_block_' + BLOG.NOTION_PAGE_ID + await delCacheData(cacheKey) } // Sort by date if (BLOG.POSTS_SORT_BY === 'date') { diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index bb91a655..61a5a934 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -59,7 +59,7 @@ export async function getGlobalNotionData ({ */ export async function getNotionPageData ({ pageId, from }) { // 尝试从缓存获取 - const cacheKey = 'page_record_map_' + pageId + const cacheKey = 'page_block_' + pageId const data = await getDataFromCache(cacheKey) if (data) { console.log('[请求缓存]:', `from:${from}`, `id:${pageId}`) diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index ecf42217..b2ef8f19 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -6,7 +6,7 @@ export async function getPostBlocks (id, from, slice) { const cacheKey = 'page_block_' + id let pageBlock = await getDataFromCache(cacheKey) if (pageBlock) { - console.log('[请求缓存]:', `from:${from}`, `id:${id}`) + console.log('[请求缓存]:', `from:${from}`, `id:${id}`, cacheKey) return filterPostBlocks(id, pageBlock, slice) } const authToken = BLOG.NOTION_ACCESS_TOKEN || null diff --git a/package.json b/package.json index be8c26f9..4e3a6dd3 100644 --- a/package.json +++ b/package.json @@ -33,14 +33,14 @@ "lodash.throttle": "^4.1.1", "memory-cache": "^0.2.0", "next": "^12.0.5", - "notion-client": "4.13.0", - "notion-utils": "4.12.0", + "notion-client": "4.14.1", + "notion-utils": "4.14.1", "preact": "^10.5.15", "qrcode.react": "^1.0.1", "react": "17.0.2", "react-cookies": "^0.1.1", "react-dom": "17.0.2", - "react-notion-x": "4.13.0", + "react-notion-x": "4.14.2", "smoothscroll-polyfill": "^0.4.4", "typed.js": "^2.0.12", "use-ackee": "^3.0.0" diff --git a/themes/Empty/index.js b/themes/Empty/index.js index aabed077..22fd5d15 100644 --- a/themes/Empty/index.js +++ b/themes/Empty/index.js @@ -1,3 +1,5 @@ +import CONFIG_EMPTY from './config_empty' +export { CONFIG_EMPTY as THEME_CONFIG } export { LayoutIndex } from './LayoutIndex' export { LayoutSearch } from './LayoutSearch' export { LayoutArchive } from './LayoutArchive' diff --git a/themes/Hexo/LayoutBase.js b/themes/Hexo/LayoutBase.js index 5fd7cef1..a3c49dcf 100644 --- a/themes/Hexo/LayoutBase.js +++ b/themes/Hexo/LayoutBase.js @@ -47,7 +47,7 @@ const LayoutBase = (props) => {
-
{children}
+
{children}
diff --git a/themes/Hexo/LayoutSlug.js b/themes/Hexo/LayoutSlug.js index b03095b9..87c69bee 100644 --- a/themes/Hexo/LayoutSlug.js +++ b/themes/Hexo/LayoutSlug.js @@ -1,10 +1,5 @@ import BLOG from '@/blog.config' -import formatDate from '@/lib/formatDate' -import { useGlobal } from '@/lib/global' import { getPageTableOfContents } from 'notion-utils' -import { faFolderOpen } from '@fortawesome/free-solid-svg-icons' -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' -import Link from 'next/link' import 'prismjs' import 'prismjs/components/prism-bash' import 'prismjs/components/prism-java' @@ -12,12 +7,12 @@ import 'prismjs/components/prism-javascript' import 'prismjs/components/prism-markup' import 'prismjs/components/prism-python' import 'prismjs/components/prism-typescript' -import CONFIG_NEXT from '../NEXT/config_next' -import ArticleDetail from './components/ArticleDetail' -import LayoutBase from './LayoutBase' -import TocDrawerButton from './components/TocDrawerButton' import { useRef } from 'react' +import ArticleDetail from './components/ArticleDetail' +import HeaderArticle from './components/HeaderArticle' import TocDrawer from './components/TocDrawer' +import TocDrawerButton from './components/TocDrawerButton' +import LayoutBase from './LayoutBase' export const LayoutSlug = props => { const { post } = props @@ -28,64 +23,11 @@ export const LayoutSlug = props => { tags: post.tags } - const { locale } = useGlobal() - const date = formatDate( - post?.date?.start_date || post.createdTime, - locale.LOCALE - ) - if (post?.blockMap?.block) { post.content = Object.keys(post.blockMap.block) post.toc = getPageTableOfContents(post, post.blockMap) } - const headerImage = post?.page_cover ? `url("${post.page_cover}")` : `url("/${CONFIG_NEXT.HOME_BANNER_IMAGE}")` - const headerSlot = ( -
-
-
- {/* 文章Title */} -
- {post.title} -
- -
-
- - - - {post.category} - - - | - - {post.type[0] !== 'Page' && ( - <> - - - {date} - - - - )} - -
- | - - 次访问 -
-
-
-
-
-
- ) const drawerRight = useRef(null) const targetRef = typeof window !== 'undefined' ? document.getElementById('container') : null @@ -104,7 +46,7 @@ export const LayoutSlug = props => { return ( } {...props} meta={meta} showCategory={false} diff --git a/themes/Hexo/components/Header.js b/themes/Hexo/components/Header.js index c55aac64..6a527cb8 100644 --- a/themes/Hexo/components/Header.js +++ b/themes/Hexo/components/Header.js @@ -1,3 +1,4 @@ +import BLOG from '@/blog.config' import { useGlobal } from '@/lib/global' import { faAngleDown } from '@fortawesome/free-solid-svg-icons' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' @@ -15,7 +16,12 @@ let autoScroll = false */ export default function Header () { const [typed, changeType] = useState() + const { theme } = useGlobal() + useEffect(() => { + scrollTrigger() + updateHeaderHeight() + updateTopNav() if (!typed && window && document.getElementById('typed')) { changeType( new Typed('#typed', { @@ -28,8 +34,13 @@ export default function Header () { }) ) } + window.addEventListener('scroll', scrollTrigger) + window.addEventListener('resize', updateHeaderHeight) + return () => { + window.removeEventListener('scroll', scrollTrigger) + window.removeEventListener('resize', updateHeaderHeight) + } }) - const { theme } = useGlobal() const autoScrollEnd = () => { if (autoScroll) { @@ -39,25 +50,29 @@ export default function Header () { } const scrollTrigger = () => { - if ( - (window.scrollY > windowTop) & - (window.scrollY < window.innerHeight) && - !autoScroll + const scrollS = window.scrollY + const nav = document.querySelector('#sticky-nav') + + if (scrollS < 300) { + nav && nav.classList.replace('bg-white', 'bg-none') + nav && nav.classList.replace('text-black', 'text-white') + } else { + nav && nav.classList.replace('bg-none', 'bg-white') + nav && nav.classList.replace('text-white', 'text-black') + } + + if ((scrollS > windowTop) & (scrollS < window.innerHeight) && !autoScroll ) { autoScroll = true window.scrollTo({ top: wrapperTop, behavior: 'smooth' }) setTimeout(autoScrollEnd, 500) } - if ( - (window.scrollY < windowTop) & - (window.scrollY < window.innerHeight) && - !autoScroll - ) { + if ((scrollS < windowTop) && (scrollS < window.innerHeight) && !autoScroll) { autoScroll = true window.scrollTo({ top: 0, behavior: 'smooth' }) setTimeout(autoScrollEnd, 500) } - windowTop = window.scrollY + windowTop = scrollS updateTopNav() } @@ -82,17 +97,6 @@ export default function Header () { }, 500) } - useEffect(() => { - updateHeaderHeight() - updateTopNav() - window.addEventListener('scroll', scrollTrigger) - window.addEventListener('resize', updateHeaderHeight) - return () => { - window.removeEventListener('scroll', scrollTrigger) - window.removeEventListener('resize', updateHeaderHeight) - } - }) - return (