From 274234e608694fdde8234b9736d656429852c275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Sat, 19 Jun 2021 16:23:22 +0200 Subject: [PATCH] Fix links with section anchors (#51) --- CHANGELOG.md | 7 ++++++- internal/adapter/sqlite/note_dao.go | 4 ++++ internal/adapter/sqlite/note_dao_test.go | 4 ++-- internal/core/notebook.go | 4 ++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25c250f..f7345d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,12 @@ All notable changes to this project will be documented in this file. - +## Unreleased + +### Fixed + +* [#16](https://github.com/mickael-menu/zk/issues/16) Links with section anchors, e.g. `[[filename#section]]`. + ## 0.5.0 diff --git a/internal/adapter/sqlite/note_dao.go b/internal/adapter/sqlite/note_dao.go index df2c4d9..2666694 100644 --- a/internal/adapter/sqlite/note_dao.go +++ b/internal/adapter/sqlite/note_dao.go @@ -290,6 +290,10 @@ func (d *NoteDAO) findIdsByPathPrefixes(paths []string) ([]core.NoteID, error) { } func (d *NoteDAO) findIdByPathPrefix(path string) (core.NoteID, error) { + // Remove any anchor at the end of the HREF, since it's most likely + // matching a sub-section in the note. + path = strings.SplitN(path, "#", 2)[0] + row, err := d.findIdByPathPrefixStmt.QueryRow(path) if err != nil { return 0, err diff --git a/internal/adapter/sqlite/note_dao_test.go b/internal/adapter/sqlite/note_dao_test.go index e80d475..d1ccc85 100644 --- a/internal/adapter/sqlite/note_dao_test.go +++ b/internal/adapter/sqlite/note_dao_test.go @@ -154,7 +154,7 @@ func TestNoteDAOAddWithLinks(t *testing.T) { }, { Title: "Second is added", - Href: "f39c8", + Href: "f39c8#anchor", Rels: core.LinkRels("second"), }, { @@ -194,7 +194,7 @@ func TestNoteDAOAddWithLinks(t *testing.T) { SourceId: id, TargetId: idPointer(4), Title: "Second is added", - Href: "f39c8", + Href: "f39c8#anchor", Rels: "\x01second\x01", }, { diff --git a/internal/core/notebook.go b/internal/core/notebook.go index 917d845..3b28c04 100644 --- a/internal/core/notebook.go +++ b/internal/core/notebook.go @@ -175,6 +175,10 @@ func (n *Notebook) FindMinimalNotes(opts NoteFindOpts) ([]MinimalNote, error) { // FindByHref retrieves the first note matching the given link href. func (n *Notebook) FindByHref(href string) (*MinimalNote, error) { + // Remove any anchor at the end of the HREF, since it's most likely + // matching a sub-section in the note. + href = strings.SplitN(href, "#", 2)[0] + notes, err := n.FindMinimalNotes(NoteFindOpts{ IncludePaths: []string{href}, Limit: 1,