~adnano/kiln

ref: 7ac653c62085191327b594a7ae1617cd561c2d32 kiln/frontmatter.go -rw-r--r-- 475 bytes
7ac653c6Adnan Maolood docs: Tweak wording 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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):]
}