fix: better frontmatter errors

This commit is contained in:
Aiden Cline
2025-10-30 10:56:30 -05:00
parent 2d5df3ad76
commit 42e0b47a7d
3 changed files with 43 additions and 10 deletions

View File

@@ -1,3 +1,7 @@
import { NamedError } from "@/util/error"
import matter from "gray-matter"
import { z } from "zod"
export namespace ConfigMarkdown {
export const FILE_REGEX = /(?<![\w`])@(\.?[^\s`,.]*(?:\.[^\s`,.]+)*)/g
export const SHELL_REGEX = /!`([^`]+)`/g
@@ -9,4 +13,29 @@ export namespace ConfigMarkdown {
export function shell(template: string) {
return Array.from(template.matchAll(SHELL_REGEX))
}
export async function parse(filePath: string) {
const template = await Bun.file(filePath).text()
try {
const md = matter(template)
return md
} catch (err) {
throw new FrontmatterError(
{
path: filePath,
message: `Failed to parse YAML frontmatter: ${err instanceof Error ? err.message : String(err)}`,
},
{ cause: err },
)
}
}
export const FrontmatterError = NamedError.create(
"ConfigFrontmatterError",
z.object({
path: z.string(),
message: z.string(),
}),
)
}