diff --git a/pages/[...slug].js b/pages/[...slug].js index c790f211..cae6d4b1 100644 --- a/pages/[...slug].js +++ b/pages/[...slug].js @@ -4,7 +4,7 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData' import { useGlobal } from '@/lib/global' import * as ThemeMap from '@/themes' import React from 'react' -import { idToUuid } from 'notion-utils' +import { idToUuid, getPageTableOfContents } from 'notion-utils' import Router from 'next/router' import { isBrowser } from '@/lib/utils' import { getNotion } from '@/lib/notion/getNotion' @@ -28,6 +28,11 @@ const Slug = props => { if (post?.password && post?.password !== '') { setLock(true) } else { + if (!lock && post?.blockMap?.block) { + post.content = Object.keys(post.blockMap.block) + post.toc = getPageTableOfContents(post, post.blockMap) + } + setLock(false) } }, [post]) @@ -51,10 +56,12 @@ const Slug = props => { * 验证文章密码 * @param {*} result */ - const validPassword = result => { - if (result) { + const validPassword = passInput => { + if (passInput && passInput === post.password) { setLock(false) + return true } + return false } props = { ...props, lock, setLock, validPassword } diff --git a/themes/example/LayoutSlug.js b/themes/example/LayoutSlug.js index 1f7fd409..b16892b7 100644 --- a/themes/example/LayoutSlug.js +++ b/themes/example/LayoutSlug.js @@ -1,4 +1,3 @@ -import { getPageTableOfContents } from 'notion-utils' import LayoutBase from './LayoutBase' import { ArticleLock } from './components/ArticleLock' import NotionPage from '@/components/NotionPage' @@ -12,15 +11,10 @@ export const LayoutSlug = props => { return } - if (!lock && post?.blockMap?.block) { - post.content = Object.keys(post.blockMap.block) - post.toc = getPageTableOfContents(post, post.blockMap) - } - return ( - {lock && } + {lock && } {!lock &&
diff --git a/themes/example/components/ArticleLock.js b/themes/example/components/ArticleLock.js index a9f5f59d..43b6647e 100644 --- a/themes/example/components/ArticleLock.js +++ b/themes/example/components/ArticleLock.js @@ -8,14 +8,12 @@ import { useGlobal } from '@/lib/global' * @returns */ export const ArticleLock = props => { - const { password, validPassword } = props + const { validPassword } = props const { locale } = useGlobal() const submitPassword = () => { const p = document.getElementById('password') - if (p && p.value && p.value === password) { - validPassword(true) - } else { + if (!validPassword(p?.value)) { const tips = document.getElementById('tips') if (tips) { tips.innerHTML = '' diff --git a/themes/fukasawa/LayoutSlug.js b/themes/fukasawa/LayoutSlug.js index 30265a6c..fb78690c 100644 --- a/themes/fukasawa/LayoutSlug.js +++ b/themes/fukasawa/LayoutSlug.js @@ -1,19 +1,13 @@ -import { getPageTableOfContents } from 'notion-utils' import ArticleDetail from './components/ArticleDetail' import LayoutBase from './LayoutBase' import { ArticleLock } from './components/ArticleLock' export const LayoutSlug = (props) => { - const { post, lock, validPassword } = props - if (!lock && post?.blockMap?.block) { - post.content = Object.keys(post.blockMap.block) - post.toc = getPageTableOfContents(post, post.blockMap) - } - + const { lock, validPassword } = props return ( {!lock && } - {lock && } + {lock && } ) } diff --git a/themes/fukasawa/components/ArticleLock.js b/themes/fukasawa/components/ArticleLock.js index 23112eeb..8ae18a4e 100644 --- a/themes/fukasawa/components/ArticleLock.js +++ b/themes/fukasawa/components/ArticleLock.js @@ -8,14 +8,12 @@ import { useGlobal } from '@/lib/global' * @returns */ export const ArticleLock = props => { - const { password, validPassword } = props + const { validPassword } = props const { locale } = useGlobal() const submitPassword = () => { const p = document.getElementById('password') - if (p && p.value && p.value === password) { - validPassword(true) - } else { + if (!validPassword(p?.value)) { const tips = document.getElementById('tips') if (tips) { tips.innerHTML = '' diff --git a/themes/hexo/LayoutSlug.js b/themes/hexo/LayoutSlug.js index 0818ba01..327497ac 100644 --- a/themes/hexo/LayoutSlug.js +++ b/themes/hexo/LayoutSlug.js @@ -1,4 +1,3 @@ -import { getPageTableOfContents } from 'notion-utils' import { useRef } from 'react' import { ArticleLock } from './components/ArticleLock' import HeaderArticle from './components/HeaderArticle' @@ -26,11 +25,6 @@ export const LayoutSlug = props => { > } - if (!lock && post?.blockMap?.block) { - post.content = Object.keys(post.blockMap.block) - post.toc = getPageTableOfContents(post, post.blockMap) - } - const targetRef = isBrowser() ? document.getElementById('container') : null const floatSlot = <> @@ -53,7 +47,7 @@ export const LayoutSlug = props => { floatSlot={floatSlot} >
- {lock && } + {lock && } {!lock &&
diff --git a/themes/hexo/components/ArticleLock.js b/themes/hexo/components/ArticleLock.js index 84ef905f..1a1bd193 100644 --- a/themes/hexo/components/ArticleLock.js +++ b/themes/hexo/components/ArticleLock.js @@ -8,13 +8,11 @@ import { useGlobal } from '@/lib/global' * @returns */ export const ArticleLock = props => { - const { password, validPassword } = props + const { validPassword } = props const { locale } = useGlobal() const submitPassword = () => { const p = document.getElementById('password') - if (p && p.value && p.value === password) { - validPassword(true) - } else { + if (!validPassword(p?.value)) { const tips = document.getElementById('tips') if (tips) { tips.innerHTML = '' diff --git a/themes/hexo/components/HeaderArticle.js b/themes/hexo/components/HeaderArticle.js index 918c75c7..8ebfe2bd 100644 --- a/themes/hexo/components/HeaderArticle.js +++ b/themes/hexo/components/HeaderArticle.js @@ -4,12 +4,13 @@ import formatDate from '@/lib/formatDate' import BLOG from '@/blog.config' export default function HeaderArticle({ post, siteInfo }) { + const { locale } = useGlobal() + if (!post) { return <> } const headerImage = post?.page_cover ? `url("${post.page_cover}")` : `url("${siteInfo?.pageCover}")` - const { locale } = useGlobal() const date = formatDate( post?.date?.start_date || post?.createdTime, locale.LOCALE diff --git a/themes/medium/LayoutSlug.js b/themes/medium/LayoutSlug.js index f7fdc4d0..bdeec1a5 100644 --- a/themes/medium/LayoutSlug.js +++ b/themes/medium/LayoutSlug.js @@ -1,5 +1,3 @@ -import { getPageTableOfContents } from 'notion-utils' - import LayoutBase from './LayoutBase' import { useGlobal } from '@/lib/global' import React from 'react' @@ -16,11 +14,6 @@ export const LayoutSlug = props => { /> } - if (!lock && post?.blockMap?.block) { - post.content = Object.keys(post.blockMap.block) - post.toc = getPageTableOfContents(post, post.blockMap) - } - const slotRight = post?.toc && post?.toc?.length > 3 && (
@@ -29,7 +22,7 @@ export const LayoutSlug = props => { return ( - {!lock ? : } + {!lock ? : } ) } diff --git a/themes/medium/components/ArticleLock.js b/themes/medium/components/ArticleLock.js index 4b969835..1e1bb228 100644 --- a/themes/medium/components/ArticleLock.js +++ b/themes/medium/components/ArticleLock.js @@ -8,14 +8,12 @@ import { useGlobal } from '@/lib/global' * @returns */ export const ArticleLock = props => { - const { password, validPassword } = props + const { validPassword } = props const { locale } = useGlobal() const submitPassword = () => { const p = document.getElementById('password') - if (p && p.value && p.value === password) { - validPassword(true) - } else { + if (!validPassword(p?.value)) { const tips = document.getElementById('tips') if (tips) { tips.innerHTML = '' diff --git a/themes/next/LayoutSlug.js b/themes/next/LayoutSlug.js index aa1cc4d2..fa1915a1 100644 --- a/themes/next/LayoutSlug.js +++ b/themes/next/LayoutSlug.js @@ -1,4 +1,3 @@ -import { getPageTableOfContents } from 'notion-utils' import TocDrawerButton from './components/TocDrawerButton' import LayoutBase from './LayoutBase' import Card from './components/Card' @@ -12,11 +11,6 @@ import { isBrowser } from '@/lib/utils' export const LayoutSlug = (props) => { const { post, latestPosts, lock, validPassword } = props - if (!lock && post?.blockMap?.block) { - post.content = Object.keys(post.blockMap.block) - post.toc = getPageTableOfContents(post, post.blockMap) - } - const drawerRight = useRef(null) const targetRef = isBrowser() ? document.getElementById('container') : null const floatSlot = post?.toc?.length > 1 @@ -45,7 +39,7 @@ export const LayoutSlug = (props) => { {!lock && } - {lock && } + {lock && } {/* 悬浮目录按钮 */}
diff --git a/themes/next/components/ArticleLock.js b/themes/next/components/ArticleLock.js index 4bb2ef65..3e4a2019 100644 --- a/themes/next/components/ArticleLock.js +++ b/themes/next/components/ArticleLock.js @@ -8,14 +8,12 @@ import { useGlobal } from '@/lib/global' * @returns */ export const ArticleLock = props => { - const { password, validPassword } = props + const { validPassword } = props const { locale } = useGlobal() const submitPassword = () => { const p = document.getElementById('password') - if (p && p.value && p.value === password) { - validPassword(true) - } else { + if (!validPassword(p?.value)) { const tips = document.getElementById('tips') if (tips) { tips.innerHTML = ''