Skip to content

Commit

Permalink
allow notebook as hidden dir (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjex authored Apr 14, 2024
1 parent 45b6812 commit 0973f99
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 4 deletions.
3 changes: 2 additions & 1 deletion internal/core/note_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func (t *indexTask) execute(callback func(change paths.DiffChange)) (NoteIndexin
return false, nil
}

source := paths.Walk(t.path, t.logger, shouldIgnorePath)
notebookPath := &NotebookPath{Path: t.path}
source := paths.Walk(t.path, t.logger, notebookPath.Filename(), shouldIgnorePath)

target, err := t.index.IndexedPaths()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a hidden file, and should not be indexed / walked.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
5 changes: 3 additions & 2 deletions internal/util/paths/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// Walk emits the metadata of each file stored in the directory if they pass
// the given shouldIgnorePath closure. Hidden files and directories are ignored.
func Walk(basePath string, logger util.Logger, shouldIgnorePath func(string) (bool, error)) <-chan Metadata {
func Walk(basePath string, logger util.Logger, notebookRoot string, shouldIgnorePath func(string) (bool, error)) <-chan Metadata {
c := make(chan Metadata, 50)
go func() {
defer close(c)
Expand All @@ -22,9 +22,10 @@ func Walk(basePath string, logger util.Logger, shouldIgnorePath func(string) (bo

filename := info.Name()
isHidden := strings.HasPrefix(filename, ".")
isNotebookRoot := filename == notebookRoot

if info.IsDir() {
if isHidden {
if isHidden && !isNotebookRoot {
return filepath.SkipDir
}

Expand Down
32 changes: 31 additions & 1 deletion internal/util/paths/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,38 @@ func TestWalk(t *testing.T) {
return filepath.Ext(path) != ".md", nil
}

notebookRoot := filepath.Base(path)
actual := make([]string, 0)
for m := range Walk(path, &util.NullLogger, shouldIgnore) {
for m := range Walk(path, &util.NullLogger, notebookRoot, shouldIgnore) {
assert.NotNil(t, m.Modified)
actual = append(actual, m.Path)
}

assert.Equal(t, actual, []string{
"Dir3/a.md",
"a.md",
"b.md",
"dir1/a.md",
"dir1/b.md",
"dir1/dir1/a.md",
"dir1 a space/a.md",
"dir2/a.md",
})
}

// Walk should ignore all hidden files and dirs (prefixed with "."), with
// exception of the notebook's root dir; i.e the root dir is allowed to be
// hidden.
func TestWalkHidden(t *testing.T) {
var path = fixtures.Path(".walk-hidden")

shouldIgnore := func(path string) (bool, error) {
return filepath.Ext(path) != ".md", nil
}

notebookRoot := filepath.Base(path)
actual := make([]string, 0)
for m := range Walk(path, &util.NullLogger, notebookRoot, shouldIgnore) {
assert.NotNil(t, m.Modified)
actual = append(actual, m.Path)
}
Expand Down

0 comments on commit 0973f99

Please sign in to comment.