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

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()) 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++

View File

@ -1,12 +1,14 @@
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"`
@ -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,7 +38,34 @@ 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
}
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 return nil
} }
@ -63,6 +93,3 @@ func index(links []Link) (index Index) {
index.Backlinks = backlinkMap index.Backlinks = backlinkMap
return index return index
} }