forked from UziTech/marked-katex-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-specs.js
123 lines (115 loc) · 3.24 KB
/
update-specs.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import fetch from 'node-fetch';
import { load } from 'cheerio';
import { unlinkSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import katex from 'katex';
function normalize(str) {
return str
.replace(/<(\w+)([^>]*)><\/\1>/g, '<$1$2/>')
.replace(/'|`|ˋ|ˊ|"|'/g, '"')
.replace(/\s| /g, '')
.replace(/\\VERT|\\\|/ig, '|')
.replace('"=""', '"/')
.replace(/\\cr/g, '\\\\');
}
const commentsInSource = [
' Requires trust option',
'TeX (strict) syntax',
'non-strict syntax',
' KaTeX supports A-Za-z',
' KaTeX supports A-Z k',
' KaTeX supports A-Z & k',
'KaTeX supports A-Z & k',
'KaTeX supports A-Z',
' supports A-Z & k',
' Must enable trust and disable strict option'
];
// const specsToIgnore = [
// '\\url',
// '\\mathclap',
// '\\hspace',
// '\\endgroup',
// '\\begingroup',
// '\\angln',
// '\\angl',
// '\\}',
// '\\{',
// '\\vcenter',
// '\\raisebox',
// '\\mathscr',
// '\\mathnormal',
// '\\mathit',
// '\\mathfrak',
// '\\mathbb',
// '\\dotsm',
// '\\$'
// ];
async function updateSpecs() {
const specsFile = resolve('./spec/specs.json');
try {
unlinkSync(specsFile);
} catch (ex) {
// no file exists
}
try {
const res = await fetch('https://katex.org/docs/support_table.html');
const html = await res.text();
const $ = load(html);
const specs = [];
$('table').each((i, table) => {
const title = $('thead th', table).text().trim();
if (title !== 'Symbol/FunctionRenderedSource or Comment') {
return;
}
$('tbody tr', table).each((i, row) => {
const tds = $('td', row);
const name = $(tds[0]).text()?.trim();
const rendered = $(tds[1]).html()?.trim();
let source = $(tds[2]).text()?.trim();
if (!rendered || rendered.includes('Not supported')) {
return;
}
if (source.includes('Requires an extension')) {
return;
}
let trust = source.includes('Requires trust option');
let strict;
if (source.includes('Must enable trust and disable strict option')) {
trust = true;
strict = false;
}
for (const comment of commentsInSource) {
source = source.replace(comment, '');
}
source = source.replace(/\u00a0+/g, '\n');
if (!source) {
source = name;
}
const spec = {
name,
rendered,
source,
options: {
displayMode: !!rendered?.includes('katex-display'),
trust,
strict
}
};
let result;
try {
result = katex.renderToString(spec.source, spec.options);
} catch {}
if (!result || normalize(result) !== normalize(spec.rendered)) {
console.log(spec.name, 'does not render correctly');
spec.skip = true;
}
specs.push(spec);
});
});
writeFileSync(specsFile, JSON.stringify(specs, null, 2).replace(/\u00a0+/g, '\\n') + '\n');
console.log('Saved specs.');
} catch (ex) {
console.log(ex);
}
}
await updateSpecs();