more cleaning up, add support for #3

This commit is contained in:
Jacky Zhao 2021-10-24 22:50:53 -07:00
parent fcef39bb73
commit 55d209053d
5 changed files with 34 additions and 61 deletions

View File

@ -9,14 +9,14 @@ This repository comes to you in two parts.
## GitHub Action ## 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/). 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) ### 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 ```shell
# Installation # Installation
go install github.com/jackyzha0/hugo-obsidian go install github.com/jackyzha0/hugo-obsidian
# Run # Run
hugo-obsidian -input=content -output=data hugo-obsidian -input=content -output=data -index=true
``` ```
### Example Usage (GitHub Action) ### Example Usage (GitHub Action)
@ -35,37 +35,6 @@ jobs:
with: with:
input: content # input folder input: content # input folder
output: data # output 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
<div id="graph-container">
{{partial "graph_partial.html" .}}
</div>
```
### 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"
```

View File

@ -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"

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/jackyzha0/hugo-obsidian
go 1.16 go 1.16
require ( require (
github.com/gernest/front v0.0.0-20210301115436-8a0b0a782d0a // indirect
github.com/nikitavoloboev/markdown-parser v0.0.0-20200626055436-f545e8dd85d3 // indirect github.com/nikitavoloboev/markdown-parser v0.0.0-20200626055436-f545e8dd85d3 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
) )

3
go.sum
View File

@ -1,4 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 h1:FeNEDxIy7XouGTJKiJ9Ze5vUbcAIW/FRhQbtKBNmEz8=
github.com/golang-commonmark/html v0.0.0-20180910111043-7d7c804e1d46/go.mod h1:LVbxopYhspqklDpfaS/qDc6HhWwkpF1ptTj3vMFRoSQ= 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= 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= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ= 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/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.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 h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

32
main.go
View File

@ -3,13 +3,13 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/gernest/front"
md "github.com/nikitavoloboev/markdown-parser" md "github.com/nikitavoloboev/markdown-parser"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
) )
@ -91,7 +91,10 @@ func getText(dir string) string {
func walk(root, ext string, index bool) (res []Link, i ContentIndex) { func walk(root, ext string, index bool) (res []Link, i ContentIndex) {
println(root) println(root)
i = make(ContentIndex) 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 { err := filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
if e != nil { if e != nil {
@ -101,18 +104,28 @@ func walk(root, ext string, index bool) (res []Link, i ContentIndex) {
res = append(res, parse(s, root)...) res = append(res, parse(s, root)...)
if index { if index {
text := getText(s) text := getText(s)
matches := titleRegex.FindStringSubmatch(text)
frontmatter, body, err := m.Parse(strings.NewReader(text))
if err != nil {
panic(err)
}
var title string var title string
if len(matches) > 1 { if parsedTitle, ok := frontmatter["title"]; ok {
title = matches[1] title = parsedTitle.(string)
} else { } else {
title = "Untitled Page" title = "Untitled Page"
} }
// check if page is private
if parsedPrivate, ok := frontmatter["draft"]; !ok || !parsedPrivate.(bool) {
adjustedPath := hugoPathTrim(trim(s, root, ".md")) adjustedPath := hugoPathTrim(trim(s, root, ".md"))
i[adjustedPath] = Content{ i[adjustedPath] = Content{
Title: title, Title: title,
Content: text, Content: body,
}
} else {
nPrivate++
} }
} }
} }
@ -121,7 +134,8 @@ func walk(root, ext string, index bool) (res []Link, i ContentIndex) {
if err != nil { if err != nil {
panic(err) 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 return res, i
} }
@ -134,7 +148,7 @@ func filter(links []Link) (res []Link) {
res = append(res, l) 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 return res
} }
@ -162,7 +176,7 @@ func index(links []Link) (index Index) {
return 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 { func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) error {
index := index(links) index := index(links)
resStruct := struct{ resStruct := struct{