Skip to content

Commit 26d39fe

Browse files
committed
replace auto category slug with explicit slug
This allows the content creator to be in control of what each category looks like in the URL. Maybe there are fewer words, etc.
1 parent 1d36741 commit 26d39fe

File tree

8 files changed

+35
-51
lines changed

8 files changed

+35
-51
lines changed

src/components/Category.astro

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
---
2-
import { cn, sluglify } from '@/utils'
2+
import { cn } from '@/utils'
33
4-
const { name = 'View All', activeCategory } = Astro.props
4+
const { category, activeCategory } = Astro.props
55
const currentPath = Astro.url.pathname
6-
const unsluglifyNameCategory = sluglify(name.toLowerCase())
76
87
const isActive =
9-
activeCategory?.toLocaleLowerCase() === unsluglifyNameCategory ||
10-
(currentPath === '/' && name === 'View All')
8+
(category && activeCategory === category) || (!category /* View All */ && currentPath === '/')
119
---
1210

1311
<a
14-
href={name === 'View All' ? '/' : sluglify(`/category/${unsluglifyNameCategory}/1`)}
12+
href={!category ? '/' : `/category/${category.slug}/1`}
1513
class={cn(
1614
`text-gray-600 dark:text-gray-300 pb-2.5 first-letter:uppercase font-medium hover:text-gray-800 border-b-2 border-opacity-0 dark:border-opacity-0 border-black dark:border-white dark:hover:border-white hover:border-opacity-100 transition-colors duration-150 ease-linear `,
1715
isActive &&
1816
`border-black border-b-2 text-black z-10 dark:border-white dark:text-white dark:border-opacity-100 border-opacity-100`
1917
)}
2018
>
21-
{name}
19+
{category?.title || 'View All'}
2220
</a>

src/components/ListCategories.astro

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ const { activeCategory } = Astro.props
88

99
<div class='relative flex flex-wrap min-w-full gap-5'>
1010
<Category />
11-
{
12-
categories.map((category: string) => (
13-
<Category name={category} activeCategory={activeCategory} />
14-
))
15-
}
11+
{categories.map((category) => <Category category={category} activeCategory={activeCategory} />)}
1612

1713
<div class='hidden sm:block absolute w-full bottom-0 border-b-2 -z-40 dark:border-gray-600'></div>
1814
</div>

src/content/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const blog = defineCollection({
1313
.or(z.date())
1414
.transform((val) => new Date(val)),
1515
heroImage: image(),
16-
category: z.enum(CATEGORIES),
16+
category: z.enum(CATEGORIES.map((category) => category.title)),
1717
tags: z.array(z.string()),
1818
draft: z.boolean().default(false)
1919
})

src/data/categories.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
// List of categories for blog posts
2-
export const CATEGORIES = [
3-
'Category 1',
4-
'Category 2',
5-
'Category 3',
6-
'Category 4',
7-
'Category 5'
8-
] as const
2+
const CATEGORY_TITLES = ['Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5']
3+
4+
function sluglify(text: string) {
5+
return text.replace(/\s+/g, '-').toLowerCase()
6+
}
7+
8+
export const CATEGORIES = CATEGORY_TITLES.map((title) => ({ title, slug: sluglify(title) }))
9+
10+
// Alternatively, you can be explicit with the slugs by replacing the content of the whole file with:
11+
//
12+
// export const CATEGORIES = [
13+
// { slug: 'category-1', title: 'Category 1' },
14+
// { slug: 'category-2', title: 'Category 2' },
15+
// { slug: 'category-3', title: 'Category 3' },
16+
// { slug: 'category-4', title: 'Category 4' },
17+
// { slug: 'category-5', title: 'Category 5' },
18+
// ] as const

src/pages/category/[category]/[page].astro

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,32 @@ import BaseLayout from '@/layouts/BaseLayout'
33
import ListPosts from '@/components/ListPosts'
44
import ListCategories from '@/components/ListCategories'
55
import TitlePage from '@/components/TitlePage'
6-
import { sluglify, unsluglify, getCategories, getPosts } from '@/utils'
6+
import { getCategories, getPosts } from '@/utils'
77
import { siteConfig } from '@/data/site.config'
88
import Pagination from '@/components/Pagination'
99
1010
export async function getStaticPaths({ paginate }: any) {
1111
const categories = await getCategories()
1212
const allPosts = await getPosts()
1313
14-
return categories.flatMap((category: string) => {
15-
const unsluglifyNameCategory = unsluglify(category!.toLowerCase())
16-
const filteredPosts = allPosts.filter(
17-
(post) => post.data.category.toLowerCase() === unsluglifyNameCategory
18-
)
14+
return categories.flatMap((category) => {
15+
const filteredPosts = allPosts.filter((post) => post.data.category === category.title)
1916
2017
return paginate(filteredPosts, {
21-
params: {
22-
category: sluglify(category.toLowerCase())
23-
},
18+
params: { category: category.slug },
19+
props: { category },
2420
pageSize: siteConfig.paginationSize
2521
})
2622
})
2723
}
28-
const params = Astro.params
29-
const { page } = Astro.props
3024
31-
const unsluglifyNameCategory = unsluglify(params.category!.toLowerCase())
32-
const categoryName = (await getCategories()).find(
33-
(category) => category.toLowerCase() === unsluglifyNameCategory
34-
)
25+
const { category, page } = Astro.props
3526
const posts = page.data
3627
---
3728

38-
<BaseLayout title={categoryName}>
39-
<TitlePage title={categoryName} />
40-
<ListCategories activeCategory={params.category} />
29+
<BaseLayout title={category.title}>
30+
<TitlePage title={category.title} />
31+
<ListCategories activeCategory={category} />
4132
<ListPosts posts={posts} />
4233
<Pagination page={page} />
4334
</BaseLayout>

src/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export { sluglify, unsluglify } from './sluglify'
21
export { cn } from './cn'
32
export { getCategories, getPosts, getTags, getPostByTag, filterPostsByCategory } from './post'
43
export { remarkReadingTime } from './readTime'

src/utils/post.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import { CATEGORIES } from '@/data/categories'
33

44
export const getCategories = async () => {
55
const posts = await getCollection('blog')
6-
const categories = new Set(
6+
const usedCategories = new Set(
77
posts.filter((post) => !post.data.draft).map((post) => post.data.category)
88
)
9-
return Array.from(categories).sort((a, b) =>
10-
CATEGORIES.indexOf(a) < CATEGORIES.indexOf(b) ? -1 : 1
11-
)
9+
return CATEGORIES.filter((category) => usedCategories.has(category.title))
1210
}
1311

1412
export const getPosts = async (max?: number) => {
@@ -50,4 +48,3 @@ export const filterPostsByCategory = async (category: string) => {
5048
.filter((post) => !post.data.draft)
5149
.filter((post) => post.data.category.toLowerCase() === category)
5250
}
53-

src/utils/sluglify.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)