Enhance URL ignoring logic in fsurls.go to support wildcard patterns and add tests for ignoring specific URLs. Introduce a new function to convert wildcard patterns to regex for improved matching accuracy.

This commit is contained in:
Luke Hagar
2025-09-13 03:34:12 +00:00
parent d2dbfe9dd1
commit 7808f2337b
3 changed files with 37 additions and 5 deletions

View File

@@ -682,18 +682,34 @@ func isURLIgnored(u string, patterns []string) bool {
if len(patterns) == 0 { if len(patterns) == 0 {
return false return false
} }
for _, p := range patterns { for _, raw := range patterns {
p := strings.TrimSpace(raw)
if p == "" { if p == "" {
continue continue
} }
// simple contains or wildcard suffix/prefix match // No wildcards: exact or substring match
if p == u || strings.Contains(u, p) { if !strings.ContainsAny(p, "*?") {
if u == p || strings.Contains(u, p) {
return true
}
continue
}
// Glob-style: allow '*' to span slashes by converting '*' -> '**'
dsPat := strings.ReplaceAll(p, "*", "**")
if ok, _ := doublestar.PathMatch(dsPat, u); ok {
return true return true
} }
// doublestar path-like match for full URL string // Regex fallback: '*' -> '.*', '?' -> '.'
if ok, _ := doublestar.PathMatch(p, u); ok { if re, err := wildcardToRegex(p); err == nil && re.MatchString(u) {
return true return true
} }
} }
return false return false
} }
func wildcardToRegex(pattern string) (*regexp.Regexp, error) {
escaped := regexp.QuoteMeta(pattern)
escaped = strings.ReplaceAll(escaped, "\\*", ".*")
escaped = strings.ReplaceAll(escaped, "\\?", ".")
return regexp.Compile("^" + escaped + "$")
}

View File

@@ -57,6 +57,17 @@ func TestCollectURLs_FromTestFiles(t *testing.T) {
t.Fatalf("expected API URL %s to be ignored via .slinkignore", u) t.Fatalf("expected API URL %s to be ignored via .slinkignore", u)
} }
} }
// URLs matching *acme* should be ignored
acmeSamples := []string{
"https://acme.com/logout",
"http://sub.acme.example/logout",
"https://docs.acme.dev",
}
for _, u := range acmeSamples {
if _, ok := urls[u]; ok {
t.Fatalf("expected %s to be ignored via *acme* pattern", u)
}
}
// Verify .slinkignore path ignores: file under ignore-me should not contribute // Verify .slinkignore path ignores: file under ignore-me should not contribute
for u, files := range urls { for u, files := range urls {

5
testdata/test2_acme.md vendored Normal file
View File

@@ -0,0 +1,5 @@
Here are some example URLs that should be ignored by pattern *acme*:
- https://acme.com/logout
- http://sub.acme.example/logout
- https://docs.acme.dev