gonotes/internal/notes/notes.go

80 lines
1.9 KiB
Go

// Notes implements a data structure for reasoning about and rendering
// markdown notes
package notes
import (
"bytes"
"encoding/base64"
"fmt"
"html/template"
"log"
"os"
"path/filepath"
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
)
// Note is the central data structure. It can be Saved, Rendered and Loaded
// using the Save, Render and LoadNote functions.
type Note struct {
Title string
BodyRendered template.HTML
Body []byte
}
func fmtPath(path string) string {
return fmt.Sprintf("%s.%s", path, conf.Conf.Extension)
}
// Save a note to a path derived from the title
func (n *Note) Save() error {
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(n.EncodedTitle()))
return os.WriteFile(filename, n.Body, 0600)
}
// Render the markdown content of the note to HTML
func (n *Note) Render() {
var buf bytes.Buffer
md := goldmark.New(
goldmark.WithExtensions(
extension.TaskList,
),
)
err := md.Convert(n.Body, &buf)
if err != nil {
log.Fatal(err)
}
n.BodyRendered = template.HTML(buf.String())
}
// Load a note from the disk. The path is derived from the title
func LoadNote(encodedTitle string) (*Note, error) {
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(encodedTitle))
body, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
title := DecodeTitle(encodedTitle)
return &Note{Title: title, Body: body}, nil
}
func DeleteNote(title string) error {
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(title))
return os.Remove(filename)
}
func (n *Note) EncodedTitle() string {
return base64.StdEncoding.EncodeToString([]byte(n.Title))
}
func DecodeTitle(encodedTitle string) string {
title, err := base64.StdEncoding.DecodeString(encodedTitle)
if err != nil {
log.Printf("Couldn't decode base64 string '%s': %s", encodedTitle, err)
}
return string(title)
}