kiln/frontmatter.go

25 lines
475 B
Go
Raw Normal View History

2021-04-21 19:13:45 +00:00
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
}
2021-04-24 17:24:31 +00:00
if len(b) > len(frontmatterOpen) && b[len(frontmatterOpen)] != '\n' {
2021-04-21 19:13:45 +00:00
return nil, b
}
2021-04-24 17:24:31 +00:00
b = b[len(frontmatterOpen):]
i := bytes.Index(b, frontmatterClose)
2021-04-21 19:13:45 +00:00
if i == -1 {
2021-04-24 17:24:31 +00:00
return b, nil
2021-04-21 19:13:45 +00:00
}
2021-04-24 17:24:31 +00:00
return b[:i], b[i+len(frontmatterClose):]
2021-04-21 19:13:45 +00:00
}