aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/index
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2025-06-12 03:17:08 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2025-06-12 03:17:08 -0400
commitfbf242cee18655daa13a85d57cf4465736380f35 (patch)
tree434c4459423e0ccd083c74aba95f6b43cd8bb556 /pkg/index
parent61d89ee58e6a4d990e6e81c951731c80c2cd1ece (diff)
Add index filter for parent directories
Diffstat (limited to 'pkg/index')
-rw-r--r--pkg/index/filters.go32
-rw-r--r--pkg/index/filters_test.go52
2 files changed, 71 insertions, 13 deletions
diff --git a/pkg/index/filters.go b/pkg/index/filters.go
index a439185..3a22910 100644
--- a/pkg/index/filters.go
+++ b/pkg/index/filters.go
@@ -3,7 +3,10 @@ package index
import (
"fmt"
"io"
+ "os"
"path/filepath"
+ "slices"
+ "strings"
)
// NOTE: in the future it would be interesting lua filters
@@ -32,29 +35,32 @@ func NewMaxFilesizeFilter(size int64) DocFilter {
}
func NewExcludeFilenameFilter(excluded []string) DocFilter {
- excludedSet := make(map[string]bool, len(excluded))
- for _, filename := range excluded {
- excludedSet[filename] = true
- }
return DocFilter{
"Excluded Filename filter",
func(ip infoPath, _ io.ReadSeeker) bool {
- _, ok := excludedSet[filepath.Base(ip.path)]
- return !ok
+ filename := filepath.Base(ip.path)
+ return !slices.Contains(excluded, filename)
},
}
}
func NewIncludeFilenameFilter(included []string) DocFilter {
- includedSet := make(map[string]bool, len(included))
- for _, filename := range included {
- includedSet[filename] = true
- }
return DocFilter{
"Included Filename filter",
func(ip infoPath, _ io.ReadSeeker) bool {
- _, ok := includedSet[filepath.Base(ip.path)]
- return ok
+ filename := filepath.Base(ip.path)
+ return slices.Contains(included, filename)
+ },
+ }
+}
+
+// exclude files if it has a parent directory badParent
+func NewExcludeParentFilter(badParent string) DocFilter {
+ return DocFilter{
+ "Excluded Parent Directory filter",
+ func(ip infoPath, _ io.ReadSeeker) bool {
+
+ return !slices.Contains(strings.Split(ip.path, string(os.PathSeparator)), badParent)
},
}
}
@@ -135,5 +141,5 @@ func yamlHeaderPos(r io.ReadSeeker) int64 {
}
func DefaultFilters() []DocFilter {
- return []DocFilter{NewExtensionFilter(".md"), NewMaxFilesizeFilter(200 * 1024), YamlHeaderFilter}
+ return []DocFilter{NewExtensionFilter(".md"), NewMaxFilesizeFilter(200 * 1024), NewExcludeParentFilter("templates"), YamlHeaderFilter}
}
diff --git a/pkg/index/filters_test.go b/pkg/index/filters_test.go
index f1226b9..f46874c 100644
--- a/pkg/index/filters_test.go
+++ b/pkg/index/filters_test.go
@@ -65,6 +65,22 @@ func markdownExtension(t *testing.T) infoPath {
return infoPath{path, info}
}
+func parentDirectory(t *testing.T) infoPath {
+ root := t.TempDir()
+ dir := root + "/parent"
+ path := dir + "/a"
+
+ return infoPath{path: path}
+}
+
+func grandparentDirectory(t *testing.T) infoPath {
+ root := t.TempDir()
+ dir := root + "/grandparent/parent"
+ path := dir + "/a"
+
+ return infoPath{path: path}
+}
+
func TestYamlHeaderFilter(t *testing.T) {
tests := []struct {
name string
@@ -110,3 +126,39 @@ func TestExtensionFilter(t *testing.T) {
})
}
}
+
+func TestExcludeParentFilter(t *testing.T) {
+ tests := []struct {
+ name string
+ infoGen func(*testing.T) infoPath
+ parent string
+ want bool
+ }{
+ {
+ "no matching parent",
+ parentDirectory,
+ "foobar", true,
+ },
+ {
+ "direct parent",
+ parentDirectory,
+ "parent", false,
+ },
+ {
+ "nested parent",
+ grandparentDirectory,
+ "grandparent", false,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ docFilter := NewExcludeParentFilter(tt.parent)
+ ip := tt.infoGen(t)
+ got := docFilter.Filter(ip, nil)
+
+ if got != tt.want {
+ t.Errorf("ExcludeParentFilter() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}