mirror of
https://github.com/jackyzha0/hugo-obsidian.git
synced 2024-06-14 11:42:35 +03:00
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"github.com/PuerkitoBio/goquery"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// parse single file for links
|
|
func parse(dir, pathPrefix string) []Link {
|
|
// read file
|
|
source, err := ioutil.ReadFile(dir)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// parse md
|
|
var links []Link
|
|
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(" Found: %d links\n", n)
|
|
|
|
return links
|
|
}
|