Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jun 29, 2024
2 parents d9ad4da + 4535dec commit 27408eb
Show file tree
Hide file tree
Showing 37 changed files with 153 additions and 123 deletions.
3 changes: 1 addition & 2 deletions lib/db/getSiteData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import getAllPageIds from '@/lib/notion/getAllPageIds'
import { getAllTags } from '@/lib/notion/getAllTags'
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
import getPageProperties, {
adjustPageProperties
adjustPageProperties
} from '@/lib/notion/getPageProperties'
import { fetchInBatches, getPage } from '@/lib/notion/getPostBlocks'
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
Expand Down Expand Up @@ -347,7 +347,6 @@ export function getNavPages({ allPages }) {
return (
post &&
post?.slug &&
!post?.slug?.startsWith('http') &&
post?.type === 'Post' &&
post?.status === 'Published'
)
Expand Down
13 changes: 9 additions & 4 deletions lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import formatDate from '../utils/formatDate'
import md5 from 'js-md5'
import { siteConfig } from '../config'
import {
checkStartWithHttp,
convertUrlStartWithOneSlash,
getLastSegmentFromUrl
checkStartWithHttp,
convertUrlStartWithOneSlash,
getLastSegmentFromUrl
} from '../utils'
import { extractLangPrefix } from '../utils/pageId'
import { mapImgUrl } from './mapImage'
Expand Down Expand Up @@ -290,5 +290,10 @@ function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
if (fullPrefix.endsWith('/')) {
fullPrefix = fullPrefix.substring(0, fullPrefix.length - 1) // 去掉尾部部的"/"
}
return `${fullPrefix}/${postProperties.slug ?? postProperties.id}`

if(fullPrefix){
return `${fullPrefix}/${postProperties.slug ?? postProperties.id}`
}else{
return `${postProperties.slug ?? postProperties.id}`
}
}
16 changes: 8 additions & 8 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ const locales = (function () {
})()

// 编译前执行
const preBuild = (function () {
// 删除 public/sitemap.xml 文件 ; 否则会和/pages/sitemap.xml.js 冲突。
const sitemapPath = path.resolve(__dirname, 'public', 'sitemap.xml')
if (fs.existsSync(sitemapPath)) {
fs.unlinkSync(sitemapPath)
console.log('Deleted existing sitemap.xml from public directory')
}
})()
// const preBuild = (function () {
// // 删除 public/sitemap.xml 文件 ; 否则会和/pages/sitemap.xml.js 冲突。
// const sitemapPath = path.resolve(__dirname, 'public', 'sitemap.xml')
// if (fs.existsSync(sitemapPath)) {
// fs.unlinkSync(sitemapPath)
// console.log('Deleted existing sitemap.xml from public directory')
// }
// })()

/**
* 扫描指定目录下的文件夹名,用于获取所有主题
Expand Down
2 changes: 1 addition & 1 deletion pages/[prefix]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export async function getStaticProps({ params: { prefix }, locale }) {
fullSlug += '.html'
}
}

// 在列表内查找文章
props.post = props?.allPages?.find(p => {
return (
Expand Down
2 changes: 1 addition & 1 deletion pages/category/[category]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function getStaticPaths() {
// 处理文章页数
const postCount = categoryPosts.length
const totalPages = Math.ceil(
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
postCount / siteConfig('POSTS_PER_PAGE', null, NOTION_CONFIG)
)
if (totalPages > 1) {
for (let i = 1; i <= totalPages; i++) {
Expand Down
2 changes: 1 addition & 1 deletion pages/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function getStaticPaths({ locale }) {
const from = 'page-paths'
const { postCount, NOTION_CONFIG } = await getGlobalData({ from, locale })
const totalPages = Math.ceil(
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
postCount / siteConfig('POSTS_PER_PAGE', null, NOTION_CONFIG)
)
return {
// remove first page, we 're not gonna handle that.
Expand Down
1 change: 0 additions & 1 deletion pages/sitemap.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const getServerSideProps = async ctx => {
'Cache-Control',
'public, max-age=3600, stale-while-revalidate=59'
)
console.log('fff', fields)
return getServerSideSitemap(ctx, fields)
}

Expand Down
2 changes: 1 addition & 1 deletion pages/tag/[tag]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function getStaticPaths() {
// 处理文章页数
const postCount = tagPosts.length
const totalPages = Math.ceil(
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
postCount / siteConfig('POSTS_PER_PAGE', null, NOTION_CONFIG)
)
if (totalPages > 1) {
for (let i = 1; i <= totalPages; i++) {
Expand Down
2 changes: 1 addition & 1 deletion themes/commerce/components/BlogPostListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ProductCard from './ProductCard'
*/
const BlogPostListPage = ({ page = 1, posts = [], postCount, siteInfo }) => {
const { NOTION_CONFIG } = useGlobal()
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', null, NOTION_CONFIG)
const totalPage = Math.ceil(postCount / POSTS_PER_PAGE)
const showPagination = postCount >= POSTS_PER_PAGE
if (!posts || posts.length === 0 || page > totalPage) {
Expand Down
2 changes: 1 addition & 1 deletion themes/commerce/components/BlogPostListScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const BlogPostListScroll = ({
siteInfo
}) => {
const { NOTION_CONFIG } = useGlobal()
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', null, NOTION_CONFIG)

const [page, updatePage] = useState(1)
const postsToShow = getListByPage(posts, page, POSTS_PER_PAGE)
Expand Down
2 changes: 1 addition & 1 deletion themes/example/components/BlogListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const BlogListPage = props => {
const { locale, NOTION_CONFIG } = useGlobal()
const router = useRouter()
const totalPage = Math.ceil(
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
postCount / siteConfig('POSTS_PER_PAGE', null, NOTION_CONFIG)
)
const currentPage = +page

Expand Down
2 changes: 1 addition & 1 deletion themes/example/components/BlogListScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const BlogListScroll = props => {
const { posts } = props
const { locale, NOTION_CONFIG } = useGlobal()
const [page, updatePage] = useState(1)
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', null, NOTION_CONFIG)

let hasMore = false
const postsToShow = posts
Expand Down
1 change: 0 additions & 1 deletion themes/example/components/SearchInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const SearchInput = ({ currentTag, keyword, cRef }) => {
const key = searchInputRef.current.value
if (key && key !== '') {
router.push({ pathname: '/search/' + key }).then(r => {
console.log('搜索', key)
})
} else {
router.push({ pathname: '/' }).then(r => {
Expand Down
7 changes: 5 additions & 2 deletions themes/fukasawa/components/BlogCard.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import LazyImage from '@/components/LazyImage'
import NotionIcon from '@/components/NotionIcon'
import { siteConfig } from '@/lib/config'
import { useGlobal } from '@/lib/global'
import Link from 'next/link'
import CONFIG from '../config'
import TagItemMini from './TagItemMini'
Expand All @@ -10,7 +11,8 @@ import TagItemMini from './TagItemMini'
* @param {*} param0
* @returns
*/
const BlogCard = ({ index, post, showSummary, siteInfo }) => {
const BlogCard = ({ showAnimate, post, showSummary }) => {
const {siteInfo} =useGlobal()
const showPreview =
siteConfig('FUKASAWA_POST_LIST_PREVIEW', null, CONFIG) && post.blockMap
// fukasawa 强制显示图片
Expand All @@ -24,11 +26,12 @@ const BlogCard = ({ index, post, showSummary, siteInfo }) => {
const showPageCover =
siteConfig('FUKASAWA_POST_LIST_COVER', null, CONFIG) &&
post?.pageCoverThumbnail

const FUKASAWA_POST_LIST_ANIMATION = siteConfig(
'FUKASAWA_POST_LIST_ANIMATION',
null,
CONFIG
)
) || showAnimate

// 动画样式 首屏卡片不用,后面翻出来的加动画
const aosProps = FUKASAWA_POST_LIST_ANIMATION
Expand Down
35 changes: 20 additions & 15 deletions themes/fukasawa/components/BlogListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import PaginationSimple from './PaginationSimple'
*/
const BlogListPage = ({ page = 1, posts = [], postCount, siteInfo }) => {
const { NOTION_CONFIG } = useGlobal()
const postsPerPage = siteConfig('POSTS_PER_PAGE', null, NOTION_CONFIG)
const totalPage = Math.ceil(
postCount / parseInt(siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG))
postCount / postsPerPage
)
const showNext = page < totalPage

Expand All @@ -33,23 +34,27 @@ const BlogListPage = ({ page = 1, posts = [], postCount, siteInfo }) => {
return () => window.removeEventListener('resize', handleResize)
}, [])

/**
* 文章重新布局,使纵向排列看起来是横向排列
*/
/**
* 文章重新布局,使纵向排列看起来是横向排列
*/
useEffect(() => {
const count = posts?.length || 0
const rows = Math.ceil(count / columns)
const newFilterPosts = []
for (let i = 0; i < columns; i++) {
for (let j = 0; j < rows; j++) {
const index = j * columns + i
if (index < count) {
newFilterPosts.push(deepClone(posts[index]))
const count = posts?.length || 0;
const rows = Math.ceil(count / columns);
const newFilterPosts = new Array(count);

let index = 0;
for (let col = 0; col < columns; col++) {
for (let row = 0; row < rows; row++) {
const sourceIndex = row * columns + col;
if (sourceIndex < count) {
newFilterPosts[index] = deepClone(posts[sourceIndex]);
index++;
}
}
}
}
setFilterPosts(newFilterPosts)
}, [columns, posts])

setFilterPosts(newFilterPosts);
}, [columns, posts]);

if (!filterPosts || filterPosts.length === 0) {
return <BlogPostListEmpty />
Expand Down
130 changes: 71 additions & 59 deletions themes/fukasawa/components/BlogListScroll.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,101 @@
import { siteConfig } from '@/lib/config'
import { useGlobal } from '@/lib/global'
import { useEffect, useRef, useState } from 'react'
import BlogCard from './BlogCard'
import BlogPostListEmpty from './BlogListEmpty'
import { siteConfig } from '@/lib/config';
import { useGlobal } from '@/lib/global';
import throttle from 'lodash.throttle';
import { useCallback, useEffect, useRef, useState } from 'react';
import BlogCard from './BlogCard';
import BlogPostListEmpty from './BlogListEmpty';

/**
* 文章列表分页表格
* @param page 当前页
* @param posts 所有文章
* @param tags 所有标签
* @returns {JSX.Element}
* @constructor
*/
const BlogListScroll = props => {
const { posts = [], siteInfo } = props
const { locale, NOTION_CONFIG } = useGlobal()
const targetRef = useRef(null)
const [page, updatePage] = useState(1)
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
const BlogListScroll = ({ posts }) => {
const { locale, NOTION_CONFIG } = useGlobal();
const [page, setPage] = useState(1);
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', null, NOTION_CONFIG);
const [filterPostsGroups, setFilterPostsGroups] = useState([]);

let hasMore = false
const postsToShow = posts
? Object.assign(posts).slice(0, POSTS_PER_PAGE * page)
: []
// 每页显示的文章数量
const postsPerPage = POSTS_PER_PAGE;

// 计算总页数
const totalPages = Math.ceil(posts.length / postsPerPage);

// 加载更多文章
const loadMorePosts = () => {
if (page < totalPages) {
setPage(page + 1);
}
};


const targetRef = useRef(null)

if (posts) {
const totalCount = posts.length
hasMore = page * POSTS_PER_PAGE < totalCount
}
const handleGetMore = () => {
if (!hasMore) return
updatePage(page + 1)
}

// 监听滚动自动分页加载
const scrollTrigger = () => {
requestAnimationFrame(() => {
const scrollTrigger = useCallback(
throttle(() => {
const scrollS = window.scrollY + window.outerHeight
const clientHeight = targetRef
? targetRef.current
? targetRef.current.clientHeight
: 0
: 0
if (scrollS > clientHeight + 100) {
handleGetMore()
loadMorePosts()
}
})
}
}, 500)
)

useEffect(() => {
window.addEventListener('scroll', scrollTrigger)
return () => {
window.removeEventListener('scroll', scrollTrigger)
}
}, [])
})

// 根据当前页和每页文章数截取应该显示的文章
useEffect(() => {
const startIndex = (page - 1) * postsPerPage;
const endIndex = startIndex + postsPerPage;
const postsToShow = posts.slice(startIndex, endIndex);
const columns = 3; // 假设有3列

// 重新排列文章,保证列优先顺序
const newFilterPosts = [];
for (let col = 0; col < columns; col++) {
for (let i = col; i < postsToShow.length; i += columns) {
newFilterPosts.push(postsToShow[i]);
}
}

setFilterPostsGroups((prev) => [...prev, newFilterPosts]);
}, [posts, page]);

if (!posts || posts.length === 0) {
return <BlogPostListEmpty />
return <BlogPostListEmpty />;
} else {
return (
<div id='posts-wrapper' ref={targetRef} className='grid-container'>
{/* 文章列表 */}
{postsToShow?.map(post => (
<div
key={post.id}
className='grid-item justify-center flex'
style={{ breakInside: 'avoid' }}>
<BlogCard
index={posts.indexOf(post)}
key={post.id}
post={post}
siteInfo={siteInfo}
/>
<div ref={targetRef}>
{filterPostsGroups.map((group, groupIndex) => (
<div key={groupIndex} id="posts-wrapper" className="grid-container mb-10">
{group.map((post) => (
<div
key={post.id}
className="grid-item justify-center flex"
style={{ breakInside: 'avoid' }}
>
<BlogCard key={post.id} post={post} showAnimate={groupIndex > 0}/>
</div>
))}
</div>
))}

<div
className='w-full my-4 py-4 text-center cursor-pointer '
onClick={handleGetMore}>
{' '}
{hasMore ? locale.COMMON.MORE : `${locale.COMMON.NO_MORE} 😰`}{' '}
className="w-full my-4 py-4 text-center cursor-pointer"
onClick={loadMorePosts}
>
{page < totalPages ? locale.COMMON.MORE : `${locale.COMMON.NO_MORE} 😰`}
</div>
</div>
)
);
}
}
};

export default BlogListScroll;

export default BlogListScroll
Loading

0 comments on commit 27408eb

Please sign in to comment.