aboutsummaryrefslogtreecommitdiff
path: root/internal/front/front.go
blob: b2c7f0ec4727448e41c4ed7afe7cf962a752c7ca (plain)
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 front provides YAML frontmatter unmarshalling.
package front

import (
	"bytes"

	"gopkg.in/yaml.v2"
)

// Delimiter.
var delim = []byte("---")

// Unmarshal parses YAML frontmatter and returns the content. When no
// frontmatter delimiters are present the original content is returned.
func Unmarshal(b []byte, v interface{}) (content []byte, err error) {
	if !bytes.HasPrefix(b, delim) {
		return b, nil
	}

	parts := bytes.SplitN(b, delim, 3)
	content = parts[2]
	err = yaml.Unmarshal(parts[1], v)
	return
}