Use block ids to deduplicate backref lines
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
d8cf6ffddc
commit
1003c19662
30
backref.go
30
backref.go
|
@ -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...)
|
||||||
|
}
|
||||||
|
} 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 {
|
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})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
5
util.go
5
util.go
|
@ -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})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user