Add body endpoint for reading/writing note content

This commit is contained in:
Levi Neely 2026-03-20 16:58:28 +01:00
parent 7b32240415
commit def12a01cc
1 changed files with 46 additions and 2 deletions

View File

@ -74,7 +74,7 @@ const (
qidDir = 999994 qidDir = 999994
) )
var fileNames = []string{"path", "title", "keywords", "signature", "ctl", "backlinks"} var fileNames = []string{"path", "title", "keywords", "signature", "ctl", "backlinks", "body"}
// Callbacks for note operations // Callbacks for note operations
type Callbacks struct { type Callbacks struct {
@ -699,6 +699,10 @@ func (s *server) dispatchWrite(f *fid, tag uint16) *plan9.Fcall {
s.callbacks.OnRename(noteID) s.callbacks.OnRename(noteID)
} }
} }
case "body":
if err := s.writeBody(note.Path, input); err != nil {
return errorFcall(fc, err.Error())
}
default: default:
return errorFcall(fc, "field is read-only") return errorFcall(fc, "field is read-only")
} }
@ -841,7 +845,7 @@ func (s *server) readDir(path string, offset int64, count uint32) []byte {
for i, fname := range fileNames { for i, fname := range fileNames {
content := s.getFileContent(path + "/" + fname) content := s.getFileContent(path + "/" + fname)
mode := uint32(0444) // read-only by default mode := uint32(0444) // read-only by default
if fname == "title" || fname == "keywords" || fname == "path" { if fname == "title" || fname == "keywords" || fname == "path" || fname == "body" {
mode = 0644 // writable mode = 0644 // writable
} else if fname == "ctl" { } else if fname == "ctl" {
mode = 0200 // write-only mode = 0200 // write-only
@ -984,10 +988,50 @@ func (s *server) getFileContent(path string) string {
return note.Signature return note.Signature
case "backlinks": case "backlinks":
return s.getBacklinks(noteID) return s.getBacklinks(noteID)
case "body":
return s.readBody(note.Path)
} }
return "" return ""
} }
// readBody reads the file at path and returns content after frontmatter
func (s *server) readBody(path string) string {
if path == "" {
return ""
}
data, err := os.ReadFile(path)
if err != nil {
return ""
}
content := string(data)
// Skip frontmatter (between --- markers)
if strings.HasPrefix(content, "---\n") {
if end := strings.Index(content[4:], "\n---\n"); end != -1 {
return content[4+end+5:]
}
}
return content
}
// writeBody writes content to file, preserving frontmatter
func (s *server) writeBody(path, body string) error {
if path == "" {
return fmt.Errorf("no path")
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
content := string(data)
var frontmatter string
if strings.HasPrefix(content, "---\n") {
if end := strings.Index(content[4:], "\n---\n"); end != -1 {
frontmatter = content[:4+end+5]
}
}
return os.WriteFile(path, []byte(frontmatter+body), 0644)
}
func errorFcall(fc *plan9.Fcall, msg string) *plan9.Fcall { func errorFcall(fc *plan9.Fcall, msg string) *plan9.Fcall {
return &plan9.Fcall{ return &plan9.Fcall{
Type: plan9.Rerror, Type: plan9.Rerror,