From 6fb27781f9e6a992111fce91c8ffe67b496097e2 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Mon, 6 Apr 2026 22:41:46 +0200 Subject: [PATCH 1/4] ref(file_handling): consolidate modules related to file content manipulation --- core/file_handling/doc.go | 2 ++ .../parser.go => file_handling/frontmatter_parser.go} | 2 +- core/{frontmatter => file_handling}/slug.go | 2 +- core/{template => file_handling}/templater.go | 2 +- core/frontmatter/doc.go | 2 -- core/handlers/create_note_handler.go | 6 +++--- core/template/doc.go | 2 -- 7 files changed, 8 insertions(+), 10 deletions(-) create mode 100644 core/file_handling/doc.go rename core/{frontmatter/parser.go => file_handling/frontmatter_parser.go} (97%) rename core/{frontmatter => file_handling}/slug.go (93%) rename core/{template => file_handling}/templater.go (97%) delete mode 100644 core/frontmatter/doc.go delete mode 100644 core/template/doc.go diff --git a/core/file_handling/doc.go b/core/file_handling/doc.go new file mode 100644 index 0000000..f5f2eff --- /dev/null +++ b/core/file_handling/doc.go @@ -0,0 +1,2 @@ +// Package filehandling provides utilities for handling files, including reading, writing, and manipulating file contents. +package filehandling diff --git a/core/frontmatter/parser.go b/core/file_handling/frontmatter_parser.go similarity index 97% rename from core/frontmatter/parser.go rename to core/file_handling/frontmatter_parser.go index 9bcb523..1216b36 100644 --- a/core/frontmatter/parser.go +++ b/core/file_handling/frontmatter_parser.go @@ -1,4 +1,4 @@ -package frontmatter +package filehandling import ( "bytes" diff --git a/core/frontmatter/slug.go b/core/file_handling/slug.go similarity index 93% rename from core/frontmatter/slug.go rename to core/file_handling/slug.go index 894b782..11676f4 100644 --- a/core/frontmatter/slug.go +++ b/core/file_handling/slug.go @@ -1,4 +1,4 @@ -package frontmatter +package filehandling import ( "bytes" diff --git a/core/template/templater.go b/core/file_handling/templater.go similarity index 97% rename from core/template/templater.go rename to core/file_handling/templater.go index aee4505..c5aab0c 100644 --- a/core/template/templater.go +++ b/core/file_handling/templater.go @@ -1,4 +1,4 @@ -package template +package filehandling import ( "os" diff --git a/core/frontmatter/doc.go b/core/frontmatter/doc.go deleted file mode 100644 index b723de9..0000000 --- a/core/frontmatter/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package frontmatter contains utilities for parsing front matter from documents. -package frontmatter diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 35d049a..416d818 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -5,7 +5,7 @@ import ( "encoding/json" "path/filepath" - "github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter" + filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling" "github.com/KristianJBorgwarth/dendrite.daemon/core/models" "github.com/KristianJBorgwarth/dendrite.daemon/core/template" "github.com/KristianJBorgwarth/dendrite.daemon/core/utils" @@ -34,7 +34,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } - slug := frontmatter.Slugify(cmd.Title) + slug := filehandling.Slugify(cmd.Title) var templatePath string if cmd.TemplateName != "" { @@ -48,7 +48,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } - tags, err := frontmatter.ParseTags(data) + tags, err := filehandling.ParseTags(data) if err != nil { return nil, err } diff --git a/core/template/doc.go b/core/template/doc.go deleted file mode 100644 index 66c2031..0000000 --- a/core/template/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package template provides implementations for generating notes from templates -package template From aa9232fd07937f2c874e24f54b2782598d2b6f49 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Mon, 6 Apr 2026 22:43:34 +0200 Subject: [PATCH 2/4] chore: remove debug logging --- core/file_handling/frontmatter_parser.go | 5 +---- core/handlers/create_note_handler.go | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/core/file_handling/frontmatter_parser.go b/core/file_handling/frontmatter_parser.go index 1216b36..907725d 100644 --- a/core/file_handling/frontmatter_parser.go +++ b/core/file_handling/frontmatter_parser.go @@ -1,9 +1,8 @@ -package filehandling +package filehandling import ( "bytes" "errors" - "log/slog" "gopkg.in/yaml.v3" ) @@ -21,7 +20,6 @@ func ParseTags(file []byte) ([]string, error) { if err != nil { return nil, err } - slog.Debug("parsed front matter", "fm", fm) return fm.Tags, nil } @@ -36,7 +34,6 @@ func parseFrontMatter(file []byte) (*FrontMatter, error) { if idx := bytes.Index(content, []byte("---")); idx != -1 { content = bytes.TrimSpace(content[:idx]) } - slog.Debug("yaml content to parse", "content", string(content)) var fm FrontMatter if err := yaml.Unmarshal(content, &fm); err != nil { diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 416d818..1afb76b 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -7,7 +7,6 @@ import ( filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling" "github.com/KristianJBorgwarth/dendrite.daemon/core/models" - "github.com/KristianJBorgwarth/dendrite.daemon/core/template" "github.com/KristianJBorgwarth/dendrite.daemon/core/utils" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/store" @@ -43,7 +42,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an notePath := filepath.Join(store.GetVaultStore().Config.VaultPath(), cmd.Directory, slug+".md") - data, err := template.RenderTemplate(templatePath, cmd.Title, slug) + data, err := filehandling.RenderTemplate(templatePath, cmd.Title, slug) if err != nil { return nil, err } From a2a6c2743c90753e3e03248fd4b3a07490b2cbd1 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Mon, 6 Apr 2026 23:04:17 +0200 Subject: [PATCH 3/4] ref(file_handling): moved template logic out of handler --- core/file_handling/frontmatter_parser.go | 10 +----- .../{templater.go => template.go} | 35 ++++++++++++++++++- core/handlers/create_note_handler.go | 24 ++++--------- makefile | 4 +-- test/test_unit/frontmatter_test.go | 32 ----------------- 5 files changed, 43 insertions(+), 62 deletions(-) rename core/file_handling/{templater.go => template.go} (50%) delete mode 100644 test/test_unit/frontmatter_test.go diff --git a/core/file_handling/frontmatter_parser.go b/core/file_handling/frontmatter_parser.go index 907725d..571c33e 100644 --- a/core/file_handling/frontmatter_parser.go +++ b/core/file_handling/frontmatter_parser.go @@ -15,15 +15,7 @@ type FrontMatter struct { Author string `yaml:"author"` } -func ParseTags(file []byte) ([]string, error) { - fm, err := parseFrontMatter(file) - if err != nil { - return nil, err - } - return fm.Tags, nil -} - -func parseFrontMatter(file []byte) (*FrontMatter, error) { +func ParseFrontMatter(file []byte) (*FrontMatter, error) { content := bytes.TrimSpace(file) if bytes.HasPrefix(content, []byte("---")) { content = bytes.TrimSpace(content[3:]) diff --git a/core/file_handling/templater.go b/core/file_handling/template.go similarity index 50% rename from core/file_handling/templater.go rename to core/file_handling/template.go index c5aab0c..e8a5ac3 100644 --- a/core/file_handling/templater.go +++ b/core/file_handling/template.go @@ -4,9 +4,42 @@ import ( "os" "strings" "time" + + "github.com/KristianJBorgwarth/dendrite.daemon/persistence/store" ) -func RenderTemplate(templatePath string, title string, slug string) ([]byte, error) { +type Template struct { + Content []byte + Title string + Slug string + FrontMatter *FrontMatter +} + +func NewTemplate(templateName string, title string) (*Template, error) { + var templatePath string + if templateName != "" { + templatePath = store.GetVaultStore().GetTemplatePath(templateName) + } + slug := Slugify(title) + content, err := renderTemplate(templatePath, title, slug) + if err != nil { + return nil, err + } + + fm, err := ParseFrontMatter(content) + if err != nil { + return nil, err + } + + return &Template{ + Content: content, + Title: title, + Slug: slug, + FrontMatter: fm, + }, nil +} + +func renderTemplate(templatePath string, title string, slug string) ([]byte, error) { template, err := readTemplate(templatePath, title) if err != nil { return nil, err diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 1afb76b..ca8d8ce 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -33,24 +33,12 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } - slug := filehandling.Slugify(cmd.Title) - - var templatePath string - if cmd.TemplateName != "" { - templatePath = store.GetVaultStore().GetTemplatePath(cmd.TemplateName) - } - - notePath := filepath.Join(store.GetVaultStore().Config.VaultPath(), cmd.Directory, slug+".md") - - data, err := filehandling.RenderTemplate(templatePath, cmd.Title, slug) + template, err := filehandling.NewTemplate(cmd.TemplateName, cmd.Title) if err != nil { return nil, err } - tags, err := filehandling.ParseTags(data) - if err != nil { - return nil, err - } + notePath := filepath.Join(store.GetVaultStore().Config.VaultPath(), cmd.Directory, template.Slug+".md") tx, err := h.uow.Begin() if err != nil { @@ -62,12 +50,12 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an tagRepo := repositories.NewTagRepository(tx) noteRepo := repositories.NewNoteRepository(tx) - dbTags, err := tagRepo.GetByNames(ctx, tags) + dbTags, err := tagRepo.GetByNames(ctx, template.FrontMatter.Tags) if err != nil { return nil, err } - newTags := utils.Filter(tags, func(name string) bool { + newTags := utils.Filter(template.FrontMatter.Tags, func(name string) bool { for _, t := range dbTags { if t.Name() == name { return false @@ -87,7 +75,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an tagModels = append(tagModels, dbTags...) - note := models.CreateNote(notePath, cmd.Title, slug) + note := models.CreateNote(notePath, cmd.Title, template.Slug) if err = noteRepo.Upsert(ctx, note); err != nil { return nil, err @@ -97,7 +85,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } - h.uow.FileStore.Stage(notePath, data) + h.uow.FileStore.Stage(notePath, template.Content) if err = h.uow.Commit(); err != nil { return nil, err diff --git a/makefile b/makefile index 66a61a1..c2eff9a 100644 --- a/makefile +++ b/makefile @@ -2,8 +2,8 @@ test: test-unit test-integration -test-unit: - go test ./test/test_unit/... +#test-unit: + #go test ./test/test_unit/... test-integration: go test ./test/test_integration/... diff --git a/test/test_unit/frontmatter_test.go b/test/test_unit/frontmatter_test.go deleted file mode 100644 index b926e96..0000000 --- a/test/test_unit/frontmatter_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package frontmatter_test - -import ( - "testing" - - "github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestParseTags_ValidFrontMatter_ReturnsTags(t *testing.T) { - input := `--- -title: My Note -tags: ["test", "note"] ---- -This is the content of the note.` - - result, err := frontmatter.ParseTags([]byte(input)) - - require.NoError(t, err) - assert.Equal(t, []string{"test", "note"}, result) -} - -func TestParseTags_MissingDelimiter_ReturnsError(t *testing.T) { - input := `title: My Note -tags: ["test", "note"] -This is the content of the note.` - - _, err := frontmatter.ParseTags([]byte(input)) - - assert.ErrorContains(t, err, "missing front matter delimiter") -} From d4c922ba53c449cb5d8f01738bf82e920c278c55 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Mon, 6 Apr 2026 23:09:52 +0200 Subject: [PATCH 4/4] fix(unit_test): frontmatter parse test --- makefile | 4 ++-- test/test_unit/frontmatter_test.go | 33 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/test_unit/frontmatter_test.go diff --git a/makefile b/makefile index c2eff9a..66a61a1 100644 --- a/makefile +++ b/makefile @@ -2,8 +2,8 @@ test: test-unit test-integration -#test-unit: - #go test ./test/test_unit/... +test-unit: + go test ./test/test_unit/... test-integration: go test ./test/test_integration/... diff --git a/test/test_unit/frontmatter_test.go b/test/test_unit/frontmatter_test.go new file mode 100644 index 0000000..5056cc6 --- /dev/null +++ b/test/test_unit/frontmatter_test.go @@ -0,0 +1,33 @@ +package filehandling + +import ( + "testing" + + filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestParseTags_ValidFrontMatter_ReturnsTags(t *testing.T) { + input := `--- +title: My Note +tags: ["test", "note"] +--- +This is the content of the note.` + + result, err := filehandling.ParseFrontMatter([]byte(input)) + + require.NoError(t, err) + assert.Equal(t, "My Note", result.Title) + assert.Equal(t, []string{"test", "note"}, result.Tags) +} + +func TestParseTags_MissingDelimiter_ReturnsError(t *testing.T) { + input := `title: My Note +tags: ["test", "note"] +This is the content of the note.` + + _, err := filehandling.ParseFrontMatter([]byte(input)) + + assert.ErrorContains(t, err, "missing front matter delimiter") +}