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
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
}
content = pageText
links = append(links, foundLinks...)
}
links, err := ParseLinks(content)
} else {
links, err = ParseLinks(page.Name, page.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})
}

View File

@ -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})
}
}