hugo-obsidian/main.go

84 lines
1.7 KiB
Go
Raw Normal View History

2021-07-17 06:26:40 +03:00
package main
import (
2021-07-17 07:51:06 +03:00
"flag"
2021-12-28 00:51:36 +03:00
"github.com/BurntSushi/toml"
wikilink "github.com/abhinav/goldmark-wikilink"
"github.com/yuin/goldmark"
2021-12-28 00:51:36 +03:00
"io/ioutil"
"path/filepath"
2021-12-28 00:58:21 +03:00
"time"
2021-07-17 06:26:40 +03:00
)
var md goldmark.Markdown
2021-12-28 00:19:05 +03:00
func init() {
md = goldmark.New(
goldmark.WithExtensions(&wikilink.Extender{}),
)
}
2021-07-17 06:26:40 +03:00
type Link struct {
2022-02-16 03:36:14 +03:00
Source string `json:"source"`
Target string `json:"target"`
Text string `json:"text"`
2021-07-17 06:26:40 +03:00
}
2021-07-17 07:01:08 +03:00
type LinkTable = map[string][]Link
type Index struct {
2022-02-16 03:36:14 +03:00
Links LinkTable `json:"links"`
Backlinks LinkTable `json:"backlinks"`
2021-07-17 07:01:08 +03:00
}
2021-08-25 20:05:12 +03:00
type Content struct {
2022-02-16 03:36:14 +03:00
Title string `json:"title"`
Content string `json:"content"`
LastModified time.Time `json:"lastmodified"`
Tags []string `json:"tags"`
2021-08-25 20:05:12 +03:00
}
type ContentIndex = map[string]Content
2021-12-28 00:51:36 +03:00
type ConfigTOML struct {
IgnoredFiles []string `toml:"ignoreFiles"`
2021-07-18 23:30:37 +03:00
}
2021-12-28 00:51:36 +03:00
func getIgnoredFiles(base string) (res map[string]struct{}) {
res = make(map[string]struct{})
2021-08-25 20:05:12 +03:00
2021-12-28 00:51:36 +03:00
source, err := ioutil.ReadFile(base + "/config.toml")
if err != nil {
return res
}
var config ConfigTOML
if _, err := toml.Decode(string(source), &config); err != nil {
return res
}
for _, glb := range config.IgnoredFiles {
matches, _ := filepath.Glob(base + glb)
for _, match := range matches {
res[match] = struct{}{}
}
}
return res
2021-07-17 07:38:12 +03:00
}
2021-07-17 06:26:40 +03:00
func main() {
2021-07-17 07:51:06 +03:00
in := flag.String("input", ".", "Input Directory")
out := flag.String("output", ".", "Output Directory")
2021-12-28 00:51:36 +03:00
root := flag.String("root", "..", "Root Directory (for config parsing)")
2021-08-25 20:05:12 +03:00
index := flag.Bool("index", false, "Whether to index the content")
2021-07-17 07:51:06 +03:00
flag.Parse()
2021-12-28 00:51:36 +03:00
ignoreBlobs := getIgnoredFiles(*root)
l, i := walk(*in, ".md", *index, ignoreBlobs)
2021-07-17 07:38:12 +03:00
f := filter(l)
2021-08-25 20:05:12 +03:00
err := write(f, i, *index, *out)
2021-07-17 07:38:12 +03:00
if err != nil {
panic(err)
}
2021-07-17 06:26:40 +03:00
}