-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmarkdown_fs.go
148 lines (120 loc) · 2.85 KB
/
markdown_fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package xlog
import (
"context"
"errors"
"io/fs"
"log/slog"
"path"
"path/filepath"
"strings"
"sync"
"github.com/emad-elsaid/memoize"
"github.com/emad-elsaid/memoize/cache/adapters/hashicorp"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/rjeczalik/notify"
)
func newMarkdownFS(p string) *markdownFS {
cache, err := lru.New[string, Page](1000)
if err != nil {
slog.Error("Can't create cache for pages", "error", err)
panic("Can't continue without cache instance")
}
m := markdownFS{
cache: cache,
path: p,
}
m._page = memoize.NewWithCache(
hashicorp.LRU(cache),
func(name string) Page {
if name == "" {
name = Config.Index
}
return &page{
name: name,
}
},
)
m.watch = sync.OnceFunc(func() {
go func() {
events := make(chan notify.EventInfo, 1)
absPath, err := filepath.Abs(m.path)
if err != nil {
slog.Error("failed to get absolute path", "error", err)
return
}
if err := notify.Watch(m.path+"/...", events, notify.All); err != nil {
slog.Error("Can't watch files for change", "error", err)
return
}
defer notify.Stop(events)
for {
switch ei := <-events; ei.Event() {
case notify.Write, notify.Rename, notify.Create:
relPath, err := filepath.Rel(absPath, ei.Path())
if err != nil {
slog.Error("Can't resolve relative path", "error", err)
continue
}
if !strings.HasSuffix(relPath, ".md") {
continue
}
if IsIgnoredPath(relPath) {
continue
}
name := strings.TrimSuffix(relPath, ".md")
cp := m._page(name)
Trigger(PageChanged, cp)
m.cache.Remove(name)
case notify.Remove:
relPath, err := filepath.Rel(absPath, ei.Path())
if err != nil {
slog.Error("Can't resolve relative path", "error", err)
continue
}
if !strings.HasSuffix(relPath, ".md") {
continue
}
if IsIgnoredPath(relPath) {
continue
}
name := strings.TrimSuffix(relPath, ".md")
cp := m._page(name)
Trigger(PageDeleted, cp)
m.cache.Remove(name)
}
}
}()
})
return &m
}
// MarkdownFS a current directory markdown pages
type markdownFS struct {
path string
cache *lru.Cache[string, Page]
_page func(string) Page
watch func()
}
// Page Creates an instance of Page with name. if no name is passed it's assumed
// xlog.Config.Index
func (m *markdownFS) Page(name string) Page {
m.watch()
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 name != "." && d.IsDir() && IsIgnoredPath(name) {
return fs.SkipDir
}
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
})
}