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
| ; (async function() {
| const encoder = (str) => str.toLowerCase().split(/([^a-z]|[^\x00-\x7F])+/)
| const contentIndex = new FlexSearch.Document({
| cache: true,
| charset: "latin:extra",
| optimize: true,
| index: [
| {
| field: "content",
| tokenize: "reverse",
| encode: encoder,
| },
| {
| field: "title",
| tokenize: "forward",
| encode: encoder,
| },
| ],
| })
|
| const { content } = await fetchData
| for (const [key, value] of Object.entries(content)) {
| contentIndex.add({
| id: key,
| title: value.title,
| content: removeMarkdown(value.content),
| })
| }
|
| const formatForDisplay = (id) => ({
| id,
| url: id,
| title: content[id].title,
| content: content[id].content,
| })
|
| registerHandlers((e) => {
| term = e.target.value
| const searchResults = contentIndex.search(term, [
| {
| field: "content",
| limit: 10,
| },
| {
| field: "title",
| limit: 5,
| },
| ])
| const getByField = (field) => {
| const results = searchResults.filter((x) => x.field === field)
| if (results.length === 0) {
| return []
| } else {
| return [...results[0].result]
| }
| }
| const allIds = new Set([...getByField("title"), ...getByField("content")])
| const finalResults = [...allIds].map(formatForDisplay)
| displayResults(finalResults, true)
| })
| })()
|
|