1
0
Fork 0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-06-01 09:36:06 +02:00
kiln/frontmatter.go

25 lines
475 B
Go
Raw Normal View History

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