mirror of
https://github.com/jackyzha0/hugo-obsidian.git
synced 2024-06-14 11:42:35 +03:00
content index building
This commit is contained in:
parent
0295d5e3d0
commit
06cc90fa12
66
main.go
66
main.go
@ -9,6 +9,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,6 +25,13 @@ type Index struct {
|
|||||||
Backlinks LinkTable
|
Backlinks LinkTable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Content struct {
|
||||||
|
Title string
|
||||||
|
Content string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ContentIndex = map[string]Content
|
||||||
|
|
||||||
func trim(source, prefix, suffix string) string {
|
func trim(source, prefix, suffix string) string {
|
||||||
return strings.TrimPrefix(strings.TrimSuffix(source, suffix), prefix)
|
return strings.TrimPrefix(strings.TrimSuffix(source, suffix), prefix)
|
||||||
}
|
}
|
||||||
@ -58,25 +66,55 @@ func parse(dir, pathPrefix string) []Link {
|
|||||||
var links []Link
|
var links []Link
|
||||||
fmt.Printf("%s\n", trim(dir, pathPrefix, ".md"))
|
fmt.Printf("%s\n", trim(dir, pathPrefix, ".md"))
|
||||||
for text, target := range md.GetAllLinks(string(bytes)) {
|
for text, target := range md.GetAllLinks(string(bytes)) {
|
||||||
fmt.Printf(" %s\n", hugoPathTrim(trim(dir, pathPrefix, ".md")))
|
target := strings.Split(processTarget(target), "#")[0]
|
||||||
|
fmt.Printf(" %s\n", target)
|
||||||
links = append(links, Link{
|
links = append(links, Link{
|
||||||
Source: hugoPathTrim(trim(dir, pathPrefix, ".md")),
|
Source: hugoPathTrim(trim(dir, pathPrefix, ".md")),
|
||||||
Target: strings.Split(processTarget(target), "#")[0],
|
Target: target,
|
||||||
Text: text,
|
Text: text,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return links
|
return links
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getText(dir string) string {
|
||||||
|
// read file
|
||||||
|
bytes, err := ioutil.ReadFile(dir)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
// recursively walk directory and return all files with given extension
|
// recursively walk directory and return all files with given extension
|
||||||
func walk(root, ext string) (res []Link) {
|
func walk(root, ext string, index bool) (res []Link, i ContentIndex) {
|
||||||
println(root)
|
println(root)
|
||||||
|
i = make(ContentIndex)
|
||||||
|
titleRegex := regexp.MustCompile(`title: "(.*)"`)
|
||||||
|
|
||||||
err := filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
|
err := filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
if filepath.Ext(d.Name()) == ext {
|
if filepath.Ext(d.Name()) == ext {
|
||||||
res = append(res, parse(s, root)...)
|
res = append(res, parse(s, root)...)
|
||||||
|
if index {
|
||||||
|
text := getText(s)
|
||||||
|
matches := titleRegex.FindStringSubmatch(text)
|
||||||
|
var title string
|
||||||
|
if len(matches) > 1 {
|
||||||
|
title = matches[1]
|
||||||
|
} else {
|
||||||
|
title = "Untitled Page"
|
||||||
|
}
|
||||||
|
|
||||||
|
adjustedPath := hugoPathTrim(trim(s, root, ".md"))
|
||||||
|
i[adjustedPath] = Content{
|
||||||
|
Title: title,
|
||||||
|
Content: text,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -84,7 +122,7 @@ func walk(root, ext string) (res []Link) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("parsed %d total links \n", len(res))
|
fmt.Printf("parsed %d total links \n", len(res))
|
||||||
return res
|
return res, i
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter out certain links (e.g. to media)
|
// filter out certain links (e.g. to media)
|
||||||
@ -125,7 +163,7 @@ func index(links []Link) (index Index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const message = "# THIS FILE WAS GENERATED using github.com/jackyzha0/hugo-obsidian\n# DO NOT EDIT\n"
|
const message = "# THIS FILE WAS GENERATED using github.com/jackyzha0/hugo-obsidian\n# DO NOT EDIT\n"
|
||||||
func write(links []Link, out string) error {
|
func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) error {
|
||||||
index := index(links)
|
index := index(links)
|
||||||
resStruct := struct{
|
resStruct := struct{
|
||||||
Index Index
|
Index Index
|
||||||
@ -143,16 +181,30 @@ func write(links []Link, out string) error {
|
|||||||
if writeErr != nil {
|
if writeErr != nil {
|
||||||
return writeErr
|
return writeErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if toIndex {
|
||||||
|
marshalledContentIndex, mcErr := yaml.Marshal(&contentIndex)
|
||||||
|
if mcErr != nil {
|
||||||
|
return mcErr
|
||||||
|
}
|
||||||
|
|
||||||
|
writeErr = ioutil.WriteFile(path.Join(out, "contentIndex.yaml"), append([]byte(message), marshalledContentIndex...), 0644)
|
||||||
|
if writeErr != nil {
|
||||||
|
return writeErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
in := flag.String("input", ".", "Input Directory")
|
in := flag.String("input", ".", "Input Directory")
|
||||||
out := flag.String("output", ".", "Output Directory")
|
out := flag.String("output", ".", "Output Directory")
|
||||||
|
index := flag.Bool("index", false, "Whether to index the content")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
l := walk(*in, ".md")
|
l, i := walk(*in, ".md", *index)
|
||||||
f := filter(l)
|
f := filter(l)
|
||||||
err := write(f, *out)
|
err := write(f, i, *index, *out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user