-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/luissimas/zettelkasten-exporter/internal/collector" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
slog.Error("Wrong arguments") | ||
os.Exit(1) | ||
} | ||
|
||
dir := os.Args[1] | ||
absolute_path, err := filepath.Abs(dir) | ||
if err != nil { | ||
slog.Error("Error getting absolute file path", slog.Any("error", err)) | ||
os.Exit(1) | ||
} | ||
|
||
metrics, err := collector.CollectMetrics(collector.CollectorConfig{Path: absolute_path}) | ||
if err != nil { | ||
slog.Error("Error collecting note metrics", slog.Any("error", err)) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Printf("There are %d markdown files in %s\n", metrics.NoteCount, absolute_path) | ||
fmt.Printf("There is a total of %d links in%s\n", metrics.LinkCount, absolute_path) | ||
for name, metrics := range metrics.Notes { | ||
fmt.Printf("\t%s: %v\n", name, metrics) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package collector | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Metrics struct { | ||
NoteCount int | ||
LinkCount int | ||
Notes map[string]NoteMetrics | ||
} | ||
|
||
type CollectorConfig struct { | ||
Path string | ||
} | ||
|
||
func CollectMetrics(config CollectorConfig) (Metrics, error) { | ||
pattern := filepath.Join(config.Path, "**/*.md") | ||
files, err := filepath.Glob(pattern) | ||
if err != nil { | ||
slog.Error("Error getting files", slog.Any("error", err)) | ||
return Metrics{}, err | ||
} | ||
|
||
noteCount := len(files) | ||
linkCount := 0 | ||
notes := make(map[string]NoteMetrics) | ||
|
||
for _, file := range files { | ||
content, err := os.ReadFile(file) | ||
if err != nil { | ||
slog.Error("Error reading file", slog.Any("error", err)) | ||
continue | ||
} | ||
metrics := CollectNoteMetrics(content) | ||
notes[file] = metrics | ||
linkCount += metrics.LinkCount | ||
} | ||
|
||
return Metrics{NoteCount: noteCount, LinkCount: linkCount, Notes: notes}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package collector | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
type NoteMetrics struct { | ||
LinkCount int | ||
} | ||
|
||
func CollectNoteMetrics(content []byte) NoteMetrics { | ||
return NoteMetrics{LinkCount: collectLinkCount(content)} | ||
} | ||
|
||
func collectLinkCount(content []byte) int { | ||
r, _ := regexp.Compile(`\[\[[^]]+\]\]`) | ||
matches := r.FindAll(content, -1) | ||
return len(matches) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package collector | ||
|
||
import "testing" | ||
|
||
func TestCollectNoteMetrics(t *testing.T) { | ||
data := []struct { | ||
name string | ||
content string | ||
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}}, | ||
{"wikilink dividers", "[[something|another]]\n\n[[link]]\n[[382dlk djfs link|yeah]]", NoteMetrics{LinkCount: 3}}, | ||
} | ||
|
||
for _, d := range data { | ||
t.Run(d.name, func(t *testing.T) { | ||
result := CollectNoteMetrics([]byte(d.content)) | ||
if result != d.expected { | ||
t.Errorf("Expected %v, got %v", d.expected, result) | ||
} | ||
}) | ||
} | ||
} |