Skip to content

Commit

Permalink
Filter by multiple tags (#412)
Browse files Browse the repository at this point in the history
* restucture /tags directory; create dynamic routes for /tags/tag/tag2

* filter posts for up to two tags; prettify

* clean up comments, rename variables e.g. x/y to path

* add all combinations of tag1/tag2 routes

* remove dual tag UI functionality
  • Loading branch information
moteanup24 authored Feb 27, 2025
1 parent 6cbf61e commit e1ca776
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
61 changes: 61 additions & 0 deletions pages/tags/[tag]/[tag2].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { TagSEO } from '@/components/SEO'
import siteMetadata from '@/data/siteMetadata'
import ListLayout from '@/layouts/ListLayout'
import { kebabCase } from 'pliny/utils/kebabCase'
import { getAllTags, allCoreContent } from 'pliny/utils/contentlayer'
import { InferGetStaticPropsType } from 'next'
import { allBlogs } from 'contentlayer/generated'

export async function getStaticPaths() {
const tags = await getAllTags(allBlogs)
const myPaths = []
for (const tagX in tags) {
for (const tagY in tags) {
if (tagX != tagY) {
// /tags/opensats/opensats gives 404 error
myPaths.push({
params: {
tag: tagX,
tag2: tagY,
},
})
}
}
}
return {
paths: myPaths,
fallback: false,
}
}

export const getStaticProps = async (context) => {
const tag = context.params.tag as string
const tag2 = context.params.tag2 as string
const filteredPosts = allCoreContent(
allBlogs.filter(
(post) =>
post.draft !== true &&
post.tags.map((t) => kebabCase(t)).includes(tag) &&
post.tags.map((t) => kebabCase(t)).includes(tag2)
)
)
return { props: { posts: filteredPosts, tag } }
}

export default function Tag({
posts,
tag,
}: InferGetStaticPropsType<typeof getStaticProps>) {
// Capitalize first letter and convert space to dash
// adjust title to account for multiple tags
const title = tag[0].toUpperCase() + tag.split(' ').join('-').slice(1)
return (
<>
<TagSEO
title={`${tag} - ${siteMetadata.title}`}
description={`${tag} tags - ${siteMetadata.author}`}
/>
<ListLayout posts={posts} title={title} />
</>
)
}
2 changes: 0 additions & 2 deletions pages/tags/[tag].tsx → pages/tags/[tag]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { allBlogs } from 'contentlayer/generated'

export async function getStaticPaths() {
const tags = await getAllTags(allBlogs)

return {
paths: Object.keys(tags).map((tag) => ({
params: {
Expand All @@ -27,7 +26,6 @@ export const getStaticProps = async (context) => {
post.draft !== true && post.tags.map((t) => kebabCase(t)).includes(tag)
)
)

return { props: { posts: filteredPosts, tag } }
}

Expand Down

0 comments on commit e1ca776

Please sign in to comment.