Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

clean up merge

Francesco Sansalvadoref770b16e2fda
diff --git a/apps/www/lib/posts.tsx b/apps/www/lib/posts.tsx
index cbcfa19354b..ced24e72cd7 100644
--- a/apps/www/lib/posts.tsx
+++ b/apps/www/lib/posts.tsx
@@ -22,6 +22,7 @@ export const getSortedPosts = ({
directory,
limit,
tags,
+ categories,
currentPostSlug,
}: GetSortedPostsParams) => {
//Finding directory named "blog" from the current working directory of Node.
@@ -30,73 +31,76 @@ export const getSortedPosts = ({
//Reads all the files in the post directory
const fileNames = fs.readdirSync(postDirectory)
- const posts = []
-
- for (const filename of fileNames) {
- const slug =
- directory === '_blog'
- ? filename.replace('.mdx', '').substring(FILENAME_SUBSTRING)
- : filename.replace('.mdx', '')
-
+ const allPosts = fileNames
+ .map((filename) => {
+ const slug =
+ directory === '_blog'
+ ? filename.replace('.mdx', '').substring(FILENAME_SUBSTRING)
+ : filename.replace('.mdx', '')
+
+ const fullPath = path.join(postDirectory, filename)
+
+ //Extracts contents of the MDX file
+ const fileContents = fs.readFileSync(fullPath, 'utf8')
+ const { data, content } = matter(fileContents) as unknown as {
+ data: { [key: string]: any; tags?: string[] }
+ content: string
+ }
+
+ const options: Intl.DateTimeFormatOptions = { month: 'long', day: 'numeric', year: 'numeric' }
+ const formattedDate = new Date(data.date).toLocaleDateString('en-IN', options)
+
+ const readingTime = generateReadingTime(content)
+
+ const url = `/${directory.replace('_', '')}/${slug}`
+ const contentPath = `/${directory.replace('_', '')}/${slug}`
+
+ const frontmatter = {
+ ...data,
+ date: formattedDate,
+ readingTime,
+ publishedAt: data.published_at ?? null,
+ url: url,
+ path: contentPath,
+ }
+
+ return {
+ slug,
+ ...frontmatter,
+ }
+ })
// avoid reading content if it's the same post as the one the user is already reading
- if (slug === currentPostSlug) {
- continue
- }
-
- const fullPath = path.join(postDirectory, filename)
-
- //Extracts contents of the MDX file
- const fileContents = fs.readFileSync(fullPath, 'utf8')
- const { data, content } = matter(fileContents) as unknown as {
- data: { [key: string]: any; tags?: string[] }
- content: string
- }
-
- // if no matching tags, there's no need to do the rest
- if (tags && !tags.some((tag) => data.tags?.includes(tag))) {
- continue
- }
-
- const options: Intl.DateTimeFormatOptions = { month: 'long', day: 'numeric', year: 'numeric' }
- const formattedDate = new Date(data.date).toLocaleDateString('en-IN', options)
-
- const readingTime = generateReadingTime(content)
-
- const url = `/${directory.replace('_', '')}/${slug}`
- const contentPath = `/${directory.replace('_', '')}/${slug}`
-
- const frontmatter = {
- ...data,
- date: formattedDate,
- readingTime,
- publishedAt: data.published_at ?? null,
- url: url,
- path: contentPath,
- }
-
- const post = {
- slug,
- ...frontmatter,
- }
+ .filter((post) => post.slug !== currentPostSlug)
- posts.push(post)
- }
+ let sortedPosts = [...allPosts]
- /* Array.prototype.sort(), mutates the original array ¯\_(ツ)_/¯
- * so no need to re-assign -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
- */
- posts.sort((a, b) => {
+ sortedPosts = sortedPosts.sort((a: any, b: any) => {
const isPublishedAtBefore =
a.publishedAt && b.publishedAt && Date.parse(a.publishedAt) < Date.parse(b.publishedAt)
-
if (isPublishedAtBefore || new Date(a.date) < new Date(b.date)) {
return 1
+ } else {
+ return -1
}
-
- return -1
})
- return posts.slice(0, limit)
+ if (categories) {
+ sortedPosts = sortedPosts.filter((post: any) => {
+ const found = categories?.some((tag: any) => post.categories?.includes(tag))
+ return found
+ })
+ }
+
+ if (tags) {
+ sortedPosts = sortedPosts.filter((post: any) => {
+ const found = tags.some((tag: any) => post.tags?.includes(tag))
+ return found
+ })
+ }
+
+ if (limit) sortedPosts = sortedPosts.slice(0, limit)
+
+ return sortedPosts
}
// Get Slugs
diff --git a/apps/www/pages/blog.tsx b/apps/www/pages/blog.tsx
index 806452ceaa8..b369a7f357d 100644
--- a/apps/www/pages/blog.tsx
+++ b/apps/www/pages/blog.tsx
@@ -7,7 +7,7 @@ import Link from 'next/link'
import { NextSeo } from 'next-seo'
import { generateRss } from '~/lib/rss'
-import { getSortedPosts, getAllCategories } from '~/lib/posts'
+import { getSortedPosts } from '~/lib/posts'
import authors from 'lib/authors.json'
import PostTypes from '~/types/post'
@@ -17,7 +17,6 @@ import BlogFilters from '~/components/Blog/BlogFilters'
export async function getStaticProps() {
const allPostsData = getSortedPosts({ directory: '_blog', runner: '** BLOG PAGE **' })
- const categories = getAllCategories('_blog')
const rss = generateRss(allPostsData)
// create a rss feed in public directory
@@ -48,8 +47,8 @@ function Blog(props: any) {
const [blogs, setBlogs] = useState(props.blogs)
const [category, setCategory] = useState<string>('all')
- // Using hard-coded categories here because
- // - they serve as a reference
+ // Using hard-coded categories as they:
+ // - serve as a reference
// - are easier to reorder
const allCategories = [
'all',
diff --git a/apps/www/pages/blog/categories/[category].tsx b/apps/www/pages/blog/categories/[category].tsx
index f07f57eaea5..f39423c14fe 100644
--- a/apps/www/pages/blog/categories/[category].tsx
+++ b/apps/www/pages/blog/categories/[category].tsx
@@ -8,7 +8,7 @@ import BlogListItem from '~/components/Blog/BlogListItem'
import PostTypes from '~/types/post'
export async function getStaticProps({ params }: any) {
- const posts = getSortedPosts('_blog', 0, undefined, [params.category])
+ const posts = getSortedPosts({ directory: '_blog', limit: 0, categories: [params.category] })
return {
props: {
category: params.category,