Keep note ownership in frontmatter and shiz
This commit is contained in:
parent
25bcf4d706
commit
03b6bb12ca
6 changed files with 136 additions and 54 deletions
|
|
@ -11,10 +11,12 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
||||
markdown "github.com/teekennedy/goldmark-markdown"
|
||||
"github.com/yuin/goldmark"
|
||||
meta "github.com/yuin/goldmark-meta"
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
astext "github.com/yuin/goldmark/extension/ast"
|
||||
|
|
@ -30,6 +32,66 @@ type Note struct {
|
|||
BodyRendered template.HTML
|
||||
Body []byte
|
||||
Owner string
|
||||
Viewers []string
|
||||
}
|
||||
|
||||
type NoteStore struct {
|
||||
notes map[string][]*Note
|
||||
}
|
||||
|
||||
var (
|
||||
Notes NoteStore
|
||||
md goldmark.Markdown
|
||||
)
|
||||
|
||||
func Init() error {
|
||||
Notes = NoteStore{
|
||||
notes: make(map[string][]*Note),
|
||||
}
|
||||
|
||||
md = goldmark.New(
|
||||
goldmark.WithExtensions(
|
||||
extension.TaskList, meta.Meta,
|
||||
),
|
||||
)
|
||||
|
||||
notesDir := conf.Conf.NotesDir
|
||||
|
||||
files, err := os.ReadDir(notesDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
os.MkdirAll(notesDir, os.FileMode(0750))
|
||||
} else {
|
||||
log.Print(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
log.Printf("Looking in %s", notesDir)
|
||||
for _, f := range files {
|
||||
if !f.IsDir() {
|
||||
encodedTitle := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
|
||||
note, err := LoadNote(encodedTitle)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
title := note.Title
|
||||
|
||||
log.Printf("Found note %s (title '%s', owner %s)", encodedTitle, title, note.Owner)
|
||||
|
||||
Notes.notes[note.Owner] = append(Notes.notes[note.Owner], note)
|
||||
|
||||
for _, viewer := range note.Viewers {
|
||||
Notes.notes[viewer] = append(Notes.notes[viewer], note)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ns *NoteStore) Get(owner string) []*Note {
|
||||
return ns.notes[owner]
|
||||
}
|
||||
|
||||
func fmtPath(path string) string {
|
||||
|
|
@ -38,18 +100,14 @@ func fmtPath(path string) string {
|
|||
|
||||
// Save a note to a path derived from the title
|
||||
func (n *Note) Save() error {
|
||||
filename := filepath.Join(conf.Conf.NotesDir, n.Owner, fmtPath(n.EncodedTitle()))
|
||||
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)
|
||||
|
|
@ -59,18 +117,45 @@ func (n *Note) Render() {
|
|||
}
|
||||
|
||||
// LoadNote from the disk. The path is derived from the title
|
||||
func LoadNote(owner string, encodedTitle string) (*Note, error) {
|
||||
filename := filepath.Join(conf.Conf.NotesDir, owner, fmtPath(encodedTitle))
|
||||
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, Owner: owner}, nil
|
||||
|
||||
var buf bytes.Buffer
|
||||
context := parser.NewContext()
|
||||
if err := md.Convert([]byte(body), &buf, parser.WithContext(context)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
metaData := meta.Get(context)
|
||||
|
||||
log.Printf("note has frontmatter %+v", metaData)
|
||||
owner, ok := metaData["owner"].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("invalid note, missing 'owner' in frontmatter")
|
||||
}
|
||||
|
||||
note := &Note{Title: title, Body: body, Owner: owner}
|
||||
|
||||
viewers := metaData["viewers"].([]interface{})
|
||||
|
||||
for _, viewer := range viewers {
|
||||
v, ok := viewer.(string)
|
||||
if !ok {
|
||||
return nil, errors.New("invalid note, non string type in 'viewers' in frontmatter")
|
||||
}
|
||||
note.Viewers = append(note.Viewers, v)
|
||||
}
|
||||
log.Printf("Note %s shared with %v", note.Title, note.Viewers)
|
||||
|
||||
return note, nil
|
||||
}
|
||||
|
||||
func DeleteNote(owner string, title string) error {
|
||||
filename := filepath.Join(conf.Conf.NotesDir, owner, fmtPath(title))
|
||||
func DeleteNote(title string) error {
|
||||
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(title))
|
||||
return os.Remove(filename)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue