feat(url_goto): link parsing on url and note

This commit is contained in:
Kristian Borgwarth 2026-04-14 23:15:55 +02:00
parent 0098badafa
commit d6d3b8a095
3 changed files with 53 additions and 22 deletions

View file

@ -0,0 +1,33 @@
package filehandling
import "strings"
type LinkType int
const (
Unknown LinkType = iota
Note
URL
)
type ParsedLink struct {
Kind LinkType
Target string
}
func ParseLink(s string) ParsedLink {
if len(s) >= 5 && strings.HasPrefix(s, "[[") && strings.HasSuffix(s, "]]") {
content := s[2 : len(s)-2]
if before, _, ok := strings.Cut(content, "|"); ok {
return ParsedLink{Note, before}
}
return ParsedLink{Note, content}
}
if strings.HasPrefix(s, "[") {
if close := strings.Index(s, "]("); close >= 0 && strings.HasSuffix(s, ")") {
url := s[close+2 : len(s)-1]
return ParsedLink{URL, url}
}
}
return ParsedLink{Note, s}
}

View file

@ -3,9 +3,9 @@ package note
import (
"context"
"encoding/json"
"log/slog"
"strings"
"errors"
"github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
)
@ -14,7 +14,8 @@ type gotoNoteCommand struct {
}
type gotoNoteResult struct {
Path string `json:"path"`
Target string `json:"target"`
Type string `json:"type,omitempty"`
}
type GotoNoteHandler struct {
@ -31,26 +32,23 @@ func (h *GotoNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any,
return nil, err
}
cmd.Link = h.resolveLink(cmd.Link)
parsedLink := filehandling.ParseLink(cmd.Link)
switch parsedLink.Kind {
note, err := h.noteRepo.GetBySlug(ctx, cmd.Link)
if err != nil {
return nil, err
}
if note == nil {
slog.Debug("Note not found for link", "link", cmd.Link)
return nil, nil
}
return gotoNoteResult{Path: note.Path()}, nil
}
case filehandling.Note:
note, err := h.noteRepo.GetBySlug(ctx, parsedLink.Target)
if err != nil {
return nil, err
}
if note == nil {
return nil, nil
}
return gotoNoteResult{Target: note.Path(), Type: "note"}, nil
func (h *GotoNoteHandler) resolveLink(link string) string {
if len(link) < 5 || link[:2] != "[[" || link[len(link)-2:] != "]]" {
return link
case filehandling.URL:
return gotoNoteResult{Target: parsedLink.Target, Type: "url"}, nil
default:
return nil, errors.New("unsupported link type")
}
content := link[2 : len(link)-2]
if before, _, ok := strings.Cut(content, "|"); ok {
return before
}
return content
}

BIN
dendrite

Binary file not shown.