wip

add file structure #2

closed opened by voigt.tngl.sh targeting main from push-ywtxylsslmvp
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:6q572hlx7omtsszji5w2fyw3/sh.tangled.repo.pull/3mbpc5ninsv22
+92
Diff #1
+92
file.go
··· 1 + package main 2 + import ( 3 + "fmt" 4 + "path/filepath" 5 + "strings" 6 + "github.com/google/uuid" 7 + ) 8 + type File struct { 9 + Id string 10 + Name string 11 + RawContent []byte 12 + ObsidianMarkdown *ast.Document 13 + HugoMarkdown *ast.Document 14 + 15 + ObsidianPath string 16 + HugoPath string 17 + 18 + Private bool 19 + 20 + Type string // file can be Markdown, PDF, Image, etc. 21 + 22 + Links []string 23 + } 24 + func InitNewFile(name string, obsidianPath string, hugoPath string) *File { 25 + id := uuid.New().String() 26 + relativePath, err := GetRelativePath(obsidianPath) 27 + if err != nil { 28 + log.Fatal(err) 29 + } 30 + 31 + return &File{ 32 + Id: id, 33 + Name: name, 34 + RawContent: []byte{}, 35 + ObsidianPath: obsidianPath, 36 + HugoPath: HugoPath(hugoPath, relativePath), 37 + Private: false, 38 + Type: getFileType(obsidianPath), 39 + } 40 + } 41 + 42 + func GetRelativePath(path string) (string, error) { 43 + if !strings.HasPrefix(path, o.ObsidianVaultDirectory) { 44 + return "", fmt.Errorf("path %s is not within the Obsidian vault", path) 45 + 46 + } 47 + return strings.TrimPrefix(path, o.ObsidianVaultDirectory), nil 48 + } 49 + 50 + func HugoPath(contentDir, relativePath string) string { 51 + return filepath.Join(contentDir, relativePath) 52 + } 53 + func getFileType(path string) string { 54 + if strings.HasSuffix(path, "/") { 55 + return "Directory" 56 + } 57 + 58 + fileExtension := filepath.Ext(path) 59 + 60 + switch fileExtension { 61 + case ".md": 62 + return "Markdown" 63 + case ".mdown": 64 + return "Markdown" 65 + case ".markdown": 66 + return "Markdown" 67 + case ".txt": 68 + return "Text" 69 + case ".html": 70 + return "HTML" 71 + case ".json": 72 + return "JSON" 73 + case ".yaml": 74 + return "YAML" 75 + case ".yml": 76 + return "YAML" 77 + // support image formats 78 + case ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg": 79 + return "Image" 80 + // support pdf 81 + case ".pdf": 82 + return "PDF" 83 + // support audio formats 84 + case ".mp3", ".wav", ".ogg", ".flac": 85 + return "Audio" 86 + // support video formats 87 + case ".mp4", ".avi", ".mkv", ".mov": 88 + return "Video" 89 + default: 90 + return "Unknown" 91 + } 92 + }

History

3 rounds 0 comments
sign up or login to add to the discussion
1 commit
expand
add file structure
expand 0 comments
closed without merging
1 commit
expand
add file structure
expand 0 comments
1 commit
expand
add file structure
expand 0 comments