diff --git a/pkg/mdformatter/linktransformer/link.go b/pkg/mdformatter/linktransformer/link.go index 9ffd16e..aef85dd 100644 --- a/pkg/mdformatter/linktransformer/link.go +++ b/pkg/mdformatter/linktransformer/link.go @@ -31,6 +31,10 @@ var ( IDNotFoundErr = LookupError(errors.New("file exists, but does not have such id")) ) +const ( + originalURLKey = "originalURLKey" +) + type chain struct { chain []mdformatter.LinkTransformer } @@ -150,15 +154,20 @@ func NewValidator(logger log.Logger, except *regexp.Regexp, anchorDir string) (m }); err != nil { return nil, err } + v.c.OnRequest(func(request *colly.Request) { + v.rMu.Lock() + defer v.rMu.Unlock() + request.Ctx.Put(originalURLKey, request.URL.String()) + }) v.c.OnScraped(func(response *colly.Response) { v.rMu.Lock() defer v.rMu.Unlock() - v.remoteLinks[response.Request.URL.String()] = nil + v.remoteLinks[response.Ctx.Get(originalURLKey)] = nil }) v.c.OnError(func(response *colly.Response, err error) { v.rMu.Lock() defer v.rMu.Unlock() - v.remoteLinks[response.Request.URL.String()] = errors.Wrapf(err, "%q not accessible; status code %v", response.Request.URL.String(), response.StatusCode) + v.remoteLinks[response.Ctx.Get(originalURLKey)] = errors.Wrapf(err, "%q not accessible; status code %v", response.Request.URL.String(), response.StatusCode) }) return v, nil } diff --git a/pkg/mdformatter/linktransformer/link_test.go b/pkg/mdformatter/linktransformer/link_test.go index c4e6a6f..0a9395d 100644 --- a/pkg/mdformatter/linktransformer/link_test.go +++ b/pkg/mdformatter/linktransformer/link_test.go @@ -146,6 +146,23 @@ func TestValidator_TransformDestination(t *testing.T) { testutil.Equals(t, 0, len(diff), diff.String()) }) + t.Run("check valid but same link in diff files", func(t *testing.T) { + testFile := filepath.Join(tmpDir, "repo", "docs", "test", "valid-link.md") + testFile2 := filepath.Join(tmpDir, "repo", "docs", "test", "valid-link2.md") + testutil.Ok(t, ioutil.WriteFile(testFile, []byte("https://bwplotka.dev/about\n"), os.ModePerm)) + testutil.Ok(t, ioutil.WriteFile(testFile2, []byte("https://bwplotka.dev/about\n"), os.ModePerm)) + + diff, err := mdformatter.IsFormatted(context.TODO(), logger, []string{testFile, testFile2}) + testutil.Ok(t, err) + testutil.Equals(t, 0, len(diff), diff.String()) + + diff, err = mdformatter.IsFormatted(context.TODO(), logger, []string{testFile, testFile2}, mdformatter.WithLinkTransformer( + MustNewValidator(logger, regexp.MustCompile(`^$`), anchorDir), + )) + testutil.Ok(t, err) + testutil.Equals(t, 0, len(diff), diff.String()) + }) + t.Run("check valid local links", func(t *testing.T) { testFile := filepath.Join(tmpDir, "repo", "docs", "test", "valid-local-links.md") testutil.Ok(t, ioutil.WriteFile(testFile, []byte(`# yolo