diff --git a/core/file_handling/link_parser.go b/core/file_handling/link_parser.go new file mode 100644 index 0000000..47468f6 --- /dev/null +++ b/core/file_handling/link_parser.go @@ -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} +} diff --git a/core/handlers/note/goto_note_handler.go b/core/handlers/note/goto_note_handler.go index 0b51a9a..ee2ed3a 100644 --- a/core/handlers/note/goto_note_handler.go +++ b/core/handlers/note/goto_note_handler.go @@ -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 } diff --git a/dendrite b/dendrite index cc35ace..a42ce71 100755 Binary files a/dendrite and b/dendrite differ