diff options
Diffstat (limited to 'internal/front/front.go')
| -rw-r--r-- | internal/front/front.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/internal/front/front.go b/internal/front/front.go new file mode 100644 index 0000000..b2c7f0e --- /dev/null +++ b/internal/front/front.go @@ -0,0 +1,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 +} |
