kiln/frontmatter.go
2021-04-21 15:13:45 -04:00

24 lines
446 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
}
fm := b[len(frontmatterOpen):]
if len(fm) != 0 && fm[0] != '\n' {
return nil, b
}
i := bytes.Index(fm, frontmatterClose)
if i == -1 {
i = len(fm)
}
return fm[:i], b[len(frontmatterOpen)+len(fm):]
}