From 7fc3cbd23d92bb65e06da1e435ff52f1b6eb4ec8 Mon Sep 17 00:00:00 2001 From: Daniel Cruz Date: Tue, 4 Jul 2023 11:30:48 -0600 Subject: [PATCH] filter posts in getSortedPosts to get correct number of posts --- apps/www/lib/posts.tsx | 26 +++++++++++++++++++------- apps/www/pages/alternatives/[slug].tsx | 8 ++++++-- apps/www/pages/blog.tsx | 2 +- apps/www/pages/blog/[slug].tsx | 11 +++++++---- apps/www/pages/blog/tags/[tag].tsx | 2 +- apps/www/pages/customers/[slug].tsx | 9 +++++++-- apps/www/pages/customers/index.tsx | 2 +- apps/www/pages/index.tsx | 2 +- 8 files changed, 43 insertions(+), 19 deletions(-) diff --git a/apps/www/lib/posts.tsx b/apps/www/lib/posts.tsx index 82b0ba2df6a..d9e8177d82d 100644 --- a/apps/www/lib/posts.tsx +++ b/apps/www/lib/posts.tsx @@ -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) => { diff --git a/apps/www/pages/alternatives/[slug].tsx b/apps/www/pages/alternatives/[slug].tsx index 69203666ed1..6c940bc7028 100644 --- a/apps/www/pages/alternatives/[slug].tsx +++ b/apps/www/pages/alternatives/[slug].tsx @@ -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) { diff --git a/apps/www/pages/blog.tsx b/apps/www/pages/blog.tsx index 29b78124eb3..5dc6dc4b886 100644 --- a/apps/www/pages/blog.tsx +++ b/apps/www/pages/blog.tsx @@ -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) diff --git a/apps/www/pages/blog/[slug].tsx b/apps/www/pages/blog/[slug].tsx index 6002c46b504..cb8e50b674b 100644 --- a/apps/www/pages/blog/[slug].tsx +++ b/apps/www/pages/blog/[slug].tsx @@ -79,11 +79,14 @@ export const getStaticProps: GetStaticProps = 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) { diff --git a/apps/www/pages/blog/tags/[tag].tsx b/apps/www/pages/blog/tags/[tag].tsx index 49aa4a9335b..fd7b7938782 100644 --- a/apps/www/pages/blog/tags/[tag].tsx +++ b/apps/www/pages/blog/tags/[tag].tsx @@ -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, diff --git a/apps/www/pages/customers/[slug].tsx b/apps/www/pages/customers/[slug].tsx index 7be097ca5b9..27b6ce5de2c 100644 --- a/apps/www/pages/customers/[slug].tsx +++ b/apps/www/pages/customers/[slug].tsx @@ -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 diff --git a/apps/www/pages/customers/index.tsx b/apps/www/pages/customers/index.tsx index 171e61b53ba..42c3c43ca2d 100644 --- a/apps/www/pages/customers/index.tsx +++ b/apps/www/pages/customers/index.tsx @@ -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) diff --git a/apps/www/pages/index.tsx b/apps/www/pages/index.tsx index 37a1365aa72..eac2ed7a905 100644 --- a/apps/www/pages/index.tsx +++ b/apps/www/pages/index.tsx @@ -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: {