forked from KaTeX/KaTeX
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
99 lines (82 loc) · 2.98 KB
/
main.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
/* eslint no-console:0 */
/**
* This is the webpack entry point for the test page.
*/
import katex from '../katex.webpack.js';
import './main.css';
import queryString from 'query-string';
function init() {
const input = document.getElementById("input");
let math = document.getElementById("math");
const permalink = document.getElementById("permalink");
input.addEventListener("input", reprocess, false);
permalink.addEventListener("click", setSearch);
const options = {displayMode: true, macros: {}};
const query = queryString.parse(window.location.search);
if (query.text) {
input.value = query.text;
}
// Use `display=0` or `displayMode=0` (or `=f`/`=false`/`=n`/`=no`)
// to turn off displayMode (which is on by default).
const displayQuery = (query.displayMode || query.display);
if (displayQuery && displayQuery.match(/^(0|f|n)/)) {
options.displayMode = false;
}
// Use `strict=warn` for warning strict mode or `strict=error`
// (or `=1`/`=t`/`=true`/`=y`/`=yes`)
// to turn off displayMode (which is on by default).
if (query.strict) {
if (query.strict.match(/^(1|t|y|e)/)) {
options.strict = "error";
} if (query.strict && query.strict.match(/^(w)/)) {
options.strict = "warn";
}
}
// The `before` or `pre` search parameter puts normal text before the math.
// The `after` or `post` search parameter puts normal text after the math.
// Example use: testing baseline alignment.
if (query.before || query.after || query.pre || query.post) {
const mathContainer = math;
mathContainer.id = "math-container";
if (query.before || query.pre) {
const before = document.createTextNode(query.before || query.pre);
mathContainer.appendChild(before);
}
math = document.createElement("span");
math.id = "math";
mathContainer.appendChild(math);
if (query.after || query.post) {
const after = document.createTextNode(query.after || query.post);
mathContainer.appendChild(after);
}
}
// Macros can be specified via `\command=expansion` or single-character
// `c=expansion`.
Object.getOwnPropertyNames(query).forEach((key) => {
if (key.match(/^\\|^[^]$/)) {
options.macros[key] = query[key];
}
});
reprocess();
function setSearch() {
const query = queryString.parse(window.location.search);
query.text = input.value;
window.location.search = queryString.stringify(query);
}
function reprocess() {
try {
katex.render(input.value, math, options);
} catch (e) {
if (e.__proto__ === katex.ParseError.prototype) {
console.error(e);
} else {
throw e;
}
}
}
if (module.hot) {
module.hot.accept('../katex.webpack.js', reprocess);
}
}
init();
export default katex;