mirror of
https://github.com/r-freeman/portfolio.git
synced 2025-05-15 21:00:21 +00:00
Group articles by year
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m15s
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m15s
This commit is contained in:
parent
816e643f55
commit
b293c36e17
@ -1,10 +1,11 @@
|
|||||||
import React from 'react'
|
import React, {ReactNode} from 'react'
|
||||||
import {SimpleLayout} from '@/components/layouts/SimpleLayout'
|
import {SimpleLayout} from '@/components/layouts/SimpleLayout'
|
||||||
|
import {groupArticlesByYear} from '@/lib/getAllArticles'
|
||||||
|
import {metadata as _metadata} from '@/lib/generateMetadata'
|
||||||
|
import {Section} from '@/components/ui/Section'
|
||||||
|
import {Article} from '@/types'
|
||||||
import {Card} from '@/components/ui/Card'
|
import {Card} from '@/components/ui/Card'
|
||||||
import {Views} from '@/components/ui/Views'
|
import {Views} from '@/components/ui/Views'
|
||||||
import {getAllArticles} from '@/lib/getAllArticles'
|
|
||||||
import {metadata as _metadata} from '@/lib/generateMetadata'
|
|
||||||
import type {Article} from '@/types'
|
|
||||||
import {format} from 'date-fns'
|
import {format} from 'date-fns'
|
||||||
|
|
||||||
const meta = {
|
const meta = {
|
||||||
@ -33,31 +34,39 @@ export let metadata: {
|
|||||||
|
|
||||||
metadata = _metadata({...meta, heading: meta.heading})
|
metadata = _metadata({...meta, heading: meta.heading})
|
||||||
|
|
||||||
function Article({article}: { article: Article }) {
|
function ArticleSection({year, children}: { year: string, children: ReactNode }) {
|
||||||
return (
|
return (
|
||||||
<article>
|
<Section title={year}>
|
||||||
<Card variant="inline">
|
{children}
|
||||||
<Card.Title href={`/writing/${article.slug}`}>
|
</Section>
|
||||||
{article.title}
|
)
|
||||||
</Card.Title>
|
}
|
||||||
<p className="flex order-first space-x-1 z-10 mb-3 md:mb-0 md:ml-4 md:order-last flex-shrink-0">
|
|
||||||
<Card.Eyebrow as="time" dateTime={article.date} decorate={false}>
|
function ArticleList({articles}: { articles: Article[] }) {
|
||||||
{format(article.date, 'd MMMM yyyy')}
|
return (
|
||||||
</Card.Eyebrow>
|
<ul role="list" className="space-y-16">
|
||||||
<Views
|
{articles.map((article) => (
|
||||||
slug={article.slug}
|
<Card key={article.slug} as="li">
|
||||||
title={article.title}
|
<Card.Title href={`/writing/${article.slug}`}>{article.title}</Card.Title>
|
||||||
className="text-sm text-zinc-500 dark:text-zinc-400"
|
<p className="flex order-first space-x-1 z-10 mb-3">
|
||||||
shouldUpdateViews={false}
|
<Card.Eyebrow as="time" dateTime={article.date} decorate={false}>
|
||||||
/>
|
{format(article.date, 'd MMMM yyyy')}
|
||||||
</p>
|
</Card.Eyebrow>
|
||||||
</Card>
|
<Views
|
||||||
</article>
|
slug={article.slug}
|
||||||
|
title={article.title}
|
||||||
|
className="text-sm text-zinc-500 dark:text-zinc-400"
|
||||||
|
shouldUpdateViews={false}
|
||||||
|
/>
|
||||||
|
</p>
|
||||||
|
</Card>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function Writing() {
|
export default async function Writing() {
|
||||||
const articles = (await getAllArticles()).map(({component, ...meta}) => meta)
|
const groupedArticles = await groupArticlesByYear()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SimpleLayout
|
<SimpleLayout
|
||||||
@ -65,12 +74,16 @@ export default async function Writing() {
|
|||||||
description={meta.description}
|
description={meta.description}
|
||||||
gradient="bg-gradient-to-r from-pink-500 to-violet-500"
|
gradient="bg-gradient-to-r from-pink-500 to-violet-500"
|
||||||
>
|
>
|
||||||
<div className="mx-auto grid max-w-xl grid-cols-1 gap-y-20 lg:max-w-none">
|
<div className="space-y-20">
|
||||||
<div className="max-w-3xl space-y-16 mt-6">
|
{Object.entries(groupedArticles)
|
||||||
{articles.map((article) => (
|
.sort(([a], [b]) => parseInt(b) - parseInt(a))
|
||||||
<Article key={article.slug} article={article}/>
|
.map(([year, articles]) => (
|
||||||
))}
|
<ArticleSection year={year} key={year}>
|
||||||
</div>
|
<ArticleList articles={articles}/>
|
||||||
|
</ArticleSection>
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
</SimpleLayout>
|
</SimpleLayout>
|
||||||
)
|
)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import glob from 'fast-glob'
|
import glob from 'fast-glob'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
|
import {Article} from '@/types'
|
||||||
|
|
||||||
async function importArticle(articleFilename: string) {
|
async function importArticle(articleFilename: string) {
|
||||||
let {meta, default: component} = await import(
|
let {meta, default: component} = await import(
|
||||||
@ -22,3 +23,16 @@ export async function getAllArticles(dateDesc = true) {
|
|||||||
return dateDesc ? articles.sort((a, z) => a.date < z.date ? 1 : -1)
|
return dateDesc ? articles.sort((a, z) => a.date < z.date ? 1 : -1)
|
||||||
: articles.sort((a, z) => a.date > z.date ? 1 : -1)
|
: articles.sort((a, z) => a.date > z.date ? 1 : -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function groupArticlesByYear() {
|
||||||
|
return (await getAllArticles())
|
||||||
|
.map(({component, ...meta}) => meta)
|
||||||
|
.reduce<{ [year: string]: Article[] }>((acc, article) => {
|
||||||
|
const year = new Date(article.date).getFullYear()
|
||||||
|
if (!acc[year]) {
|
||||||
|
acc[year] = []
|
||||||
|
}
|
||||||
|
acc[year].push(article)
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user