filter posts in getSortedPosts to get correct number of posts

This commit is contained in:
Daniel Cruz committed 2023-07-04 11:30:48 -06:00
1 parent 9ee5833274
commit 7fc3cbd23d
8 files changed
+43 -19

No files matched your search

+19 -7
View File
@@ -9,12 +9,20 @@ type Directories = '_blog' | '_case-studies' | '_customers' | '_alternatives'
// based on YYYY-MM-DD format
const FILENAME_SUBSTRING = 11
export const getSortedPosts = (
directory: Directories,
limit?: number,
tags?: any,
runner?: any
) => {
type GetSortedPostsParams = {
directory: Directories
limit?: number
tags?: unknown[]
runner?: unknown
currentPostSlug?: string
}
export const getSortedPosts = ({
directory,
limit,
tags,
currentPostSlug,
}: GetSortedPostsParams) => {
//Finding directory named "blog" from the current working directory of Node.
const postDirectory = path.join(process.cwd(), directory)
@@ -83,6 +91,10 @@ export const getSortedPosts = (
})
}
if (currentPostSlug) {
sortedPosts = sortedPosts.filter((post) => post.slug !== currentPostSlug)
}
if (limit) sortedPosts = sortedPosts.slice(0, limit)
return sortedPosts
@@ -146,7 +158,7 @@ export const getPostdata = async (slug: string, directory: string) => {
}
export const getAllCategories = (directory: Directories) => {
const posts = getSortedPosts(directory)
const posts = getSortedPosts({ directory })
let categories: any = []
posts.map((post: any) => {
+6 -2
View File
@@ -26,9 +26,13 @@ export async function getStaticProps({ params }: any) {
const mdxSource: any = await mdxSerialize(content)
const relatedPosts = getSortedPosts('_alternatives', 5, mdxSource.scope.tags)
const relatedPosts = getSortedPosts({
directory: '_alternatives',
limit: 5,
tags: mdxSource.scope.tags,
})
const allPosts = getSortedPosts('_alternatives')
const allPosts = getSortedPosts({ directory: '_alternatives' })
const currentIndex = allPosts
.map(function (e) {
+1 -1
View File
@@ -17,7 +17,7 @@ import BlogListItem from '~/components/Blog/BlogListItem'
import { useParams } from '~/hooks/useParams'
export async function getStaticProps() {
const allPostsData = getSortedPosts('_blog', undefined, undefined, '** BLOG PAGE **')
const allPostsData = getSortedPosts({ directory: '_blog', runner: '** BLOG PAGE **' })
const categories = getAllCategories('_blog')
const rss = generateRss(allPostsData)
+7 -4
View File
@@ -79,11 +79,14 @@ export const getStaticProps: GetStaticProps<BlogPostPageProps, Params> = async (
const mdxSource: any = await mdxSerialize(content)
const relatedPosts = getSortedPosts('_blog', 5, mdxSource.scope.tags).filter(
(post) => post.slug !== filePath
) as unknown as (BlogData & Post)[]
const relatedPosts = getSortedPosts({
directory: '_blog',
limit: 5,
tags: mdxSource.scope.tags,
currentPostSlug: filePath,
}) as unknown as (BlogData & Post)[]
const allPosts = getSortedPosts('_blog')
const allPosts = getSortedPosts({ directory: '_blog' })
const currentIndex = allPosts
.map(function (e) {
+1 -1
View File
@@ -7,7 +7,7 @@ import BlogListItem from '~/components/Blog/BlogListItem'
import PostTypes from '~/types/post'
export async function getStaticProps({ params }: any) {
const posts = getSortedPosts('_blog', 0, [params.tag])
const posts = getSortedPosts({ directory: '_blog', limit: 0, tags: [params.tag] })
return {
props: {
tag: params.tag,
+7 -2
View File
@@ -30,9 +30,14 @@ export async function getStaticProps({ params }: any) {
const { data, content } = matter(postContent)
const mdxSource: any = await mdxSerialize(content)
const relatedPosts = getSortedPosts('_customers', 5, mdxSource.scope.tags)
const relatedPosts = getSortedPosts({
directory: '_customers',
limit: 5,
tags: mdxSource.scope.tags,
currentPostSlug: filePath,
})
const allPosts = getSortedPosts('_customers')
const allPosts = getSortedPosts({ directory: '_customers' })
const currentIndex = allPosts
.map(function (e) {
return e.slug
+1 -1
View File
@@ -16,7 +16,7 @@ import Link from 'next/link'
import { GlassPanel } from 'ui'
export async function getStaticProps() {
const allPostsData = getSortedPosts('_customers')
const allPostsData = getSortedPosts({ directory: '_customers' })
const categories = getAllCategories('_customers')
const rss = generateRss(allPostsData)
+1 -1
View File
@@ -35,7 +35,7 @@ const Index = ({ customerStories }: Props) => {
}
export async function getStaticProps() {
const customerStories = getSortedPosts('_customers', 3)
const customerStories = getSortedPosts({ directory: '_customers', limit: 3 })
return {
props: {