mirror of
https://github.com/jackyzha0/hugo-obsidian.git
synced 2024-06-14 11:42:35 +03:00
54 lines
1004 B
Go
54 lines
1004 B
Go
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"))
|
|
|
|
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"))
|
|
|
|
// fmt.Printf(" '%s' => %s\n", source, target)
|
|
if !strings.HasPrefix(text, "^"){
|
|
links = append(links, Link{
|
|
Source: source,
|
|
Target: target,
|
|
Text: text,
|
|
})
|
|
n++
|
|
}
|
|
})
|
|
fmt.Printf("found: %d links\n", n)
|
|
|
|
return links
|
|
}
|