Use block ids to deduplicate backref lines
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Peter Stuifzand 2020-07-01 00:49:42 +02:00
parent d8cf6ffddc
commit 1003c19662
2 changed files with 22 additions and 13 deletions

View File

@ -50,28 +50,36 @@ func processBackrefsForPage(page Page, refs Refs) error {
content := page.Content content := page.Content
var listItems []struct { var listItems []struct {
Id string
Indented int Indented int
Text string Text string
} }
var links []ParsedLink
err := json.NewDecoder(strings.NewReader(content)).Decode(&listItems) err := json.NewDecoder(strings.NewReader(content)).Decode(&listItems)
if err == nil { if err == nil {
pageText := ""
for _, item := range listItems { 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...)
content = pageText
} }
} else {
links, err := ParseLinks(content) links, err = ParseLinks(page.Name, page.Content)
if err != nil { if err != nil {
return err return err
} }
}
link:
for _, link := range links { 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}) refs[link.PageName] = append(refs[link.PageName], Reference{link, page.Name})
} }

View File

@ -10,6 +10,7 @@ import (
) )
type ParsedLink struct { type ParsedLink struct {
Id string
Name string Name string
PageName string PageName string
Line string Line string
@ -31,7 +32,7 @@ func RandStringBytes(n int) string {
return string(b) return string(b)
} }
func ParseLinks(content string) ([]ParsedLink, error) { func ParseLinks(blockId string, content string) ([]ParsedLink, error) {
hrefRE := regexp.MustCompile(`(#?\[\[\s*([^\]]+)\s*\]\])`) hrefRE := regexp.MustCompile(`(#?\[\[\s*([^\]]+)\s*\]\])`)
scanner := bufio.NewScanner(strings.NewReader(content)) scanner := bufio.NewScanner(strings.NewReader(content))
@ -56,7 +57,7 @@ func ParseLinks(content string) ([]ParsedLink, error) {
link = strings.TrimSuffix(link, "]]") link = strings.TrimSuffix(link, "]]")
link = strings.TrimSpace(link) link = strings.TrimSpace(link)
l := cleanNameURL(link) l := cleanNameURL(link)
result = append(result, ParsedLink{link, l, line}) result = append(result, ParsedLink{blockId, link, l, line})
} }
} }