-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
231 lines (207 loc) · 6.24 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"bufio"
"fmt"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/spf13/afero"
"golang.org/x/exp/slices"
)
/* textFile captures all data about a text file stored on disk that we need for exporting logseq graph */
type textFile struct {
absoluteFSPath string
content string
}
type parsedContent struct {
/* content without attributes */
content string
attributes map[string]string
assets []string
}
type parsedPage struct {
exportFilename string
originalPath string
pc parsedContent
}
const publicAttributeSubstring = "public::"
func loadPublicPages(appFS afero.Fs, logseqFolder string) ([]textFile, error) {
logseqPagesFolder := filepath.Join(logseqFolder, "pages")
// Find all files that contain `public::`
var publicFiles []string
err := afero.Walk(appFS, logseqPagesFolder, func(path string, info fs.FileInfo, walkError error) error {
if walkError != nil {
return walkError
}
if info.IsDir() {
return nil
}
file, err := appFS.OpenFile(path, os.O_RDONLY, os.ModePerm)
if err != nil {
return err
}
defer file.Close()
fileScanner := bufio.NewScanner(file)
for fileScanner.Scan() {
line := fileScanner.Text()
if strings.Contains(line, publicAttributeSubstring) {
publicFiles = append(publicFiles, path)
return nil
}
}
return nil
})
// FIXME: test this error
if err != nil {
return nil, fmt.Errorf("error during walking through the logseq folder (%q): %w", logseqPagesFolder, err)
}
pages := make([]textFile, 0, len(publicFiles))
for _, publicFile := range publicFiles {
srcContent, err := afero.ReadFile(appFS, publicFile)
if err != nil {
return nil, fmt.Errorf("reading the %q file failed: %w", publicFile, err)
}
santitizedContent := strings.ReplaceAll(string(srcContent), "\r", "")
pages = append(pages, textFile{
absoluteFSPath: publicFile,
content: santitizedContent,
})
}
return pages, nil
}
func main() {
err := Run(os.Args)
if err != nil {
log.Fatal(err.Error())
}
}
func Run(args []string) error {
appFS := afero.NewOsFs()
config, err := parseConfig(args)
if err != nil {
return fmt.Errorf("the configuration could not be parsed: %w", err)
}
publicPages, err := loadPublicPages(appFS, config.LogseqFolder)
if err != nil {
return fmt.Errorf("Error during walking through a folder %v", err)
}
// parse pages
parsedPages := make([]parsedPage, 0, len(publicPages))
for _, publicPage := range publicPages {
parsedPages = append(parsedPages, parsePage(publicPage))
}
err = exportAssets(appFS, config.OutputFolder, parsedPages)
if err != nil {
return fmt.Errorf("failed to export assets: %w", err)
}
titleToSlug := map[string]string{}
for _, p := range parsedPages {
titleToSlug[p.pc.attributes["title"]] = p.pc.attributes["slug"]
}
for _, page := range parsedPages {
exportPath := filepath.Join(config.OutputFolder, "logseq-pages", page.exportFilename)
folder, _ := filepath.Split(exportPath)
err = appFS.MkdirAll(folder, os.ModePerm)
if err != nil {
return fmt.Errorf("creating parent directory for %q failed: %v", exportPath, err)
}
// TODO: more processing on the content (linking pages, attributes)
contentWithAssets := replaceAssetPaths(page)
links := detectPageLinks(contentWithAssets)
for _, l := range links {
slug, ok := titleToSlug[l]
if !ok {
continue
}
contentWithAssets = strings.ReplaceAll(
contentWithAssets,
fmt.Sprintf("[[%s]]", l),
// we use path here on purpose since we create URL
fmt.Sprintf("[%s](%s)", l, path.Join("/logseq-pages", slug)),
)
}
// TODO find out what properties should I not quote
err = afero.WriteFile(
appFS,
exportPath,
[]byte(render(transformAttributes(page.pc.attributes, config.UnquotedProperties), contentWithAssets)),
0644,
)
if err != nil {
return fmt.Errorf("copying file %q failed: %v", exportPath, err)
}
}
return nil
}
func transformAttributes(attributes map[string]string, dontQuote []string) map[string]string {
dontQuote = append(dontQuote, "tags")
if _, ok := attributes["tags"]; ok {
attributes["tags"] = fmt.Sprintf("[%s]", attributes["tags"])
}
for name, value := range attributes {
if !slices.Contains(dontQuote, name) {
attributes[name] = fmt.Sprintf("%q", value)
}
}
return attributes
}
func detectPageLinks(content string) []string {
result := regexp.MustCompile(`\[\[([^\/\n\r]+?)]]`).FindAllStringSubmatch(content, -1)
links := make([]string, 0, len(result))
for _, r := range result {
links = append(links, r[1])
}
return links
}
func exportAssets(appFS afero.Fs, outputFolder string, exportPages []parsedPage) error {
// get all asset paths (deduplicated)
assetFullPaths := map[string]struct{}{}
for _, page := range exportPages {
for _, assetPath := range page.pc.assets {
fullPath := filepath.Clean(filepath.Join(filepath.Dir(page.originalPath), assetPath))
assetFullPaths[fullPath] = struct{}{}
}
}
assetOutputFolder := filepath.Join(outputFolder, "logseq-assets")
assetSrcAndDest := map[string]string{}
for fullPath := range assetFullPaths {
dest := filepath.Join(assetOutputFolder, filepath.Base(fullPath))
assetSrcAndDest[fullPath] = dest
}
err := appFS.MkdirAll(assetOutputFolder, os.ModePerm)
if err != nil {
log.Fatalf("Error when making assets folder %q: %v", assetOutputFolder, err)
}
for src, dest := range assetSrcAndDest {
err = copy(appFS, src, dest)
if err != nil {
log.Printf("failed copying asset from %q to %q: %v", src, dest, err)
}
}
return nil
}
func replaceAssetPaths(p parsedPage) string {
newContent := p.pc.content
for _, link := range p.pc.assets {
fileName := filepath.Base(link)
// we do want to use `path` package here, we are creating web URL
newContent = strings.ReplaceAll(newContent, link, path.Join("/logseq-assets", fileName))
}
return newContent
}
func render(attributes map[string]string, content string) string {
sortedKeys := make([]string, 0, len(attributes))
for k := range attributes {
sortedKeys = append(sortedKeys, k)
}
slices.Sort(sortedKeys)
attributeBuilder := strings.Builder{}
for _, key := range sortedKeys {
attributeBuilder.WriteString(fmt.Sprintf("%s: %s\n", key, attributes[key]))
}
return fmt.Sprintf("---\n%s---\n%s", attributeBuilder.String(), content)
}