portfolio/lib/getAllArticles.ts

24 lines
654 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) {
2023-07-29 22:40:36 +00:00
let {metadata, default: component} = await import(
`/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$/, ''),
...metadata,
2023-01-14 19:31:05 +00:00
component,
}
2022-12-06 12:54:34 +00:00
}
export async function getAllArticles() {
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
2023-01-14 22:42:49 +00:00
return articles.sort((a, z) => a.date < z.date ? 1 : -1)
2022-12-06 12:54:34 +00:00
}