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