Skip to content

Commit

Permalink
🔧 includePages option to filter posts
Browse files Browse the repository at this point in the history
  • Loading branch information
yokinist committed Jun 20, 2021
1 parent ad312d6 commit 2f38a36
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/notion.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { getAllPosts } from './notion/getAllPosts'
export { getPosts } from './notion/getPosts'
export { getAllTagsFromPosts } from './notion/getAllTagsFromPosts'
export { getPostBlocks } from './notion/getPostBlocks'
29 changes: 17 additions & 12 deletions lib/notion/filterPublishedPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import BLOG from '@/blog.config'

const currentDate = new Date().toLocaleDateString(BLOG.lang ?? 'en-US')

export default function filterPublishedPosts(posts) {
export default function filterPublishedPosts({ posts, includePages }) {
if (!posts || !posts.length) return []
const publishedPosts = posts.filter(post => {
const postDate = new Date(
post?.date?.start_date || post.createdTime
).toLocaleDateString(BLOG.lang ?? 'en-US')
return (
post.title &&
post.slug &&
post?.status?.[0] === 'Published' &&
(post?.type?.[0] === 'Post' || post?.type?.[0] === 'Page') &&
postDate <= currentDate
const publishedPosts = posts
.filter(post =>
includePages
? post?.type?.[0] === 'Post' || post?.type?.[0] === 'Page'
: post?.type?.[0] === 'Post'
)
})
.filter(post => {
const postDate = new Date(
post?.date?.start_date || post.createdTime
).toLocaleDateString(BLOG.lang ?? 'en-US')
return (
post.title &&
post.slug &&
post?.status?.[0] === 'Published' &&
postDate <= currentDate
)
})
return publishedPosts
}
15 changes: 11 additions & 4 deletions lib/notion/getAllPosts.js → lib/notion/getPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { NotionAPI } from 'notion-client'
import { idToUuid } from 'notion-utils'
import getAllPageIds from './getAllPageIds'
import getPageProperties from './getPageProperties'
import { filterPublishedPosts } from './filterPublishedPosts'
import filterPublishedPosts from './filterPublishedPosts'

export async function getAllPosts() {
/**
* @param {{ includePages: boolean }} - false: posts only / true: include pages
*/
export async function getPosts({ includePages = false }) {
let id = BLOG.notionPageId
const authToken = BLOG.notionAccessToken || null
const api = new NotionAPI({ authToken })
Expand All @@ -20,7 +23,10 @@ export async function getAllPosts() {
const rawMetadata = block[id].value

// Check Type
if (rawMetadata?.type !== 'collection_view_page' && rawMetadata?.type !== 'collection_view') {
if (
rawMetadata?.type !== 'collection_view_page' &&
rawMetadata?.type !== 'collection_view'
) {
console.log(`pageId "${id}" is not a database`)
return null
} else {
Expand All @@ -39,8 +45,9 @@ export async function getAllPosts() {

data.push(properties)
}

// remove all the the items doesn't meet requirements
const posts = filterPublishedPosts(data)
const posts = filterPublishedPosts({ posts: data, includePages })

// Sort by date
if (BLOG.sortByDate) {
Expand Down
6 changes: 3 additions & 3 deletions pages/[slug].js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import DefaultLayout from '@/layouts/default'
import FullWidthLayout from '@/layouts/fullwidth'
import { getAllPosts, getPostBlocks } from '@/lib/notion'
import { getPosts, getPostBlocks } from '@/lib/notion'
import BLOG from '@/blog.config'
import { createHash } from 'crypto'

Expand All @@ -26,15 +26,15 @@ const BlogPost = ({ post, blockMap, emailHash }) => {
}

export async function getStaticPaths() {
const posts = await getAllPosts()
const posts = await getPosts({ includePages: true })
return {
paths: posts.map(row => `${BLOG.path}/${row.slug}`),
fallback: true
}
}

export async function getStaticProps({ params: { slug } }) {
const posts = await getAllPosts()
const posts = await getPosts({ includePages: true })
const post = posts.find(t => t.slug === slug)
const blockMap = await getPostBlocks(post.id)
const emailHash = createHash('md5').update(BLOG.email).digest('hex')
Expand Down
4 changes: 2 additions & 2 deletions pages/feed.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getAllPosts } from '@/lib/notion'
import { getPosts } from '@/lib/notion'
import { generateRss } from '@/lib/rss'
export async function getServerSideProps({ res }) {
res.setHeader('Content-Type', 'text/xml')
const posts = await getAllPosts()
const posts = await getPosts({ includePages: false })
const latestPosts = posts.slice(0, 10)
const xmlFeed = generateRss(latestPosts)
res.write(xmlFeed)
Expand Down
4 changes: 2 additions & 2 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Container from '@/components/Container'
import BlogPost from '@/components/BlogPost'
import Pagination from '@/components/Pagination'
import { getAllPosts } from '@/lib/notion'
import { getPosts } from '@/lib/notion'
import BLOG from '@/blog.config'

export async function getStaticProps() {
const posts = await getAllPosts()
const posts = await getPosts({ includePages: false })
const postsToShow = posts.slice(0, BLOG.postsPerPage)
const totalPosts = posts.length
const showNext = totalPosts > BLOG.postsPerPage
Expand Down
6 changes: 3 additions & 3 deletions pages/page/[page].js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Container from '@/components/Container'
import BlogPost from '@/components/BlogPost'
import Pagination from '@/components/Pagination'
import { getAllPosts } from '@/lib/notion'
import { getPosts } from '@/lib/notion'
import BLOG from '@/blog.config'

const Page = ({ postsToShow, page, showNext }) => {
Expand All @@ -16,7 +16,7 @@ const Page = ({ postsToShow, page, showNext }) => {

export async function getStaticProps(context) {
const { page } = context.params // Get Current Page No.
const posts = await getAllPosts()
const posts = await getPosts({ includePages: false })
const postsToShow = posts.slice(
BLOG.postsPerPage * (page - 1),
BLOG.postsPerPage * page
Expand All @@ -34,7 +34,7 @@ export async function getStaticProps(context) {
}

export async function getStaticPaths() {
const posts = await getAllPosts()
const posts = await getPosts({ includePages: false })
const totalPosts = posts.length
const totalPages = Math.ceil(totalPosts / BLOG.postsPerPage)
return {
Expand Down
4 changes: 2 additions & 2 deletions pages/search.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getAllPosts, getAllTagsFromPosts } from '@/lib/notion'
import { getPosts, getAllTagsFromPosts } from '@/lib/notion'
import SearchLayout from '@/layouts/search'

export default function search({ tags, posts }) {
return <SearchLayout tags={tags} posts={posts} />
}
export async function getStaticProps() {
const posts = await getAllPosts()
const posts = await getPosts({ includePages: false })
const tags = getAllTagsFromPosts(posts)
return {
props: {
Expand Down
6 changes: 3 additions & 3 deletions pages/tag/[tag].js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAllPosts, getAllTagsFromPosts } from '@/lib/notion'
import { getPosts, getAllTagsFromPosts } from '@/lib/notion'
import SearchLayout from '@/layouts/search'

export default function Tag({ tags, posts, currentTag }) {
Expand All @@ -7,7 +7,7 @@ export default function Tag({ tags, posts, currentTag }) {

export async function getStaticProps({ params }) {
const currentTag = params.tag
const posts = await getAllPosts()
const posts = await getPosts({ includePages: false })
const tags = getAllTagsFromPosts(posts)
const filteredPosts = posts.filter(
post => post && post.tags && post.tags.includes(currentTag)
Expand All @@ -23,7 +23,7 @@ export async function getStaticProps({ params }) {
}

export async function getStaticPaths() {
const posts = await getAllPosts()
const posts = await getPosts({ includePages: false })
const tags = getAllTagsFromPosts(posts)
return {
paths: Object.keys(tags).map(tag => ({ params: { tag } })),
Expand Down

0 comments on commit 2f38a36

Please sign in to comment.