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