Skip to content

Commit

Permalink
✨ scheduled posts
Browse files Browse the repository at this point in the history
  • Loading branch information
yokinist committed Jun 20, 2021
1 parent f6ee9b0 commit ad312d6
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 63 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 { getAllTags } from './notion/getAllTags'
export { getAllTagsFromPosts } from './notion/getAllTagsFromPosts'
export { getPostBlocks } from './notion/getPostBlocks'
20 changes: 20 additions & 0 deletions lib/notion/filterPublishedPosts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import BLOG from '@/blog.config'

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

export default function filterPublishedPosts(posts) {
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
)
})
return publishedPosts
}
10 changes: 2 additions & 8 deletions lib/notion/getAllPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NotionAPI } from 'notion-client'
import { idToUuid } from 'notion-utils'
import getAllPageIds from './getAllPageIds'
import getPageProperties from './getPageProperties'
import { filterPublishedPosts } from './filterPublishedPosts'

export async function getAllPosts() {
let id = BLOG.notionPageId
Expand Down Expand Up @@ -39,14 +40,7 @@ export async function getAllPosts() {
data.push(properties)
}
// remove all the the items doesn't meet requirements
const posts = data.filter(post => {
return (
post.title &&
post.slug &&
post?.status?.[0] === 'Published' &&
(post?.type?.[0] === 'Post' || post?.type?.[0] === 'Page')
)
})
const posts = filterPublishedPosts(data)

// Sort by date
if (BLOG.sortByDate) {
Expand Down
20 changes: 0 additions & 20 deletions lib/notion/getAllTags.js

This file was deleted.

13 changes: 13 additions & 0 deletions lib/notion/getAllTagsFromPosts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function getAllTagsFromPosts(posts) {
const taggedPosts = posts.filter(post => post?.tags)
const tags = [...taggedPosts.map(p => p.tags).flat()]
const tagObj = {}
tags.forEach(tag => {
if (tag in tagObj) {
tagObj[tag]++
} else {
tagObj[tag] = 1
}
})
return tagObj
}
6 changes: 2 additions & 4 deletions pages/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ const BlogPost = ({ post, blockMap, emailHash }) => {
}

export async function getStaticPaths() {
let posts = await getAllPosts()
posts = posts.filter(post => post.status[0] === 'Published')
const posts = await getAllPosts()
return {
paths: posts.map(row => `${BLOG.path}/${row.slug}`),
fallback: true
}
}

export async function getStaticProps({ params: { slug } }) {
let posts = await getAllPosts()
posts = posts.filter(post => post.status[0] === 'Published')
const posts = await getAllPosts()
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
8 changes: 3 additions & 5 deletions pages/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { getAllPosts } from '@/lib/notion'
import { generateRss } from '@/lib/rss'
export async function getServerSideProps({ res }) {
res.setHeader('Content-Type', 'text/xml')
let posts = await getAllPosts()
posts = posts
.filter(post => post.status[0] === 'Published' && post.type[0] === 'Post')
.slice(0, 10)
const xmlFeed = generateRss(posts)
const posts = await getAllPosts()
const latestPosts = posts.slice(0, 10)
const xmlFeed = generateRss(latestPosts)
res.write(xmlFeed)
res.end()
return {
Expand Down
5 changes: 1 addition & 4 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { getAllPosts } from '@/lib/notion'
import BLOG from '@/blog.config'

export async function getStaticProps() {
let posts = await getAllPosts()
posts = posts.filter(
post => post.status[0] === 'Published' && post.type[0] === 'Post'
)
const posts = await getAllPosts()
const postsToShow = posts.slice(0, BLOG.postsPerPage)
const totalPosts = posts.length
const showNext = totalPosts > BLOG.postsPerPage
Expand Down
10 changes: 2 additions & 8 deletions pages/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ const Page = ({ postsToShow, page, showNext }) => {

export async function getStaticProps(context) {
const { page } = context.params // Get Current Page No.
let posts = await getAllPosts()
posts = posts.filter(
post => post.status[0] === 'Published' && post.type[0] === 'Post'
)
const posts = await getAllPosts()
const postsToShow = posts.slice(
BLOG.postsPerPage * (page - 1),
BLOG.postsPerPage * page
Expand All @@ -37,10 +34,7 @@ export async function getStaticProps(context) {
}

export async function getStaticPaths() {
let posts = await getAllPosts()
posts = posts.filter(
post => post.status[0] === 'Published' && post.type[0] === 'Post'
)
const posts = await getAllPosts()
const totalPosts = posts.length
const totalPages = Math.ceil(totalPosts / BLOG.postsPerPage)
return {
Expand Down
9 changes: 3 additions & 6 deletions pages/search.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { getAllPosts, getAllTags } from '@/lib/notion'
import { getAllPosts, 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() {
let posts = await getAllPosts()
posts = posts.filter(
post => post.status[0] === 'Published' && post.type[0] === 'Post'
)
const tags = await getAllTags()
const posts = await getAllPosts()
const tags = getAllTagsFromPosts(posts)
return {
props: {
tags,
Expand Down
12 changes: 5 additions & 7 deletions pages/tag/[tag].js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAllPosts, getAllTags } from '@/lib/notion'
import { getAllPosts, getAllTagsFromPosts } from '@/lib/notion'
import SearchLayout from '@/layouts/search'

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

export async function getStaticProps({ params }) {
const currentTag = params.tag
let posts = await getAllPosts()
posts = posts.filter(
post => post.status[0] === 'Published' && post.type[0] === 'Post'
)
const tags = await getAllTags()
const posts = await getAllPosts()
const tags = getAllTagsFromPosts(posts)
const filteredPosts = posts.filter(
post => post && post.tags && post.tags.includes(currentTag)
)
Expand All @@ -26,7 +23,8 @@ export async function getStaticProps({ params }) {
}

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

0 comments on commit ad312d6

Please sign in to comment.