-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-page.js
51 lines (51 loc) · 2.4 KB
/
test-page.js
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
import { CodeMirror6 } from '/codemirror-mediawiki/dist/main.min.js';
(async () => {
const tests = await (await fetch('./test/parserTests.json')).json(), key = 'codemirror-mediawiki-done', dones = new Set(JSON.parse(localStorage.getItem(key))), select = document.querySelector('select'), btn = document.querySelector('button'), textarea = document.querySelector('textarea'), pre = document.querySelector('pre');
Parser.config = await (await fetch('/wikiparser-node/config/default.json')).json();
const cm = new CodeMirror6(textarea, 'mediawiki', CodeMirror6.getMwConfig(Parser.config));
Object.assign(globalThis, { cm });
wikiparse.print = (wikitext, include, stage) => {
const printed = Parser.parse(wikitext, include, stage).print();
return Promise.resolve([[stage !== null && stage !== void 0 ? stage : Infinity, wikitext, printed]]);
};
void wikiparse.highlight(pre, false, true);
btn.disabled = !select.value;
let optgroup;
for (const [i, { desc, wikitext }] of tests.entries()) {
if (wikitext === undefined) {
optgroup = document.createElement('optgroup');
optgroup.label = desc;
select.append(optgroup);
}
else if (!dones.has(desc)) {
const option = document.createElement('option');
option.value = String(i);
option.textContent = desc;
optgroup.append(option);
}
}
select.addEventListener('change', () => {
const { wikitext, desc } = tests[Number(select.value)];
cm.setContent(wikitext);
pre.textContent = wikitext;
pre.classList.remove('wikiparser');
void wikiparse.highlight(pre, false, true);
select.selectedOptions[0].disabled = true;
btn.disabled = false;
location.hash = `#${encodeURIComponent(desc)}`;
});
btn.addEventListener('click', () => {
dones.add(tests[Number(select.value)].desc);
localStorage.setItem(key, JSON.stringify([...dones]));
select.selectedIndex++;
select.dispatchEvent(new Event('change'));
});
addEventListener('hashchange', () => {
const hash = decodeURIComponent(location.hash.slice(1)), i = tests.findIndex(({ desc }) => desc === hash);
if (i !== -1) {
select.value = String(i);
select.dispatchEvent(new Event('change'));
}
});
dispatchEvent(new Event('hashchange'));
})();