From 0c0900cdea12364a25ebf9bb8205a795416e8d6d Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Fri, 1 Aug 2025 16:52:52 +0700 Subject: feat: support of multi-page blog via b2 feat: use markdown + frontmatter for page content feat: add main page with dynamic page lookup feat: add night theme feat: extract timestamp of photo taken from picture name --- internal/frontmatter/parser.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 internal/frontmatter/parser.go (limited to 'internal/frontmatter/parser.go') diff --git a/internal/frontmatter/parser.go b/internal/frontmatter/parser.go new file mode 100644 index 0000000..6cc3ee8 --- /dev/null +++ b/internal/frontmatter/parser.go @@ -0,0 +1,37 @@ +package frontmatter + +import ( + "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"` +} + +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 len(matches) != 3 { + return nil, content, nil + } + + yamlContent := matches[1] + markdownContent := matches[2] + + 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 +} -- cgit v1.3.1+13