Skip to content

Commit

Permalink
fix: Exclude drafts for categories and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
cameroncf committed Aug 30, 2024
1 parent 865adae commit 1f72941
Showing 1 changed file with 47 additions and 39 deletions.
86 changes: 47 additions & 39 deletions src/utils/post.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
import { getCollection } from 'astro:content'

export const getCategories = async () => {
const posts = await getCollection('blog')
const categories = new Set(posts.map((post) => post.data.category))
return Array.from(categories)
}

export const getPosts = async (max?: number) => {
return (await getCollection('blog'))
.filter((post) => !post.data.draft)
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.slice(0, max)
}

export const getTags = async () => {
const posts = await getCollection('blog')
const tags = new Set()
posts.forEach((post) => {
post.data.tags.forEach((tag) => {
tags.add(tag.toLowerCase())
})
})

return Array.from(tags)
}

export const getPostByTag = async (tag: string) => {
const posts = await getPosts()
const lowercaseTag = tag.toLowerCase()
return posts.filter((post) => {
return post.data.tags.some((postTag) => postTag.toLowerCase() === lowercaseTag)
})
}

export const filterPostsByCategory = async (category: string) => {
const posts = await getPosts()
return posts.filter((post) => post.data.category.toLowerCase() === category)
}
import { getCollection } from 'astro:content'

export const getCategories = async () => {
const posts = await getCollection('blog')
const categories = new Set(
posts.filter((post) => !post.data.draft).map((post) => post.data.category)
)
return Array.from(categories)
}

export const getPosts = async (max?: number) => {
return (await getCollection('blog'))
.filter((post) => !post.data.draft)
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.slice(0, max)
}

export const getTags = async () => {
const posts = await getCollection('blog')
const tags = new Set()
posts
.filter((post) => !post.data.draft)
.forEach((post) => {
post.data.tags.forEach((tag) => {
tags.add(tag.toLowerCase())
})
})

return Array.from(tags)
}

export const getPostByTag = async (tag: string) => {
const posts = await getPosts()
const lowercaseTag = tag.toLowerCase()
return posts
.filter((post) => !post.data.draft)
.filter((post) => {
return post.data.tags.some((postTag) => postTag.toLowerCase() === lowercaseTag)
})
}

export const filterPostsByCategory = async (category: string) => {
const posts = await getPosts()
return posts
.filter((post) => !post.data.draft)
.filter((post) => post.data.category.toLowerCase() === category)
}

0 comments on commit 1f72941

Please sign in to comment.