hugo-obsidian/walk.go

99 lines
2.0 KiB
Go
Raw Normal View History

2021-12-28 00:19:05 +03:00
package main
import (
"fmt"
"io/fs"
"io/ioutil"
2021-12-28 00:58:21 +03:00
"os"
2021-12-28 00:19:05 +03:00
"path/filepath"
"strings"
"time"
2022-05-05 07:55:59 +03:00
"github.com/adrg/frontmatter"
"gopkg.in/yaml.v2"
2021-12-28 00:19:05 +03:00
)
2022-05-05 07:55:59 +03:00
type Front struct {
Title string `yaml:"title"`
Draft bool `yaml:"draft"`
}
2021-12-28 00:19:05 +03:00
// recursively walk directory and return all files with given extension
2021-12-28 00:51:36 +03:00
func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res []Link, i ContentIndex) {
2021-12-28 00:19:05 +03:00
fmt.Printf("Scraping %s\n", root)
i = make(ContentIndex)
nPrivate := 0
2022-05-05 07:55:59 +03:00
formats := []*frontmatter.Format{
frontmatter.NewFormat("---", "---", yaml.Unmarshal),
}
start := time.Now()
2022-03-15 10:50:03 +03:00
err := filepath.WalkDir(root, func(fp string, d fs.DirEntry, e error) error {
2021-12-28 00:19:05 +03:00
if e != nil {
return e
}
2022-03-15 10:50:03 +03:00
// path normalize fp
s := filepath.ToSlash(fp)
2021-12-28 00:51:36 +03:00
if _, ignored := ignorePaths[s]; ignored {
fmt.Printf("[Ignored] %s\n", d.Name())
2021-12-28 00:58:21 +03:00
nPrivate++
2021-12-28 00:51:36 +03:00
} else if filepath.Ext(d.Name()) == ext {
2021-12-28 00:19:05 +03:00
if index {
text := getText(s)
2022-05-05 07:55:59 +03:00
var matter Front
raw_body, err := frontmatter.Parse(strings.NewReader(text), &matter, formats...)
body := string(raw_body)
2021-12-28 00:19:05 +03:00
if err != nil {
2022-05-05 07:55:59 +03:00
matter = Front{
Title: "Untitled Page",
Draft: false,
}
2021-12-28 00:19:05 +03:00
body = text
}
// check if page is private
2022-05-05 07:55:59 +03:00
if !matter.Draft {
2021-12-28 00:58:21 +03:00
info, _ := os.Stat(s)
source := processSource(trim(s, root, ".md"))
// add to content and link index
i[source] = Content{
2021-12-28 00:58:21 +03:00
LastModified: info.ModTime(),
2022-05-05 07:55:59 +03:00
Title: matter.Title,
2022-03-15 10:50:03 +03:00
Content: body,
2021-12-28 00:19:05 +03:00
}
res = append(res, parse(s, root)...)
2021-12-28 00:19:05 +03:00
} else {
2021-12-28 00:58:21 +03:00
fmt.Printf("[Ignored] %s\n", d.Name())
2021-12-28 00:19:05 +03:00
nPrivate++
}
}
}
return nil
})
if err != nil {
panic(err)
}
end := time.Now()
fmt.Printf("[DONE] in %s\n", end.Sub(start).Round(time.Millisecond))
2021-12-28 00:19:05 +03:00
fmt.Printf("Ignored %d private files \n", nPrivate)
fmt.Printf("Parsed %d total links \n", len(res))
return res, i
}
func getText(dir string) string {
// read file
fileBytes, err := ioutil.ReadFile(dir)
if err != nil {
panic(err)
}
return string(fileBytes)
}