katex 0.16.16 → 0.16.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/katex.mjs CHANGED
@@ -6677,7 +6677,19 @@ class MathNode {
6677
6677
  }
6678
6678
 
6679
6679
  for (var i = 0; i < this.children.length; i++) {
6680
- node.appendChild(this.children[i].toNode());
6680
+ // Combine multiple TextNodes into one TextNode, to prevent
6681
+ // screen readers from reading each as a separate word [#3995]
6682
+ if (this.children[i] instanceof TextNode && this.children[i + 1] instanceof TextNode) {
6683
+ var text = this.children[i].toText() + this.children[++i].toText();
6684
+
6685
+ while (this.children[i + 1] instanceof TextNode) {
6686
+ text += this.children[++i].toText();
6687
+ }
6688
+
6689
+ node.appendChild(new TextNode(text).toNode());
6690
+ } else {
6691
+ node.appendChild(this.children[i].toNode());
6692
+ }
6681
6693
  }
6682
6694
 
6683
6695
  return node;
@@ -6947,12 +6959,34 @@ var getVariant = function getVariant(group, options) {
6947
6959
 
6948
6960
  return null;
6949
6961
  };
6962
+ /**
6963
+ * Check for <mi>.</mi> which is how a dot renders in MathML,
6964
+ * or <mo separator="true" lspace="0em" rspace="0em">,</mo>
6965
+ * which is how a braced comma {,} renders in MathML
6966
+ */
6967
+
6968
+ function isNumberPunctuation(group) {
6969
+ if (!group) {
6970
+ return false;
6971
+ }
6972
+
6973
+ if (group.type === 'mi' && group.children.length === 1) {
6974
+ var child = group.children[0];
6975
+ return child instanceof TextNode && child.text === '.';
6976
+ } else if (group.type === 'mo' && group.children.length === 1 && group.getAttribute('separator') === 'true' && group.getAttribute('lspace') === '0em' && group.getAttribute('rspace') === '0em') {
6977
+ var _child = group.children[0];
6978
+ return _child instanceof TextNode && _child.text === ',';
6979
+ } else {
6980
+ return false;
6981
+ }
6982
+ }
6950
6983
  /**
6951
6984
  * Takes a list of nodes, builds them, and returns a list of the generated
6952
6985
  * MathML nodes. Also combine consecutive <mtext> outputs into a single
6953
6986
  * <mtext> tag.
6954
6987
  */
6955
6988
 
6989
+
6956
6990
  var buildExpression = function buildExpression(expression, options, isOrdgroup) {
6957
6991
  if (expression.length === 1) {
6958
6992
  var group = buildGroup(expression[0], options);
@@ -6981,22 +7015,30 @@ var buildExpression = function buildExpression(expression, options, isOrdgroup)
6981
7015
  } else if (_group.type === 'mn' && lastGroup.type === 'mn') {
6982
7016
  lastGroup.children.push(..._group.children);
6983
7017
  continue; // Concatenate <mn>...</mn> followed by <mi>.</mi>
6984
- } else if (_group.type === 'mi' && _group.children.length === 1 && lastGroup.type === 'mn') {
6985
- var child = _group.children[0];
7018
+ } else if (isNumberPunctuation(_group) && lastGroup.type === 'mn') {
7019
+ lastGroup.children.push(..._group.children);
7020
+ continue; // Concatenate <mi>.</mi> followed by <mn>...</mn>
7021
+ } else if (_group.type === 'mn' && isNumberPunctuation(lastGroup)) {
7022
+ _group.children = [...lastGroup.children, ..._group.children];
7023
+ groups.pop(); // Put preceding <mn>...</mn> or <mi>.</mi> inside base of
7024
+ // <msup><mn>...base...</mn>...exponent...</msup> (or <msub>)
7025
+ } else if ((_group.type === 'msup' || _group.type === 'msub') && _group.children.length >= 1 && (lastGroup.type === 'mn' || isNumberPunctuation(lastGroup))) {
7026
+ var base = _group.children[0];
7027
+
7028
+ if (base instanceof MathNode && base.type === 'mn') {
7029
+ base.children = [...lastGroup.children, ...base.children];
7030
+ groups.pop();
7031
+ } // \not
6986
7032
 
6987
- if (child instanceof TextNode && child.text === '.') {
6988
- lastGroup.children.push(..._group.children);
6989
- continue;
6990
- }
6991
7033
  } else if (lastGroup.type === 'mi' && lastGroup.children.length === 1) {
6992
7034
  var lastChild = lastGroup.children[0];
6993
7035
 
6994
7036
  if (lastChild instanceof TextNode && lastChild.text === '\u0338' && (_group.type === 'mo' || _group.type === 'mi' || _group.type === 'mn')) {
6995
- var _child = _group.children[0];
7037
+ var child = _group.children[0];
6996
7038
 
6997
- if (_child instanceof TextNode && _child.text.length > 0) {
7039
+ if (child instanceof TextNode && child.text.length > 0) {
6998
7040
  // Overlay with combining character long solidus
6999
- _child.text = _child.text.slice(0, 1) + "\u0338" + _child.text.slice(1);
7041
+ child.text = child.text.slice(0, 1) + "\u0338" + child.text.slice(1);
7000
7042
  groups.pop();
7001
7043
  }
7002
7044
  }
@@ -18371,7 +18413,7 @@ var renderToHTMLTree = function renderToHTMLTree(expression, options) {
18371
18413
  }
18372
18414
  };
18373
18415
 
18374
- var version = "0.16.16";
18416
+ var version = "0.16.18";
18375
18417
  var __domTree = {
18376
18418
  Span,
18377
18419
  Anchor,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "katex",
3
- "version": "0.16.16",
3
+ "version": "0.16.18",
4
4
  "description": "Fast math typesetting for the web.",
5
5
  "main": "dist/katex.js",
6
6
  "exports": {
@@ -50,7 +50,8 @@
50
50
  "cli.js",
51
51
  "src/",
52
52
  "contrib/",
53
- "dist/"
53
+ "dist/",
54
+ "types/"
54
55
  ],
55
56
  "license": "MIT",
56
57
  "packageManager": "yarn@4.1.1",
@@ -128,6 +128,30 @@ export const getVariant = function(
128
128
  return null;
129
129
  };
130
130
 
131
+ /**
132
+ * Check for <mi>.</mi> which is how a dot renders in MathML,
133
+ * or <mo separator="true" lspace="0em" rspace="0em">,</mo>
134
+ * which is how a braced comma {,} renders in MathML
135
+ */
136
+ function isNumberPunctuation(group: ?MathNode): boolean {
137
+ if (!group) {
138
+ return false;
139
+ }
140
+ if (group.type === 'mi' && group.children.length === 1) {
141
+ const child = group.children[0];
142
+ return child instanceof TextNode && child.text === '.';
143
+ } else if (group.type === 'mo' && group.children.length === 1 &&
144
+ group.getAttribute('separator') === 'true' &&
145
+ group.getAttribute('lspace') === '0em' &&
146
+ group.getAttribute('rspace') === '0em'
147
+ ) {
148
+ const child = group.children[0];
149
+ return child instanceof TextNode && child.text === ',';
150
+ } else {
151
+ return false;
152
+ }
153
+ }
154
+
131
155
  /**
132
156
  * Takes a list of nodes, builds them, and returns a list of the generated
133
157
  * MathML nodes. Also combine consecutive <mtext> outputs into a single
@@ -165,13 +189,25 @@ export const buildExpression = function(
165
189
  lastGroup.children.push(...group.children);
166
190
  continue;
167
191
  // Concatenate <mn>...</mn> followed by <mi>.</mi>
168
- } else if (group.type === 'mi' && group.children.length === 1 &&
169
- lastGroup.type === 'mn') {
170
- const child = group.children[0];
171
- if (child instanceof TextNode && child.text === '.') {
172
- lastGroup.children.push(...group.children);
173
- continue;
192
+ } else if (isNumberPunctuation(group) && lastGroup.type === 'mn') {
193
+ lastGroup.children.push(...group.children);
194
+ continue;
195
+ // Concatenate <mi>.</mi> followed by <mn>...</mn>
196
+ } else if (group.type === 'mn' && isNumberPunctuation(lastGroup)) {
197
+ group.children = [...lastGroup.children, ...group.children];
198
+ groups.pop();
199
+ // Put preceding <mn>...</mn> or <mi>.</mi> inside base of
200
+ // <msup><mn>...base...</mn>...exponent...</msup> (or <msub>)
201
+ } else if ((group.type === 'msup' || group.type === 'msub') &&
202
+ group.children.length >= 1 &&
203
+ (lastGroup.type === 'mn' || isNumberPunctuation(lastGroup))
204
+ ) {
205
+ const base = group.children[0];
206
+ if (base instanceof MathNode && base.type === 'mn') {
207
+ base.children = [...lastGroup.children, ...base.children];
208
+ groups.pop();
174
209
  }
210
+ // \not
175
211
  } else if (lastGroup.type === 'mi' && lastGroup.children.length === 1) {
176
212
  const lastChild = lastGroup.children[0];
177
213
  if (lastChild instanceof TextNode && lastChild.text === '\u0338' &&
package/src/mathMLTree.js CHANGED
@@ -95,7 +95,18 @@ export class MathNode implements MathDomNode {
95
95
  }
96
96
 
97
97
  for (let i = 0; i < this.children.length; i++) {
98
- node.appendChild(this.children[i].toNode());
98
+ // Combine multiple TextNodes into one TextNode, to prevent
99
+ // screen readers from reading each as a separate word [#3995]
100
+ if (this.children[i] instanceof TextNode &&
101
+ this.children[i + 1] instanceof TextNode) {
102
+ let text = this.children[i].toText() + this.children[++i].toText();
103
+ while (this.children[i + 1] instanceof TextNode) {
104
+ text += this.children[++i].toText();
105
+ }
106
+ node.appendChild(new TextNode(text).toNode());
107
+ } else {
108
+ node.appendChild(this.children[i].toNode());
109
+ }
99
110
  }
100
111
 
101
112
  return node;
@@ -0,0 +1,221 @@
1
+ // Adapted from
2
+ // - https://katex.org/docs/options
3
+ // - https://katex.org/docs/api
4
+ // - https://katex.org/docs/error
5
+ // for v0.16.11 on 2024/12/01
6
+ // with some references from https://www.npmjs.com/package/@types/katex
7
+
8
+ /**
9
+ * For the `trust` option in `KatexOptions`, a custom function
10
+ * `handler(context)` can be provided to customize behavior depending on the
11
+ * context (command, arguments e.g. a URL, etc.)
12
+ * @see https://katex.org/docs/options
13
+ */
14
+ export type TrustContext =
15
+ | { command: "\\url", url: string, protocol?: string }
16
+ | { command: "\\href", url: string, protocol?: string }
17
+ | { command: "\\includegraphics", url: string, protocol?: string }
18
+ | { command: "\\htmlClass", class: string }
19
+ | { command: "\\htmlId", id: string }
20
+ | { command: "\\htmlStyle", style: string }
21
+ | { command: "\\htmlData", attributes: Record<string, string> }
22
+
23
+ /**
24
+ * Options for `katex.render` and `katex.renderToString`.
25
+ * @see https://katex.org/docs/options
26
+ */
27
+ export interface KatexOptions {
28
+ /**
29
+ * If `true` the math will be rendered in display mode.
30
+ * If `false` the math will be rendered in inline mode.
31
+ * @see https://katex.org/docs/options
32
+ *
33
+ * @default false
34
+ */
35
+ displayMode?: boolean;
36
+ /**
37
+ * Determines the markup language of the output. The valid choices are:
38
+ * - `html`: Outputs KaTeX in HTML only.
39
+ * - `mathml`: Outputs KaTeX in MathML only.
40
+ * - `htmlAndMathml`: Outputs HTML for visual rendering and includes MathML
41
+ * for accessibility.
42
+ *
43
+ * @default "htmlAndMathml"
44
+ */
45
+ output?: "html" | "mathml" | "htmlAndMathml";
46
+ /**
47
+ * If `true`, display math has `\tag`s rendered on the left instead of the
48
+ * right, like `\usepackage[leqno]{amsmath}` in LaTeX.
49
+ *
50
+ * @default false
51
+ */
52
+ leqno?: boolean;
53
+ /**
54
+ * If `true`, display math renders flush left with a `2em` left margin,
55
+ * like `\documentclass[fleqn]` in LaTeX with the `amsmath` package.
56
+ *
57
+ * @default false
58
+ */
59
+ fleqn?: boolean;
60
+ /**
61
+ * If `true`, KaTeX will throw a `ParseError` when it encounters an
62
+ * unsupported command or invalid LaTeX.
63
+ * If `false`, KaTeX will render unsupported commands as text, and render
64
+ * invalid LaTeX as its source code with hover text giving the error, in
65
+ * the color given by `errorColor`.
66
+ *
67
+ * @default true
68
+ */
69
+ throwOnError?: boolean;
70
+ /**
71
+ * A color string given in the format `"#XXX"` or `"#XXXXXX"`. This option
72
+ * determines the color that unsupported commands and invalid LaTeX are
73
+ * rendered in when `throwOnError` is set to `false`.
74
+ *
75
+ * @default "#cc0000"
76
+ */
77
+ errorColor?: string;
78
+ /**
79
+ * A collection of custom macros.
80
+ * @see https://katex.org/docs/options
81
+ */
82
+ macros?: Record<string, string | object | ((macroExpander:object) => string | object)>;
83
+ /**
84
+ * Specifies a minimum thickness, in ems, for fraction lines, `\sqrt` top
85
+ * lines, `{array}` vertical lines, `\hline`, `\hdashline`, `\underline`,
86
+ * `\overline`, and the borders of `\fbox`, `\boxed`, and `\fcolorbox`.
87
+ * The usual value for these items is `0.04`, so for `minRuleThickness`
88
+ * to be effective it should probably take a value slightly above `0.04`,
89
+ * say `0.05` or `0.06`. Negative values will be ignored.
90
+ */
91
+ minRuleThickness?: number;
92
+ /**
93
+ * In early versions of both KaTeX (<0.8.0) and MathJax, the `\color`
94
+ * function expected the content to be a function argument, as in
95
+ * `\color{blue}{hello}`. In current KaTeX, `\color` is a switch, as in
96
+ * `\color{blue}` hello. This matches LaTeX behavior. If you want the old
97
+ * `\color` behavior, set option colorIsTextColor to true.
98
+ */
99
+ colorIsTextColor?: boolean;
100
+ /**
101
+ * All user-specified sizes, e.g. in `\rule{500em}{500em}`, will be capped
102
+ * to `maxSize` ems. If set to `Infinity` (the default), users can make
103
+ * elements and spaces arbitrarily large.
104
+ *
105
+ * @default Infinity
106
+ */
107
+ maxSize?: number;
108
+ /**
109
+ * Limit the number of macro expansions to the specified number, to prevent
110
+ * e.g. infinite macro loops. `\edef` expansion counts all expanded tokens.
111
+ * If set to `Infinity`, the macro expander will try to fully expand as in
112
+ * LaTeX.
113
+ *
114
+ * @default 1000
115
+ */
116
+ maxExpand?: number;
117
+ /**
118
+ * If `false` or `"ignore"`, allow features that make writing LaTeX
119
+ * convenient but are not actually supported by (Xe)LaTeX
120
+ * (similar to MathJax).
121
+ * If `true` or `"error"` (LaTeX faithfulness mode), throw an error for any
122
+ * such transgressions.
123
+ * If `"warn"` (the default), warn about such behavior via `console.warn`.
124
+ * Provide a custom function `handler(errorCode, errorMsg, token)` to
125
+ * customize behavior depending on the type of transgression (summarized by
126
+ * the string code `errorCode` and detailed in `errorMsg`); this function
127
+ * can also return `"ignore"`, `"error"`, or `"warn"` to use a built-in
128
+ * behavior.
129
+ * @see https://katex.org/docs/options
130
+ *
131
+ * @default "warn"
132
+ */
133
+ strict?:
134
+ | boolean
135
+ | "ignore" | "warn" | "error"
136
+ | ((errorCode: string, errorMsg: string, token: object) => boolean | "ignore" | "warn" | "error" | undefined | null);
137
+ /**
138
+ * If `false` (do not trust input), prevent any commands like
139
+ * `\includegraphics` that could enable adverse behavior, rendering them
140
+ * instead in `errorColor`.
141
+ * If `true` (trust input), allow all such commands.
142
+ * Provide a custom function `handler(context)` to customize behavior
143
+ * depending on the context (command, arguments e.g. a URL, etc.).
144
+ * @see https://katex.org/docs/options
145
+ *
146
+ * @default false
147
+ */
148
+ trust?: boolean | ((context: TrustContext) => boolean);
149
+ /**
150
+ * Run KaTeX code in the global group. As a consequence, macros defined at
151
+ * the top level by `\def` and `\newcommand` are added to the macros
152
+ * argument and can be used in subsequent render calls. In LaTeX,
153
+ * constructs such as `\begin{equation}` and `$$` create a local group and
154
+ * prevent definitions other than `\gdef` from becoming visible outside of
155
+ * those blocks, so this is KaTeX's default behavior.
156
+ *
157
+ * @default false
158
+ */
159
+ globalGroup?: boolean;
160
+ }
161
+
162
+ /**
163
+ * In-browser rendering
164
+ *
165
+ * Call the `render` function with a TeX expression and a DOM element to
166
+ * render into.
167
+ *
168
+ * @param {string} tex A TeX expression.
169
+ * @param {HTMLElement} element A HTML DOM element.
170
+ * @param {KatexOptions} options An options object.
171
+ * @returns {void}
172
+ * @see https://katex.org/docs/api
173
+ */
174
+ export function render(
175
+ tex: string,
176
+ element: HTMLElement,
177
+ options?: KatexOptions,
178
+ ): void;
179
+
180
+ /**
181
+ * Server-side rendering or rendering to a string
182
+ *
183
+ * Use the `renderToString` function to generate HTML on the server or to
184
+ * generate an HTML string of the rendered math.
185
+ *
186
+ * @param {string} tex A TeX expression.
187
+ * @param {KatexOptions} options An options object.
188
+ * @returns {string} The HTML string of the rendered math.
189
+ * @see https://katex.org/docs/api
190
+ */
191
+ export function renderToString(tex: string, options?: KatexOptions): string;
192
+
193
+ /**
194
+ * If KaTeX encounters an error (invalid or unsupported LaTeX) and
195
+ * `throwOnError` hasn't been set to `false`, then `katex.render` and
196
+ * `katex.renderToString` will throw an exception of type
197
+ * `ParseError`. The message in this error includes some of the LaTeX source
198
+ * code, so needs to be escaped if you want to render it to HTML.
199
+ * @see https://katex.org/docs/error
200
+ */
201
+ export class ParseError implements Error {
202
+ constructor(message: string, token?: object);
203
+ name: "ParseError";
204
+ position: number;
205
+ length: number;
206
+ rawMessage: string;
207
+ message: string;
208
+ }
209
+
210
+ export const version: string;
211
+
212
+ export as namespace katex;
213
+
214
+ declare const katex: {
215
+ version: string;
216
+ render: typeof render;
217
+ renderToString: typeof renderToString;
218
+ ParseError: typeof ParseError;
219
+ };
220
+
221
+ export default katex;