-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import slugify from "slugify"; | ||
import {getSortedPostsData} from "./posts"; | ||
|
||
async function getTaggedPosts(){ | ||
const allPosts = await getSortedPostsData() | ||
const taggedPosts = allPosts.filter(post => post.tags) | ||
return taggedPosts | ||
} | ||
|
||
export async function getAllPostTags() { | ||
const taggedPosts = await getTaggedPosts() | ||
let tagSet = new Set() | ||
taggedPosts.map(post => { | ||
post.tags.forEach(item => { | ||
const tagSlug = slugify(item.toLowerCase()) | ||
tagSet.add("/tag/" + tagSlug + "/") | ||
}) | ||
}) | ||
return [...tagSet] | ||
} | ||
|
||
|
||
export async function getPostDataForTag(tagSlug) { | ||
const taggedPosts = await getTaggedPosts() | ||
return taggedPosts.filter(post => { | ||
const slugs = post.tags.map(tag => slugify(tag.toLowerCase())) | ||
return slugs.includes(tagSlug) | ||
}) | ||
} |
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,47 @@ | ||
import Head from 'next/head' | ||
|
||
import Layout, {PostColumn, PostSummary} from '../../components/layout' | ||
import {H1} from "../../components/headings"; | ||
import {getAllPostTags, getPostDataForTag} from "../../lib/tags"; | ||
import {getSortedPostsData} from "../../lib/posts"; | ||
|
||
export async function getStaticProps({params}) { | ||
const taggedPostsData = await getPostDataForTag(params.tag) | ||
const allPostsData = await getSortedPostsData() | ||
return { | ||
props: { | ||
taggedPostsData, | ||
allPostsData, | ||
tag: params.tag, | ||
} | ||
} | ||
} | ||
|
||
export async function getStaticPaths() { | ||
const tags = await getAllPostTags() | ||
return { | ||
paths: tags, | ||
fallback: false, | ||
} | ||
} | ||
|
||
|
||
export default function TagPage({taggedPostsData, allPostsData, tag}) { | ||
return ( | ||
<Layout allPosts={allPostsData} home={false}> | ||
<Head> | ||
<title>Posts tagged with "{tag}" | Free Law Project</title> | ||
</Head> | ||
<PostColumn> | ||
<div className="pt-10"> | ||
<H1>Posts Tagged with "{tag}"</H1> | ||
</div> | ||
<div className="divide-y divide-gray-300"> | ||
{taggedPostsData.map((post) => ( | ||
<PostSummary post={post}/> | ||
))} | ||
</div> | ||
</PostColumn> | ||
</Layout> | ||
) | ||
} |