Skip to content

Commit

Permalink
完善编译,数据库支持多视图排序
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Jul 30, 2023
1 parent b4d1a1a commit 4983108
Show file tree
Hide file tree
Showing 16 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/notion/getAllCategories.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { isIterable } from '../utils'
* @returns {Promise<{}|*[]>}
*/
export function getAllCategories({ allPages, categoryOptions, sliceCount = 0 }) {
const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published')
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
if (!allPosts || !categoryOptions) {
return []
}
Expand Down
16 changes: 9 additions & 7 deletions lib/notion/getAllPageIds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ export default function getAllPageIds (collectionQuery, collectionId, collection
return []
}
let pageIds = []
if (collectionQuery && Object.values(collectionQuery).length > 0) {
// 优先按照第一个视图排序
if (viewIds && viewIds.length > 0) {
const ids = collectionView[viewIds[0]].value.page_sort
// console.log('PageIds: 从viewId获取', viewIds)
for (const id of ids) {
pageIds.push(id)
}
// 否则按照数据库原始排序
} else if (collectionQuery && Object.values(collectionQuery).length > 0) {
const pageSet = new Set()
Object.values(collectionQuery[collectionId]).forEach(view => {
view?.blockIds?.forEach(id => pageSet.add(id)) // group视图
view?.collection_group_results?.blockIds?.forEach(id => pageSet.add(id)) // table视图
})
pageIds = [...pageSet]
// console.log('PageIds: 从collectionQuery获取', collectionQuery, pageIds.length)
} else if (viewIds && viewIds.length > 0) {
const ids = collectionView[viewIds[0]].value.page_sort
// console.log('PageIds: 从viewId获取', viewIds)
for (const id of ids) {
pageIds.push(id)
}
}
return pageIds
}
2 changes: 1 addition & 1 deletion lib/notion/getAllTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isIterable } from '../utils'
* @returns {Promise<{}|*[]>}
*/
export function getAllTags({ allPages, sliceCount = 0, tagOptions }) {
const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published')
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')

if (!allPosts || !tagOptions) {
return []
Expand Down
2 changes: 1 addition & 1 deletion lib/notion/getNotionData.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function getSiteInfo({ collection, block }) {
* @param {*} param0
*/
export function getNavPages({ allPages }) {
const allNavPages = allPages.filter(post => {
const allNavPages = allPages?.filter(post => {
return post && post?.slug && (!post?.slug?.startsWith('http')) && post?.type === 'Post' && post?.status === 'Published'
})

Expand Down
2 changes: 1 addition & 1 deletion pages/[prefix]/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function getStaticProps({ params: { prefix, slug } }) {
}

// 推荐关联文章处理
const allPosts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
const allPosts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
if (allPosts && allPosts.length > 0) {
const index = allPosts.indexOf(props.post)
props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
Expand Down
2 changes: 1 addition & 1 deletion pages/[prefix]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export async function getStaticProps({ params: { prefix } }) {
}

// 推荐关联文章处理
const allPosts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
const allPosts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
if (allPosts && allPosts.length > 0) {
const index = allPosts.indexOf(props.post)
props.prev = allPosts.slice(index - 1, index)[0] ?? allPosts.slice(-1)[0]
Expand Down
2 changes: 1 addition & 1 deletion pages/archive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const ArchiveIndex = props => {
export async function getStaticProps() {
const props = await getGlobalData({ from: 'archive-index' })
// 处理分页
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
delete props.allPages

const postsSortByDate = Object.create(props.posts)
Expand Down
2 changes: 1 addition & 1 deletion pages/category/[category]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function getStaticProps({ params: { category } }) {
let props = await getGlobalData({ from })

// 过滤状态
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
// 处理过滤
props.posts = props.posts.filter(post => post && post.category && post.category.includes(category))
// 处理文章页数
Expand Down
4 changes: 2 additions & 2 deletions pages/category/[category]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function getStaticProps({ params: { category, page } }) {
let props = await getGlobalData({ from })

// 过滤状态类型
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category))
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category))
// 处理文章页数
props.postCount = props.posts.length
// 处理分页
Expand All @@ -61,7 +61,7 @@ export async function getStaticPaths() {

categoryOptions?.forEach(category => {
// 过滤状态类型
const categoryPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category.name))
const categoryPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post.category && post.category.includes(category.name))
// 处理文章页数
const postCount = categoryPosts.length
const totalPages = Math.ceil(postCount / BLOG.POSTS_PER_PAGE)
Expand Down
2 changes: 1 addition & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function getStaticProps() {
const props = await getGlobalData({ from })

const { siteInfo } = props
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published')
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published')

const meta = {
title: `${siteInfo?.title} | ${siteInfo?.description}`,
Expand Down
2 changes: 1 addition & 1 deletion pages/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function getStaticProps({ params: { page } }) {
const from = `page-${page}`
const props = await getGlobalData({ from })
const { allPages } = props
const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published')
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
// 处理分页
props.posts = allPosts.slice(BLOG.POSTS_PER_PAGE * (page - 1), BLOG.POSTS_PER_PAGE * page)
props.page = page
Expand Down
2 changes: 1 addition & 1 deletion pages/search/[keyword]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getStaticProps({ params: { keyword } }) {
pageType: ['Post']
})
const { allPages } = props
const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published')
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
props.posts = await filterByMemCache(allPosts, keyword)
props.postCount = props.posts.length
// 处理分页
Expand Down
2 changes: 1 addition & 1 deletion pages/search/[keyword]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getStaticProps({ params: { keyword, page } }) {
pageType: ['Post']
})
const { allPages } = props
const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published')
const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
props.posts = await filterByMemCache(allPosts, keyword)
props.postCount = props.posts.length
// 处理分页
Expand Down
2 changes: 1 addition & 1 deletion pages/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function getStaticProps() {
pageType: ['Post']
})
const { allPages } = props
props.posts = allPages.filter(page => page.type === 'Post' && page.status === 'Published')
props.posts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published')
return {
props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND)
Expand Down
2 changes: 1 addition & 1 deletion pages/tag/[tag]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function getStaticProps({ params: { tag } }) {
const props = await getGlobalData({ from })

// 过滤状态
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post?.tags && post?.tags.includes(tag))
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post?.tags && post?.tags.includes(tag))

// 处理文章页数
props.postCount = props.posts.length
Expand Down
4 changes: 2 additions & 2 deletions pages/tag/[tag]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function getStaticProps({ params: { tag, page } }) {
const from = 'tag-page-props'
const props = await getGlobalData({ from })
// 过滤状态、标签
props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post?.tags && post?.tags.includes(tag))
props.posts = props.allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post?.tags && post?.tags.includes(tag))
// 处理文章数
props.postCount = props.posts.length
// 处理分页
Expand All @@ -48,7 +48,7 @@ export async function getStaticPaths() {
const paths = []
tagOptions?.forEach(tag => {
// 过滤状态类型
const tagPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post?.tags && post?.tags.includes(tag.name))
const tagPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published').filter(post => post && post?.tags && post?.tags.includes(tag.name))
// 处理文章页数
const postCount = tagPosts.length
const totalPages = Math.ceil(postCount / BLOG.POSTS_PER_PAGE)
Expand Down

0 comments on commit 4983108

Please sign in to comment.