@@ -6,21 +6,23 @@ import (
66)
77
88// ExtractYAMLError extracts line and column information from YAML parsing errors
9- func ExtractYAMLError (err error , frontmatterStartLine int ) (line int , column int , message string ) {
9+ // frontmatterLineOffset is the line number where the frontmatter content begins in the document (1-based)
10+ // This allows proper line number reporting when frontmatter is not at the beginning of the document
11+ func ExtractYAMLError (err error , frontmatterLineOffset int ) (line int , column int , message string ) {
1012 errStr := err .Error ()
1113
1214 // First try to extract from goccy/go-yaml's [line:column] format
13- line , column , message = extractFromGoccyFormat (errStr , frontmatterStartLine )
15+ line , column , message = extractFromGoccyFormat (errStr , frontmatterLineOffset )
1416 if line > 0 || column > 0 {
1517 return line , column , message
1618 }
1719
1820 // Fallback to standard YAML error string parsing for other libraries
19- return extractFromStringParsing (errStr , frontmatterStartLine )
21+ return extractFromStringParsing (errStr , frontmatterLineOffset )
2022}
2123
2224// extractFromGoccyFormat extracts line/column from goccy/go-yaml's [line:column] message format
23- func extractFromGoccyFormat (errStr string , frontmatterStartLine int ) (line int , column int , message string ) {
25+ func extractFromGoccyFormat (errStr string , frontmatterLineOffset int ) (line int , column int , message string ) {
2426 // Look for goccy format like "[5:10] mapping value is not allowed in this context"
2527 if strings .Contains (errStr , "[" ) && strings .Contains (errStr , "]" ) {
2628 start := strings .Index (errStr , "[" )
@@ -41,11 +43,11 @@ func extractFromGoccyFormat(errStr string, frontmatterStartLine int) (line int,
4143 if _ , parseErr := fmt .Sscanf (columnStr , "%d" , & column ); parseErr == nil {
4244 // Adjust line number to account for frontmatter position in file
4345 if line > 0 {
44- line += frontmatterStartLine
46+ line += frontmatterLineOffset - 1 // -1 because line numbers in YAML errors are 1-based relative to YAML content
4547 }
4648
4749 // Only return valid positions - avoid returning 1,1 when location is unknown
48- if line <= frontmatterStartLine && column <= 1 {
50+ if line <= frontmatterLineOffset && column <= 1 {
4951 return 0 , 0 , messagePart
5052 }
5153
@@ -61,7 +63,7 @@ func extractFromGoccyFormat(errStr string, frontmatterStartLine int) (line int,
6163}
6264
6365// extractFromStringParsing provides fallback string parsing for other YAML libraries
64- func extractFromStringParsing (errStr string , frontmatterStartLine int ) (line int , column int , message string ) {
66+ func extractFromStringParsing (errStr string , frontmatterLineOffset int ) (line int , column int , message string ) {
6567 // Parse "yaml: line X: column Y: message" format (enhanced parsers that provide column info)
6668 if strings .Contains (errStr , "yaml: line " ) && strings .Contains (errStr , "column " ) {
6769 parts := strings .SplitN (errStr , "yaml: line " , 2 )
@@ -89,7 +91,7 @@ func extractFromStringParsing(errStr string, frontmatterStartLine int) (line int
8991 // Parse column number
9092 if _ , parseErr := fmt .Sscanf (columnStr , "%d" , & column ); parseErr == nil {
9193 // Adjust line number to account for frontmatter position in file
92- line += frontmatterStartLine
94+ line += frontmatterLineOffset - 1 // -1 because line numbers in YAML errors are 1-based relative to YAML content
9395 return
9496 }
9597 }
@@ -113,7 +115,7 @@ func extractFromStringParsing(errStr string, frontmatterStartLine int) (line int
113115 // Parse line number
114116 if _ , parseErr := fmt .Sscanf (lineStr , "%d" , & line ); parseErr == nil {
115117 // Adjust line number to account for frontmatter position in file
116- line += frontmatterStartLine
118+ line += frontmatterLineOffset - 1 // -1 because line numbers in YAML errors are 1-based relative to YAML content
117119 // Don't default to column 1 when not provided - return 0 instead
118120 column = 0
119121 return
@@ -139,8 +141,8 @@ func extractFromStringParsing(errStr string, frontmatterStartLine int) (line int
139141 // Parse line number
140142 if _ , parseErr := fmt .Sscanf (lineStr , "%d" , & line ); parseErr == nil {
141143 // Adjust line number to account for frontmatter position in file
142- line += frontmatterStartLine
143- column = 0 // Don't default to column 1
144+ line += frontmatterLineOffset - 1 // -1 because line numbers in YAML errors are 1-based relative to YAML content
145+ column = 0 // Don't default to column 1
144146 message = restOfMessage
145147 return
146148 }
0 commit comments