Skip to content

Commit

Permalink
feat: parse markdown links
Browse files Browse the repository at this point in the history
  • Loading branch information
luissimas committed May 29, 2024
1 parent 664bbdf commit 8202f76
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
- [X] Expose prometheus metrics endpoint
- [X] Read config
- [X] Find all files recursivelly
- [ ] Configurable ignore file patterns
- [ ] Handle uncreated links
- [X] Parse markdown links
- [ ] Exclude links to non existing files
- [ ] Collect backlinks
- [ ] Parse markdown links
- [ ] Configurable ignore file patterns
- [ ] Get zettelkasten from git url
- [ ] Support private repositories (Maybe with Github's PAT?)

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require (
github.com/gookit/validate v1.5.2
github.com/knadh/koanf v1.5.0
github.com/prometheus/client_golang v1.19.1
github.com/yuin/goldmark v1.7.1
go.abhg.dev/goldmark/wikilink v0.5.0
)

require (
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,13 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U=
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.abhg.dev/goldmark/wikilink v0.5.0 h1:/Gndy7+PoXzOc3reVWtXAh7Cni7wSqSxiuXDfmoYlm4=
go.abhg.dev/goldmark/wikilink v0.5.0/go.mod h1:W1NzvDIpo6uoayolBTCsIL6y/QRAHmLTKfUUDfR75DA=
go.etcd.io/etcd/api/v3 v3.5.4/go.mod h1:5GB2vv4A4AOn3yk7MftYGHkUfGtDHnEraIjym4dYz5A=
go.etcd.io/etcd/client/pkg/v3 v3.5.4/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v3 v3.5.4/go.mod h1:ZaRkVgBZC+L+dLCjTcF1hRXpgZXQPOvnA/Ak/gq3kiY=
Expand Down
27 changes: 23 additions & 4 deletions internal/collector/note.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package collector

import (
"regexp"
"slices"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"go.abhg.dev/goldmark/wikilink"
)

var md = goldmark.New(
goldmark.WithExtensions(
&wikilink.Extender{},
),
)

type NoteMetrics struct {
Expand All @@ -13,7 +24,15 @@ func CollectNoteMetrics(content []byte) NoteMetrics {
}

func collectLinkCount(content []byte) int {
r, _ := regexp.Compile(`\[\[[^]]+\]\]`)
matches := r.FindAll(content, -1)
return len(matches)
linkKinds := []ast.NodeKind{ast.KindLink, wikilink.Kind}
reader := text.NewReader(content)
root := md.Parser().Parse(reader)
linkCount := 0
ast.Walk(root, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering && slices.Contains(linkKinds, n.Kind()) {
linkCount += 1
}
return ast.WalkContinue, nil
})
return linkCount
}
6 changes: 4 additions & 2 deletions internal/collector/note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ func TestCollectNoteMetrics(t *testing.T) {
expected NoteMetrics
}{
{"empty file", "", NoteMetrics{LinkCount: 0}},
{"file with a single link", "[[Link]]", NoteMetrics{LinkCount: 1}},
{"file with multiple links", "[[Link]]aksdjf[[anotherlink]]\n[[link]]", NoteMetrics{LinkCount: 3}},
{"file with a single wiki link", "[[Link]]", NoteMetrics{LinkCount: 1}},
{"file with multiple wiki links", "[[Link]]aksdjf[[anotherlink]]\n[[link]]", NoteMetrics{LinkCount: 3}},
{"wikilink dividers", "[[something|another]]\n\n[[link]]\n[[382dlk djfs link|yeah]]", NoteMetrics{LinkCount: 3}},
{"file with markdown link", "[Link](target.md)", NoteMetrics{LinkCount: 1}},
{"file with multiple links", "okok[Link](target.md)\n**ddk**[[linked]]`test`[[anothe|link]]\n\n[test](yet-another.md)", NoteMetrics{LinkCount: 4}},
}

for _, d := range data {
Expand Down

0 comments on commit 8202f76

Please sign in to comment.