-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
47 lines (40 loc) · 808 Bytes
/
files.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
)
type ObsidianDoc struct {
path string
filename string
extension string
attachments []string
}
func (o *ObsidianDoc) setAttachments() {
if o.extension != "md" && o.extension != "txt" {
return
}
file, err := os.Open(o.path)
if err != nil {
fmt.Println(err)
}
defer file.Close()
re, err := regexp.Compile(`!\[{2}([^\]]+)\]]`)
if err != nil {
fmt.Println(err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
foundAttachments := re.FindAllStringSubmatch(scanner.Text(), -1)
for _, attachment := range foundAttachments {
o.attachments = append(o.attachments, attachment[1])
}
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
}
func (o *ObsidianDoc) deleteFile() {
os.Remove(o.path)
}