Skip to content

Commit

Permalink
Merge pull request #1371 from tangly1024/feat/heo-top-posts
Browse files Browse the repository at this point in the history
Feat/heo top posts
  • Loading branch information
tangly1024 authored Jul 30, 2023
2 parents 78183c4 + fd6449e commit 6cfc8ae
Show file tree
Hide file tree
Showing 22 changed files with 151 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .env.local
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# 环境变量 @see https://www.nextjs.cn/docs/basic-features/environment-variables
NEXT_PUBLIC_VERSION=4.0.6
NEXT_PUBLIC_VERSION=4.0.7
2 changes: 1 addition & 1 deletion blog.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ const BLOG = {
// 开发相关
NOTION_ACCESS_TOKEN: process.env.NOTION_ACCESS_TOKEN || '', // Useful if you prefer not to make your database public
DEBUG: process.env.NEXT_PUBLIC_DEBUG || false, // 是否显示调试按钮
ENABLE_CACHE: process.env.ENABLE_CACHE || false, // 开启缓存会将Notion数据缓存在内存中,通常在开发调试中使用,正式部署开启此功能意义不大。
ENABLE_CACHE: process.env.ENABLE_CACHE || process.env.npm_lifecycle_event === 'build', // 缓存在开发调试和打包过程中选择性开启,正式部署开启此功能意义不大。
isProd: process.env.VERCEL_ENV === 'production', // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables) isProd: process.env.VERCEL_ENV === 'production' // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables)
VERSION: process.env.NEXT_PUBLIC_VERSION // 版本号
}
Expand Down
2 changes: 1 addition & 1 deletion components/AlgoliaSearchModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default function AlgoliaSearchModal({ cRef }) {

<Pagination totalPage={totalPage} page={page} switchPage={switchPage}/>
<div>{totalHit > 0 && <div>共搜索到 {totalHit} 条结果,用时 {useTime} 毫秒</div> }</div>
<div className='text-gray-600 mt-2'><span><i class="fa-brands fa-algolia"></i> Algolia 提供搜索服务</span> </div>
<div className='text-gray-600 mt-2'><span><i className="fa-brands fa-algolia"></i> Algolia 提供搜索服务</span> </div>
</div>

{/* 遮罩 */}
Expand Down
20 changes: 3 additions & 17 deletions lib/notion/getNotionData.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,31 +174,17 @@ function getSiteInfo({ collection, block }) {
}

/**
* 获取导航pages
* 转为gitbook这类文档主题设计,精减的标题和内容
* 获取导航用的精减文章列表
* gitbook主题用到,只保留文章的标题分类标签分类信息,精减掉摘要密码日期等数据
* 导航页面的条件,必须是Posts
* @param {*} param0
*/
export function getNavPages({ allPages }) {
const allNavPages = allPages.filter(post => {
return post && post?.slug && (!post?.slug?.startsWith('http')) && post?.type === 'Post' && post?.status === 'Published'
})
const result = allNavPages.map(item => ({ id: item.id, title: item.title || '', category: item.category || null, tags: item.tags || null, summary: item.summary || null, slug: item.slug }))

const groupedArray = result.reduce((groups, item) => {
const categoryName = item?.category ? item?.category : '' // 将category转换为字符串
const lastGroup = groups[groups.length - 1] // 获取最后一个分组

if (!lastGroup || lastGroup?.category !== categoryName) { // 如果当前元素的category与上一个元素不同,则创建新分组
groups.push({ category: categoryName, items: [] })
}

groups[groups.length - 1].items.push(item) // 将元素加入对应的分组

return groups
}, [])

return groupedArray
return allNavPages.map(item => ({ id: item.id, title: item.title || '', pageCoverThumbnail: item.pageCoverThumbnail || '', category: item.category || null, tags: item.tags || null, summary: item.summary || null, slug: item.slug }))
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notion-next",
"version": "4.0.6",
"version": "4.0.7",
"homepage": "https://github.com/tangly1024/NotionNext.git",
"license": "MIT",
"repository": {
Expand Down
4 changes: 0 additions & 4 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ import dynamic from 'next/dynamic'

// 自定义样式css和js引入
import ExternalScript from '@/components/ExternalScript'
import CommonHead from '@/components/CommonHead'

// 各种扩展插件 动画等
const ExternalPlugins = dynamic(() => import('@/components/ExternalPlugins'))

const MyApp = ({ Component, pageProps }) => {
useEffect(() => {
AOS.init()
}, [])
const { meta } = pageProps

return (
<GlobalContextProvider {...pageProps}>
<CommonHead meta={meta}/>
<ExternalScript />
<Component {...pageProps} />
<ExternalPlugins {...pageProps} />
Expand Down
2 changes: 0 additions & 2 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { getPostBlocks } from '@/lib/notion'
import { getGlobalData } from '@/lib/notion/getNotionData'
import { generateRss } from '@/lib/rss'
import { generateRobotsTxt } from '@/lib/robots.txt'

import { useRouter } from 'next/router'
import { getLayoutByTheme } from '@/themes/theme'

Expand All @@ -12,7 +11,6 @@ import { getLayoutByTheme } from '@/themes/theme'
* @param {*} props
* @returns
*/

const Index = props => {
// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
Expand Down
7 changes: 6 additions & 1 deletion themes/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import TagItem from './components/TagItem'
import { useRouter } from 'next/router'
import { Transition } from '@headlessui/react'
import { Style } from './style'
import CommonHead from '@/components/CommonHead'

/**
* 基础布局框架
Expand All @@ -35,7 +36,7 @@ import { Style } from './style'
* @constructor
*/
const LayoutBase = props => {
const { children, slotTop } = props
const { children, slotTop, meta } = props
const { onLoading } = useGlobal()

// 增加一个状态以触发 Transition 组件的动画
Expand All @@ -48,6 +49,10 @@ const LayoutBase = props => {

return (
<div id='theme-example' className='dark:text-gray-300 bg-white dark:bg-black'>

{/* SEO信息 */}
<CommonHead meta={meta}/>

<Style/>

{/* 页头 */}
Expand Down
5 changes: 4 additions & 1 deletion themes/fukasawa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import dynamic from 'next/dynamic'
import { AdSlot } from '@/components/GoogleAdsense'
import { Style } from './style'
import replaceSearchResult from '@/components/Mark'
import CommonHead from '@/components/CommonHead'

const Live2D = dynamic(() => import('@/components/Live2D'))

Expand All @@ -42,7 +43,7 @@ export const useFukasawaGlobal = () => useContext(ThemeGlobalFukasawa)
* @constructor
*/
const LayoutBase = (props) => {
const { children, headerSlot } = props
const { children, headerSlot, meta } = props
const leftAreaSlot = <Live2D />
const { onLoading } = useGlobal()

Expand All @@ -65,6 +66,8 @@ const LayoutBase = (props) => {
<ThemeGlobalFukasawa.Provider value={{ isCollapsed, setIsCollapse }}>

<div id='theme-fukasawa'>
{/* SEO信息 */}
<CommonHead meta={meta}/>
<Style/>

<TopNav {...props} />
Expand Down
24 changes: 18 additions & 6 deletions themes/gitbook/components/NavPostList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ import NavPostItem from './NavPostItem'
* @constructor
*/
const NavPostList = (props) => {
const { filteredPostGroups } = props
const { filteredNavPages } = props
const router = useRouter()
let selectedSth = false
const groupedArray = filteredNavPages?.reduce((groups, item) => {
const categoryName = item?.category ? item?.category : '' // 将category转换为字符串
const lastGroup = groups[groups.length - 1] // 获取最后一个分组

if (!lastGroup || lastGroup?.category !== categoryName) { // 如果当前元素的category与上一个元素不同,则创建新分组
groups.push({ category: categoryName, items: [] })
}

groups[groups.length - 1].items.push(item) // 将元素加入对应的分组

return groups
}, [])

// 处理是否选中
filteredPostGroups?.map((group) => {
groupedArray?.map((group) => {
let groupSelected = false
for (const post of group?.items) {
if (router.asPath.split('?')[0] === '/' + post.slug) {
Expand All @@ -28,16 +40,16 @@ const NavPostList = (props) => {
})

// 如果都没有选中默认打开第一个
if (!selectedSth && filteredPostGroups && filteredPostGroups?.length > 0) {
filteredPostGroups[0].selected = true
if (!selectedSth && groupedArray && groupedArray?.length > 0) {
groupedArray[0].selected = true
}

if (!filteredPostGroups || filteredPostGroups.length === 0) {
if (!groupedArray || groupedArray.length === 0) {
return <NavPostListEmpty />
} else {
return <div id='posts-wrapper' className='w-full flex-grow'>
{/* 文章列表 */}
{filteredPostGroups?.map((group, index) => <NavPostItem key={index} group={group} onHeightChange={props.onHeightChange}/>)}
{groupedArray?.map((group, index) => <NavPostItem key={index} group={group} onHeightChange={props.onHeightChange}/>)}
</div>
}
}
Expand Down
18 changes: 16 additions & 2 deletions themes/gitbook/components/PageNavDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@ import NavPostList from './NavPostList'
*/
const PageNavDrawer = (props) => {
const { pageNavVisible, changePageNavVisible } = useGitBookGlobal()
const { filteredPostGroups } = props
const { filteredNavPages } = props

const groupedArray = filteredNavPages.reduce((groups, item) => {
const categoryName = item?.category ? item?.category : '' // 将category转换为字符串
const lastGroup = groups[groups.length - 1] // 获取最后一个分组

if (!lastGroup || lastGroup?.category !== categoryName) { // 如果当前元素的category与上一个元素不同,则创建新分组
groups.push({ category: categoryName, items: [] })
}

groups[groups.length - 1].items.push(item) // 将元素加入对应的分组

return groups
}, [])

const switchVisible = () => {
changePageNavVisible(!pageNavVisible)
}
Expand All @@ -23,7 +37,7 @@ const PageNavDrawer = (props) => {
' overflow-y-hidden shadow-card w-72 duration-200 fixed left-1 top-16 rounded py-2 bg-white dark:bg-gray-600'}>
<div className='dark:text-gray-400 text-gray-600 h-96 overflow-y-scroll p-3'>
{/* 所有文章列表 */}
<NavPostList filteredPostGroups={filteredPostGroups} />
<NavPostList groupedArray={groupedArray} />
</div>
</div>
</div>
Expand Down
49 changes: 33 additions & 16 deletions themes/gitbook/components/SearchInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let lock = false

const SearchInput = ({ currentSearch, cRef, className }) => {
const searchInputRef = useRef()
const { setFilterPosts, allNavPages } = useGitBookGlobal()
const { setFilteredNavPages, allNavPages } = useGitBookGlobal()

useImperativeHandle(cRef, () => {
return {
Expand All @@ -17,38 +17,55 @@ const SearchInput = ({ currentSearch, cRef, className }) => {

const handleSearch = () => {
let keyword = searchInputRef.current.value
const filterPosts = []
if (keyword) {
keyword = keyword.trim()
} else {
setFilterPosts(allNavPages)
setFilteredNavPages(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 (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--) {
const post = filterAllNavPages[i]
const articleInfo = post.title + ''
const hit = articleInfo.toLowerCase().indexOf(keyword.toLowerCase()) > -1
if (!hit) {
// 删除
filterAllNavPages.splice(i, 1)
}
}

// 更新完
setFilterPosts(filterPosts)
setFilteredNavPages(filterAllNavPages)
}

/**
* 回车键
* @param {*} e
*/
const handleKeyUp = (e) => {
if (e.keyCode === 13) { // 回车
handleSearch(searchInputRef.current.value)
} else if (e.keyCode === 27) { // ESC
cleanSearch()
}
}

/**
* 清理搜索
*/
const cleanSearch = () => {
searchInputRef.current.value = ''
handleSearch()
Expand Down
15 changes: 9 additions & 6 deletions themes/gitbook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import NotionPage from '@/components/NotionPage'
import { ArticleLock } from './components/ArticleLock'
import { Transition } from '@headlessui/react'
import { Style } from './style'
import CommonHead from '@/components/CommonHead'

// 主题全局变量
const ThemeGlobalGitbook = createContext()
Expand All @@ -42,21 +43,23 @@ export const useGitBookGlobal = () => useContext(ThemeGlobalGitbook)
* @constructor
*/
const LayoutBase = (props) => {
const { children, post, allNavPages, slotLeft, slotRight, slotTop } = props
const { children, post, allNavPages, slotLeft, slotRight, slotTop, meta } = props
const { onLoading } = useGlobal()
const router = useRouter()
const [tocVisible, changeTocVisible] = useState(false)
const [pageNavVisible, changePageNavVisible] = useState(false)
const [filteredPostGroups, setFilteredPostGroups] = useState(allNavPages)
const [filteredNavPages, setFilteredNavPages] = useState(allNavPages)

const showTocButton = post?.toc?.length > 1

useEffect(() => {
setFilteredPostGroups(allNavPages)
console.log('更新导航', allNavPages)
setFilteredNavPages(allNavPages)
}, [post])

return (
<ThemeGlobalGitbook.Provider value={{ tocVisible, changeTocVisible, filteredPostGroups, setFilteredPostGroups, allNavPages, pageNavVisible, changePageNavVisible }}>
<ThemeGlobalGitbook.Provider value={{ tocVisible, changeTocVisible, filteredNavPages, setFilteredNavPages, allNavPages, pageNavVisible, changePageNavVisible }}>
<CommonHead meta={meta}/>
<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'>
Expand All @@ -72,7 +75,7 @@ const LayoutBase = (props) => {
<SearchInput className='my-3 rounded-md' />
<div className='mb-20'>
{/* 所有文章列表 */}
<NavPostList filteredPostGroups={filteredPostGroups} />
<NavPostList filteredNavPages={filteredNavPages} />
</div>

</div>
Expand Down Expand Up @@ -146,7 +149,7 @@ const LayoutBase = (props) => {
</div>}

{/* 移动端导航抽屉 */}
<PageNavDrawer {...props} filteredPostGroups={filteredPostGroups} />
<PageNavDrawer {...props} filteredNavPages={filteredNavPages} />

{/* 移动端底部导航栏 */}
{/* <BottomMenuBar {...props} className='block md:hidden' /> */}
Expand Down
Loading

0 comments on commit 6cfc8ae

Please sign in to comment.