diff --git a/internal/collector/collector.go b/internal/collector/collector.go index 35a29d1..cae0fdb 100644 --- a/internal/collector/collector.go +++ b/internal/collector/collector.go @@ -47,8 +47,8 @@ func (c *Collector) CollectMetrics(root fs.FS, collectionTime time.Time) error { } func (c *Collector) collectMetrics(root fs.FS) (metrics.Metrics, error) { - noteCount := 0 - linkCount := 0 + var noteCount uint + var linkCount uint notes := make(map[string]metrics.NoteMetrics) err := fs.WalkDir(root, ".", func(path string, dir fs.DirEntry, err error) error { diff --git a/internal/collector/collector_test.go b/internal/collector/collector_test.go index 06e460f..34b4149 100644 --- a/internal/collector/collector_test.go +++ b/internal/collector/collector_test.go @@ -55,19 +55,19 @@ Link to [one](./one.md) and also a full link [[./dir1/dir2/three]] and a [[./dir LinkCount: 8, Notes: map[string]metrics.NoteMetrics{ "zettel/one.md": { - Links: map[string]int{"./dir1/two.md": 2}, + Links: map[string]uint{"./dir1/two.md": 2}, LinkCount: 2, }, "zettel/dir1/two.md": { - Links: map[string]int{"one": 1}, + Links: map[string]uint{"one": 1}, LinkCount: 1, }, "zettel/dir1/dir2/three.md": { - Links: map[string]int{"one": 1, "two": 1}, + Links: map[string]uint{"one": 1, "two": 1}, LinkCount: 2, }, "zettel/four.md": { - Links: map[string]int{"./one.md": 1, "./dir1/dir2/three": 1, "./dir1/two.md": 1}, + Links: map[string]uint{"./one.md": 1, "./dir1/dir2/three": 1, "./dir1/two.md": 1}, LinkCount: 3, }, }, diff --git a/internal/collector/note.go b/internal/collector/note.go index bebfd64..722c959 100644 --- a/internal/collector/note.go +++ b/internal/collector/note.go @@ -18,19 +18,19 @@ var md = goldmark.New( ) func CollectNoteMetrics(content []byte) metrics.NoteMetrics { + var linkCount uint links := collectLinks(content) - linkCount := 0 for _, v := range links { linkCount += v } return metrics.NoteMetrics{Links: links, LinkCount: linkCount} } -func collectLinks(content []byte) map[string]int { +func collectLinks(content []byte) map[string]uint { linkKinds := []ast.NodeKind{ast.KindLink, wikilink.Kind} reader := text.NewReader(content) root := md.Parser().Parse(reader) - links := make(map[string]int) + links := make(map[string]uint) err := ast.Walk(root, func(n ast.Node, entering bool) (ast.WalkStatus, error) { if entering && slices.Contains(linkKinds, n.Kind()) { var target string diff --git a/internal/collector/note_test.go b/internal/collector/note_test.go index 20f223a..83f4834 100644 --- a/internal/collector/note_test.go +++ b/internal/collector/note_test.go @@ -17,7 +17,7 @@ func TestCollectNoteMetrics(t *testing.T) { name: "empty file", content: "", expected: metrics.NoteMetrics{ - Links: map[string]int{}, + Links: map[string]uint{}, LinkCount: 0, }, }, @@ -25,7 +25,7 @@ func TestCollectNoteMetrics(t *testing.T) { name: "wiki links", content: "[[Link]]aksdjf[[something|another]]\n[[link]]", expected: metrics.NoteMetrics{ - Links: map[string]int{"Link": 1, "something": 1, "link": 1}, + Links: map[string]uint{"Link": 1, "something": 1, "link": 1}, LinkCount: 3, }, }, @@ -33,7 +33,7 @@ func TestCollectNoteMetrics(t *testing.T) { name: "markdown link", content: "[Link](target.md)", expected: metrics.NoteMetrics{ - Links: map[string]int{"target.md": 1}, + Links: map[string]uint{"target.md": 1}, LinkCount: 1, }, }, @@ -41,7 +41,7 @@ func TestCollectNoteMetrics(t *testing.T) { name: "mixed links", content: "okok[Link](target.md)\n**ddk**[[linked]]`test`[[another|link]]\n\n[test](yet-another.md)", expected: metrics.NoteMetrics{ - Links: map[string]int{"target.md": 1, "linked": 1, "another": 1, "yet-another.md": 1}, + Links: map[string]uint{"target.md": 1, "linked": 1, "another": 1, "yet-another.md": 1}, LinkCount: 4, }, }, @@ -49,7 +49,7 @@ func TestCollectNoteMetrics(t *testing.T) { name: "repeated links", content: "[[target.md|link]]\n[link](target.md)\n[[link]]", expected: metrics.NoteMetrics{ - Links: map[string]int{"target.md": 2, "link": 1}, + Links: map[string]uint{"target.md": 2, "link": 1}, LinkCount: 3, }, }, @@ -57,7 +57,7 @@ func TestCollectNoteMetrics(t *testing.T) { name: "ignore embeddedlinks", content: "![[target.png]]\n!()[another.jpeg]\n[[link]]", expected: metrics.NoteMetrics{ - Links: map[string]int{"link": 1}, + Links: map[string]uint{"link": 1}, LinkCount: 1, }, }, diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index b0901f1..71fc36d 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -1,12 +1,12 @@ package metrics type Metrics struct { - NoteCount int - LinkCount int + NoteCount uint + LinkCount uint Notes map[string]NoteMetrics } type NoteMetrics struct { - Links map[string]int - LinkCount int + Links map[string]uint + LinkCount uint }