-
Notifications
You must be signed in to change notification settings - Fork 15
/
versions.go
56 lines (44 loc) · 977 Bytes
/
versions.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
package versions
import (
"crypto/sha256"
"fmt"
"os"
"path"
"regexp"
. "github.com/emad-elsaid/xlog"
)
func init() {
Listen(BeforeWrite, WriteVersion)
RegisterProperty(VersionProps)
IgnoreDirectory(regexp.MustCompile(`\.versions$`))
}
func WriteVersion(p Page) error {
if !p.Exists() {
return nil
}
content := []byte(p.Content())
sum := fmt.Sprintf("%x.md", sha256.Sum256(content))
dir := p.FileName() + ".versions"
os.Mkdir(dir, 0700)
return os.WriteFile(path.Join(dir, sum), content, 0644)
}
func VersionProps(p Page) []Property {
dir := p.FileName() + ".versions"
files, err := os.ReadDir(dir)
if err != nil {
return nil
}
i := 0
for _, f := range files {
if f.Type().IsRegular() && path.Ext(f.Name()) == ".md" {
i++
}
}
if i == 0 {
return nil
}
return []Property{prop(i)}
}
type prop int
func (_ prop) Icon() string { return "fa-solid fa-code-branch" }
func (l prop) Name() string { return fmt.Sprintf("%d versions", l) }