forked from benrowe/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_book_toc_search.go
105 lines (90 loc) · 2.49 KB
/
gen_book_toc_search.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"strings"
"github.com/kjk/notionapi"
"github.com/kjk/u"
)
/*
Generates a javascript file that looks like:
gTocItems = [
[${chapter or aticle id}, ${parentIdx}, ${childIdx}, ${title}, ${synonym 1}, ${synonym 2}, ...],
];
It's saved in wwww/essential/${bookname}/toc_search.js
Maybe: optimize this as a one long string to search instead of multiple strings
in an array. Use 0 to separate chapter/article. Use 1 to separate title
from synonims.
Also, have original and lower-cased version of the string. We search lower-cased
but show the original. That avoids lowercasing during search.
*/
const (
itemIdxURL = 0
itemIdxParent = 1
itemIdxFirstChild = 2
itemIdxTitle = 3
itemIdxFirstSynonym = 4
)
// TODO: make it recursive and with arbitrary nesting
func genBookTOCSearchMust(book *Book) {
var toc [][]interface{}
for _, chapter := range book.Chapters() {
title := strings.TrimSpace(chapter.Title)
uri := chapter.URLLastPath()
tocItem := []interface{}{uri, -1, -1, title}
toc = append(toc, tocItem)
chapIdx := len(toc) - 1
u.PanicIf(chapIdx < 0)
headings := chapter.Headings
for _, heading := range headings {
title := heading.Text
id := heading.ID
if len(id) > 0 {
id = uri + "#" + notionapi.ToDashID(id)
}
tocItem = []interface{}{id, chapIdx, -1, title}
toc = append(toc, tocItem)
}
for _, article := range chapter.Pages {
title := strings.TrimSpace(article.Title)
uri := article.URLLastPath()
tocItem = []interface{}{uri, chapIdx, -1, title}
for _, syn := range article.getSearch() {
tocItem = append(tocItem, syn)
}
toc = append(toc, tocItem)
headings := article.Headings
articleIdx := len(toc) - 1
for _, heading := range headings {
title := heading.Text
id := heading.ID
if len(id) > 0 {
id = uri + "#" + notionapi.ToDashID(id)
}
tocItem = []interface{}{id, articleIdx, -1, title}
toc = append(toc, tocItem)
}
}
}
// set first child idx from parent idx
for i, tocItem := range toc {
parentIdx := tocItem[itemIdxParent].(int)
if parentIdx != -1 {
parentTocItem := toc[parentIdx]
idx := parentTocItem[itemIdxFirstChild].(int)
if idx == -1 {
parentTocItem[itemIdxFirstChild] = i
}
}
}
var d []byte
var err error
if doMinify {
d, err = json.Marshal(toc)
} else {
d, err = json.MarshalIndent(toc, "", " ")
}
u.PanicIfErr(err)
s := "gTocItems = " + string(d) + ";"
book.tocData = []byte(s)
updateBookAppJS(book)
}