summaryrefslogtreecommitdiff
path: root/internal/frontmatter/parser.go
diff options
from:
to:
context:
space:
mode:
authorGravatar SayaAndy <montferrat@tuta.io> 2025-07-30 20:58:44 +0700
committerGravatar SayaAndy <montferrat@tuta.io> 2025-07-30 20:58:44 +0700
commit924c717ed86c9f6fa23973d8b36cc2b3904ebb4c (patch)
tree03345edbadc6426961b4501568315601d6114e30 /internal/frontmatter/parser.go
parent68d671127bd03c019081599d069810a81f871095 (diff)
downloadarticlator-924c717ed86c9f6fa23973d8b36cc2b3904ebb4c.tar.gz
articlator-924c717ed86c9f6fa23973d8b36cc2b3904ebb4c.zip
feat: basic metadata extractor
Diffstat (limited to 'internal/frontmatter/parser.go')
-rw-r--r--internal/frontmatter/parser.go37
1 files changed, 37 insertions, 0 deletions
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
+}