1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-11-08 14:19:20 +01:00
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):]
}