Diffstat (limited to 'internal/frontmatter/parser.go')
| -rw-r--r-- | internal/frontmatter/parser.go | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/internal/frontmatter/parser.go b/internal/frontmatter/parser.go index 299d4ee..6cc3ee8 100644 --- a/internal/frontmatter/parser.go +++ b/internal/frontmatter/parser.go @@ -1,8 +1,8 @@ package frontmatter import ( - "bytes" "fmt" + "regexp" "time" "gopkg.in/yaml.v3" @@ -15,23 +15,18 @@ type Metadata struct { 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 { |