From 87cd1ea55747a8eac8628b3d63e6737d31b06097 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 12 Apr 2022 12:31:04 +0800 Subject: [PATCH 01/14] =?UTF-8?q?Next=E4=B8=BB=E9=A2=98=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/next/components/Footer.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/themes/next/components/Footer.js b/themes/next/components/Footer.js index 8247f344..ac93dc0f 100644 --- a/themes/next/components/Footer.js +++ b/themes/next/components/Footer.js @@ -7,17 +7,17 @@ const Footer = ({ title }) => { const startYear = BLOG.SINCE && BLOG.SINCE !== currentYear && BLOG.SINCE + '-' return ( From b70df22f0c33d829b20f887986a9028a244681dd Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 12 Apr 2022 12:44:46 +0800 Subject: [PATCH 02/14] =?UTF-8?q?effect=E6=B1=A1=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/NotionPage.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/components/NotionPage.js b/components/NotionPage.js index c2fe6c3c..0267571a 100644 --- a/components/NotionPage.js +++ b/components/NotionPage.js @@ -11,6 +11,7 @@ import { NotionRenderer } from 'react-notion-x' import mediumZoom from 'medium-zoom' import { useEffect, useRef } from 'react' import dynamic from 'next/dynamic' +import { useRouter } from 'next/router' const Code = dynamic(() => import('react-notion-x/build/third-party/code').then((m) => m.Code) @@ -43,6 +44,8 @@ const NotionPage = ({ post }) => { }) const zoomRef = useRef(zoom ? zoom.clone() : null) + const router = useRouter() + useEffect(() => { // 将所有container下的所有图片添加medium-zoom const container = document?.getElementById('container') @@ -52,7 +55,7 @@ const NotionPage = ({ post }) => { (zoomRef.current).attach(imgList[i]) } } - }) + }, [router.events]) return Date: Tue, 12 Apr 2022 12:45:04 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E4=B8=BB=E9=A2=98=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/global.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/global.js b/lib/global.js index 10bfae28..ea93caff 100644 --- a/lib/global.js +++ b/lib/global.js @@ -6,7 +6,6 @@ import { initDarkMode, initTheme, saveThemeToCookies } from '@/lib/theme' import { ALL_THEME } from '@/themes' const GlobalContext = createContext() -let hasInit = false /** * 全局变量Provider,包括语言本地化、样式主题、搜索词 @@ -14,7 +13,7 @@ let hasInit = false * @returns {JSX.Element} * @constructor */ -export function GlobalContextProvider ({ children }) { +export function GlobalContextProvider({ children }) { const [locale, updateLocale] = useState(generateLocaleDict('en-US')) const [isDarkMode, updateDarkMode] = useState(false) const [onLoading, changeLoadingState] = useState(false) @@ -27,13 +26,13 @@ export function GlobalContextProvider ({ children }) { changeLoadingState(false) }) - function switchTheme () { + function switchTheme() { const currentIndex = ALL_THEME.indexOf(theme) const newIndex = currentIndex < ALL_THEME.length - 1 ? currentIndex + 1 : 0 changeTheme(ALL_THEME[newIndex]) } - function changeTheme (theme) { + function changeTheme(theme) { Router.query.theme = '' if (ALL_THEME.indexOf(theme) > -1) { setTheme(theme) @@ -44,13 +43,10 @@ export function GlobalContextProvider ({ children }) { } useEffect(() => { - if (!hasInit) { - initLocale(locale, updateLocale) - initDarkMode(isDarkMode, updateDarkMode) - initTheme(theme, changeTheme) - hasInit = true - } - }) + initLocale(locale, updateLocale) + initDarkMode(isDarkMode, updateDarkMode) + initTheme(theme, changeTheme) + }, []) return ( From 4c9e84e11d3aa5f735ef9faa7c03ef699f97aa09 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 12 Apr 2022 13:27:31 +0800 Subject: [PATCH 04/14] =?UTF-8?q?=E6=A0=B7=E5=BC=8F=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/hexo/components/BlogPostListPage.js | 4 ++-- themes/next/components/ArticleDetail.js | 2 +- themes/next/components/Toc.js | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/themes/hexo/components/BlogPostListPage.js b/themes/hexo/components/BlogPostListPage.js index 038bb322..4964f024 100644 --- a/themes/hexo/components/BlogPostListPage.js +++ b/themes/hexo/components/BlogPostListPage.js @@ -13,7 +13,7 @@ import BlogPostListEmpty from './BlogPostListEmpty' */ const BlogPostListPage = ({ page = 1, posts = [], postCount }) => { const totalPage = Math.ceil(postCount / BLOG.POSTS_PER_PAGE) - const showPagination = posts.length === BLOG.POSTS_PER_PAGE + const showPagination = postCount >= BLOG.POSTS_PER_PAGE if (!posts || posts.length === 0) { return @@ -26,7 +26,7 @@ const BlogPostListPage = ({ page = 1, posts = [], postCount }) => { ))} - {showPagination && } + {showPagination && } ) } diff --git a/themes/next/components/ArticleDetail.js b/themes/next/components/ArticleDetail.js index ba49b0a6..094b6b22 100644 --- a/themes/next/components/ArticleDetail.js +++ b/themes/next/components/ArticleDetail.js @@ -25,7 +25,7 @@ export default function ArticleDetail(props) { return (
{showArticleInfo &&
diff --git a/themes/next/components/Toc.js b/themes/next/components/Toc.js index ce83e41b..adc4f7f6 100644 --- a/themes/next/components/Toc.js +++ b/themes/next/components/Toc.js @@ -60,10 +60,10 @@ const Toc = ({ toc }) => { return
- +
-
diff --git a/themes/fukasawa/components/ArticleDetail.js b/themes/fukasawa/components/ArticleDetail.js index cbdfa0b7..77271f5f 100644 --- a/themes/fukasawa/components/ArticleDetail.js +++ b/themes/fukasawa/components/ArticleDetail.js @@ -10,11 +10,15 @@ import ArticleAround from './ArticleAround' * @param {*} param0 * @returns */ -export default function ArticleDetail({ post, recommendPosts, prev, next }) { +export default function ArticleDetail(props) { + const { post, prev, next } = props + if (!post) { + return <> + } const { locale } = useGlobal() - const date = formatDate(post?.date?.start_date || post.createdTime, locale.LOCALE) + const date = formatDate(post?.date?.start_date || post?.createdTime, locale.LOCALE) return (
- {post.type && !post.type.includes('Page') && post?.page_cover && ( + {post?.type && !post?.type.includes('Page') && post?.page_cover && (
{/* eslint-disable-next-line @next/next/no-img-element */} {post.title} @@ -41,7 +45,7 @@ export default function ArticleDetail({ post, recommendPosts, prev, next }) { | - {post.type[0] !== 'Page' && (<> + {post?.type[0] !== 'Page' && (<> - {post.blockMap && } + {post && }
diff --git a/themes/hexo/LayoutSlug.js b/themes/hexo/LayoutSlug.js index e2fec724..ee3143cf 100644 --- a/themes/hexo/LayoutSlug.js +++ b/themes/hexo/LayoutSlug.js @@ -15,6 +15,15 @@ import ArticleRecommend from './components/ArticleRecommend' export const LayoutSlug = props => { const { post, lock, validPassword } = props + if (!post) { + return } + {...props} + showCategory={false} + showTag={false} + > + } + if (!lock && post?.blockMap?.block) { post.content = Object.keys(post.blockMap.block) post.toc = getPageTableOfContents(post, post.blockMap) @@ -50,7 +59,7 @@ export const LayoutSlug = props => {
{/* Notion文章主体 */}
- {post.blockMap && } + {post && }
diff --git a/themes/hexo/components/HeaderArticle.js b/themes/hexo/components/HeaderArticle.js index 3e91179d..bd0bea4a 100644 --- a/themes/hexo/components/HeaderArticle.js +++ b/themes/hexo/components/HeaderArticle.js @@ -4,12 +4,15 @@ import formatDate from '@/lib/formatDate' import { useEffect } from 'react' export default function HeaderArticle({ post, siteInfo }) { + if (!post) { + return <>loading... + } const headerImage = post?.page_cover ? `url("${post.page_cover}")` : `url("${siteInfo?.pageCover}")` const { isDarkMode } = useGlobal() const { locale } = useGlobal() const date = formatDate( - post?.date?.start_date || post.createdTime, + post?.date?.start_date || post?.createdTime, locale.LOCALE ) @@ -71,7 +74,7 @@ export default function HeaderArticle({ post, siteInfo }) { }
- {post.type[0] !== 'Page' && ( + {post?.type[0] !== 'Page' && ( <> { const { post, lock, validPassword } = props + if (!post) { + return + } if (!lock && post?.blockMap?.block) { post.content = Object.keys(post.blockMap.block) diff --git a/themes/medium/components/ArticleDetail.js b/themes/medium/components/ArticleDetail.js index a0ed321e..ddaa9a76 100644 --- a/themes/medium/components/ArticleDetail.js +++ b/themes/medium/components/ArticleDetail.js @@ -15,7 +15,7 @@ export const ArticleDetail = props => { const { locale } = useGlobal() const date = formatDate( - post?.date?.start_date || post.createdTime, + post?.date?.start_date || post?.createdTime, locale.LOCALE ) return
@@ -48,7 +48,7 @@ export const ArticleDetail = props => { {/* Notion文章主体 */}
- {post.blockMap && ()} + {post && ()}
diff --git a/themes/medium/components/Progress.js b/themes/medium/components/Progress.js index 3680e157..d9a13ad9 100644 --- a/themes/medium/components/Progress.js +++ b/themes/medium/components/Progress.js @@ -10,7 +10,6 @@ const Progress = ({ targetRef, showPercent = true }) => { const [percent, changePercent] = useState(0) const scrollListener = () => { const target = currentRef || document.getElementById('container') - console.log(target) if (target) { const clientHeight = target.clientHeight const scrollY = window.pageYOffset diff --git a/themes/next/LayoutSlug.js b/themes/next/LayoutSlug.js index 097f457b..8e7ddd72 100644 --- a/themes/next/LayoutSlug.js +++ b/themes/next/LayoutSlug.js @@ -11,6 +11,14 @@ import { ArticleLock } from './components/ArticleLock' export const LayoutSlug = (props) => { const { post, latestPosts, lock, validPassword } = props + if (!post) { + return + } + /> + } if (!lock && post?.blockMap?.block) { post.content = Object.keys(post.blockMap.block) diff --git a/themes/next/components/ArticleDetail.js b/themes/next/components/ArticleDetail.js index 094b6b22..ab7f9608 100644 --- a/themes/next/components/ArticleDetail.js +++ b/themes/next/components/ArticleDetail.js @@ -21,7 +21,7 @@ export default function ArticleDetail(props) { const { post, recommendPosts, prev, next, showArticleInfo } = props const url = BLOG.LINK + useRouter().asPath const { locale } = useGlobal() - const date = formatDate(post?.date?.start_date || post.createdTime, locale.LOCALE) + const date = formatDate(post?.date?.start_date || post?.createdTime, locale.LOCALE) return (
{showArticleInfo &&
- {post.type && !post.type.includes('Page') && post?.page_cover && ( + {post?.type && !post?.type.includes('Page') && post?.page_cover && (
{/* eslint-disable-next-line @next/next/no-img-element */} {post.title} @@ -51,7 +51,7 @@ export default function ArticleDetail(props) { | } - {post.type[0] !== 'Page' && (<> + {post?.type[0] !== 'Page' && (<> - {post.blockMap && ()} + {post && ()}
From 52e707ced3212070ac9797330aab9b0c59adfd30 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 13 Apr 2022 13:42:09 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E3=80=81=E7=BC=93?= =?UTF-8?q?=E5=AD=98=20=E3=80=81=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/NotionPage.js | 2 +- lib/cache/memory_cache.js | 8 ++++---- lib/notion/getNotionData.js | 2 +- package.json | 4 ++-- pages/search/[keyword].js | 8 +++++--- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/components/NotionPage.js b/components/NotionPage.js index ace4fe5b..4295f224 100644 --- a/components/NotionPage.js +++ b/components/NotionPage.js @@ -38,7 +38,7 @@ const Modal = dynamic( ) const NotionPage = ({ post }) => { if (!post || !post.blockMap) { - return <>Loading... + return <>{post?.summary || ''} } const zoom = typeof window !== 'undefined' && mediumZoom({ diff --git a/lib/cache/memory_cache.js b/lib/cache/memory_cache.js index f0ff0eec..5bfee605 100644 --- a/lib/cache/memory_cache.js +++ b/lib/cache/memory_cache.js @@ -3,14 +3,14 @@ import BLOG from 'blog.config' const cacheTime = BLOG.isProd ? 10 * 60 : 120 * 60 // 120 minutes for dev,10 minutes for prod -export async function getCacheFromMemory (key, options) { - return cache.get(key) +export async function getCacheFromMemory(key, options) { + return await cache.get(key) } -export async function setCacheToMemory (key, data) { +export async function setCacheToMemory(key, data) { await cache.put(key, data, cacheTime * 1000) } -export async function delCacheFromMemory (key) { +export async function delCacheFromMemory(key) { await cache.del(key) } diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index 68c10f7e..55a3d031 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -80,7 +80,7 @@ export async function getNotionPageData({ pageId, from }) { const cacheKey = 'page_block_' + pageId const data = await getDataFromCache(cacheKey) if (data) { - console.log('[请求缓存]:', `from:${from}`, `id:${pageId}`) + console.log('[请求缓存]:', `from:${from}`, `root-page-id:${pageId}`) return data } const pageRecordMap = await getPageRecordMapByNotionAPI({ pageId, from }) diff --git a/package.json b/package.json index be7d70c0..d47bdc78 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,8 @@ "lodash.throttle": "^4.1.1", "memory-cache": "^0.2.0", "next": "^12.0.5", - "notion-client": "6.5.0", - "notion-utils": "6.5.0", + "notion-client": "6.10.0", + "notion-utils": "6.10.0", "preact": "^10.5.15", "qrcode.react": "^1.0.1", "react": "17.0.2", diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index a45ae9bf..706ae7ae 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -93,9 +93,11 @@ const isIterable = obj => */ async function filterByMemCache(allPosts, keyword) { const filterPosts = [] + if (keyword) { + keyword = keyword.trim() + } for (const post of allPosts) { const cacheKey = 'page_block_' + post.id - // const page = await getPostBlocks(post.id, 'search') const page = await getDataFromCache(cacheKey) const tagContent = post.tags ? post.tags.join(' ') : '' const categoryContent = post.category ? post.category.join(' ') : '' @@ -110,7 +112,7 @@ async function filterByMemCache(allPosts, keyword) { indexContent = appendText(indexContent, properties, 'caption') }) } - console.log('搜索是否命中缓存', page !== null, indexContent) + console.log('全文搜索缓存', cacheKey, page) post.results = [] let hitCount = 0 for (const i in indexContent) { @@ -118,7 +120,7 @@ async function filterByMemCache(allPosts, keyword) { if (!c) { continue } - const index = c.toLowerCase().indexOf(keyword.toLowerCase()) || -1 + const index = c.toLowerCase().indexOf(keyword.toLowerCase()) if (index > -1) { hit = true hitCount += 1 From a00241040d7e42d1b982659a0cb04203747fc064 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 13 Apr 2022 14:25:15 +0800 Subject: [PATCH 12/14] =?UTF-8?q?=E5=85=A8=E6=96=87=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 706ae7ae..12ab9dda 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -2,14 +2,14 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData' import { useGlobal } from '@/lib/global' import { getDataFromCache } from '@/lib/cache/cache_manager' import * as ThemeMap from '@/themes' +import BLOG from '@/blog.config' const Index = props => { const { keyword, siteInfo } = props const { locale } = useGlobal() const meta = { - title: `${keyword || ''}${keyword ? ' | ' : ''}${locale.NAV.SEARCH} | ${siteInfo.title - }`, - description: siteInfo.title, + title: `${keyword || ''}${keyword ? ' | ' : ''}${locale.NAV.SEARCH} | ${siteInfo?.title}`, + description: siteInfo?.title, slug: 'search/' + (keyword || ''), type: 'website' } @@ -29,7 +29,7 @@ const Index = props => { * @param {*} param0 * @returns */ -export async function getServerSideProps({ params: { keyword } }) { +export async function getStaticProps({ params: { keyword } }) { const props = await getGlobalNotionData({ from: 'search-props', pageType: ['Post'] @@ -41,6 +41,13 @@ export async function getServerSideProps({ params: { keyword } }) { } } +export async function getStaticPaths() { + return { + paths: [{ params: { keyword: BLOG.TITLE } }], + fallback: true + } +} + /** * 将对象的指定字段拼接到字符串 * @param sourceTextArray From e6ff7255005196d0cab08e4f4c6344313cf30897 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 13 Apr 2022 14:38:17 +0800 Subject: [PATCH 13/14] =?UTF-8?q?=E7=B4=A2=E5=BC=95=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index 12ab9dda..b91db5e2 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -37,7 +37,8 @@ export async function getStaticProps({ params: { keyword } }) { props.posts = await filterByMemCache(props.allPosts, keyword) props.keyword = keyword return { - props + props, + revalidate: 1 } } From d6eca572c99ed73b491b1628cbef65ebe46a43bd Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 13 Apr 2022 14:46:37 +0800 Subject: [PATCH 14/14] =?UTF-8?q?=E7=B4=A2=E5=BC=95=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/search/[keyword].js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index b91db5e2..74591872 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -120,7 +120,7 @@ async function filterByMemCache(allPosts, keyword) { indexContent = appendText(indexContent, properties, 'caption') }) } - console.log('全文搜索缓存', cacheKey, page) + console.log('全文搜索缓存', cacheKey, page != null) post.results = [] let hitCount = 0 for (const i in indexContent) {