diff --git a/README.md b/README.md index 153b169..895392a 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ The JSON error envelope makes this trivial to automate: } ``` -Broken cross-links are reported as warnings, never errors: OKF §6.1 says consumers must tolerate them because a link may point at not-yet-written knowledge, so they do not flip `valid` or the exit code. +Broken cross-links are reported as warnings, never errors: OKF §6.1 says consumers must tolerate them because a link may point at not-yet-written knowledge, so they do not flip `valid` or the exit code. Link syntax inside inline code spans and fenced code blocks is literal text and is not checked as a link. Every finding also carries a stable rule ID (`okf//`, e.g. `okf/links/broken`, `okf/frontmatter/type-required`), so automation can suppress or route specific rules instead of matching message text. diff --git a/internal/validate/v02.go b/internal/validate/v02.go index 43d13e1..aab281e 100644 --- a/internal/validate/v02.go +++ b/internal/validate/v02.go @@ -115,8 +115,9 @@ var ( // maskCode replaces the contents of fenced code blocks and inline code spans // with spaces. Length and newlines are preserved, so byte offsets into the -// result line up with the original body. Footnote and heading syntax inside -// code renders as literal text, so scanners must not see it (issue #26). +// result line up with the original body. Footnote, heading, and link syntax +// inside code renders as literal text, so scanners must not see it (issues +// #26 and #48). // // Fences open on a line whose first non-blank characters are three or more // backticks or tildes and close on a line with at least as many of the same diff --git a/internal/validate/validate.go b/internal/validate/validate.go index 8169e24..275b1ba 100644 --- a/internal/validate/validate.go +++ b/internal/validate/validate.go @@ -198,27 +198,31 @@ func ExtractFrontmatterLinks(links []string) []Link { } // extractLinks parses all markdown links [text](target) from a body. +// +// Link syntax inside code is literal text, so the scan runs over the masked +// body (issue #48). Offsets line up, so text and target come from the original. func extractLinks(body string) []Link { var links []Link + scan := maskCode(body) i := 0 - for i < len(body) { + for i < len(scan) { // Find '[' - bracket := strings.IndexByte(body[i:], '[') + bracket := strings.IndexByte(scan[i:], '[') if bracket == -1 { break } start := i + bracket - closeBracket := strings.IndexByte(body[start:], ']') + closeBracket := strings.IndexByte(scan[start:], ']') if closeBracket == -1 { break } textEnd := start + closeBracket // Check for '(' immediately after ']' - if textEnd+1 >= len(body) || body[textEnd+1] != '(' { + if textEnd+1 >= len(scan) || scan[textEnd+1] != '(' { i = textEnd + 1 continue } - closeParen := strings.IndexByte(body[textEnd+2:], ')') + closeParen := strings.IndexByte(scan[textEnd+2:], ')') if closeParen == -1 { break } diff --git a/internal/validate/validate_test.go b/internal/validate/validate_test.go index 31a9728..9ba6041 100644 --- a/internal/validate/validate_test.go +++ b/internal/validate/validate_test.go @@ -356,6 +356,43 @@ func TestValidateLinks_LinkToExistingLogIsValid(t *testing.T) { } } +// --- issue #48: link syntax inside code is literal text --- + +func TestExtractLinks_IgnoresCode(t *testing.T) { + body := "Real: [real](a.md).\n" + + "Inline: `errors.AsType[*MyErr](err)` returns the match.\n" + + "```go\n" + + "if e, ok := errors.AsType[*MyErr](err); ok {\n" + + "\thandle(e)\n" + + "}\n" + + "```\n" + + "Code in text: [`cfg`](cfg.md).\n" + + links := extractLinks(body) + want := []Link{ + {Text: "real", Target: "a.md"}, + {Text: "`cfg`", Target: "cfg.md"}, + } + if len(links) != len(want) { + t.Fatalf("got %d links, want %d: %+v", len(links), len(want), links) + } + for i := range want { + if links[i] != want[i] { + t.Errorf("link[%d] = %+v, want %+v", i, links[i], want[i]) + } + } +} + +func TestValidate_LinkInCodeIgnored(t *testing.T) { + b := testBundle(t, map[string]string{ + "a.md": "---\ntype: T\ntitle: A\ndescription: d\ntags: [x]\n---\n\n" + + "Inline: `errors.AsType[*MyErr](err)`.\n\n" + + "```go\nif e, ok := errors.AsType[*MyErr](err); ok {\n\thandle(e)\n}\n```\n", + }) + r := Validate(b) + mustNotFinding(t, r, "broken link") +} + // --- helpers --- func testBundle(t *testing.T, files map[string]string) *bundle.Bundle {