import { NamedError } from "@opencode-ai/util/error" import matter from "gray-matter" import { z } from "zod" import { Filesystem } from "../util/filesystem" export namespace ConfigMarkdown { export const FILE_REGEX = /(?" || value === "|" || value.startsWith('"') || value.startsWith("'")) { result.push(line) continue } // if value contains a colon, convert to block scalar if (value.includes(":")) { result.push(`${key}: |-`) result.push(` ${value}`) continue } result.push(line) } const processed = result.join("\n") return content.replace(frontmatter, () => processed) } export async function parse(filePath: string) { const template = await Filesystem.readText(filePath) try { const md = matter(template) return md } catch { try { return matter(fallbackSanitization(template)) } catch (err) { throw new FrontmatterError( { path: filePath, message: `${filePath}: 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(), }), ) }