extractFrontmatter splits content into frontmatter and body
(content []byte)
| 275 | |
| 276 | // extractFrontmatter splits content into frontmatter and body |
| 277 | func extractFrontmatter(content []byte) (frontmatter []byte, body string, err error) { |
| 278 | lines := bytes.Split(content, []byte("\n")) |
| 279 | |
| 280 | // Check if content starts with frontmatter delimiter |
| 281 | if len(lines) == 0 || string(bytes.TrimSpace(lines[0])) != frontmatterDelimiter { |
| 282 | // No frontmatter, entire content is body |
| 283 | return nil, string(content), nil |
| 284 | } |
| 285 | |
| 286 | // Find closing delimiter |
| 287 | closingIndex := -1 |
| 288 | for i := 1; i < len(lines); i++ { |
| 289 | if string(bytes.TrimSpace(lines[i])) == frontmatterDelimiter { |
| 290 | closingIndex = i |
| 291 | break |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if closingIndex == -1 { |
| 296 | return nil, "", fmt.Errorf("unclosed frontmatter delimiter") |
| 297 | } |
| 298 | |
| 299 | // Extract frontmatter (between delimiters) |
| 300 | frontmatterLines := lines[1:closingIndex] |
| 301 | frontmatter = bytes.Join(frontmatterLines, []byte("\n")) |
| 302 | |
| 303 | // Extract body (after closing delimiter) |
| 304 | if closingIndex+1 < len(lines) { |
| 305 | bodyLines := lines[closingIndex+1:] |
| 306 | body = strings.TrimSpace(string(bytes.Join(bodyLines, []byte("\n")))) |
| 307 | } |
| 308 | |
| 309 | return frontmatter, body, nil |
| 310 | } |
no outgoing calls