tangled
alpha
login
or
join now
voigt.tngl.sh
/
hugo-digital-garden
0
fork
atom
wip
0
fork
atom
overview
issues
pulls
pipelines
add file structure
voigt.tngl.sh
2 months ago
fcbf85f1
9699c68c
verified
This commit was signed with the committer's
known signature
.
voigt.tngl.sh
SSH Key Fingerprint:
SHA256:6ShT3iHt6IpCPJtT9Ndevva6c3xzr4RI2LrvKeOXH2U=
+92
1 changed file
expand all
collapse all
unified
split
file.go
+92
file.go
reviewed
···
1
1
+
package main
2
2
+
import (
3
3
+
"fmt"
4
4
+
"path/filepath"
5
5
+
"strings"
6
6
+
"github.com/google/uuid"
7
7
+
)
8
8
+
type File struct {
9
9
+
Id string
10
10
+
Name string
11
11
+
RawContent []byte
12
12
+
ObsidianMarkdown *ast.Document
13
13
+
HugoMarkdown *ast.Document
14
14
+
15
15
+
ObsidianPath string
16
16
+
HugoPath string
17
17
+
18
18
+
Private bool
19
19
+
20
20
+
Type string // file can be Markdown, PDF, Image, etc.
21
21
+
22
22
+
Links []string
23
23
+
}
24
24
+
func InitNewFile(name string, obsidianPath string, hugoPath string) *File {
25
25
+
id := uuid.New().String()
26
26
+
relativePath, err := GetRelativePath(obsidianPath)
27
27
+
if err != nil {
28
28
+
log.Fatal(err)
29
29
+
}
30
30
+
31
31
+
return &File{
32
32
+
Id: id,
33
33
+
Name: name,
34
34
+
RawContent: []byte{},
35
35
+
ObsidianPath: obsidianPath,
36
36
+
HugoPath: HugoPath(hugoPath, relativePath),
37
37
+
Private: false,
38
38
+
Type: getFileType(obsidianPath),
39
39
+
}
40
40
+
}
41
41
+
42
42
+
func GetRelativePath(path string) (string, error) {
43
43
+
if !strings.HasPrefix(path, o.ObsidianVaultDirectory) {
44
44
+
return "", fmt.Errorf("path %s is not within the Obsidian vault", path)
45
45
+
46
46
+
}
47
47
+
return strings.TrimPrefix(path, o.ObsidianVaultDirectory), nil
48
48
+
}
49
49
+
50
50
+
func HugoPath(contentDir, relativePath string) string {
51
51
+
return filepath.Join(contentDir, relativePath)
52
52
+
}
53
53
+
func getFileType(path string) string {
54
54
+
if strings.HasSuffix(path, "/") {
55
55
+
return "Directory"
56
56
+
}
57
57
+
58
58
+
fileExtension := filepath.Ext(path)
59
59
+
60
60
+
switch fileExtension {
61
61
+
case ".md":
62
62
+
return "Markdown"
63
63
+
case ".mdown":
64
64
+
return "Markdown"
65
65
+
case ".markdown":
66
66
+
return "Markdown"
67
67
+
case ".txt":
68
68
+
return "Text"
69
69
+
case ".html":
70
70
+
return "HTML"
71
71
+
case ".json":
72
72
+
return "JSON"
73
73
+
case ".yaml":
74
74
+
return "YAML"
75
75
+
case ".yml":
76
76
+
return "YAML"
77
77
+
// support image formats
78
78
+
case ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg":
79
79
+
return "Image"
80
80
+
// support pdf
81
81
+
case ".pdf":
82
82
+
return "PDF"
83
83
+
// support audio formats
84
84
+
case ".mp3", ".wav", ".ogg", ".flac":
85
85
+
return "Audio"
86
86
+
// support video formats
87
87
+
case ".mp4", ".avi", ".mkv", ".mov":
88
88
+
return "Video"
89
89
+
default:
90
90
+
return "Unknown"
91
91
+
}
92
92
+
}