@codecademy/brand 0.13.48 → 0.13.50-alpha.14a36c6df8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AppHeader/Search/SearchButton.js +0 -19
- package/dist/AppHeader/Search/SearchPane.js +11 -16
- package/dist/AppHeader/Search/SearchWorker/PromiseLookup.d.ts +18 -0
- package/dist/AppHeader/Search/SearchWorker/PromiseLookup.js +35 -0
- package/dist/AppHeader/Search/SearchWorker/index.d.ts +4 -34
- package/dist/AppHeader/Search/SearchWorker/index.js +22 -73
- package/dist/AppHeader/Search/SearchWorker/src.d.ts +0 -1
- package/dist/AppHeader/Search/SearchWorker/src.js +195 -199
- package/dist/AppHeader/Search/SearchWorker/types.d.ts +33 -0
- package/dist/AppHeader/Search/SearchWorker/types.js +5 -0
- package/dist/AppHeader/Search/SearchWorker/worker.d.ts +2 -0
- package/dist/AppHeader/Search/SearchWorker/worker.js +32 -0
- package/dist/EmptySection/index.d.ts +1 -1
- package/dist/EmptySection/index.js +30 -9
- package/package.json +2 -2
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type Resolver<T> = (value: T) => void;
|
|
2
|
+
declare class SplitPromise<T> {
|
|
3
|
+
promise: Promise<T>;
|
|
4
|
+
resolver: Resolver<T>;
|
|
5
|
+
constructor();
|
|
6
|
+
}
|
|
7
|
+
export declare class PromiseLookup<Types> {
|
|
8
|
+
private args;
|
|
9
|
+
constructor(args: {
|
|
10
|
+
onKeyInit: (namespace: keyof Types, key: string) => void;
|
|
11
|
+
});
|
|
12
|
+
namespaces: Map<keyof Types, Map<string, SplitPromise<unknown>>>;
|
|
13
|
+
private getNamespace;
|
|
14
|
+
private getSplitPromise;
|
|
15
|
+
get<N extends keyof Types>(namespace: N, key: string): Promise<Types[N]>;
|
|
16
|
+
resolve<N extends keyof Types>(namespace: N, key: string, value: Types[N]): void;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
class SplitPromise {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.promise = new Promise(r => this.resolver = r);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export class PromiseLookup {
|
|
7
|
+
constructor(args) {
|
|
8
|
+
this.args = args;
|
|
9
|
+
}
|
|
10
|
+
namespaces = (() => new Map())();
|
|
11
|
+
getNamespace(namespace) {
|
|
12
|
+
let ns = this.namespaces.get(namespace);
|
|
13
|
+
if (!ns) {
|
|
14
|
+
ns = new Map();
|
|
15
|
+
this.namespaces.set(namespace, ns);
|
|
16
|
+
}
|
|
17
|
+
return ns;
|
|
18
|
+
}
|
|
19
|
+
getSplitPromise(namespace, key) {
|
|
20
|
+
const ns = this.getNamespace(namespace);
|
|
21
|
+
let splitPromise = ns.get(key);
|
|
22
|
+
if (!splitPromise) {
|
|
23
|
+
splitPromise = new SplitPromise();
|
|
24
|
+
ns.set(key, splitPromise);
|
|
25
|
+
this.args.onKeyInit(namespace, key);
|
|
26
|
+
}
|
|
27
|
+
return splitPromise;
|
|
28
|
+
}
|
|
29
|
+
get(namespace, key) {
|
|
30
|
+
return this.getSplitPromise(namespace, key).promise;
|
|
31
|
+
}
|
|
32
|
+
resolve(namespace, key, value) {
|
|
33
|
+
this.getSplitPromise(namespace, key).resolver(value);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -1,35 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export declare const searchWorker: {
|
|
2
|
+
init(): void;
|
|
3
|
+
autocomplete(query: string): Promise<import("./types").AutocompleteSuggestion[]>;
|
|
4
|
+
searchAsYouType(query: string): Promise<import("./types").SearchAsYouTypeResults>;
|
|
4
5
|
};
|
|
5
|
-
export type AutocompleteSuggestion = {
|
|
6
|
-
title: string;
|
|
7
|
-
segments: HighlightSegment[];
|
|
8
|
-
key: string;
|
|
9
|
-
};
|
|
10
|
-
export type SearchAsYouTypeResults = {
|
|
11
|
-
query: string;
|
|
12
|
-
top: {
|
|
13
|
-
type: string;
|
|
14
|
-
title: string;
|
|
15
|
-
slug: string;
|
|
16
|
-
contentId?: string;
|
|
17
|
-
urlPath: string;
|
|
18
|
-
key: string;
|
|
19
|
-
segments: HighlightSegment[];
|
|
20
|
-
}[];
|
|
21
|
-
queryLoadTime: number;
|
|
22
|
-
searchId: string;
|
|
23
|
-
resultsCount: number;
|
|
24
|
-
};
|
|
25
|
-
declare const Worker: {
|
|
26
|
-
new (scriptURL: string | URL, options?: WorkerOptions): Worker;
|
|
27
|
-
prototype: Worker;
|
|
28
|
-
};
|
|
29
|
-
export declare class SearchWorker extends Worker {
|
|
30
|
-
constructor();
|
|
31
|
-
private promises;
|
|
32
|
-
autocomplete(query: string, max: number): Promise<AutocompleteSuggestion[]>;
|
|
33
|
-
searchAsYouType(query: string, max: number): Promise<SearchAsYouTypeResults>;
|
|
34
|
-
}
|
|
35
|
-
export {};
|
|
@@ -1,75 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
16
|
-
postMessage() {}
|
|
17
|
-
};
|
|
18
|
-
export class SearchWorker extends Worker {
|
|
19
|
-
constructor() {
|
|
20
|
-
const src = getSearchWorkerSrc();
|
|
21
|
-
const blob = new Blob([src], {
|
|
22
|
-
type: 'text/javascript'
|
|
23
|
-
});
|
|
24
|
-
super(URL.createObjectURL?.(blob));
|
|
25
|
-
this.onmessage = _ref => {
|
|
26
|
-
let {
|
|
27
|
-
data
|
|
28
|
-
} = _ref;
|
|
29
|
-
const {
|
|
30
|
-
query,
|
|
31
|
-
topAutocompleteTitles,
|
|
32
|
-
searchAsYouTypeResults
|
|
33
|
-
} = data;
|
|
34
|
-
if (topAutocompleteTitles) {
|
|
35
|
-
this.promises.autocomplete.get(query)?.resolver(topAutocompleteTitles);
|
|
36
|
-
}
|
|
37
|
-
if (searchAsYouTypeResults) {
|
|
38
|
-
this.promises.searchAsYouType.get(query)?.resolver(searchAsYouTypeResults);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
promises = (() => ({
|
|
43
|
-
autocomplete: new Map(),
|
|
44
|
-
searchAsYouType: new Map()
|
|
45
|
-
}))();
|
|
46
|
-
autocomplete(query, max) {
|
|
47
|
-
const q = query.trim().toLowerCase();
|
|
48
|
-
let p = this.promises.autocomplete.get(q);
|
|
49
|
-
if (!p) {
|
|
50
|
-
p = new PromiseResolverPair();
|
|
51
|
-
this.promises.autocomplete.set(q, p);
|
|
52
|
-
this.postMessage({
|
|
53
|
-
action: 'autocomplete',
|
|
54
|
-
query: q,
|
|
55
|
-
max
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
return p.promise;
|
|
59
|
-
}
|
|
60
|
-
searchAsYouType(query, max) {
|
|
61
|
-
const q = query.trim().toLowerCase();
|
|
62
|
-
let p = this.promises.searchAsYouType.get(q);
|
|
63
|
-
if (!p) {
|
|
64
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
65
|
-
p = new PromiseResolverPair();
|
|
66
|
-
this.promises.searchAsYouType.set(q, p);
|
|
67
|
-
this.postMessage({
|
|
68
|
-
action: 'search-as-you-type',
|
|
69
|
-
query: q,
|
|
70
|
-
max
|
|
71
|
-
});
|
|
1
|
+
import { SearchAction } from './types';
|
|
2
|
+
let workerPromise;
|
|
3
|
+
const initErr = 'Search worker not initialized';
|
|
4
|
+
export const searchWorker = {
|
|
5
|
+
init() {
|
|
6
|
+
if (!workerPromise) {
|
|
7
|
+
workerPromise = (async () => {
|
|
8
|
+
// lazy load worker
|
|
9
|
+
const {
|
|
10
|
+
createSearchWorker
|
|
11
|
+
} = await import('./worker');
|
|
12
|
+
return createSearchWorker();
|
|
13
|
+
})();
|
|
72
14
|
}
|
|
73
|
-
|
|
15
|
+
},
|
|
16
|
+
async autocomplete(query) {
|
|
17
|
+
if (!workerPromise) throw new Error(initErr);
|
|
18
|
+
return (await workerPromise)(SearchAction.Autocomplete, query);
|
|
19
|
+
},
|
|
20
|
+
async searchAsYouType(query) {
|
|
21
|
+
if (!workerPromise) throw new Error(initErr);
|
|
22
|
+
return (await workerPromise)(SearchAction.SearchAsYouType, query);
|
|
74
23
|
}
|
|
75
|
-
}
|
|
24
|
+
};
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* but we still use this algorythm for character highlighting.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
function getPortalOrigin() {
|
|
23
23
|
const {
|
|
24
24
|
origin
|
|
25
25
|
} = window.location;
|
|
@@ -36,220 +36,216 @@ export function getPortalOrigin() {
|
|
|
36
36
|
// otherwise (e.g. in monolith pr env) use staging portal-app
|
|
37
37
|
return 'https://staging.codecademy.com';
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
const
|
|
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
|
-
// Used by autocomplete, both for finding top 5, and for highlighting
|
|
66
|
-
// Used by search-as-you-type, only for highlighting
|
|
67
|
-
// Returns number[], where each number is the match score for a char in the title
|
|
68
|
-
function getTitleCharScores(query, queryWordSet, titleLower) {
|
|
69
|
-
// prefill with an empty array for each character in title
|
|
70
|
-
const overlap = Array(titleLower.length)
|
|
71
|
-
.fill(0)
|
|
72
|
-
.map(() => []);
|
|
73
|
-
|
|
74
|
-
for (
|
|
75
|
-
let queryShift = 1 - query.length;
|
|
76
|
-
queryShift < titleLower.length;
|
|
77
|
-
queryShift++
|
|
78
|
-
) {
|
|
79
|
-
let cmc = null; // consecutive matching characters
|
|
80
|
-
const startOverlap = Math.max(queryShift, 0);
|
|
81
|
-
const endOverlap = Math.min(query.length + queryShift, titleLower.length);
|
|
82
|
-
for (let i = startOverlap; i < endOverlap; i++) {
|
|
83
|
-
if (query[i - queryShift] === titleLower[i] && titleLower[i] !== ' ') {
|
|
84
|
-
if (!cmc) {
|
|
85
|
-
// start a new cmc
|
|
86
|
-
cmc = { str: '' };
|
|
87
|
-
}
|
|
88
|
-
// append this char to the current cmc
|
|
89
|
-
cmc.str += query[i - queryShift];
|
|
90
|
-
overlap[i].push(cmc);
|
|
91
|
-
} else if (cmc) {
|
|
92
|
-
cmc = null; // set to null so that the next match starts a new cmc
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const avgQueryWordLength = query.length / queryWordSet.size;
|
|
98
|
-
|
|
99
|
-
// If avgQueryWordLength is 1-3, we want to the min match to be about that same size.
|
|
100
|
-
// If avgQueryWordLength is longer, we want min match to be longer...but not too long.
|
|
101
|
-
// e.g. For avgQueryWordLength of 9, min should be 5
|
|
102
|
-
// The misspelling "regresion" should still match "regression", but not "encryption",
|
|
103
|
-
// "authentication" or other words ending in "ion"
|
|
104
|
-
const minMatchLength = Math.ceil(avgQueryWordLength ** 0.65);
|
|
39
|
+
function worker() {
|
|
40
|
+
const splitWords = /[ :-]/g;
|
|
41
|
+
function getWordSet(str) {
|
|
42
|
+
const words = str.split(splitWords);
|
|
43
|
+
const filtered = words.map(w => w.trim()).filter(Boolean);
|
|
44
|
+
return new Set(filtered);
|
|
45
|
+
}
|
|
46
|
+
function preparseTitle(_ref) {
|
|
47
|
+
let {
|
|
48
|
+
value: title,
|
|
49
|
+
popularity
|
|
50
|
+
} = _ref;
|
|
51
|
+
const titleLower = title.toLowerCase();
|
|
52
|
+
const titleWordSet = getWordSet(titleLower);
|
|
53
|
+
return {
|
|
54
|
+
title,
|
|
55
|
+
titleLower,
|
|
56
|
+
titleWordSet,
|
|
57
|
+
popularity
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async function preloadAutocompleteTitles() {
|
|
61
|
+
const f = await fetch('{BASE_URL}/autocomplete-preload/{SEARCH}');
|
|
62
|
+
const titles = await f.json();
|
|
63
|
+
return titles.map(preparseTitle);
|
|
64
|
+
}
|
|
105
65
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
66
|
+
// Used by autocomplete, both for finding top 5, and for highlighting
|
|
67
|
+
// Used by search-as-you-type, only for highlighting
|
|
68
|
+
// Returns number[], where each number is the match score for a char in the title
|
|
69
|
+
function getTitleCharScores(query, queryWordSet, titleLower) {
|
|
70
|
+
// prefill with an empty array for each character in title
|
|
71
|
+
const overlap = Array(titleLower.length).fill(0).map(() => []);
|
|
72
|
+
for (let queryShift = 1 - query.length; queryShift < titleLower.length; queryShift++) {
|
|
73
|
+
let cmc = null; // consecutive matching characters
|
|
74
|
+
const startOverlap = Math.max(queryShift, 0);
|
|
75
|
+
const endOverlap = Math.min(query.length + queryShift, titleLower.length);
|
|
76
|
+
for (let i = startOverlap; i < endOverlap; i++) {
|
|
77
|
+
if (query[i - queryShift] === titleLower[i] && titleLower[i] !== ' ') {
|
|
78
|
+
if (!cmc) {
|
|
79
|
+
// start a new cmc
|
|
80
|
+
cmc = {
|
|
81
|
+
str: ''
|
|
82
|
+
};
|
|
114
83
|
}
|
|
84
|
+
// append this char to the current cmc
|
|
85
|
+
cmc.str += query[i - queryShift];
|
|
86
|
+
overlap[i].push(cmc);
|
|
87
|
+
} else if (cmc) {
|
|
88
|
+
cmc = null; // set to null so that the next match starts a new cmc
|
|
115
89
|
}
|
|
116
|
-
titleCharScores.push(score);
|
|
117
90
|
}
|
|
118
|
-
return titleCharScores;
|
|
119
91
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
//
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
) {
|
|
130
|
-
let
|
|
131
|
-
for (const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
// for each complete word present in both the title and query
|
|
137
|
-
for (const qw of queryWordSet) {
|
|
138
|
-
if (titleWordSet.has(qw)) {
|
|
139
|
-
bonus += 100;
|
|
92
|
+
const avgQueryWordLength = query.length / queryWordSet.size;
|
|
93
|
+
|
|
94
|
+
// If avgQueryWordLength is 1-3, we want to the min match to be about that same size.
|
|
95
|
+
// If avgQueryWordLength is longer, we want min match to be longer...but not too long.
|
|
96
|
+
// e.g. For avgQueryWordLength of 9, min should be 5
|
|
97
|
+
// The misspelling "regresion" should still match "regression", but not "encryption",
|
|
98
|
+
// "authentication" or other words ending in "ion"
|
|
99
|
+
const minMatchLength = Math.ceil(avgQueryWordLength ** 0.65);
|
|
100
|
+
const titleCharScores = [];
|
|
101
|
+
for (const cmcs of overlap) {
|
|
102
|
+
let score = 0;
|
|
103
|
+
for (const cmc of cmcs) {
|
|
104
|
+
// if the string of consecutive matching chars meets the minMatchLength
|
|
105
|
+
// or if it's a standalone word in the query (e.g "C")
|
|
106
|
+
if (cmc.str.length >= minMatchLength || queryWordSet.has(cmc.str)) {
|
|
107
|
+
score += cmc.str.length;
|
|
140
108
|
}
|
|
141
109
|
}
|
|
110
|
+
titleCharScores.push(score);
|
|
111
|
+
}
|
|
112
|
+
return titleCharScores;
|
|
113
|
+
}
|
|
142
114
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
115
|
+
// Total used in sorting to find autocomplet top 5
|
|
116
|
+
// Bonus scores are includes in certain cases
|
|
117
|
+
function getTotalScore(query, queryWordSet, titleLower, titleWordSet, titleCharScores) {
|
|
118
|
+
let fromChars = 0;
|
|
119
|
+
for (const s of titleCharScores) {
|
|
120
|
+
fromChars += s;
|
|
121
|
+
}
|
|
122
|
+
let bonus = 0;
|
|
123
|
+
// for each complete word present in both the title and query
|
|
124
|
+
for (const qw of queryWordSet) {
|
|
125
|
+
if (titleWordSet.has(qw)) {
|
|
150
126
|
bonus += 100;
|
|
151
127
|
}
|
|
152
|
-
|
|
153
|
-
return fromChars + bonus;
|
|
154
128
|
}
|
|
155
129
|
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const segments = [];
|
|
160
|
-
|
|
161
|
-
const shouldHighlight = (i) => titleCharScores[i] > 0;
|
|
162
|
-
|
|
163
|
-
const isDifferentFromPrev = (i) =>
|
|
164
|
-
shouldHighlight(i - 1) !== shouldHighlight(i);
|
|
165
|
-
|
|
166
|
-
for (let i = 0; i < title.length; i++) {
|
|
167
|
-
if (i === 0 || isDifferentFromPrev(i)) {
|
|
168
|
-
// If this char should be highlighted, but the prev one should not
|
|
169
|
-
// or vice versa, start a new segment.
|
|
170
|
-
currentSegment = { value: '', highlight: shouldHighlight(i) };
|
|
171
|
-
segments.push(currentSegment);
|
|
172
|
-
}
|
|
173
|
-
currentSegment.value += title[i];
|
|
174
|
-
}
|
|
175
|
-
return segments;
|
|
130
|
+
// if the title starts with the query
|
|
131
|
+
if (titleLower.startsWith(query)) {
|
|
132
|
+
bonus += 100;
|
|
176
133
|
}
|
|
177
134
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const { action, query, max } = data;
|
|
182
|
-
const queryWordSet = getWordSet(query);
|
|
183
|
-
if (action === 'autocomplete') {
|
|
184
|
-
processAutcomplete(query, queryWordSet, max);
|
|
185
|
-
}
|
|
186
|
-
if (action === 'search-as-you-type') {
|
|
187
|
-
processSearchAsYouType(query, queryWordSet, max);
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
async function processAutcomplete(query, queryWordSet, max) {
|
|
192
|
-
const autocompleteTitles = await preloadPromise;
|
|
193
|
-
|
|
194
|
-
const scoredAutocompleteTitles = autocompleteTitles.map(
|
|
195
|
-
({ title, titleLower, titleWordSet, popularity }) => {
|
|
196
|
-
const titleCharScores = getTitleCharScores(
|
|
197
|
-
query,
|
|
198
|
-
queryWordSet,
|
|
199
|
-
titleLower
|
|
200
|
-
);
|
|
201
|
-
const titleScore = getTotalScore(
|
|
202
|
-
query,
|
|
203
|
-
queryWordSet,
|
|
204
|
-
titleLower,
|
|
205
|
-
titleWordSet,
|
|
206
|
-
titleCharScores
|
|
207
|
-
);
|
|
208
|
-
return { title, titleCharScores, titleScore, popularity };
|
|
209
|
-
}
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
/*
|
|
213
|
-
* For the search "alge", .04 is just low enough so that "Linear algebra"
|
|
214
|
-
* (which matches on 4 letters) comes in above "Data structures and algorithms"
|
|
215
|
-
* (which only matches on 3 letters, but has a high popularity score).
|
|
216
|
-
*/
|
|
217
|
-
const popularityStrength = 0.04;
|
|
218
|
-
|
|
219
|
-
const topAutocompleteTitles = scoredAutocompleteTitles
|
|
220
|
-
.filter((x) => x.titleScore > 0)
|
|
221
|
-
.sort((a, b) =>
|
|
222
|
-
a.titleScore + (a.popularity || 0) * popularityStrength >
|
|
223
|
-
b.titleScore + (b.popularity || 0) * popularityStrength
|
|
224
|
-
? -1
|
|
225
|
-
: 1
|
|
226
|
-
)
|
|
227
|
-
.slice(0, max)
|
|
228
|
-
.map(({ title, titleCharScores }) => ({
|
|
229
|
-
title,
|
|
230
|
-
segments: getHighlightSegments(title, titleCharScores),
|
|
231
|
-
key: query + ':' + title,
|
|
232
|
-
}));
|
|
233
|
-
|
|
234
|
-
postMessage({ query, topAutocompleteTitles });
|
|
135
|
+
// if the title is an exact match with the query
|
|
136
|
+
if (titleLower === query) {
|
|
137
|
+
bonus += 100;
|
|
235
138
|
}
|
|
139
|
+
return fromChars + bonus;
|
|
140
|
+
}
|
|
236
141
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
142
|
+
// returns { value: string, highlight: boolean }[]
|
|
143
|
+
function getHighlightSegments(title, titleCharScores) {
|
|
144
|
+
let currentSegment = null;
|
|
145
|
+
const segments = [];
|
|
146
|
+
const shouldHighlight = i => titleCharScores[i] > 0;
|
|
147
|
+
const isDifferentFromPrev = i => shouldHighlight(i - 1) !== shouldHighlight(i);
|
|
148
|
+
for (let i = 0; i < title.length; i++) {
|
|
149
|
+
if (i === 0 || isDifferentFromPrev(i)) {
|
|
150
|
+
// If this char should be highlighted, but the prev one should not
|
|
151
|
+
// or vice versa, start a new segment.
|
|
152
|
+
currentSegment = {
|
|
153
|
+
value: '',
|
|
154
|
+
highlight: shouldHighlight(i)
|
|
155
|
+
};
|
|
156
|
+
segments.push(currentSegment);
|
|
251
157
|
}
|
|
252
|
-
|
|
158
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
159
|
+
currentSegment.value += title[i];
|
|
160
|
+
}
|
|
161
|
+
return segments;
|
|
162
|
+
}
|
|
163
|
+
const preloadPromise = preloadAutocompleteTitles();
|
|
164
|
+
onmessage = _ref2 => {
|
|
165
|
+
let {
|
|
166
|
+
data
|
|
167
|
+
} = _ref2;
|
|
168
|
+
const {
|
|
169
|
+
action,
|
|
170
|
+
query
|
|
171
|
+
} = data;
|
|
172
|
+
const queryWordSet = getWordSet(query);
|
|
173
|
+
if (action === 'autocomplete') {
|
|
174
|
+
processAutcomplete(query, queryWordSet, 5);
|
|
175
|
+
}
|
|
176
|
+
if (action === 'search-as-you-type') {
|
|
177
|
+
processSearchAsYouType(query, queryWordSet, 5);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
async function processAutcomplete(query, queryWordSet, max) {
|
|
181
|
+
const autocompleteTitles = await preloadPromise;
|
|
182
|
+
const scoredAutocompleteTitles = autocompleteTitles.map(_ref3 => {
|
|
183
|
+
let {
|
|
184
|
+
title,
|
|
185
|
+
titleLower,
|
|
186
|
+
titleWordSet,
|
|
187
|
+
popularity
|
|
188
|
+
} = _ref3;
|
|
189
|
+
const titleCharScores = getTitleCharScores(query, queryWordSet, titleLower);
|
|
190
|
+
const titleScore = getTotalScore(query, queryWordSet, titleLower, titleWordSet, titleCharScores);
|
|
191
|
+
return {
|
|
192
|
+
title,
|
|
193
|
+
titleCharScores,
|
|
194
|
+
titleScore,
|
|
195
|
+
popularity
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
/*
|
|
200
|
+
* For the search "alge", .04 is just low enough so that "Linear algebra"
|
|
201
|
+
* (which matches on 4 letters) comes in above "Data structures and algorithms"
|
|
202
|
+
* (which only matches on 3 letters, but has a high popularity score).
|
|
203
|
+
*/
|
|
204
|
+
const popularityStrength = 0.04;
|
|
205
|
+
const topAutocompleteTitles = scoredAutocompleteTitles.filter(x => x.titleScore > 0).sort((a, b) => a.titleScore + (a.popularity || 0) * popularityStrength > b.titleScore + (b.popularity || 0) * popularityStrength ? -1 : 1).slice(0, max).map(_ref4 => {
|
|
206
|
+
let {
|
|
207
|
+
title,
|
|
208
|
+
titleCharScores
|
|
209
|
+
} = _ref4;
|
|
210
|
+
return {
|
|
211
|
+
title,
|
|
212
|
+
segments: getHighlightSegments(title, titleCharScores),
|
|
213
|
+
key: query + ':' + title
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
postMessage({
|
|
217
|
+
query,
|
|
218
|
+
result: topAutocompleteTitles,
|
|
219
|
+
action: 'autocomplete'
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
async function processSearchAsYouType(query, queryWordSet, max) {
|
|
223
|
+
const f = await fetch('${BASE_URL}/search-as-you-type/${search}', {
|
|
224
|
+
body: JSON.stringify({
|
|
225
|
+
query,
|
|
226
|
+
max
|
|
227
|
+
}),
|
|
228
|
+
method: 'POST'
|
|
229
|
+
});
|
|
230
|
+
const searchAsYouTypeResults = await f.json();
|
|
231
|
+
for (const entry of searchAsYouTypeResults.top) {
|
|
232
|
+
const charScores = getTitleCharScores(query, queryWordSet, entry.title.toLowerCase());
|
|
233
|
+
entry.segments = getHighlightSegments(entry.title, charScores);
|
|
234
|
+
entry.key = query + ':' + entry.title;
|
|
253
235
|
}
|
|
254
|
-
|
|
236
|
+
postMessage({
|
|
237
|
+
query,
|
|
238
|
+
result: searchAsYouTypeResults,
|
|
239
|
+
action: 'search-as-you-type'
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
export function getSearchWorkerSrc() {
|
|
244
|
+
const {
|
|
245
|
+
search
|
|
246
|
+
} = window.location;
|
|
247
|
+
const BASE_URL = `${getPortalOrigin()}/api/portal/search`;
|
|
248
|
+
return worker.toString().trim().replace('function worker() {', '').slice(-0, -1) // remove ending bracket
|
|
249
|
+
.replaceAll(' ', '') // remove indentation
|
|
250
|
+
.replaceAll('{BASE_URL}', BASE_URL).replaceAll('{SEARCH}', search);
|
|
255
251
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export type HighlightSegment = {
|
|
2
|
+
value: string;
|
|
3
|
+
highlight: boolean;
|
|
4
|
+
};
|
|
5
|
+
export type AutocompleteSuggestion = {
|
|
6
|
+
title: string;
|
|
7
|
+
segments: HighlightSegment[];
|
|
8
|
+
key: string;
|
|
9
|
+
};
|
|
10
|
+
export type SearchAsYouTypeResults = {
|
|
11
|
+
query: string;
|
|
12
|
+
top: {
|
|
13
|
+
type: string;
|
|
14
|
+
title: string;
|
|
15
|
+
slug: string;
|
|
16
|
+
contentId?: string;
|
|
17
|
+
urlPath: string;
|
|
18
|
+
key: string;
|
|
19
|
+
segments: HighlightSegment[];
|
|
20
|
+
}[];
|
|
21
|
+
queryLoadTime: number;
|
|
22
|
+
searchId: string;
|
|
23
|
+
resultsCount: number;
|
|
24
|
+
};
|
|
25
|
+
export declare enum SearchAction {
|
|
26
|
+
Autocomplete = "autocomplete",
|
|
27
|
+
SearchAsYouType = "search-as-you-type"
|
|
28
|
+
}
|
|
29
|
+
export type SearchResultTypes = {
|
|
30
|
+
[SearchAction.Autocomplete]: AutocompleteSuggestion[];
|
|
31
|
+
[SearchAction.SearchAsYouType]: SearchAsYouTypeResults;
|
|
32
|
+
};
|
|
33
|
+
export type SearchWorker = <A extends SearchAction>(action: A, query: string) => Promise<SearchResultTypes[A]>;
|