Treat image labels as Markdown alternative text

This commit is contained in:
2026-10-08 01:32:29 +00:00
parent 95599e96f6
commit 2e0d302178
4 changed files with 46 additions and 2 deletions

View File

@@ -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),

View File

@@ -46,6 +46,39 @@ func TestCheckMarkdownLinksResolvesCommittedDestinationsAndFragments(t *testing.
}
}
func TestCheckMarkdownLinksTreatsImageLabelsAsAlternativeText(t *testing.T) {
_, tree := committedMarkdownTree(t, map[string][]byte{
"guide.md": []byte("" +
"![![inner](missing.png)](image.png)\n" +
"![[label](missing.md)](image.png)\n" +
"![[anchor](target.md#absent)](image.png)\n" +
"![[outside](../outside.md)](image.png)\n" +
"[![logo](image.png)](target.md#present)\n" +
"[![missing](absent.png)](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("" +