-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from thedevdavid/feature/tags
Feature/tags
- Loading branch information
Showing
16 changed files
with
190 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Metadata } from "next"; | ||
import { notFound } from "next/navigation"; | ||
import { allPosts, Post } from "@/.contentlayer/generated"; | ||
import { compareDesc } from "date-fns"; | ||
|
||
import PostPreview from "@/components/post-preview"; | ||
|
||
// Get sorted articles from the contentlayer | ||
async function getSortedArticles(): Promise<Post[]> { | ||
let articles = await allPosts; | ||
|
||
articles = articles.filter((article: Post) => article.status === "published"); | ||
|
||
return articles.sort((a: Post, b: Post) => { | ||
if (a.publishedDate && b.publishedDate) { | ||
return new Date(b.publishedDate).getTime() - new Date(a.publishedDate).getTime(); | ||
} | ||
return 0; | ||
}); | ||
} | ||
|
||
// Dynamic metadata for the page | ||
export async function generateMetadata({ params }: { params: { slug: string } }): Promise<Metadata> { | ||
return { | ||
title: `All posts in ${params.slug}`, | ||
description: `All posts in ${params.slug}`, | ||
}; | ||
} | ||
|
||
export default async function TagPage({ params }: { params: { slug: string } }) { | ||
const tag = params.slug; | ||
|
||
const posts = allPosts | ||
.filter((post) => post.status === "published") | ||
.filter((post) => post.tags?.includes(tag)) | ||
.sort((a, b) => compareDesc(new Date(a.publishedDate), new Date(b.publishedDate))); | ||
|
||
if (!posts) { | ||
notFound(); | ||
} | ||
|
||
return ( | ||
<div className="container mb-4"> | ||
<div className="prose mx-auto max-w-5xl dark:prose-invert prose-headings:font-heading prose-headings:font-bold prose-headings:leading-tight hover:prose-a:text-accent-foreground prose-a:prose-headings:no-underline"> | ||
<h1 className="mt-0">All posts in {tag}</h1> | ||
<hr className="my-4" /> | ||
<div className="grid grid-flow-row gap-2"> | ||
{posts.map((post) => ( | ||
<PostPreview post={post} key={post._id} /> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Metadata } from "next"; | ||
import Link from "next/link"; | ||
import { notFound } from "next/navigation"; | ||
import { allPosts } from "@/.contentlayer/generated"; | ||
|
||
import siteMetadata from "@/lib/metadata"; | ||
import { getTagsWithCount } from "@/lib/utils"; | ||
import { Badge } from "@/components/ui/badge"; | ||
|
||
export async function generateMetadata(): Promise<Metadata> { | ||
return { | ||
title: "Tags", | ||
description: `All tags in ${siteMetadata.title}`, | ||
}; | ||
} | ||
|
||
export default function TagsPage() { | ||
const posts = allPosts.filter((post) => post.status === "published"); | ||
|
||
const tags = getTagsWithCount(posts); | ||
|
||
if (!tags || Object.keys(tags).length === 0) { | ||
notFound(); | ||
} | ||
|
||
return ( | ||
<div className="container mb-4"> | ||
<div className="prose mx-auto max-w-5xl dark:prose-invert prose-headings:font-heading prose-headings:font-bold prose-headings:leading-tight hover:prose-a:text-accent-foreground prose-a:prose-headings:no-underline"> | ||
<h1 className="mt-0">All tags</h1> | ||
<hr className="my-4" /> | ||
<div className="grid grid-flow-row gap-2"> | ||
<ul className="flex list-none flex-wrap gap-2 p-0"> | ||
{Object.keys(tags).map((tag) => { | ||
return ( | ||
<li className="list-none" key={tag}> | ||
<Link href={`/tags/${tag}`} className="group"> | ||
<Badge | ||
variant="outline" | ||
className="inline-block rounded-full border border-muted-foreground/50 bg-muted-foreground/10 px-3 py-1 text-base font-medium text-muted-foreground transition hover:bg-muted-foreground hover:text-foreground" | ||
> | ||
<span> | ||
{tag} · ({tags[tag]}) | ||
</span> | ||
</Badge> | ||
</Link> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters