-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
wiki.ts
67 lines (58 loc) · 2.79 KB
/
wiki.ts
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
import debounce from 'common/debounce';
export type WikiTheory = (nodes: Tree.Node[]) => void;
export default function wikiTheory(): WikiTheory {
const cache = new Map<string, string>();
const show = (html: string) => {
$('.analyse__wiki').html(html).toggleClass('empty', !html);
lichess.pubsub.emit('chat.resize');
};
const plyPrefix = (node: Tree.Node) => `${Math.floor((node.ply + 1) / 2)}${node.ply % 2 === 1 ? '._' : '...'}`;
const wikiBooksUrl = 'https://en.wikibooks.org';
const apiArgs = 'redirects&origin=*&action=query&prop=extracts&formatversion=2&format=json&exchars=1200';
const removeEmptyParagraph = (html: string) => html.replace(/<p>(<br \/>|\s)*<\/p>/g, '');
const removeTableHeader = (html: string) => html.replace('<h2><span id="Theory_table">Theory table</span></h2>', '');
const removeTableExpl = (html: string) =>
html.replace(/For explanation of theory tables see theory table and for notation see algebraic notation.?/, '');
const removeContributing = (html: string) =>
html.replace('When contributing to this Wikibook, please follow the Conventions for organization.', '');
const readMore = (title: string) =>
`<p><a target="_blank" href="${wikiBooksUrl}/wiki/${title}">Read more on WikiBooks</a></p>`;
const transform = (html: string, title: string) =>
removeEmptyParagraph(removeTableHeader(removeTableExpl(removeContributing(html)))) + readMore(title);
return debounce(
async (nodes: Tree.Node[]) => {
const pathParts = nodes.slice(1).map(n => `${plyPrefix(n)}${n.san}`);
const path = pathParts.join('/').replace(/[+!#?]/g, '') ?? '';
if (pathParts.length > 30 || !path || path.length > 255 - 21) show('');
else if (cache.has(path)) show(cache.get(path)!);
else if (
Array.from({ length: pathParts.length }, (_, i) => -i - 1)
.map(i => pathParts.slice(0, i).join('/'))
.some(sub => cache.has(sub) && !cache.get(sub)!.length)
)
show('');
else {
const title = `Chess_Opening_Theory/${path}`;
try {
const res = await fetch(`${wikiBooksUrl}/w/api.php?titles=${title}&${apiArgs}`);
const saveAndShow = (html: string) => {
cache.set(path, html);
show(html);
};
if (res.ok) {
const json = await res.json();
const page = json.query.pages[0];
if (page.missing) saveAndShow('');
else if (page.invalid) show('invalid request: ' + page.invalidreason);
else if (!page.extract) show('error: unexpected API response:<br><pre>' + JSON.stringify(page) + '</pre>');
else saveAndShow(transform(page.extract, title));
} else saveAndShow('');
} catch (err) {
show('error: ' + err);
}
}
},
500,
true
);
}