From bed96aee397b036ccbd0b8686540d0de6ecb5d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Simas?= Date: Wed, 26 Jun 2024 06:55:53 -0300 Subject: [PATCH] feat: exclude http links from metrics --- internal/collector/note.go | 11 ++++++++++- internal/collector/note_test.go | 10 +++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/collector/note.go b/internal/collector/note.go index 722c959..c83eb2a 100644 --- a/internal/collector/note.go +++ b/internal/collector/note.go @@ -2,6 +2,7 @@ package collector import ( "log/slog" + "net/url" "slices" "github.com/luissimas/zettelkasten-exporter/internal/metrics" @@ -46,7 +47,10 @@ func collectLinks(content []byte) map[string]uint { return ast.WalkContinue, nil } - // TODO: check if target is not a http link + if isUrl(target) { + return ast.WalkContinue, nil + } + v, ok := links[target] if !ok { links[target] = 0 @@ -61,3 +65,8 @@ func collectLinks(content []byte) map[string]uint { slog.Debug("Collected links", slog.Any("links", links)) return links } + +func isUrl(s string) bool { + u, err := url.Parse(s) + return err == nil && u.Scheme != "" && u.Host != "" +} diff --git a/internal/collector/note_test.go b/internal/collector/note_test.go index 83f4834..abc6821 100644 --- a/internal/collector/note_test.go +++ b/internal/collector/note_test.go @@ -55,12 +55,20 @@ func TestCollectNoteMetrics(t *testing.T) { }, { name: "ignore embeddedlinks", - content: "![[target.png]]\n!()[another.jpeg]\n[[link]]", + content: "![[target.png]]\n![](another.jpeg)\n[[link]]", expected: metrics.NoteMetrics{ Links: map[string]uint{"link": 1}, LinkCount: 1, }, }, + { + name: "ignore http links", + content: "[[one]][this is an http link](https://go.dev/)[[not/an/http/link]]", + expected: metrics.NoteMetrics{ + Links: map[string]uint{"one": 1, "not/an/http/link": 1}, + LinkCount: 1, + }, + }, } for _, d := range data {