hugo-obsidian/parse.go

52 lines
958 B
Go
Raw Permalink Normal View History

2021-12-28 00:19:05 +03:00
package main
import (
"bytes"
"fmt"
"github.com/PuerkitoBio/goquery"
"io/ioutil"
"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 => ", trim(dir, pathPrefix, ".md"))
2021-12-28 00:19:05 +03:00
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 = processTarget(target)
source := processSource(trim(dir, pathPrefix, ".md"))
2021-12-28 00:19:05 +03:00
// fmt.Printf(" '%s' => %s\n", source, target)
2021-12-28 00:19:05 +03:00
links = append(links, Link{
Source: source,
Target: target,
2021-12-28 00:19:05 +03:00
Text: text,
})
n++
})
fmt.Printf("found: %d links\n", n)
2021-12-28 00:19:05 +03:00
return links
}