portfolio/lib/getAllArticles.ts

25 lines
732 B
TypeScript
Raw Normal View History

2022-12-06 12:54:34 +00:00
import glob from 'fast-glob'
import * as path from 'path'
2023-01-14 22:42:49 +00:00
async function importArticle(articleFilename: string) {
2024-10-11 21:01:19 +00:00
let {meta, default: component} = await import(
2023-07-29 22:40:36 +00:00
`/app/writing/${articleFilename}`
2023-01-14 19:31:05 +00:00
)
return {
2023-07-29 22:40:36 +00:00
slug: articleFilename.replace(/(\/page)?\.mdx$/, ''),
2024-10-11 21:01:19 +00:00
...meta,
2023-01-14 19:31:05 +00:00
component,
}
2022-12-06 12:54:34 +00:00
}
2024-10-01 21:00:02 +00:00
export async function getAllArticles(dateDesc = true) {
2023-07-29 22:40:36 +00:00
let articleFilenames = await glob(['*.mdx', '*/page.mdx'], {
cwd: path.join(process.cwd(), './app/writing'),
2023-01-14 19:31:05 +00:00
})
2022-12-06 12:54:34 +00:00
2023-01-14 19:31:05 +00:00
let articles = await Promise.all(articleFilenames.map(importArticle))
2022-12-06 12:54:34 +00:00
2024-10-01 21:00:02 +00:00
return dateDesc ? articles.sort((a, z) => a.date < z.date ? 1 : -1)
: articles.sort((a, z) => a.date > z.date ? 1 : -1)
2022-12-06 12:54:34 +00:00
}