forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
katex.js
218 lines (204 loc) · 6.32 KB
/
katex.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// @flow
/* eslint no-console:0 */
/**
* This is the main entry point for KaTeX. Here, we expose functions for
* rendering expressions either to DOM nodes or to markup strings.
*
* We also expose the ParseError class to check if errors thrown from KaTeX are
* errors in the expression, or errors in javascript handling.
*/
import ParseError from "./src/ParseError";
import Settings, {SETTINGS_SCHEMA} from "./src/Settings";
import {buildTree, buildHTMLTree} from "./src/buildTree";
import parseTree from "./src/parseTree";
import buildCommon from "./src/buildCommon";
import {
Span,
Anchor,
SymbolNode,
SvgNode,
PathNode,
LineNode,
} from "./src/domTree";
import type {SettingsOptions} from "./src/Settings";
import type {AnyParseNode} from "./src/parseNode";
import type {DomSpan} from "./src/domTree";
import {defineSymbol} from './src/symbols';
import defineMacro from './src/defineMacro';
import {setFontMetrics} from './src/fontMetrics';
declare var __VERSION__: string;
/**
* Parse and build an expression, and place that expression in the DOM node
* given.
*/
let render: (string, Node, SettingsOptions) => void = function(
expression: string,
baseNode: Node,
options: SettingsOptions,
) {
baseNode.textContent = "";
const node = renderToDomTree(expression, options).toNode();
baseNode.appendChild(node);
};
// KaTeX's styles don't work properly in quirks mode. Print out an error, and
// disable rendering.
if (typeof document !== "undefined") {
if (document.compatMode !== "CSS1Compat") {
typeof console !== "undefined" && console.warn(
"Warning: KaTeX doesn't work in quirks mode. Make sure your " +
"website has a suitable doctype.");
render = function() {
throw new ParseError("KaTeX doesn't work in quirks mode.");
};
}
}
/**
* Parse and build an expression, and return the markup for that.
*/
const renderToString = function(
expression: string,
options: SettingsOptions,
): string {
const markup = renderToDomTree(expression, options).toMarkup();
return markup;
};
/**
* Parse an expression and return the parse tree.
*/
const generateParseTree = function(
expression: string,
options: SettingsOptions,
): AnyParseNode[] {
const settings = new Settings(options);
return parseTree(expression, settings);
};
/**
* If the given error is a KaTeX ParseError and options.throwOnError is false,
* renders the invalid LaTeX as a span with hover title giving the KaTeX
* error message. Otherwise, simply throws the error.
*/
const renderError = function(
error,
expression: string,
options: Settings,
) {
if (options.throwOnError || !(error instanceof ParseError)) {
throw error;
}
const node = buildCommon.makeSpan(["katex-error"],
[new SymbolNode(expression)]);
node.setAttribute("title", error.toString());
node.setAttribute("style", `color:${options.errorColor}`);
return node;
};
/**
* Generates and returns the katex build tree. This is used for advanced
* use cases (like rendering to custom output).
*/
const renderToDomTree = function(
expression: string,
options: SettingsOptions,
): DomSpan {
const settings = new Settings(options);
try {
const tree = parseTree(expression, settings);
return buildTree(tree, expression, settings);
} catch (error) {
return renderError(error, expression, settings);
}
};
/**
* Generates and returns the katex build tree, with just HTML (no MathML).
* This is used for advanced use cases (like rendering to custom output).
*/
const renderToHTMLTree = function(
expression: string,
options: SettingsOptions,
): DomSpan {
const settings = new Settings(options);
try {
const tree = parseTree(expression, settings);
return buildHTMLTree(tree, expression, settings);
} catch (error) {
return renderError(error, expression, settings);
}
};
export default {
/**
* Current KaTeX version
*/
version: __VERSION__,
/**
* Renders the given LaTeX into an HTML+MathML combination, and adds
* it as a child to the specified DOM node.
*/
render,
/**
* Renders the given LaTeX into an HTML+MathML combination string,
* for sending to the client.
*/
renderToString,
/**
* KaTeX error, usually during parsing.
*/
ParseError,
/**
* The shema of Settings
*/
SETTINGS_SCHEMA,
/**
* Parses the given LaTeX into KaTeX's internal parse tree structure,
* without rendering to HTML or MathML.
*
* NOTE: This method is not currently recommended for public use.
* The internal tree representation is unstable and is very likely
* to change. Use at your own risk.
*/
__parse: generateParseTree,
/**
* Renders the given LaTeX into an HTML+MathML internal DOM tree
* representation, without flattening that representation to a string.
*
* NOTE: This method is not currently recommended for public use.
* The internal tree representation is unstable and is very likely
* to change. Use at your own risk.
*/
__renderToDomTree: renderToDomTree,
/**
* Renders the given LaTeX into an HTML internal DOM tree representation,
* without MathML and without flattening that representation to a string.
*
* NOTE: This method is not currently recommended for public use.
* The internal tree representation is unstable and is very likely
* to change. Use at your own risk.
*/
__renderToHTMLTree: renderToHTMLTree,
/**
* extends internal font metrics object with a new object
* each key in the new object represents a font name
*/
__setFontMetrics: setFontMetrics,
/**
* adds a new symbol to builtin symbols table
*/
__defineSymbol: defineSymbol,
/**
* adds a new macro to builtin macro list
*/
__defineMacro: defineMacro,
/**
* Expose the dom tree node types, which can be useful for type checking nodes.
*
* NOTE: This method is not currently recommended for public use.
* The internal tree representation is unstable and is very likely
* to change. Use at your own risk.
*/
__domTree: {
Span,
Anchor,
SymbolNode,
SvgNode,
PathNode,
LineNode,
},
};