@@ -3,11 +3,13 @@ import { parse as parseYaml } from "yaml";
33
44export class SkillParseError extends Error {
55 readonly field ?: string ;
6+ readonly path ?: string ;
67
7- constructor ( message : string , field ?: string ) {
8+ constructor ( message : string , field ?: string , filePath ?: string ) {
89 super ( message ) ;
910 this . name = "SkillParseError" ;
1011 this . field = field ;
12+ this . path = filePath ;
1113 }
1214}
1315
@@ -34,41 +36,57 @@ type ParseOptions = {
3436} ;
3537
3638export function parseSkillMarkdown ( markdown : string , options : ParseOptions = { } ) : SkillFile {
37- const { frontmatterText, body } = splitFrontmatter ( markdown ) ;
38- const data = parseYaml ( frontmatterText ) ;
39-
40- if ( ! data || typeof data !== "object" || Array . isArray ( data ) ) {
41- throw new SkillParseError ( "Frontmatter must be a YAML object" ) ;
42- }
43-
44- const record = data as Record < string , unknown > ;
45- const name = readStringField ( record , "name" , true ) ;
46- const description = readStringField ( record , "description" , true ) ;
47- const license = readStringField ( record , "license" ) ;
48- const compatibility = readStringField ( record , "compatibility" ) ;
49- const allowedTools = readAllowedTools ( record ) ;
50- const metadata = normalizeMetadata ( record . metadata ) ;
51-
52- const expectedName = options . expectedName ?? deriveExpectedName ( options . path ) ;
53-
54- validateName ( name , expectedName ) ;
55- validateDescription ( description ) ;
56- validateCompatibility ( compatibility ) ;
57-
58- const frontmatter : SkillFrontmatter = {
59- name,
60- description,
61- license,
62- compatibility,
63- metadata,
64- allowedTools,
65- } ;
66-
67- return {
68- frontmatter,
69- body,
70- isInternal : metadata ?. internal === true ,
71- } ;
39+ try {
40+ const { frontmatterText, body } = splitFrontmatter ( markdown ) ;
41+ const data = parseYaml ( frontmatterText ) ;
42+
43+ if ( ! data || typeof data !== "object" || Array . isArray ( data ) ) {
44+ throw new SkillParseError ( "Frontmatter must be a YAML object" ) ;
45+ }
46+
47+ const record = data as Record < string , unknown > ;
48+ const name = readStringField ( record , "name" , true ) ;
49+ const description = readStringField ( record , "description" , true ) ;
50+ const license = readStringField ( record , "license" ) ;
51+ const compatibility = readStringField ( record , "compatibility" ) ;
52+ const allowedTools = readAllowedTools ( record ) ;
53+ const metadata = normalizeMetadata ( record . metadata ) ;
54+
55+ const expectedName = options . expectedName ?? deriveExpectedName ( options . path ) ;
56+
57+ validateName ( name , expectedName ) ;
58+ validateDescription ( description ) ;
59+ validateCompatibility ( compatibility ) ;
60+
61+ const frontmatter : SkillFrontmatter = {
62+ name,
63+ description,
64+ license,
65+ compatibility,
66+ metadata,
67+ allowedTools,
68+ } ;
69+
70+ return {
71+ frontmatter,
72+ body,
73+ isInternal : metadata ?. internal === true ,
74+ } ;
75+ } catch ( error ) {
76+ if ( error instanceof SkillParseError ) {
77+ throw withPath ( error , options . path ) ;
78+ }
79+
80+ throw error ;
81+ }
82+ }
83+
84+ function withPath ( error : SkillParseError , filePath ?: string ) : SkillParseError {
85+ if ( ! filePath || error . path === filePath ) {
86+ return error ;
87+ }
88+
89+ return new SkillParseError ( error . message , error . field , filePath ) ;
7290}
7391
7492function splitFrontmatter ( markdown : string ) : { frontmatterText : string ; body : string } {
0 commit comments