From 4a24cb78ee618aa02706eb5bf6c621014ae4833a Mon Sep 17 00:00:00 2001 From: y-hidaka Date: Wed, 25 Nov 2026 04:43:52 +0000 Subject: [PATCH 1/2] fix: skip link syntax inside code when extracting links extractLinks scanned the raw body for [text](target), so Go code such as errors.AsType[*E](err) inside an inline code span or a fenced code block was reported as a broken link. Scan a copy masked by maskCode, the same masking footnote scanning has used since #33. maskCode preserves byte offsets, so text and target are still sliced from the original body. graph and backlinks call ExtractLinks, so they stop seeing these false links too. Closes #48 Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- internal/validate/v02.go | 5 ++-- internal/validate/validate.go | 17 ++++++++++---- internal/validate/validate_test.go | 37 ++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) 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..6a62ef6 100644 --- a/internal/validate/validate.go +++ b/internal/validate/validate.go @@ -198,27 +198,34 @@ func ExtractFrontmatterLinks(links []string) []Link { } // extractLinks parses all markdown links [text](target) from a body. +// +// Link syntax inside fenced code blocks and inline code spans renders as +// literal text (for example a Go call such as errors.AsType[*E](err)), so the +// scan runs over a masked copy of the body (issue #48). maskCode preserves +// byte offsets, so text and target are sliced from the original body and keep +// any code they contain, as in [`cfg`](cfg.md). 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..c76ed0c 100644 --- a/internal/validate/validate_test.go +++ b/internal/validate/validate_test.go @@ -376,3 +376,40 @@ func testBundle(t *testing.T, files map[string]string) *bundle.Bundle { } return b } + +// --- 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") +} From a987c097690cbd103d3586bffa6a446e39aa72cd Mon Sep 17 00:00:00 2001 From: y-hidaka Date: Wed, 25 Nov 2026 04:47:20 +0000 Subject: [PATCH 2/2] test: keep the issue #48 tests above the helpers section Also shorten the extractLinks doc comment; maskCode's comment already explains why code is masked. Co-Authored-By: Claude Opus 5.5 --- internal/validate/validate.go | 7 ++--- internal/validate/validate_test.go | 42 +++++++++++++++--------------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/internal/validate/validate.go b/internal/validate/validate.go index 6a62ef6..275b1ba 100644 --- a/internal/validate/validate.go +++ b/internal/validate/validate.go @@ -199,11 +199,8 @@ func ExtractFrontmatterLinks(links []string) []Link { // extractLinks parses all markdown links [text](target) from a body. // -// Link syntax inside fenced code blocks and inline code spans renders as -// literal text (for example a Go call such as errors.AsType[*E](err)), so the -// scan runs over a masked copy of the body (issue #48). maskCode preserves -// byte offsets, so text and target are sliced from the original body and keep -// any code they contain, as in [`cfg`](cfg.md). +// 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) diff --git a/internal/validate/validate_test.go b/internal/validate/validate_test.go index c76ed0c..9ba6041 100644 --- a/internal/validate/validate_test.go +++ b/internal/validate/validate_test.go @@ -356,27 +356,6 @@ func TestValidateLinks_LinkToExistingLogIsValid(t *testing.T) { } } -// --- helpers --- - -func testBundle(t *testing.T, files map[string]string) *bundle.Bundle { - t.Helper() - dir := t.TempDir() - for path, content := range files { - full := filepath.Join(dir, path) - if err := os.MkdirAll(filepath.Dir(full), 0755); err != nil { - t.Fatalf("mkdir: %v", err) - } - if err := os.WriteFile(full, []byte(content), 0644); err != nil { - t.Fatalf("write: %v", err) - } - } - b, err := bundle.Load(dir) - if err != nil { - t.Fatalf("Load: %v", err) - } - return b -} - // --- issue #48: link syntax inside code is literal text --- func TestExtractLinks_IgnoresCode(t *testing.T) { @@ -413,3 +392,24 @@ func TestValidate_LinkInCodeIgnored(t *testing.T) { r := Validate(b) mustNotFinding(t, r, "broken link") } + +// --- helpers --- + +func testBundle(t *testing.T, files map[string]string) *bundle.Bundle { + t.Helper() + dir := t.TempDir() + for path, content := range files { + full := filepath.Join(dir, path) + if err := os.MkdirAll(filepath.Dir(full), 0755); err != nil { + t.Fatalf("mkdir: %v", err) + } + if err := os.WriteFile(full, []byte(content), 0644); err != nil { + t.Fatalf("write: %v", err) + } + } + b, err := bundle.Load(dir) + if err != nil { + t.Fatalf("Load: %v", err) + } + return b +}