summaryrefslogtreecommitdiffci
path: root/internal/frontmatter/parser.go
diff refs
from:
to:
flip
diff options
context:
space:
mode:
Diffstat (limited to 'internal/frontmatter/parser.go')
-rw-r--r--internal/frontmatter/parser.go31
1 files changed, 13 insertions, 18 deletions
diff --git a/internal/frontmatter/parser.go b/internal/frontmatter/parser.go
index 942760c..1a521c0 100644
--- a/internal/frontmatter/parser.go
+++ b/internal/frontmatter/parser.go
@@ -1,38 +1,33 @@
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"`
}
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 {