fix: draft links being added, linkmap writer

This commit is contained in:
Jacky Zhao 2022-07-31 09:46:39 -07:00
parent b081ab02b0
commit eec81e45b4
3 changed files with 36 additions and 9 deletions

View File

@ -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)
}

View File

@ -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++

View File

@ -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
}