Preserve upstream policy wording in link examples

This commit is contained in:
2026-09-28 02:27:30 +00:00
parent e08f083ec8
commit debef98536
2 changed files with 42 additions and 3 deletions

View File

@@ -12,6 +12,46 @@ import (
)
var markdownLinkPattern = regexp.MustCompile(`\[[^\]]+\]\(([^)]+)\)`)
var markdownBackticks = regexp.MustCompile("`+")
// documentationLinks excludes literal examples enclosed by matching backtick
// runs before applying the existing lightweight link check.
func documentationLinks(markdown string) [][]string {
runs := markdownBackticks.FindAllStringIndex(markdown, -1)
text := []byte(markdown)
for i := 0; i < len(runs); i++ {
width := runs[i][1] - runs[i][0]
for j := i + 1; j < len(runs); j++ {
if runs[j][1]-runs[j][0] != width {
continue
}
for k := runs[i][0]; k < runs[j][1]; k++ {
text[k] = ' '
}
i = j
break
}
}
return markdownLinkPattern.FindAllStringSubmatch(string(text), -1)
}
func TestDocumentationLinksIgnoreCodeExamples(t *testing.T) {
for _, example := range []string{
"`[Source](COMMIT_URL).`",
"``[Source](COMMIT_URL) with a ` character``",
"```markdown\n[Source](COMMIT_URL)\n```",
} {
t.Run(example, func(t *testing.T) {
matches := documentationLinks("[before](before.md) " + example + " [after](after.md)")
if len(matches) != 2 || matches[0][1] != "before.md" || matches[1][1] != "after.md" {
t.Fatalf("links = %#v, want only the surrounding real links", matches)
}
})
}
if matches := documentationLinks("unmatched ` [real](real.md)"); len(matches) != 1 || matches[0][1] != "real.md" {
t.Fatalf("unmatched backtick hid a real link: %#v", matches)
}
}
func TestDocumentationLinksResolve(t *testing.T) {
root := repositoryRoot(t)
@@ -163,7 +203,7 @@ func walkDocumentationLinks(t *testing.T, root, directory string) {
if err != nil {
return err
}
for _, match := range markdownLinkPattern.FindAllStringSubmatch(string(data), -1) {
for _, match := range documentationLinks(string(data)) {
target := strings.TrimSpace(match[1])
if !isLocalDocumentationTarget(target) {
continue