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)
|
ignoreBlobs := getIgnoredFiles(*root)
|
||||||
l, i := walk(*in, ".md", *index, ignoreBlobs)
|
l, i := walk(*in, ".md", *index, ignoreBlobs)
|
||||||
f := filter(l)
|
f := filter(l)
|
||||||
err := write(f, i, *index, *out)
|
err := write(f, i, *index, *out, *root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
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())
|
fmt.Printf("[Ignored] %s\n", d.Name())
|
||||||
nPrivate++
|
nPrivate++
|
||||||
} else if filepath.Ext(d.Name()) == ext {
|
} else if filepath.Ext(d.Name()) == ext {
|
||||||
res = append(res, parse(s, root)...)
|
|
||||||
if index {
|
if index {
|
||||||
text := getText(s)
|
text := getText(s)
|
||||||
|
|
||||||
@ -61,12 +60,13 @@ func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res []
|
|||||||
info, _ := os.Stat(s)
|
info, _ := os.Stat(s)
|
||||||
source := processSource(trim(s, root, ".md"))
|
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{
|
i[source] = Content{
|
||||||
LastModified: info.ModTime(),
|
LastModified: info.ModTime(),
|
||||||
Title: matter.Title,
|
Title: matter.Title,
|
||||||
Content: body,
|
Content: body,
|
||||||
}
|
}
|
||||||
|
res = append(res, parse(s, root)...)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("[Ignored] %s\n", d.Name())
|
fmt.Printf("[Ignored] %s\n", d.Name())
|
||||||
nPrivate++
|
nPrivate++
|
||||||
|
39
write.go
39
write.go
@ -1,16 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"path"
|
"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)
|
index := index(links)
|
||||||
resStruct := struct {
|
resStruct := struct {
|
||||||
Index Index `json:"index"`
|
Index Index `json:"index"`
|
||||||
Links []Link `json:"links"`
|
Links []Link `json:"links"`
|
||||||
}{
|
}{
|
||||||
Index: index,
|
Index: index,
|
||||||
Links: links,
|
Links: links,
|
||||||
@ -25,6 +27,7 @@ func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) er
|
|||||||
return writeErr
|
return writeErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check whether to index content
|
||||||
if toIndex {
|
if toIndex {
|
||||||
marshalledContentIndex, mcErr := json.MarshalIndent(&contentIndex, "", " ")
|
marshalledContentIndex, mcErr := json.MarshalIndent(&contentIndex, "", " ")
|
||||||
if mcErr != nil {
|
if mcErr != nil {
|
||||||
@ -35,11 +38,38 @@ func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) er
|
|||||||
if writeErr != nil {
|
if writeErr != nil {
|
||||||
return writeErr
|
return writeErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write linkmap
|
||||||
|
writeErr = writeLinkMap(&contentIndex, root)
|
||||||
|
if writeErr != nil {
|
||||||
|
return writeErr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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
|
// constructs index from links
|
||||||
func index(links []Link) (index Index) {
|
func index(links []Link) (index Index) {
|
||||||
linkMap := make(map[string][]Link)
|
linkMap := make(map[string][]Link)
|
||||||
@ -63,6 +93,3 @@ func index(links []Link) (index Index) {
|
|||||||
index.Backlinks = backlinkMap
|
index.Backlinks = backlinkMap
|
||||||
return index
|
return index
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user