Added reader and diagnostics

This commit is contained in:
2026-09-21 21:43:15 +02:00
parent f82d631ba6
commit 854f4af393
6 changed files with 127 additions and 0 deletions

45
src/lib/content/load.ts Normal file
View File

@@ -0,0 +1,45 @@
import { ARTICLES, PAGES, TAGS } from '../constants/collections'
import { createDiagnostics } from '../diagnostics'
import type { Diagnostics } from '../diagnostics/types'
import { createContentReader } from './reader'
import type { ArticleEntry, Content, PageEntry, TagEntry } from './types'
interface CollectionReader<T> {
list(): Promise<string[]>
read(id: string): Promise<T | null>
}
function reason(error: unknown): string {
const [first, ...rest] = (error instanceof Error ? error.message : String(error)).split('\n')
return first.endsWith(':') && rest.length ? rest.join('; ') : first
}
async function readAll<T>(
reader: CollectionReader<T>,
collection: string,
problems: Diagnostics,
): Promise<Map<string, T>> {
const entries = new Map<string, T>()
await Promise.all(
(await reader.list()).map(async (id) => {
try {
const entry = await reader.read(id)
if (entry) entries.set(id, entry)
} catch (error) {
problems.error(collection, id, reason(error))
}
}),
)
return entries
}
export async function loadContent(): Promise<Content> {
const { collections } = createContentReader()
const problems = createDiagnostics()
const [articles, pages, tags] = await Promise.all([
readAll<ArticleEntry>(collections.articles, ARTICLES, problems),
readAll<PageEntry>(collections.pages, PAGES, problems),
readAll<TagEntry>(collections.tags, TAGS, problems),
])
return { articles, pages, tags, diagnostics: problems.list }
}