Treat image labels as Markdown alternative text
This commit is contained in:
@@ -82,6 +82,8 @@ func parseMarkdown(source []byte) *markdownDocument {
|
||||
Reference: node.Reference != nil,
|
||||
Location: markdownLocationAt(lineStarts, node.Pos()),
|
||||
})
|
||||
// Image labels contribute alternative text, not navigable destinations.
|
||||
return ast.WalkSkipChildren, nil
|
||||
case *ast.AutoLink:
|
||||
document.links = append(document.links, markdownLink{
|
||||
Destination: node.Destination.Value(source),
|
||||
|
||||
@@ -46,6 +46,39 @@ func TestCheckMarkdownLinksResolvesCommittedDestinationsAndFragments(t *testing.
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckMarkdownLinksTreatsImageLabelsAsAlternativeText(t *testing.T) {
|
||||
_, tree := committedMarkdownTree(t, map[string][]byte{
|
||||
"guide.md": []byte("" +
|
||||
"](image.png)\n" +
|
||||
"](image.png)\n" +
|
||||
"](image.png)\n" +
|
||||
"](image.png)\n" +
|
||||
"[](target.md#present)\n" +
|
||||
"[](absent.md)\n"),
|
||||
"image.png": []byte("image"),
|
||||
"target.md": []byte("# Present\n"),
|
||||
})
|
||||
for _, enforceBoundary := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("boundary=%t", enforceBoundary), func(t *testing.T) {
|
||||
err := checkMarkdownLinks(context.Background(), tree, enforceBoundary, nil)
|
||||
var failure *Failure
|
||||
if !errors.As(err, &failure) || failure.Check != MarkdownLinks || len(failure.Findings) != 2 {
|
||||
t.Fatalf("error = %v, want only the missing linked-image source and link target", err)
|
||||
}
|
||||
want := map[string]bool{"absent.png": true, "absent.md": true}
|
||||
for _, finding := range failure.Findings {
|
||||
if !want[finding.Target] {
|
||||
t.Errorf("unexpected destination finding: %#v", finding)
|
||||
}
|
||||
delete(want, finding.Target)
|
||||
}
|
||||
if len(want) != 0 {
|
||||
t.Errorf("missing findings for real destinations: %v", want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckMarkdownLinksReportsMissingPathsAnchorsAndCycles(t *testing.T) {
|
||||
_, tree := committedMarkdownTree(t, map[string][]byte{
|
||||
"guide.md": []byte("" +
|
||||
|
||||
Reference in New Issue
Block a user