feat(frontmatter): track parsing errors and frontmatter existence in result type
Add hadFrontmatter and parseError flags to FrontmatterResult interface to enable error handling in skill loading. 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
@@ -3,6 +3,8 @@ import yaml from "js-yaml"
|
|||||||
export interface FrontmatterResult<T = Record<string, unknown>> {
|
export interface FrontmatterResult<T = Record<string, unknown>> {
|
||||||
data: T
|
data: T
|
||||||
body: string
|
body: string
|
||||||
|
hadFrontmatter: boolean
|
||||||
|
parseError: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseFrontmatter<T = Record<string, unknown>>(
|
export function parseFrontmatter<T = Record<string, unknown>>(
|
||||||
@@ -12,7 +14,7 @@ export function parseFrontmatter<T = Record<string, unknown>>(
|
|||||||
const match = content.match(frontmatterRegex)
|
const match = content.match(frontmatterRegex)
|
||||||
|
|
||||||
if (!match) {
|
if (!match) {
|
||||||
return { data: {} as T, body: content }
|
return { data: {} as T, body: content, hadFrontmatter: false, parseError: false }
|
||||||
}
|
}
|
||||||
|
|
||||||
const yamlContent = match[1]
|
const yamlContent = match[1]
|
||||||
@@ -22,8 +24,8 @@ export function parseFrontmatter<T = Record<string, unknown>>(
|
|||||||
// Use JSON_SCHEMA for security - prevents code execution via YAML tags
|
// Use JSON_SCHEMA for security - prevents code execution via YAML tags
|
||||||
const parsed = yaml.load(yamlContent, { schema: yaml.JSON_SCHEMA })
|
const parsed = yaml.load(yamlContent, { schema: yaml.JSON_SCHEMA })
|
||||||
const data = (parsed ?? {}) as T
|
const data = (parsed ?? {}) as T
|
||||||
return { data, body }
|
return { data, body, hadFrontmatter: true, parseError: false }
|
||||||
} catch {
|
} catch {
|
||||||
return { data: {} as T, body }
|
return { data: {} as T, body, hadFrontmatter: true, parseError: true }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user