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

27 lines
567 B
Go
Raw Normal View History

2021-04-21 21:13:45 +02:00
package main
import "bytes"
var (
frontmatterOpen = []byte("---")
frontmatterClose = []byte("\n---")
2022-06-14 23:42:23 +02:00
lf = []byte("\n")
crlf = []byte("\r\n")
2021-04-21 21:13:45 +02:00
)
func extractFrontmatter(b []byte) (frontmatter, content []byte) {
if !bytes.HasPrefix(b, frontmatterOpen) {
return nil, b
}
2022-06-14 23:42:23 +02:00
next := b[len(frontmatterOpen):]
if len(next) > 0 && !bytes.HasPrefix(next, lf) && !bytes.HasPrefix(next, crlf) {
2021-04-21 21:13:45 +02:00
return nil, b
}
2022-06-14 23:42:23 +02:00
b = next
2021-04-24 19:24:31 +02:00
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
}