Diffstat (limited to 'internal/frontmatter/parser.go')
| -rw-r--r-- | internal/frontmatter/parser.go | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/internal/frontmatter/parser.go b/internal/frontmatter/parser.go index 3c00a7f..942760c 100644 --- a/internal/frontmatter/parser.go +++ b/internal/frontmatter/parser.go @@ -1,34 +1,38 @@ 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"` - Timezone string `yaml:"timezone"` + 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"` } func ParseFrontmatter(content []byte) (metadata *Metadata, markdown []byte, err error) { - frontmatterRegex := regexp.MustCompile(`^---\s*\r?\n([\s\S]*?)\r?\n---\s*\r?\n([\s\S]*)$`) - matches := frontmatterRegex.FindSubmatch(content) + if !bytes.HasPrefix(content, []byte("---\n")) { + return nil, content, nil + } - if len(matches) != 3 { + end := bytes.Index(content[4:], []byte("\n---\n")) + if end == -1 { return nil, content, nil } - yamlContent := matches[1] - markdownContent := matches[2] + yamlContent := content[4 : end+4] + markdownContent := content[end+9:] metadata = &Metadata{} if err := yaml.Unmarshal([]byte(yamlContent), &metadata); err != nil { |