42 lines
889 B
Go
42 lines
889 B
Go
package frontmatter
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type FrontMatter struct {
|
|
Title string `yaml:"title"`
|
|
Tags []string `yaml:"tags"`
|
|
Created string `yaml:"created"`
|
|
Updated string `yaml:"updated"`
|
|
Author string `yaml:"author"`
|
|
}
|
|
|
|
func ParseTags(file []byte) ([]string, error) {
|
|
fm, err := parseFrontMatter(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return fm.Tags, nil
|
|
}
|
|
|
|
func parseFrontMatter(file []byte) (*FrontMatter, error) {
|
|
content := bytes.TrimSpace(file)
|
|
if bytes.HasPrefix(content, []byte("---")) {
|
|
content = bytes.TrimSpace(content[3:])
|
|
} else {
|
|
return nil, errors.New("missing front matter delimiter")
|
|
}
|
|
|
|
if idx := bytes.Index(content, []byte("---")); idx != -1 {
|
|
content = bytes.TrimSpace(content[:idx])
|
|
}
|
|
|
|
var fm FrontMatter
|
|
if err := yaml.Unmarshal(content, &fm); err != nil {
|
|
return nil, err
|
|
}
|
|
return &fm, nil
|
|
}
|