Skip to content

Commit

Permalink
Add tag pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Oct 18, 2021
1 parent 470252f commit f15f0d8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/components/ArticlePreview.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import Article from "$data/articles"
import { Article } from "$data/articles"
import { readableDate } from "../utils"
export interface Props {
article: Article
Expand All @@ -9,12 +10,12 @@ const { article } = Astro.props
---

<section class="mb-3">
<a href={`/article/${article.slug}`}>
<a href={article.url}>
<h4 class="inline-block m-0 text-xl">{article.title}</h4>
</a>

<div class="mb-2 text-creative-work text-sm">
{article.dateString}
{readableDate(article.date)}
·
{article.tags
.map((tag) => <a href="{ '/articles/tags/' + tag}">{tag}</a>)
Expand Down
10 changes: 7 additions & 3 deletions src/data/articles.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type { BaseObject } from "./shared"
import { postProcessBase } from "./shared"
import { readableDate } from "../utils"

interface Article extends BaseObject {
title: string
tagline?: string
date: Date
dateString?: string
tags: string[]
}

Expand All @@ -16,7 +14,13 @@ interface Article extends BaseObject {

function postProcessArticle(article: Article): Article {
article = postProcessBase(article) as Article
article.dateString = readableDate(new Date(article.date)) // NOTE: For some reason, our date become a string, weird

article.url = new URL(`/article/${article.slug}`, "http://localhost:3000")

// NOTE: For some reason, Astro transform our dates into string so let's check for that and return a proper date
if (typeof article.date === "string") {
article.date = new Date(article.date)
}

return article
}
Expand Down
6 changes: 5 additions & 1 deletion src/pages/articles/[...page].astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { postProcessArticle } from "$data/articles"
export async function getStaticPaths({ paginate }) {
let articles = await Astro.fetchContent("../../content/articles/**/*.md")
articles = articles.map((article) => postProcessArticle(article))
articles = articles
.map((article) => postProcessArticle(article))
.sort((a, b) => {
return b.date.getTime() - a.date.getTime()
})
return paginate(articles, { pageSize: 5 })
}
Expand Down
45 changes: 45 additions & 0 deletions src/pages/articles/tags/[tag]/[...page].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
import BaseLayout from "$layouts/BaseLayout.astro"
import ArticlePreview from "$components/ArticlePreview.astro"
import { postProcessArticle } from "$data/articles"
export async function getStaticPaths({ paginate }) {
let articles = await Astro.fetchContent("../../../../content/articles/**/*.md")
articles = articles
.map((article) => postProcessArticle(article))
.sort((a, b) => {
return b.date.getTime() - a.date.getTime()
})
const tagList = new Array()
articles.forEach((article) => {
tagList.push(...article.tags)
})
return tagList.map((tag) => {
const filteredArticles = articles.filter((article) => article.tags.includes(tag))
return paginate(filteredArticles, { params: { tag }, pageSize: 5 })
})
}
const { page } = Astro.props
---

<BaseLayout title="Articles">
<article class="w-articleList mx-auto flex flex-col md:flex-row">
<div class="sm:w-[80%]">
<h2>Articles</h2>
{page.data.map((item) => <ArticlePreview article={item} />)}
</div>

<aside class="sm:w-[20%]">
<section>
<h2>Tags</h2>
<ul></ul>
</section>
<section>
<h2>Year</h2>
</section>
</aside>
</article>
</BaseLayout>

0 comments on commit f15f0d8

Please sign in to comment.