diff --git a/go.mod b/go.mod index 8003706..d3ec720 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,10 @@ 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 + github.com/PuerkitoBio/goquery v1.8.0 + github.com/abhinav/goldmark-wikilink v0.3.0 + github.com/gernest/front v0.0.0-20210301115436-8a0b0a782d0a + github.com/yuin/goldmark v1.4.4 + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index 710c751..2377062 100644 --- a/go.sum +++ b/go.sum @@ -1,26 +1,32 @@ +github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= +github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= +github.com/abhinav/goldmark-wikilink v0.3.0 h1:ry8CBaULn410PKCSkwLz/WVI2f/g7EB+yqY7LKHDcPQ= +github.com/abhinav/goldmark-wikilink v0.3.0/go.mod h1:MHRZiLRE1ZDZDjHCFYwKEEgITXGbB7N0Yr00dbmfHM8= +github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= +github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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= -github.com/golang-commonmark/linkify v0.0.0-20180910111149-f05efb453a0e/go.mod h1:LeGY+WmPag1dlC0xKy+xYXommuQLX0O9gGXOP8SdBmY= -github.com/golang-commonmark/markdown v0.0.0-20180910011815-a8f139058164 h1:YaQaotRjMcVth1VzHUEQlD2oeyQAglA7CXdxp9QLvKM= -github.com/golang-commonmark/markdown v0.0.0-20180910011815-a8f139058164/go.mod h1:TRHkBtRpQJqTtJnCqhNU5oPkZw5pMr8Ac6DHndXYd5k= -github.com/golang-commonmark/mdurl v0.0.0-20180910110917-8d018c6567d6 h1:XkgfhPs5AotQfcu3EfDEjyAUx91KdtjrxHXYGnZJhoU= -github.com/golang-commonmark/mdurl v0.0.0-20180910110917-8d018c6567d6/go.mod h1:J66ZGl/dC2mj4ElzGfrLUq0N90HvQoUbrYgYNJUjuMs= -github.com/golang-commonmark/puny v0.0.0-20180910110745-050be392d8b8 h1:DUgQdQmDg4sk4SfNR+qOkXcopGz36BL02vp/V7WbPQI= -github.com/golang-commonmark/puny v0.0.0-20180910110745-050be392d8b8/go.mod h1:/8a6mcbf/Hwg6MjnHHp5vqCWw0Bsves9HLPObHAj7XA= -github.com/gomarkdown/markdown v0.0.0-20210514010506-3b9f47219fe7 h1:oKYOfNR7Hp6XpZ4JqolL5u642Js5Z0n7psPVl+S5heo= -github.com/gomarkdown/markdown v0.0.0-20210514010506-3b9f47219fe7/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU= -github.com/nikitavoloboev/markdown-parser v0.0.0-20200626055436-f545e8dd85d3 h1:68qdIIXf2kCV7+LlIM78OtcdKh3TXO00nOqraFor+VI= -github.com/nikitavoloboev/markdown-parser v0.0.0-20200626055436-f545e8dd85d3/go.mod h1:cL/O6Dew5Lsfksx+WljXgEf4gZsRjBXSjI7SuibGcEY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.4.4 h1:zNWRjYUW32G9KirMXYHQHVNFkXvMI7LpgNW2AgYAoIs= +github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg= +golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 h1:/6y1LfuqNuQdHAm0jjtPtgRcxIxjVZgm5OTu8/QhZvk= +golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 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.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 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 2fbb657..86571a5 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,30 @@ package main import ( + "bytes" "flag" "fmt" "github.com/gernest/front" - md "github.com/nikitavoloboev/markdown-parser" "gopkg.in/yaml.v3" "io/fs" "io/ioutil" "path" "path/filepath" "strings" + + "github.com/PuerkitoBio/goquery" + wikilink "github.com/abhinav/goldmark-wikilink" + "github.com/yuin/goldmark" ) + +var md goldmark.Markdown +func init() { + md = goldmark.New( + goldmark.WithExtensions(&wikilink.Extender{}), + ) +} + type Link struct { Source string Target string @@ -47,7 +59,7 @@ func processTarget(source string) string { if strings.HasPrefix(source, "/") { return strings.TrimSuffix(source, ".md") } - return "/" + strings.TrimSuffix(source, ".md") + return "/" + strings.TrimSuffix(strings.TrimSuffix(source, ".html"), ".md") } func isInternal(link string) bool { @@ -57,23 +69,44 @@ func isInternal(link string) bool { // parse single file for links func parse(dir, pathPrefix string) []Link { // read file - bytes, err := ioutil.ReadFile(dir) + source, err := ioutil.ReadFile(dir) if err != nil { panic(err) } // parse md var links []Link - fmt.Printf("%s\n", trim(dir, pathPrefix, ".md")) - for text, target := range md.GetAllLinks(string(bytes)) { - target := strings.Split(processTarget(target), "#")[0] - fmt.Printf(" %s\n", target) + fmt.Printf("[Parsing note] %s\n", trim(dir, pathPrefix, ".md")) + + var buf bytes.Buffer + if err := md.Convert(source, &buf); err != nil { + panic(err) + } + + doc, err := goquery.NewDocumentFromReader(&buf) + var n int + doc.Find("a").Each(func(i int, s *goquery.Selection) { + text := strings.TrimSpace(s.Text()) + target, ok := s.Attr("href") + if !ok { + target = "#" + } + + target = strings.Replace(target, "%20", " ", -1) + target = strings.Split(processTarget(target), "#")[0] + target = strings.TrimSpace(target) + target = strings.Replace(target, " ", "-", -1) + + fmt.Printf(" '%s' => %s\n", text, target) links = append(links, Link{ Source: filepath.ToSlash(hugoPathTrim(trim(dir, pathPrefix, ".md"))), Target: target, Text: text, }) - } + n++ + }) + fmt.Printf(":: %d links\n", n) + return links } @@ -120,7 +153,7 @@ func walk(root, ext string, index bool) (res []Link, i ContentIndex) { // check if page is private if parsedPrivate, ok := frontmatter["draft"]; !ok || !parsedPrivate.(bool) { - adjustedPath := hugoPathTrim(trim(s, root, ".md")) + adjustedPath := strings.Replace(hugoPathTrim(trim(s, root, ".md")), " ", "-", -1) i[adjustedPath] = Content{ Title: title, Content: body,