From 55d209053dd12288707fc480ff46dbd1fc2d4c69 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Sun, 24 Oct 2021 22:50:53 -0700 Subject: [PATCH 1/2] more cleaning up, add support for #3 --- README.md | 39 ++++----------------------------------- example_graphConfig.yaml | 14 -------------- go.mod | 1 + go.sum | 3 +++ main.go | 38 ++++++++++++++++++++++++++------------ 5 files changed, 34 insertions(+), 61 deletions(-) delete mode 100644 example_graphConfig.yaml diff --git a/README.md b/README.md index 05ffe4e..41ce473 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,14 @@ This repository comes to you in two parts. ## GitHub Action GitHub action and binary to scrape [Obsidian](http://obsidian.md/) vault for links and exposes them as a `.yml` file for easy consumption by [Hugo](https://gohugo.io/). ### Example Usage (Binary) -Read Markdown from the `/content` folder and place the resulting `linkIndex.yaml` into `/data` +Read Markdown from the `/content` folder and place the resulting `linkIndex.yaml` (and `contentIndex.yaml` if the `index` flag is enabled) into `/data` ```shell # Installation go install github.com/jackyzha0/hugo-obsidian # Run -hugo-obsidian -input=content -output=data +hugo-obsidian -input=content -output=data -index=true ``` ### Example Usage (GitHub Action) @@ -35,37 +35,6 @@ jobs: with: input: content # input folder output: data # output folder + index: true # whether to index content ... -``` - -## Hugo Partial -To then embed this information in your Hugo site, you can copy and use the provided partials in `/partials`. Graph provides a graph view of all nodes and links and Backlinks provides a list of pages that link to this page. - -To start, create a `graphConfig.yaml` file in `/data` in your Hugo folder. This will be our main point of configuration for the graph partial. - -Then, in one of your Hugo templates, do something like the following to render the graph. - -```html -
- {{partial "graph_partial.html" .}} -
-``` - -### Configuration -Example: - -```yaml -enableLegend: false -enableDrag: true -enableZoom: false -base: - node: "#284b63" - activeNode: "#f28482" - inactiveNode: "#a8b3bd" - link: "#babdbf" - activeLink: "#5a7282" -paths: - - /toc: "#4388cc" - - /newsletters: "#e0b152" - - /posts: "#42c988" -``` +``` \ No newline at end of file diff --git a/example_graphConfig.yaml b/example_graphConfig.yaml deleted file mode 100644 index adb9c6e..0000000 --- a/example_graphConfig.yaml +++ /dev/null @@ -1,14 +0,0 @@ -enableLegend: false -enableDrag: true -enableZoom: false -base: - node: "#284b63" - activeNode: "#f28482" - inactiveNode: "#a8b3bd" - hoverNode: "#afbfc9" - link: "#aeb4b8" - activeLink: "#5a7282" -paths: - - /toc: "#4388cc" - - /newsletters: "#e0b152" - - /posts: "#42c988" \ No newline at end of file diff --git a/go.mod b/go.mod index 33b1cdc..8003706 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/jackyzha0/hugo-obsidian go 1.16 require ( + github.com/gernest/front v0.0.0-20210301115436-8a0b0a782d0a // indirect github.com/nikitavoloboev/markdown-parser v0.0.0-20200626055436-f545e8dd85d3 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index b2fc18b..710c751 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gernest/front v0.0.0-20210301115436-8a0b0a782d0a h1:z7BePknRd4Nz3CeWDhcmCkuCliM2YY/RnjWpdPUuQQo= +github.com/gernest/front v0.0.0-20210301115436-8a0b0a782d0a/go.mod h1:FwEMwQ5+xky8tbzDLj72k2RAqXnFByLNwxg+9UZDtqU= github.com/golang-commonmark/html v0.0.0-20180910111043-7d7c804e1d46 h1:FeNEDxIy7XouGTJKiJ9Ze5vUbcAIW/FRhQbtKBNmEz8= github.com/golang-commonmark/html v0.0.0-20180910111043-7d7c804e1d46/go.mod h1:LVbxopYhspqklDpfaS/qDc6HhWwkpF1ptTj3vMFRoSQ= github.com/golang-commonmark/linkify v0.0.0-20180910111149-f05efb453a0e h1:TkuRzcq232K5ytXtQ+BPicsjYWZgt/lS6gJ5HqcUifQ= @@ -18,6 +20,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 7fa5f6d..3647485 100644 --- a/main.go +++ b/main.go @@ -3,13 +3,13 @@ package main import ( "flag" "fmt" + "github.com/gernest/front" md "github.com/nikitavoloboev/markdown-parser" "gopkg.in/yaml.v3" "io/fs" "io/ioutil" "path" "path/filepath" - "regexp" "strings" ) @@ -91,7 +91,10 @@ func getText(dir string) string { func walk(root, ext string, index bool) (res []Link, i ContentIndex) { println(root) i = make(ContentIndex) - titleRegex := regexp.MustCompile(`title: "(.*)"`) + + m := front.NewMatter() + m.Handle("---", front.YAMLHandler) + nPrivate := 0 err := filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error { if e != nil { @@ -101,18 +104,28 @@ func walk(root, ext string, index bool) (res []Link, i ContentIndex) { res = append(res, parse(s, root)...) if index { text := getText(s) - matches := titleRegex.FindStringSubmatch(text) + + frontmatter, body, err := m.Parse(strings.NewReader(text)) + if err != nil { + panic(err) + } + var title string - if len(matches) > 1 { - title = matches[1] + if parsedTitle, ok := frontmatter["title"]; ok { + title = parsedTitle.(string) } else { title = "Untitled Page" } - adjustedPath := hugoPathTrim(trim(s, root, ".md")) - i[adjustedPath] = Content{ - Title: title, - Content: text, + // check if page is private + if parsedPrivate, ok := frontmatter["draft"]; !ok || !parsedPrivate.(bool) { + adjustedPath := hugoPathTrim(trim(s, root, ".md")) + i[adjustedPath] = Content{ + Title: title, + Content: body, + } + } else { + nPrivate++ } } } @@ -121,7 +134,8 @@ func walk(root, ext string, index bool) (res []Link, i ContentIndex) { if err != nil { panic(err) } - fmt.Printf("parsed %d total links \n", len(res)) + fmt.Printf("Ignored %d private files \n", nPrivate) + fmt.Printf("Parsed %d total links \n", len(res)) return res, i } @@ -134,7 +148,7 @@ func filter(links []Link) (res []Link) { res = append(res, l) } } - fmt.Printf("removed %d external and non-markdown links\n", len(links) - len(res)) + fmt.Printf("Removed %d external and non-markdown links\n", len(links) - len(res)) return res } @@ -162,7 +176,7 @@ func index(links []Link) (index Index) { return 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, contentIndex ContentIndex, toIndex bool, out string) error { index := index(links) resStruct := struct{ From 15440a1f592349da4e8bfd34e7edeafd76fbd5a5 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Sun, 24 Oct 2021 22:56:24 -0700 Subject: [PATCH 2/2] fix unexpected panic --- README.md | 2 +- main.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 41ce473..ac6392a 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,4 @@ jobs: output: data # output folder index: true # whether to index content ... -``` \ No newline at end of file +``` diff --git a/main.go b/main.go index f0b2e96..2fbb657 100644 --- a/main.go +++ b/main.go @@ -107,7 +107,8 @@ func walk(root, ext string, index bool) (res []Link, i ContentIndex) { frontmatter, body, err := m.Parse(strings.NewReader(text)) if err != nil { - panic(err) + frontmatter = map[string]interface{}{} + body = text } var title string