hugo-obsidian/parse.go
2021-12-27 16:19:05 -05:00

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
}