diff --git a/examples/markdown.feature b/examples/markdown.feature index 41f7fb0..d21a727 100644 --- a/examples/markdown.feature +++ b/examples/markdown.feature @@ -24,3 +24,20 @@ Feature: Markdown """ When I successfully run `linkcheck foo.md` Then the stdout should contain exactly "" + + Scenario: Check a markdown file which contains a live link + Given a file named "foo.md" with: + """ + [Google](https://google.com) + """ + When I successfully run `linkcheck foo.md` + Then the stdout should contain exactly "" + + Scenario: Check a markdown file which contains a dead link + Given a file named "foo.md" with: + """ + [The answer](https://some-say-the-answer-is-42.com) + """ + When I run `linkcheck foo.md` + Then the exit status should be 1 + And the stderr should contain "ERROR: " diff --git a/main.go b/main.go index 352db4a..dc1baaf 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io/ioutil" + "net/http" "os" "strings" @@ -14,7 +15,7 @@ import ( func main() { defer func() { if r := recover(); r != nil { - fmt.Fprintln(os.Stderr, r.(error).Error()) + printToStderr(r.(error).Error()) os.Exit(1) } }() @@ -27,7 +28,25 @@ func main() { panic(err) } - html.Parse(strings.NewReader(mark.Render(string(bs)))) + n, err := html.Parse(strings.NewReader(mark.Render(string(bs)))) + + if err != nil { + panic(err) + } + + ss := extractUrls(n) + ok := true + + for s, _ := range ss { + if _, err := http.Get(s); err != nil { + printToStderr("ERROR: " + err.Error()) + ok = false + } + } + + if !ok { + os.Exit(1) + } } func extractUrls(n *html.Node) map[string]bool { @@ -71,3 +90,7 @@ Usage: return args } + +func printToStderr(xs ...interface{}) { + fmt.Fprintln(os.Stderr, xs...) +}