draft shits

This commit is contained in:
2026-02-01 11:55:16 +01:00
parent d17ed8c650
commit e27aadc603
22 changed files with 1341 additions and 0 deletions

53
internal/note/note.go Normal file
View File

@ -0,0 +1,53 @@
package note
import (
"crypto/sha256"
"fmt"
"strings"
"time"
)
type Note struct {
ID string
Path string
Title string
Content string
// HTMLContent string
ModTime time.Time
// Directly in Note
Tags []string
CreatedAt time.Time
UpdatedAt time.Time
Published bool
}
func NewNote() *Note {
return &Note{}
}
// ExtractTitle return the first level heading content ('# title')
func ExtractTitle(mkd string) string {
if mkd == "" {
return ""
}
if !strings.HasPrefix(mkd, "# ") {
return ""
}
var title string
for _, c := range strings.TrimLeft(mkd, "# ") {
if strings.Contains("*~", string(c)) {
continue
}
if string(c) == "\n" {
break
}
title = title + string(c)
}
return title
}
func GenerateNoteID(path string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(path)))[:10]
}

View File

@ -0,0 +1,87 @@
package note
import (
"testing"
)
func TestExtractTitle(t *testing.T) {
cases := []struct {
name string
markdown string
want string
}{
{
name: "first h1 becomes title",
markdown: "# My Note\n\nSome content",
want: "My Note",
},
{
name: "no h1 uses filename",
markdown: "## Just h2\n\nContent",
want: "", // or filename from context
},
{
name: "multiple h1s takes first",
markdown: "# First\n\n# Second",
want: "First",
},
{
name: "h1 with formatting",
markdown: "# **Bold** and *italic* title",
want: "Bold and italic title",
},
{
name: "empty file",
markdown: "",
want: "",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got := ExtractTitle(tt.markdown)
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
})
}
}
func TestGenerateNoteID(t *testing.T) {
tests := []struct {
name string
path string
want string // SHA-256 hash
}{
{
name: "consistent hashing",
path: "notes/test.md",
want: "110bab5b8f",
},
{
name: "different paths different ids",
path: "notes/other.md",
want: "858d15849b",
},
{
name: "root folder as 'current folder'",
path: ".",
want: "cdb4ee2aea",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GenerateNoteID(tt.path)
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
// Test consistency
got2 := GenerateNoteID(tt.path)
if got != got2 {
t.Error("same path should produce same ID")
}
})
}
}