Diffstat (limited to 'internal/frontmatter/parser.go')
| -rw-r--r-- | internal/frontmatter/parser.go | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/internal/frontmatter/parser.go b/internal/frontmatter/parser.go index 942760c..83312dc 100644 --- a/internal/frontmatter/parser.go +++ b/internal/frontmatter/parser.go @@ -1,38 +1,35 @@ package frontmatter import ( - "bytes" "fmt" + "regexp" "time" "gopkg.in/yaml.v3" ) type Metadata struct { - Title string `yaml:"title"` - ShortDescription string `yaml:"shortDescription"` - ActionDate string `yaml:"actionDate"` - PublishedTime time.Time `yaml:"publishedTime"` - Thumbnail string `yaml:"thumbnail"` - Tags []string `yaml:"tags"` - Geolocation string `yaml:"geolocation"` - Medley string `yaml:"medley"` - MedleyPart int `yaml:"medleyPart"` - ContentSettings map[string]string `yaml:"contentSettings"` + Title string `yaml:"title"` + ShortDescription string `yaml:"shortDescription"` + ActionDate string `yaml:"actionDate"` + PublishedTime time.Time `yaml:"publishedTime"` + Thumbnail string `yaml:"thumbnail"` + Tags []string `yaml:"tags"` + Geolocation string `yaml:"geolocation"` + Medley string `yaml:"medley"` + MedleyPart int `yaml:"medleyPart"` } func ParseFrontmatter(content []byte) (metadata *Metadata, markdown []byte, err error) { - if !bytes.HasPrefix(content, []byte("---\n")) { - return nil, content, nil - } + frontmatterRegex := regexp.MustCompile(`^---\s*\r?\n([\s\S]*?)\r?\n---\s*\r?\n([\s\S]*)$`) + matches := frontmatterRegex.FindSubmatch(content) - end := bytes.Index(content[4:], []byte("\n---\n")) - if end == -1 { + if len(matches) != 3 { return nil, content, nil } - yamlContent := content[4 : end+4] - markdownContent := content[end+9:] + yamlContent := matches[1] + markdownContent := matches[2] metadata = &Metadata{} if err := yaml.Unmarshal([]byte(yamlContent), &metadata); err != nil { |