mirror of
https://github.com/r-freeman/portfolio.git
synced 2025-05-15 15:10:19 +00:00
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m16s
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import glob from 'fast-glob'
|
|
import * as path from 'path'
|
|
import {Article} from '@/types'
|
|
|
|
async function importArticle(articleFilename: string) {
|
|
let {meta, default: component} = await import(
|
|
`/app/writing/${articleFilename}`
|
|
)
|
|
return {
|
|
slug: articleFilename.replace(/(\/page)?\.mdx$/, ''),
|
|
...meta,
|
|
component
|
|
}
|
|
}
|
|
|
|
export async function getAllArticles(dateDesc = true) {
|
|
let articleFilenames = await glob(['*.mdx', '*/page.mdx'], {
|
|
cwd: path.join(process.cwd(), './app/writing')
|
|
})
|
|
|
|
let articles = await Promise.all(articleFilenames.map(importArticle))
|
|
|
|
return dateDesc ? articles.sort((a, b) => a.date < b.date ? 1 : -1)
|
|
: articles.sort((a, b) => a.date > b.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
|
|
}, {})
|
|
}
|