diff --git a/backref.go b/backref.go index b316af2..ad6d89b 100644 --- a/backref.go +++ b/backref.go @@ -50,28 +50,36 @@ func processBackrefsForPage(page Page, refs Refs) error { content := page.Content var listItems []struct { + Id string Indented int Text string } + var links []ParsedLink err := json.NewDecoder(strings.NewReader(content)).Decode(&listItems) if err == nil { - pageText := "" for _, item := range listItems { - pageText += strings.Repeat(" ", item.Indented) + "* " + item.Text + "\n" + foundLinks, err := ParseLinks(item.Id, item.Text) + if err != nil { + continue + } + links = append(links, foundLinks...) + } + } else { + links, err = ParseLinks(page.Name, page.Content) + if err != nil { + return err } - - content = pageText - } - - links, err := ParseLinks(content) - - if err != nil { - return err } +link: for _, link := range links { - // FIXME: this produces duplicates + for i, ref := range refs[link.PageName] { + if ref.Link.Id == link.Id { + refs[link.PageName][i].Link = link + continue link + } + } refs[link.PageName] = append(refs[link.PageName], Reference{link, page.Name}) } diff --git a/util.go b/util.go index 8005c09..c20895e 100644 --- a/util.go +++ b/util.go @@ -10,6 +10,7 @@ import ( ) type ParsedLink struct { + Id string Name string PageName string Line string @@ -31,7 +32,7 @@ func RandStringBytes(n int) string { return string(b) } -func ParseLinks(content string) ([]ParsedLink, error) { +func ParseLinks(blockId string, content string) ([]ParsedLink, error) { hrefRE := regexp.MustCompile(`(#?\[\[\s*([^\]]+)\s*\]\])`) scanner := bufio.NewScanner(strings.NewReader(content)) @@ -56,7 +57,7 @@ func ParseLinks(content string) ([]ParsedLink, error) { link = strings.TrimSuffix(link, "]]") link = strings.TrimSpace(link) l := cleanNameURL(link) - result = append(result, ParsedLink{link, l, line}) + result = append(result, ParsedLink{blockId, link, l, line}) } }