75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package note
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Note struct {
|
|
ID string
|
|
Path string
|
|
Title string
|
|
Content string
|
|
Size int64
|
|
// HTMLContent string
|
|
// Directly in Note
|
|
Tags []string
|
|
UpdatedAt time.Time
|
|
Published bool
|
|
}
|
|
|
|
func NewNote() *Note {
|
|
return &Note{}
|
|
}
|
|
|
|
func formatDateRep(date time.Time) string {
|
|
return date.Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
func (n *Note) GetUpdateDateRep() string {
|
|
return formatDateRep(n.UpdatedAt)
|
|
}
|
|
|
|
// 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]
|
|
}
|