Files
donniemarko/internal/note/note.go
2026-02-01 16:55:40 +01:00

72 lines
1.5 KiB
Go

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{}
}
func (n *Note) GetUpdateDateRep() string {
return n.UpdatedAt.Format("2006-01-02 15:04:05")
}
// ExtractTitle return the first level heading content ('# title')
func ExtractTitle(mkd string) string {
if mkd == "" {
return ""
}
lines := strings.Split(mkd, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "# ") {
// Extract title from # heading
title := strings.TrimPrefix(line, "# ")
title = strings.TrimSpace(title)
// Remove common markdown formatting
title = removeMarkdownFormatting(title)
return title
}
}
return ""
}
// removeMarkdownFormatting removes common markdown formatting from text
func removeMarkdownFormatting(text string) string {
// Remove **bold** and *italic* formatting
result := text
result = strings.ReplaceAll(result, "**", "")
result = strings.ReplaceAll(result, "*", "")
result = strings.ReplaceAll(result, "_", "")
result = strings.ReplaceAll(result, "`", "")
result = strings.ReplaceAll(result, "~~", "")
// Clean up multiple spaces
result = strings.Join(strings.Fields(result), " ")
return result
}
func GenerateNoteID(path string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(path)))[:10]
}