This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
/
wikilink.go
71 lines (62 loc) · 1.62 KB
/
wikilink.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"github.com/blevesearch/bleve"
"bytes"
"strings"
)
func linkWorker(out *bytes.Buffer, b []byte) {
link := string(b)
found := resolveLink(link)
var href string
if len(found) != 0 { // page found
// TODO disambiguation page
log.Debug("%s -> %s", link, found[0])
href = queryEscapeWikiPath(found[0])
out.WriteString(`<a href="` + href + `">` + link + `</a>`)
} else { // page not found
log.Debug("%s -> NOTFOUND", link)
href = queryEscapeWikiPath(link)
out.WriteString(`<a class="notfound" href="` + href + `">` + link + `</a>`)
}
}
func resolveLink(link string) []string {
if strings.Contains(link, "/") || isMarkdown(link) {
// absolute and relative path not supported
return nil
}
log.Debug("Searching name... term: %s", link)
query := bleve.NewTermQuery(link)
query.SetField("name")
request := bleve.NewSearchRequest(query)
request.Fields = []string{"name"}
result, err := bleveIndex.Search(request)
if err != nil {
log.Debug("Error: %v", err)
}
log.Debug(result.String())
if result.Total > 0 {
links := make([]string, len(result.Hits))
for i, hit := range result.Hits {
links[i] = hit.ID
}
return links
}
log.Debug("Searching aliases... term: %s", link)
query.SetField("aliases")
request = bleve.NewSearchRequest(query)
request.Fields = []string{"aliases"}
result, err = bleveIndex.Search(request)
if err != nil {
log.Debug("Error: %v", err)
}
log.Debug(result.String())
if result.Total > 0 {
log.Debug(result.String())
links := make([]string, len(result.Hits))
for i, hit := range result.Hits {
links[i] = hit.ID
}
return links
}
return nil
}