MCPcopy Create free account
hub / github.com/github/gh-aw / extractFrontmatterAndBodyText

Function extractFrontmatterAndBodyText

pkg/parser/frontmatter_hash.go:253–289  ·  view source on GitHub ↗

extractFrontmatterAndBodyText extracts frontmatter as raw text without parsing YAML Returns: frontmatterText, markdownBody, error

(content string)

Source from the content-addressed store, hash-verified

251// extractFrontmatterAndBodyText extracts frontmatter as raw text without parsing YAML
252// Returns: frontmatterText, markdownBody, error
253func extractFrontmatterAndBodyText(content string) (string, string, error) {
254 // Normalize CRLF to LF so that files with Windows line-endings produce the
255 // same frontmatter text (and therefore the same hash) as equivalent LF files.
256 content = strings.ReplaceAll(content, "\r\n", "\n")
257
258 lines := strings.Split(content, "\n")
259
260 // Check if content starts with frontmatter delimiter
261 if len(lines) == 0 || strings.TrimSpace(lines[0]) != "---" {
262 // No frontmatter
263 return "", content, nil
264 }
265
266 // Find end of frontmatter
267 endIndex := -1
268 for i := 1; i < len(lines); i++ {
269 if strings.TrimSpace(lines[i]) == "---" {
270 endIndex = i
271 break
272 }
273 }
274
275 if endIndex == -1 {
276 return "", "", errors.New("frontmatter not properly closed")
277 }
278
279 // Extract frontmatter text (lines between --- delimiters)
280 frontmatterText := strings.Join(lines[1:endIndex], "\n")
281
282 // Extract markdown body (everything after closing ---)
283 var markdown string
284 if endIndex+1 < len(lines) {
285 markdown = strings.Join(lines[endIndex+1:], "\n")
286 }
287
288 return frontmatterText, markdown, nil
289}
290
291// normalizeFrontmatterText normalizes frontmatter text for consistent hashing
292// Removes leading/trailing whitespace and normalizes line endings

Callers 4

processImportsTextBasedFunction · 0.85
collectImportedBodiesFunction · 0.85
ComputeBodyHashFromFileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected