summaryrefslogtreecommitdiffci
path: root/internal/frontmatter/parser.go
blob: 942760cc98a215ca2a5dcb9dcbd8df31bc2c8580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package frontmatter

import (
	"bytes"
	"fmt"
	"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"`
}

func ParseFrontmatter(content []byte) (metadata *Metadata, markdown []byte, err error) {
	if !bytes.HasPrefix(content, []byte("---\n")) {
		return nil, content, nil
	}

	end := bytes.Index(content[4:], []byte("\n---\n"))
	if end == -1 {
		return nil, content, nil
	}

	yamlContent := content[4 : end+4]
	markdownContent := content[end+9:]

	metadata = &Metadata{}
	if err := yaml.Unmarshal([]byte(yamlContent), &metadata); err != nil {
		return nil, nil, fmt.Errorf("failed to parse YAML frontmatter: %w", err)
	}

	return metadata, markdownContent, nil
}