summaryrefslogtreecommitdiff
path: root/internal/frontmatter/parser.go
blob: 299d4ee4f31cd8a0b5f11f850d4ede7438059984 (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
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"`
}

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
}