-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change memoization to the scope of markdownFS instead of all page sou…
…rces This should allow for watching filesystem
- Loading branch information
1 parent
ed333f5
commit aaed1c1
Showing
2 changed files
with
72 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package xlog | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io/fs" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/emad-elsaid/memoize" | ||
) | ||
|
||
func newMarkdownFS(path string) *markdownFS { | ||
m := markdownFS{path: path} | ||
|
||
m._page = memoize.New(func(name string) Page { | ||
if name == "" { | ||
name = INDEX | ||
} | ||
|
||
return &page{ | ||
name: name, | ||
} | ||
}) | ||
|
||
return &m | ||
} | ||
|
||
// MarkdownCWDFS a current directory markdown pages | ||
type markdownFS struct { | ||
path string | ||
_page func(string) Page | ||
} | ||
|
||
// Page Creates an instance of Page with name. if no name is passed it's assumed INDEX | ||
func (m *markdownFS) Page(name string) Page { | ||
return m._page(name) | ||
} | ||
|
||
func (m *markdownFS) Each(ctx context.Context, f func(Page)) { | ||
filepath.WalkDir(m.path, func(name string, d fs.DirEntry, err error) error { | ||
if d.IsDir() { | ||
for _, v := range ignoredDirs { | ||
if v.MatchString(name) { | ||
return fs.SkipDir | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
select { | ||
|
||
case <-ctx.Done(): | ||
return errors.New("context stopped") | ||
|
||
default: | ||
ext := path.Ext(name) | ||
basename := name[:len(name)-len(ext)] | ||
|
||
if ext == ".md" { | ||
f(m.Page(basename)) | ||
} | ||
|
||
} | ||
|
||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters