kiln/frontmatter.go
2021-04-24 13:24:31 -04:00

25 lines
475 B
Go

package main
import "bytes"
var (
frontmatterOpen = []byte("---")
frontmatterClose = []byte("\n---")
)
func extractFrontmatter(b []byte) (frontmatter, content []byte) {
if !bytes.HasPrefix(b, frontmatterOpen) {
return nil, b
}
if len(b) > len(frontmatterOpen) && b[len(frontmatterOpen)] != '\n' {
return nil, b
}
b = b[len(frontmatterOpen):]
i := bytes.Index(b, frontmatterClose)
if i == -1 {
return b, nil
}
return b[:i], b[i+len(frontmatterClose):]
}