Skip to content

Commit

Permalink
feat: exclude http links from metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
luissimas committed Jun 26, 2024
1 parent fb379d4 commit c1b74d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion internal/collector/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collector

import (
"log/slog"
"net/url"
"slices"

"github.com/luissimas/zettelkasten-exporter/internal/metrics"
Expand Down Expand Up @@ -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
Expand All @@ -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 != ""
}
10 changes: 9 additions & 1 deletion internal/collector/note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c1b74d5

Please sign in to comment.