summaryrefslogtreecommitdiff
path: root/internal/links
diff options
context:
space:
mode:
authordemo <demo@antix1>2026-05-10 11:57:56 -0400
committerdemo <demo@antix1>2026-05-10 11:57:56 -0400
commit2735a0388e96fd4d09201ae5451e1a972ef2130a (patch)
tree799c4275d0622deb5f4bddd282726c4a4e7af042 /internal/links
parentd4d5f28c889aca0654b52b8af5960ac274af828e (diff)
feat: implement find for finding links
Diffstat (limited to 'internal/links')
-rw-r--r--internal/links/find.go35
-rw-r--r--internal/links/find_test.go34
2 files changed, 69 insertions, 0 deletions
diff --git a/internal/links/find.go b/internal/links/find.go
new file mode 100644
index 0000000..33b24d5
--- /dev/null
+++ b/internal/links/find.go
@@ -0,0 +1,35 @@
+package links
+
+import (
+ "io"
+
+ "golang.org/x/net/html"
+ "golang.org/x/net/html/atom"
+)
+
+func find(htmlInput io.Reader) ([]string, error) {
+ doc, err := html.Parse(htmlInput)
+ if err != nil {
+ return nil, err
+ }
+
+ hrefs := findHrefs(doc)
+
+ return hrefs, nil
+}
+
+// findHrefs returns all link addresses inside doc.
+func findHrefs(doc *html.Node) []string {
+ var hrefs []string
+ for node := range doc.Descendants() {
+ if node.Type == html.ElementNode && node.DataAtom == atom.A {
+ for _, attr := range node.Attr {
+ if attr.Key == "href" {
+ hrefs = append(hrefs, attr.Val)
+ }
+ }
+ }
+ }
+
+ return hrefs
+}
diff --git a/internal/links/find_test.go b/internal/links/find_test.go
new file mode 100644
index 0000000..a4c952e
--- /dev/null
+++ b/internal/links/find_test.go
@@ -0,0 +1,34 @@
+package links
+
+import (
+ "strings"
+ "testing"
+)
+
+const (
+ ex1 = `
+<html>
+ <head>
+ <title>Ex 1</title>
+ <head>
+ <body>
+ <a href="https://example.com/">Example Page</a>
+ <a href="/posts">Posts</a>
+ </body>
+</html>
+`
+)
+
+func TestFindCountHrefs(t *testing.T) {
+ r := strings.NewReader(ex1)
+ hrefs, err := find(r)
+ if err != nil {
+ t.Error(err)
+ }
+
+ const expectedLen = 2
+
+ if actualLen := len(hrefs); actualLen != expectedLen {
+ t.Errorf("got %d, want %d", actualLen, expectedLen)
+ }
+}