Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<family>/<check>`, e.g. `okf/links/broken`, `okf/frontmatter/type-required`), so automation can suppress or route specific rules instead of matching message text.

Expand Down
5 changes: 3 additions & 2 deletions internal/validate/v02.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
37 changes: 37 additions & 0 deletions internal/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down