Skip to content

Commit e6c1a30

Browse files
committed
improve the performance for autolinking
1 parent f56a2a1 commit e6c1a30

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

extensions/autolink_pages/autolink_pages.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,32 @@ import (
1919
//go:embed templates
2020
var templates embed.FS
2121

22-
type fileInfoByNameLength []Page
22+
type NormalizedPage struct {
23+
page Page
24+
normalizedName string
25+
}
26+
27+
type fileInfoByNameLength []NormalizedPage
2328

24-
func (a fileInfoByNameLength) Len() int { return len(a) }
25-
func (a fileInfoByNameLength) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
26-
func (a fileInfoByNameLength) Less(i, j int) bool { return len(a[i].Name()) > len(a[j].Name()) }
29+
func (a fileInfoByNameLength) Len() int { return len(a) }
30+
func (a fileInfoByNameLength) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
31+
func (a fileInfoByNameLength) Less(i, j int) bool {
32+
return len(a[i].normalizedName) > len(a[j].normalizedName)
33+
}
2734

28-
var autolinkPages []Page
35+
var autolinkPages []NormalizedPage
2936
var autolinkPage_lck sync.Mutex
3037

3138
func UpdatePagesList(Page) (err error) {
3239
autolinkPage_lck.Lock()
3340
defer autolinkPage_lck.Unlock()
3441

35-
ps := Pages(context.Background())
42+
ps := MapPage(context.Background(), func(p Page) NormalizedPage {
43+
return NormalizedPage{
44+
page: p,
45+
normalizedName: strings.ToLower(p.Name()),
46+
}
47+
})
3648
sort.Sort(fileInfoByNameLength(ps))
3749
autolinkPages = ps
3850
return

extensions/autolink_pages/parser.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ func (s *pageLinkParser) Parse(parent ast.Node, block text.Reader, pc parser.Con
4343

4444
var found Page
4545
var m int
46+
normalizedLine := strings.ToLower(string(line))
4647

4748
for _, p := range autolinkPages {
48-
if len(line) < len(p.Name()) {
49+
if len(line) < len(p.normalizedName) {
4950
continue
5051
}
5152

5253
// Found a page
53-
if strings.EqualFold(string(line[0:len(p.Name())]), p.Name()) {
54-
found = p
55-
m = len(p.Name())
54+
if strings.HasPrefix(normalizedLine, p.normalizedName) {
55+
found = p.page
56+
m = len(p.normalizedName)
5657
break
5758
}
5859
}

0 commit comments

Comments
 (0)