Browse Source

Add extractUrls function

renovate/configure
Yota Toyama 8 years ago
parent
commit
b0b5ed0d44
  1. 27
      main.go
  2. 41
      main_test.go

27
main.go

@ -30,6 +30,33 @@ func main() {
html.Parse(strings.NewReader(mark.Render(string(bs)))) html.Parse(strings.NewReader(mark.Render(string(bs))))
} }
func extractUrls(n *html.Node) map[string]bool {
ss := make(map[string]bool)
ns := make([]*html.Node, 0, 1024)
ns = append(ns, n)
for len(ns) > 0 {
i := len(ns) - 1
n := ns[i]
ns = ns[:i]
if n.Type == html.ElementNode && n.Data == "a" {
for _, a := range n.Attr {
if a.Key == "href" {
ss[a.Val] = true
break
}
}
}
for n := n.FirstChild; n != nil; n = n.NextSibling {
ns = append(ns, n)
}
}
return ss
}
func getArgs() map[string]interface{} { func getArgs() map[string]interface{} {
usage := `Link checker for Markdown and HTML usage := `Link checker for Markdown and HTML

41
main_test.go

@ -0,0 +1,41 @@
package main
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/net/html"
)
func TestExtractUrls(t *testing.T) {
for _, c := range []struct {
html string
numUrls int
}{
{`<a href="https://google.com">Google</a>`, 1},
{
`
<div>
<a href="https://google.com">Google</a>
<a href="https://google.com">Google</a>
</div>
`,
1,
},
{
`
<div>
<a href="https://google.com">Google</a>
<a href="https://yahoo.com">Yahoo!</a>
</div>
`,
2,
},
} {
n, err := html.Parse(strings.NewReader(c.html))
assert.Equal(t, nil, err)
assert.Equal(t, c.numUrls, len(extractUrls(n)))
}
}
Loading…
Cancel
Save