mirror of
https://github.com/jackyzha0/hugo-obsidian.git
synced 2024-06-14 11:42:35 +03:00
Merge pull request #23 from jackyzha0/dev
This commit is contained in:
commit
3c17ec3cf6
2
main.go
2
main.go
@ -76,7 +76,7 @@ func main() {
|
||||
ignoreBlobs := getIgnoredFiles(*root)
|
||||
l, i := walk(*in, ".md", *index, ignoreBlobs)
|
||||
f := filter(l)
|
||||
err := write(f, i, *index, *out)
|
||||
err := write(f, i, *index, *out, *root)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
4
walk.go
4
walk.go
@ -42,7 +42,6 @@ func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res []
|
||||
fmt.Printf("[Ignored] %s\n", d.Name())
|
||||
nPrivate++
|
||||
} else if filepath.Ext(d.Name()) == ext {
|
||||
res = append(res, parse(s, root)...)
|
||||
if index {
|
||||
text := getText(s)
|
||||
|
||||
@ -61,12 +60,13 @@ func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res []
|
||||
info, _ := os.Stat(s)
|
||||
source := processSource(trim(s, root, ".md"))
|
||||
|
||||
// adjustedPath := UnicodeSanitize(strings.Replace(hugoPathTrim(trim(s, root, ".md")), " ", "-", -1))
|
||||
// add to content and link index
|
||||
i[source] = Content{
|
||||
LastModified: info.ModTime(),
|
||||
Title: matter.Title,
|
||||
Content: body,
|
||||
}
|
||||
res = append(res, parse(s, root)...)
|
||||
} else {
|
||||
fmt.Printf("[Ignored] %s\n", d.Name())
|
||||
nPrivate++
|
||||
|
39
write.go
39
write.go
@ -1,16 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) error {
|
||||
func write(links []Link, contentIndex ContentIndex, toIndex bool, out string, root string) error {
|
||||
index := index(links)
|
||||
resStruct := struct {
|
||||
Index Index `json:"index"`
|
||||
Links []Link `json:"links"`
|
||||
Index Index `json:"index"`
|
||||
Links []Link `json:"links"`
|
||||
}{
|
||||
Index: index,
|
||||
Links: links,
|
||||
@ -25,6 +27,7 @@ func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) er
|
||||
return writeErr
|
||||
}
|
||||
|
||||
// check whether to index content
|
||||
if toIndex {
|
||||
marshalledContentIndex, mcErr := json.MarshalIndent(&contentIndex, "", " ")
|
||||
if mcErr != nil {
|
||||
@ -35,11 +38,38 @@ func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) er
|
||||
if writeErr != nil {
|
||||
return writeErr
|
||||
}
|
||||
|
||||
// write linkmap
|
||||
writeErr = writeLinkMap(&contentIndex, root)
|
||||
if writeErr != nil {
|
||||
return writeErr
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeLinkMap(contentIndex *ContentIndex, root string) error {
|
||||
fp := path.Join(root, "linkmap")
|
||||
file, err := os.OpenFile(fp, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
datawriter := bufio.NewWriter(file)
|
||||
for path := range *contentIndex {
|
||||
if path == "/" {
|
||||
_, _ = datawriter.WriteString("/index.html /\n")
|
||||
} else {
|
||||
_, _ = datawriter.WriteString(path + "/$1.{html} " + path + "/$1\n")
|
||||
}
|
||||
}
|
||||
datawriter.Flush()
|
||||
file.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// constructs index from links
|
||||
func index(links []Link) (index Index) {
|
||||
linkMap := make(map[string][]Link)
|
||||
@ -63,6 +93,3 @@ func index(links []Link) (index Index) {
|
||||
index.Backlinks = backlinkMap
|
||||
return index
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user