From 5e0dbe5873d98cebee509b14ea462e563307f315 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Wed, 25 Mar 2026 20:34:53 +0100 Subject: [PATCH] feat(frontmatter): parsing and tag extract --- core/frontmatter/parser.go | 23 +++++++++++++++++++++++ test/doc.go | 2 -- test/frontmatter_test.go | 25 ++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) delete mode 100644 test/doc.go diff --git a/core/frontmatter/parser.go b/core/frontmatter/parser.go index 2b82de1..ce382e8 100644 --- a/core/frontmatter/parser.go +++ b/core/frontmatter/parser.go @@ -3,10 +3,20 @@ package frontmatter import ( "bufio" "bytes" + "encoding/json" "errors" "io" ) +type FrontMatter struct { + Title string + Tags []string + Created string + Updated string + Author string + +} + func ParseFrontMatter(r io.Reader) (map[string]string, error) { scanner := bufio.NewScanner(r) frontMatter := make(map[string]string) @@ -31,3 +41,16 @@ func ParseFrontMatter(r io.Reader) (map[string]string, error) { return frontMatter, nil } + +func ExtractTags(frontMatter map[string]string) ([]string, error) { + tagsStr, ok := frontMatter["tags"] + if !ok { + return nil, errors.New("tags not found in front matter") + } + var tags []string + err := json.Unmarshal([]byte(tagsStr), &tags) + if err != nil { + return nil, err + } + return tags, nil +} diff --git a/test/doc.go b/test/doc.go deleted file mode 100644 index 361c240..0000000 --- a/test/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package test contains all test files for the project. -package test diff --git a/test/frontmatter_test.go b/test/frontmatter_test.go index 3f84a90..858eb5f 100644 --- a/test/frontmatter_test.go +++ b/test/frontmatter_test.go @@ -1,4 +1,4 @@ -package test +package frontmatter_test import ( "strings" @@ -31,3 +31,26 @@ This is the content of the note.` } } } + +func TestExtractTags(t *testing.T) { + frontMatter := map[string]string{ + "tags": `["test", "note"]`, + } + + expected := []string{"test", "note"} + + result, err := frontmatter.ExtractTags(frontMatter) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if len(result) != len(expected) { + t.Fatalf("expected %d tags, got %d", len(expected), len(result)) + } + + for i, expectedTag := range expected { + if result[i] != expectedTag { + t.Errorf("expected tag %d to be %s, got %s", i, expectedTag, result[i]) + } + } +}