Merge branch 'main' into feat/theme-landing-2

This commit is contained in:
tangly1024.com
2024-02-21 15:17:28 +08:00
9 changed files with 45 additions and 29 deletions

View File

@@ -106,7 +106,7 @@ export async function getNotionPageData({ pageId, from }) {
const cacheKey = 'page_block_' + pageId const cacheKey = 'page_block_' + pageId
const data = await getDataFromCache(cacheKey) const data = await getDataFromCache(cacheKey)
if (data && data.pageIds?.length > 0) { if (data && data.pageIds?.length > 0) {
console.log('[缓存]:', `from:${from}`, `root-page-id:${pageId}`) console.log('[API<<--缓存]', `from:${from}`, `root-page-id:${pageId}`)
return data return data
} }
const db = await getDataBaseInfoByNotionAPI({ pageId, from }) const db = await getDataBaseInfoByNotionAPI({ pageId, from })

View File

@@ -14,7 +14,7 @@ export async function getPostBlocks(id, from, slice) {
const cacheKey = 'page_block_' + id const cacheKey = 'page_block_' + id
let pageBlock = await getDataFromCache(cacheKey) let pageBlock = await getDataFromCache(cacheKey)
if (pageBlock) { if (pageBlock) {
console.log('[缓存]:', `from:${from}`, cacheKey) console.log('[API<<--缓存]', `from:${from}`, cacheKey)
return filterPostBlocks(id, pageBlock, slice) return filterPostBlocks(id, pageBlock, slice)
} }
@@ -31,7 +31,7 @@ export async function getSingleBlock(id, from) {
const cacheKey = 'single_block_' + id const cacheKey = 'single_block_' + id
let pageBlock = await getDataFromCache(cacheKey) let pageBlock = await getDataFromCache(cacheKey)
if (pageBlock) { if (pageBlock) {
console.log('[缓存]:', `from:${from}`, cacheKey) console.log('[API<<--缓存]', `from:${from}`, cacheKey)
return pageBlock return pageBlock
} }

View File

@@ -8,7 +8,7 @@ const withBundleAnalyzer = require('@next/bundle-analyzer')({
}) })
/** /**
* 扫描指定目录下的文件夹名,用于获取当前有几个主题 * 扫描指定目录下的文件夹名,用于获取所有主题
* @param {*} directory * @param {*} directory
* @returns * @returns
*/ */
@@ -21,6 +21,8 @@ function scanSubdirectories(directory) {
if (stats.isDirectory()) { if (stats.isDirectory()) {
subdirectories.push(file) subdirectories.push(file)
} }
// subdirectories.push(file)
}) })
return subdirectories return subdirectories

View File

@@ -20,7 +20,7 @@ const NavPostList = (props) => {
let existingGroup = null let existingGroup = null
// 开启自动分组排序 // 开启自动分组排序
if (JSON.parse(siteConfig('GITBOOK_AUTO_SORT', true, CONFIG))) { if (siteConfig('GITBOOK_AUTO_SORT', true, CONFIG)) {
existingGroup = groups.find(group => group.category === categoryName) // 搜索同名的最后一个分组 existingGroup = groups.find(group => group.category === categoryName) // 搜索同名的最后一个分组
} else { } else {
existingGroup = groups[groups.length - 1] // 获取最后一个分组 existingGroup = groups[groups.length - 1] // 获取最后一个分组

View File

@@ -1,11 +1,15 @@
import { useImperativeHandle, useRef, useState } from 'react' import { useImperativeHandle, useRef, useState } from 'react'
import { deepClone } from '@/lib/utils' import { deepClone } from '@/lib/utils'
import { useGitBookGlobal } from '@/themes/gitbook' import { useGitBookGlobal } from '@/themes/gitbook'
import { siteConfig } from '@/lib/config'
let lock = false let lock = false
/**
* 搜索栏
*/
const SearchInput = ({ currentSearch, cRef, className }) => { const SearchInput = ({ currentSearch, cRef, className }) => {
const searchInputRef = useRef() const searchInputRef = useRef()
const { setFilteredNavPages, allNavPages } = useGitBookGlobal() const { searchModal, setFilteredNavPages, allNavPages } = useGitBookGlobal()
useImperativeHandle(cRef, () => { useImperativeHandle(cRef, () => {
return { return {
@@ -16,6 +20,10 @@ const SearchInput = ({ currentSearch, cRef, className }) => {
}) })
const handleSearch = () => { const handleSearch = () => {
// 使用Algolia
if (siteConfig('ALGOLIA_APP_ID')) {
searchModal?.current?.openSearch()
}
let keyword = searchInputRef.current.value let keyword = searchInputRef.current.value
if (keyword) { if (keyword) {
keyword = keyword.trim() keyword = keyword.trim()
@@ -23,20 +31,7 @@ const SearchInput = ({ currentSearch, cRef, className }) => {
setFilteredNavPages(allNavPages) setFilteredNavPages(allNavPages)
} }
const filterAllNavPages = deepClone(allNavPages) const filterAllNavPages = deepClone(allNavPages)
// for (const filterGroup of filterAllNavPages) {
// for (let i = filterGroup.items.length - 1; i >= 0; i--) {
// const post = filterGroup.items[i]
// const articleInfo = post.title + ''
// const hit = articleInfo.toLowerCase().indexOf(keyword.toLowerCase()) > -1
// if (!hit) {
// // 删除
// filterGroup.items.splice(i, 1)
// }
// }
// if (filterGroup.items && filterGroup.items.length > 0) {
// filterPosts.push(filterGroup)
// }
// }
for (let i = filterAllNavPages.length - 1; i >= 0; i--) { for (let i = filterAllNavPages.length - 1; i >= 0; i--) {
const post = filterAllNavPages[i] const post = filterAllNavPages[i]
const articleInfo = post.title + '' const articleInfo = post.title + ''
@@ -56,6 +51,12 @@ const SearchInput = ({ currentSearch, cRef, className }) => {
* @param {*} e * @param {*} e
*/ */
const handleKeyUp = (e) => { const handleKeyUp = (e) => {
// 使用Algolia
if (siteConfig('ALGOLIA_APP_ID')) {
searchModal?.current?.openSearch()
return
}
if (e.keyCode === 13) { // 回车 if (e.keyCode === 13) { // 回车
handleSearch(searchInputRef.current.value) handleSearch(searchInputRef.current.value)
} else if (e.keyCode === 27) { // ESC } else if (e.keyCode === 27) { // ESC
@@ -63,6 +64,13 @@ const SearchInput = ({ currentSearch, cRef, className }) => {
} }
} }
const handleFocus = () => {
// 使用Algolia
if (siteConfig('ALGOLIA_APP_ID')) {
searchModal?.current?.openSearch()
}
}
/** /**
* 清理搜索 * 清理搜索
*/ */
@@ -77,7 +85,6 @@ const SearchInput = ({ currentSearch, cRef, className }) => {
return return
} }
searchInputRef.current.value = val searchInputRef.current.value = val
if (val) { if (val) {
setShowClean(true) setShowClean(true)
} else { } else {
@@ -97,6 +104,7 @@ const SearchInput = ({ currentSearch, cRef, className }) => {
ref={searchInputRef} ref={searchInputRef}
type='text' type='text'
className={`${className} outline-none w-full text-sm pl-2 transition focus:shadow-lg font-light leading-10 text-black bg-gray-100 dark:bg-gray-900 dark:text-white`} className={`${className} outline-none w-full text-sm pl-2 transition focus:shadow-lg font-light leading-10 text-black bg-gray-100 dark:bg-gray-900 dark:text-white`}
onFocus={handleFocus}
onKeyUp={handleKeyUp} onKeyUp={handleKeyUp}
onCompositionStart={lockSearchInput} onCompositionStart={lockSearchInput}
onCompositionUpdate={lockSearchInput} onCompositionUpdate={lockSearchInput}
@@ -111,7 +119,7 @@ const SearchInput = ({ currentSearch, cRef, className }) => {
</div> </div>
{(showClean && {(showClean &&
<div className='-ml-12 cursor-pointer float-right items-center justify-center py-2'> <div className='-ml-12 cursor-pointer flex float-right items-center justify-center py-2'>
<i className='fas fa-times hover:text-black transform duration-200 text-gray-400 cursor-pointer dark:hover:text-gray-300' onClick={cleanSearch} /> <i className='fas fa-times hover:text-black transform duration-200 text-gray-400 cursor-pointer dark:hover:text-gray-300' onClick={cleanSearch} />
</div> </div>
)} )}

View File

@@ -39,7 +39,7 @@ export default function TopNavBar(props) {
} }
return ( return (
<div id='top-nav' className={'fixed top-0 w-full z-40 ' + className}> <div id='top-nav' className={'fixed top-0 w-full ' + className}>
{/* 移动端折叠菜单 */} {/* 移动端折叠菜单 */}
<Collapse type='vertical' collapseRef={collapseRef} isOpen={isOpen} className='md:hidden'> <Collapse type='vertical' collapseRef={collapseRef} isOpen={isOpen} className='md:hidden'>

View File

@@ -2,7 +2,7 @@
import CONFIG from './config' import CONFIG from './config'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useEffect, useState, createContext, useContext } from 'react' import { useEffect, useState, createContext, useContext, useRef } from 'react'
import { isBrowser } from '@/lib/utils' import { isBrowser } from '@/lib/utils'
import Footer from './components/Footer' import Footer from './components/Footer'
import InfoCard from './components/InfoCard' import InfoCard from './components/InfoCard'
@@ -35,6 +35,7 @@ import dynamic from 'next/dynamic'
import { siteConfig } from '@/lib/config' import { siteConfig } from '@/lib/config'
import NotionIcon from '@/components/NotionIcon' import NotionIcon from '@/components/NotionIcon'
import { LAYOUT_MAPPINGS } from '@/blog.config' import { LAYOUT_MAPPINGS } from '@/blog.config'
import AlgoliaSearchModal from '@/components/AlgoliaSearchModal'
const WWAds = dynamic(() => import('@/components/WWAds'), { ssr: false }) const WWAds = dynamic(() => import('@/components/WWAds'), { ssr: false })
// 主题全局变量 // 主题全局变量
@@ -56,16 +57,19 @@ const LayoutBase = (props) => {
const [filteredNavPages, setFilteredNavPages] = useState(allNavPages) const [filteredNavPages, setFilteredNavPages] = useState(allNavPages)
const showTocButton = post?.toc?.length > 1 const showTocButton = post?.toc?.length > 1
const searchModal = useRef(null)
useEffect(() => { useEffect(() => {
setFilteredNavPages(allNavPages) setFilteredNavPages(allNavPages)
}, [post]) }, [post])
return ( return (
<ThemeGlobalGitbook.Provider value={{ tocVisible, changeTocVisible, filteredNavPages, setFilteredNavPages, allNavPages, pageNavVisible, changePageNavVisible }}> <ThemeGlobalGitbook.Provider value={{ searchModal, tocVisible, changeTocVisible, filteredNavPages, setFilteredNavPages, allNavPages, pageNavVisible, changePageNavVisible }}>
<Style/> <Style/>
<div id='theme-gitbook' className='bg-white dark:bg-hexo-black-gray w-full h-full min-h-screen justify-center dark:text-gray-300'> <div id='theme-gitbook' className='bg-white dark:bg-hexo-black-gray w-full h-full min-h-screen justify-center dark:text-gray-300'>
<AlgoliaSearchModal cRef={searchModal} {...props}/>
{/* 顶部导航栏 */} {/* 顶部导航栏 */}
<TopNavBar {...props} /> <TopNavBar {...props} />

View File

@@ -38,6 +38,8 @@ export default function SideRight(props) {
<InfoCard {...props} className='w-72' /> <InfoCard {...props} className='w-72' />
<div className='sticky top-20 space-y-4'> <div className='sticky top-20 space-y-4'>
{/* 文章页显示目录 */}
{post && post.toc && post.toc.length > 0 && ( {post && post.toc && post.toc.length > 0 && (
<Card className='bg-white dark:bg-[#1e1e1e]'> <Card className='bg-white dark:bg-[#1e1e1e]'>
<Catalog toc={post.toc} /> <Catalog toc={post.toc} />

View File

@@ -328,7 +328,10 @@ const LayoutSlug = props => {
? null ? null
: <div className={`${commentEnable && post ? '' : 'hidden'}`}> : <div className={`${commentEnable && post ? '' : 'hidden'}`}>
<hr className="my-4 border-dashed" /> <hr className="my-4 border-dashed" />
{/* 评论区上方广告 */}
<div className="py-2">
<AdSlot />
</div>
{/* 评论互动 */} {/* 评论互动 */}
<div className="duration-200 overflow-x-auto px-5"> <div className="duration-200 overflow-x-auto px-5">
<div className="text-2xl dark:text-white"> <div className="text-2xl dark:text-white">
@@ -336,9 +339,6 @@ const LayoutSlug = props => {
{locale.COMMON.COMMENTS} {locale.COMMON.COMMENTS}
</div> </div>
<Comment frontMatter={post} className="" /> <Comment frontMatter={post} className="" />
<div className="py-2">
<AdSlot />
</div>
</div> </div>
</div>} </div>}
</div> </div>