katex 0.15.4 → 0.16.0
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/README.md +3 -3
- package/contrib/copy-tex/README.md +9 -19
- package/contrib/copy-tex/copy-tex.js +39 -12
- package/contrib/copy-tex/katex2tex.js +16 -7
- package/contrib/mathtex-script-type/README.md +5 -5
- package/contrib/mhchem/README.md +1 -1
- package/contrib/mhchem/mhchem.js +1 -1
- package/dist/README.md +3 -3
- package/dist/contrib/copy-tex.js +38 -24
- package/dist/contrib/copy-tex.min.js +1 -1
- package/dist/contrib/copy-tex.mjs +35 -16
- package/dist/contrib/mhchem.js +1 -1
- package/dist/contrib/mhchem.min.js +1 -1
- package/dist/contrib/mhchem.mjs +1 -1
- package/dist/katex.css +1 -1
- package/dist/katex.js +68 -3
- package/dist/katex.min.css +1 -1
- package/dist/katex.min.js +1 -1
- package/dist/katex.mjs +65 -3
- package/package.json +2 -2
- package/src/MacroExpander.js +3 -1
- package/src/defineMacro.js +7 -0
- package/src/macros.js +52 -0
- package/contrib/copy-tex/copy-tex.css +0 -10
- package/contrib/copy-tex/copy-tex.webpack.js +0 -6
- package/contrib/mhchem/mhchem.patch +0 -235
- package/dist/contrib/copy-tex.css +0 -13
- package/dist/contrib/copy-tex.min.css +0 -1
package/README.md
CHANGED
|
@@ -31,13 +31,13 @@ Try out KaTeX [on the demo page](https://katex.org/#demo)!
|
|
|
31
31
|
<!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
|
|
32
32
|
<html>
|
|
33
33
|
<head>
|
|
34
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.
|
|
34
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" integrity="sha384-Xi8rHCmBmhbuyyhbI88391ZKP2dmfnOl4rT9ZfRI7mLTdk1wblIUnrIq35nqwEvC" crossorigin="anonymous">
|
|
35
35
|
|
|
36
36
|
<!-- The loading of KaTeX is deferred to speed up page rendering -->
|
|
37
|
-
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
37
|
+
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js" integrity="sha384-X/XCfMm41VSsqRNQgDerQczD69XqmjOOOwYQvr/uuC+j4OPoNhVgjdGFwhvN02Ja" crossorigin="anonymous"></script>
|
|
38
38
|
|
|
39
39
|
<!-- To automatically render math in text elements, include the auto-render extension: -->
|
|
40
|
-
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
40
|
+
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
|
|
41
41
|
onload="renderMathInElement(document.body);"></script>
|
|
42
42
|
</head>
|
|
43
43
|
...
|
|
@@ -2,29 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
This extension modifies the copy/paste behavior in any browser supporting the
|
|
4
4
|
[Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent)
|
|
5
|
-
so that, when selecting and copying
|
|
5
|
+
so that, when selecting and copying KaTeX-rendered elements, the text
|
|
6
6
|
content of the resulting clipboard renders KaTeX elements as their LaTeX source
|
|
7
7
|
surrounded by specified delimiters. (The HTML content of the resulting
|
|
8
8
|
clipboard remains the selected HTML content, as it normally would.)
|
|
9
9
|
The default delimiters are `$...$` for inline math and `$$...$$` for display
|
|
10
10
|
math, but you can easy switch them to e.g. `\(...\)` and `\[...\]` by
|
|
11
11
|
modifying `copyDelimiters` in [the source code](copy-tex.js).
|
|
12
|
+
Note that a selection containing part of a KaTeX formula gets extended to
|
|
13
|
+
include the entire KaTeX formula.
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
## Usage
|
|
14
16
|
|
|
15
17
|
This extension isn't part of KaTeX proper, so the script should be separately
|
|
16
|
-
included in the page.
|
|
17
|
-
defines KaTeX equations as
|
|
18
|
-
[`user-select: all`](https://developer.mozilla.org/en-US/docs/Web/CSS/user-select)
|
|
19
|
-
so that they get selected all-or-nothing (and thus trigger the good behavior
|
|
20
|
-
provided by this extension). Without this CSS, partially selected equations
|
|
21
|
-
will just get the usual HTML copy/paste behavior.
|
|
18
|
+
included in the page.
|
|
22
19
|
|
|
23
20
|
```html
|
|
24
|
-
<
|
|
25
|
-
<script src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/copy-tex.min.js" integrity="sha384-Ep9Es0VCjVn9dFeaN2uQxgGcGmG+pfZ4eBaHxUpxXDORrrVACZVOpywyzvFRGbmv" crossorigin="anonymous"></script>
|
|
21
|
+
<script src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/copy-tex.min.js" integrity="sha384-ww/583aHhxWkz5DEVn6OKtNiIaLi2iBRNZXfJRiY1Ai7tnJ9UXpEsyvOITVpTl4A" crossorigin="anonymous"></script>
|
|
26
22
|
```
|
|
27
23
|
|
|
24
|
+
(Note that, as of KaTeX 0.16.0, there is no longer a corresponding CSS file.)
|
|
25
|
+
|
|
28
26
|
See [index.html](index.html) for an example.
|
|
29
27
|
(To run this example from a clone of the repository, run `yarn start`
|
|
30
28
|
in the root KaTeX directory, and then visit
|
|
@@ -37,13 +35,5 @@ statement with `require('katex/contrib/copy-tex/katex2tex.js')`.
|
|
|
37
35
|
|
|
38
36
|
ECMAScript module is also available:
|
|
39
37
|
```html
|
|
40
|
-
<script type="module" src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
38
|
+
<script type="module" src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/copy-tex.mjs" integrity="sha384-bVEnwt0PtX+1EuJoOEcm4rgTUWvb2ILTdjHfI1gUe/r5fdqrTcQaUuRdHG2DciuQ" crossorigin="anonymous"></script>
|
|
41
39
|
```
|
|
42
|
-
|
|
43
|
-
### Known Issues
|
|
44
|
-
|
|
45
|
-
This extension has been tested on Chrome, Firefox, Edge, and Safari.
|
|
46
|
-
|
|
47
|
-
Safari copies correctly, but the selection rectangle renders strangely
|
|
48
|
-
(too big) when interacting with display math
|
|
49
|
-
(because of the `user-select: all` CSS).
|
|
@@ -1,23 +1,50 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
1
3
|
import katexReplaceWithTex from './katex2tex';
|
|
2
4
|
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
+
// Return <div class="katex"> element containing node, or null if not found.
|
|
6
|
+
function closestKatex(node: Node): ?Element {
|
|
7
|
+
// If node is a Text Node, for example, go up to containing Element,
|
|
8
|
+
// where we can apply the `closest` method.
|
|
9
|
+
const element: ?Element =
|
|
10
|
+
(node instanceof Element ? node : node.parentElement);
|
|
11
|
+
return element && element.closest('.katex');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Global copy handler to modify behavior on/within .katex elements.
|
|
15
|
+
document.addEventListener('copy', function(event: ClipboardEvent) {
|
|
5
16
|
const selection = window.getSelection();
|
|
6
|
-
if (selection.isCollapsed) {
|
|
7
|
-
return;
|
|
17
|
+
if (selection.isCollapsed || !event.clipboardData) {
|
|
18
|
+
return; // default action OK if selection is empty or unchangeable
|
|
19
|
+
}
|
|
20
|
+
const clipboardData = event.clipboardData;
|
|
21
|
+
const range = selection.getRangeAt(0);
|
|
22
|
+
|
|
23
|
+
// When start point is within a formula, expand to entire formula.
|
|
24
|
+
const startKatex = closestKatex(range.startContainer);
|
|
25
|
+
if (startKatex) {
|
|
26
|
+
range.setStartBefore(startKatex);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Similarly, when end point is within a formula, expand to entire formula.
|
|
30
|
+
const endKatex = closestKatex(range.endContainer);
|
|
31
|
+
if (endKatex) {
|
|
32
|
+
range.setEndAfter(endKatex);
|
|
8
33
|
}
|
|
9
|
-
|
|
34
|
+
|
|
35
|
+
const fragment = range.cloneContents();
|
|
10
36
|
if (!fragment.querySelector('.katex-mathml')) {
|
|
11
|
-
return;
|
|
37
|
+
return; // default action OK if no .katex-mathml elements
|
|
12
38
|
}
|
|
39
|
+
|
|
40
|
+
const htmlContents = Array.prototype.map.call(fragment.childNodes,
|
|
41
|
+
(el) => (el instanceof Text ? el.textContent : el.outerHTML)
|
|
42
|
+
).join('');
|
|
43
|
+
|
|
13
44
|
// Preserve usual HTML copy/paste behavior.
|
|
14
|
-
|
|
15
|
-
for (let i = 0; i < fragment.childNodes.length; i++) {
|
|
16
|
-
html.push(fragment.childNodes[i].outerHTML);
|
|
17
|
-
}
|
|
18
|
-
event.clipboardData.setData('text/html', html.join(''));
|
|
45
|
+
clipboardData.setData('text/html', htmlContents);
|
|
19
46
|
// Rewrite plain-text version.
|
|
20
|
-
|
|
47
|
+
clipboardData.setData('text/plain',
|
|
21
48
|
katexReplaceWithTex(fragment).textContent);
|
|
22
49
|
// Prevent normal copy handling.
|
|
23
50
|
event.preventDefault();
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
export interface CopyDelimiters {
|
|
4
|
+
inline: [string, string],
|
|
5
|
+
display: [string, string],
|
|
6
|
+
}
|
|
7
|
+
|
|
1
8
|
// Set these to how you want inline and display math to be delimited.
|
|
2
|
-
export const defaultCopyDelimiters = {
|
|
9
|
+
export const defaultCopyDelimiters: CopyDelimiters = {
|
|
3
10
|
inline: ['$', '$'], // alternative: ['\(', '\)']
|
|
4
11
|
display: ['$$', '$$'], // alternative: ['\[', '\]']
|
|
5
12
|
};
|
|
@@ -7,16 +14,18 @@ export const defaultCopyDelimiters = {
|
|
|
7
14
|
// Replace .katex elements with their TeX source (<annotation> element).
|
|
8
15
|
// Modifies fragment in-place. Useful for writing your own 'copy' handler,
|
|
9
16
|
// as in copy-tex.js.
|
|
10
|
-
export
|
|
11
|
-
|
|
17
|
+
export function katexReplaceWithTex(
|
|
18
|
+
fragment: DocumentFragment,
|
|
19
|
+
copyDelimiters: CopyDelimiters = defaultCopyDelimiters
|
|
20
|
+
): DocumentFragment {
|
|
12
21
|
// Remove .katex-html blocks that are preceded by .katex-mathml blocks
|
|
13
22
|
// (which will get replaced below).
|
|
14
23
|
const katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html');
|
|
15
24
|
for (let i = 0; i < katexHtml.length; i++) {
|
|
16
25
|
const element = katexHtml[i];
|
|
17
26
|
if (element.remove) {
|
|
18
|
-
element.remove(
|
|
19
|
-
} else {
|
|
27
|
+
element.remove();
|
|
28
|
+
} else if (element.parentNode) {
|
|
20
29
|
element.parentNode.removeChild(element);
|
|
21
30
|
}
|
|
22
31
|
}
|
|
@@ -29,7 +38,7 @@ export const katexReplaceWithTex = function(fragment,
|
|
|
29
38
|
if (texSource) {
|
|
30
39
|
if (element.replaceWith) {
|
|
31
40
|
element.replaceWith(texSource);
|
|
32
|
-
} else {
|
|
41
|
+
} else if (element.parentNode) {
|
|
33
42
|
element.parentNode.replaceChild(texSource, element);
|
|
34
43
|
}
|
|
35
44
|
texSource.innerHTML = copyDelimiters.inline[0] +
|
|
@@ -47,6 +56,6 @@ export const katexReplaceWithTex = function(fragment,
|
|
|
47
56
|
+ copyDelimiters.display[1];
|
|
48
57
|
}
|
|
49
58
|
return fragment;
|
|
50
|
-
}
|
|
59
|
+
}
|
|
51
60
|
|
|
52
61
|
export default katexReplaceWithTex;
|
|
@@ -11,7 +11,7 @@ included in the page, in addition to KaTeX.
|
|
|
11
11
|
Load the extension by adding the following line to your HTML file.
|
|
12
12
|
|
|
13
13
|
```html
|
|
14
|
-
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
14
|
+
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-jiBVvJ8NGGj5n7kJaiWwWp9AjC+Yh8rhZY3GtAX8yU28azcLgoRo4oukO87g7zDT" crossorigin="anonymous"></script>
|
|
15
15
|
```
|
|
16
16
|
You can download the script and use it locally, or from a local KaTeX installation instead.
|
|
17
17
|
|
|
@@ -23,9 +23,9 @@ Then, in the body, we use a `math/tex` script to typeset the equation `x+\sqrt{1
|
|
|
23
23
|
<!DOCTYPE html>
|
|
24
24
|
<html>
|
|
25
25
|
<head>
|
|
26
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.
|
|
27
|
-
<script src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
28
|
-
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
26
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" integrity="sha384-Xi8rHCmBmhbuyyhbI88391ZKP2dmfnOl4rT9ZfRI7mLTdk1wblIUnrIq35nqwEvC" crossorigin="anonymous">
|
|
27
|
+
<script src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js" integrity="sha384-X/XCfMm41VSsqRNQgDerQczD69XqmjOOOwYQvr/uuC+j4OPoNhVgjdGFwhvN02Ja" crossorigin="anonymous"></script>
|
|
28
|
+
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-jiBVvJ8NGGj5n7kJaiWwWp9AjC+Yh8rhZY3GtAX8yU28azcLgoRo4oukO87g7zDT" crossorigin="anonymous"></script>
|
|
29
29
|
</head>
|
|
30
30
|
<body>
|
|
31
31
|
<script type="math/tex">x+\sqrt{1-x^2}</script>
|
|
@@ -35,4 +35,4 @@ Then, in the body, we use a `math/tex` script to typeset the equation `x+\sqrt{1
|
|
|
35
35
|
|
|
36
36
|
ECMAScript module is also available:
|
|
37
37
|
```html
|
|
38
|
-
<script type="module" src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
38
|
+
<script type="module" src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/mathtex-script-type.mjs" integrity="sha384-4EJvC5tvqq9XJxXvdD4JutBokuFw/dCe2AB4gZ9sRpwFFXECpL3qT43tmE0PkpVg" crossorigin="anonymous"></script>
|
package/contrib/mhchem/README.md
CHANGED
|
@@ -7,7 +7,7 @@ This extension adds to KaTeX the `\ce` and `\pu` functions from the [mhchem](htt
|
|
|
7
7
|
This extension isn't part of core KaTeX, so the script should be separately included. Write the following line into the HTML page's `<head>`. Place it *after* the line that calls `katex.js`, and if you make use of the [auto-render](https://katex.org/docs/autorender.html) extension, place it *before* the line that calls `auto-render.js`.
|
|
8
8
|
|
|
9
9
|
```html
|
|
10
|
-
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
10
|
+
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/mhchem.min.js" integrity="sha384-RTN08a0AXIioPBcVosEqPUfKK+rPp+h1x/izR7xMkdMyuwkcZCWdxO+RSwIFtJXN" crossorigin="anonymous"></script>
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
If you remove the `defer` attribute from this tag, then you must also remove the `defer` attribute from the `<script src="https://../katex.min.js">` tag.
|
package/contrib/mhchem/mhchem.js
CHANGED
|
@@ -77,7 +77,7 @@ import katex from "katex";
|
|
|
77
77
|
var chemParse = function (tokens, stateMachine) {
|
|
78
78
|
// Recreate the argument string from KaTeX's array of tokens.
|
|
79
79
|
var str = "";
|
|
80
|
-
var expectedLoc = tokens[tokens.length - 1].loc.start
|
|
80
|
+
var expectedLoc = tokens.length && tokens[tokens.length - 1].loc.start
|
|
81
81
|
for (var i = tokens.length - 1; i >= 0; i--) {
|
|
82
82
|
if(tokens[i].loc.start > expectedLoc) {
|
|
83
83
|
// context.consumeArgs has eaten a space.
|
package/dist/README.md
CHANGED
|
@@ -31,13 +31,13 @@ Try out KaTeX [on the demo page](https://katex.org/#demo)!
|
|
|
31
31
|
<!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
|
|
32
32
|
<html>
|
|
33
33
|
<head>
|
|
34
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.
|
|
34
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" integrity="sha384-Xi8rHCmBmhbuyyhbI88391ZKP2dmfnOl4rT9ZfRI7mLTdk1wblIUnrIq35nqwEvC" crossorigin="anonymous">
|
|
35
35
|
|
|
36
36
|
<!-- The loading of KaTeX is deferred to speed up page rendering -->
|
|
37
|
-
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
37
|
+
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js" integrity="sha384-X/XCfMm41VSsqRNQgDerQczD69XqmjOOOwYQvr/uuC+j4OPoNhVgjdGFwhvN02Ja" crossorigin="anonymous"></script>
|
|
38
38
|
|
|
39
39
|
<!-- To automatically render math in text elements, include the auto-render extension: -->
|
|
40
|
-
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.
|
|
40
|
+
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
|
|
41
41
|
onload="renderMathInElement(document.body);"></script>
|
|
42
42
|
</head>
|
|
43
43
|
...
|
package/dist/contrib/copy-tex.js
CHANGED
|
@@ -23,7 +23,7 @@ var defaultCopyDelimiters = {
|
|
|
23
23
|
// Modifies fragment in-place. Useful for writing your own 'copy' handler,
|
|
24
24
|
// as in copy-tex.js.
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
function katexReplaceWithTex(fragment, copyDelimiters) {
|
|
27
27
|
if (copyDelimiters === void 0) {
|
|
28
28
|
copyDelimiters = defaultCopyDelimiters;
|
|
29
29
|
}
|
|
@@ -36,8 +36,8 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
|
|
|
36
36
|
var element = katexHtml[i];
|
|
37
37
|
|
|
38
38
|
if (element.remove) {
|
|
39
|
-
element.remove(
|
|
40
|
-
} else {
|
|
39
|
+
element.remove();
|
|
40
|
+
} else if (element.parentNode) {
|
|
41
41
|
element.parentNode.removeChild(element);
|
|
42
42
|
}
|
|
43
43
|
} // Replace .katex-mathml elements with their annotation (TeX source)
|
|
@@ -54,7 +54,7 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
|
|
|
54
54
|
if (texSource) {
|
|
55
55
|
if (_element.replaceWith) {
|
|
56
56
|
_element.replaceWith(texSource);
|
|
57
|
-
} else {
|
|
57
|
+
} else if (_element.parentNode) {
|
|
58
58
|
_element.parentNode.replaceChild(texSource, _element);
|
|
59
59
|
}
|
|
60
60
|
|
|
@@ -71,44 +71,58 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
return fragment;
|
|
74
|
-
}
|
|
74
|
+
}
|
|
75
75
|
/* harmony default export */ var katex2tex = (katexReplaceWithTex);
|
|
76
76
|
;// CONCATENATED MODULE: ./contrib/copy-tex/copy-tex.js
|
|
77
|
-
//
|
|
77
|
+
// Return <div class="katex"> element containing node, or null if not found.
|
|
78
|
+
|
|
79
|
+
function closestKatex(node) {
|
|
80
|
+
// If node is a Text Node, for example, go up to containing Element,
|
|
81
|
+
// where we can apply the `closest` method.
|
|
82
|
+
var element = node instanceof Element ? node : node.parentElement;
|
|
83
|
+
return element && element.closest('.katex');
|
|
84
|
+
} // Global copy handler to modify behavior on/within .katex elements.
|
|
85
|
+
|
|
78
86
|
|
|
79
87
|
document.addEventListener('copy', function (event) {
|
|
80
88
|
var selection = window.getSelection();
|
|
81
89
|
|
|
82
|
-
if (selection.isCollapsed) {
|
|
83
|
-
return; // default action OK if selection is empty
|
|
90
|
+
if (selection.isCollapsed || !event.clipboardData) {
|
|
91
|
+
return; // default action OK if selection is empty or unchangeable
|
|
84
92
|
}
|
|
85
93
|
|
|
86
|
-
var
|
|
94
|
+
var clipboardData = event.clipboardData;
|
|
95
|
+
var range = selection.getRangeAt(0); // When start point is within a formula, expand to entire formula.
|
|
87
96
|
|
|
88
|
-
|
|
89
|
-
return; // default action OK if no .katex-mathml elements
|
|
90
|
-
} // Preserve usual HTML copy/paste behavior.
|
|
97
|
+
var startKatex = closestKatex(range.startContainer);
|
|
91
98
|
|
|
99
|
+
if (startKatex) {
|
|
100
|
+
range.setStartBefore(startKatex);
|
|
101
|
+
} // Similarly, when end point is within a formula, expand to entire formula.
|
|
92
102
|
|
|
93
|
-
var html = [];
|
|
94
103
|
|
|
95
|
-
|
|
96
|
-
|
|
104
|
+
var endKatex = closestKatex(range.endContainer);
|
|
105
|
+
|
|
106
|
+
if (endKatex) {
|
|
107
|
+
range.setEndAfter(endKatex);
|
|
97
108
|
}
|
|
98
109
|
|
|
99
|
-
|
|
110
|
+
var fragment = range.cloneContents();
|
|
100
111
|
|
|
101
|
-
|
|
112
|
+
if (!fragment.querySelector('.katex-mathml')) {
|
|
113
|
+
return; // default action OK if no .katex-mathml elements
|
|
114
|
+
}
|
|
102
115
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* This is the webpack entry point for KaTeX. As ECMAScript doesn't support
|
|
108
|
-
* CSS modules natively, a separate entry point is used.
|
|
109
|
-
*/
|
|
116
|
+
var htmlContents = Array.prototype.map.call(fragment.childNodes, function (el) {
|
|
117
|
+
return el instanceof Text ? el.textContent : el.outerHTML;
|
|
118
|
+
}).join(''); // Preserve usual HTML copy/paste behavior.
|
|
110
119
|
|
|
120
|
+
clipboardData.setData('text/html', htmlContents); // Rewrite plain-text version.
|
|
111
121
|
|
|
122
|
+
clipboardData.setData('text/plain', katex2tex(fragment).textContent); // Prevent normal copy handling.
|
|
123
|
+
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
});
|
|
112
126
|
__webpack_exports__ = __webpack_exports__["default"];
|
|
113
127
|
/******/ return __webpack_exports__;
|
|
114
128
|
/******/ })()
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var
|
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}("undefined"!=typeof self?self:this,(function(){return function(){"use strict";var e={},t={inline:["$","$"],display:["$$","$$"]};var n=function(e,n){void 0===n&&(n=t);for(var r=e.querySelectorAll(".katex-mathml + .katex-html"),a=0;a<r.length;a++){var o=r[a];o.remove?o.remove():o.parentNode&&o.parentNode.removeChild(o)}for(var i=e.querySelectorAll(".katex-mathml"),l=0;l<i.length;l++){var f=i[l],c=f.querySelector("annotation");c&&(f.replaceWith?f.replaceWith(c):f.parentNode&&f.parentNode.replaceChild(c,f),c.innerHTML=n.inline[0]+c.innerHTML+n.inline[1])}for(var d=e.querySelectorAll(".katex-display annotation"),s=0;s<d.length;s++){var p=d[s];p.innerHTML=n.display[0]+p.innerHTML.substr(n.inline[0].length,p.innerHTML.length-n.inline[0].length-n.inline[1].length)+n.display[1]}return e};function r(e){var t=e instanceof Element?e:e.parentElement;return t&&t.closest(".katex")}return document.addEventListener("copy",(function(e){var t=window.getSelection();if(!t.isCollapsed&&e.clipboardData){var a=e.clipboardData,o=t.getRangeAt(0),i=r(o.startContainer);i&&o.setStartBefore(i);var l=r(o.endContainer);l&&o.setEndAfter(l);var f=o.cloneContents();if(f.querySelector(".katex-mathml")){var c=Array.prototype.map.call(f.childNodes,(function(e){return e instanceof Text?e.textContent:e.outerHTML})).join("");a.setData("text/html",c),a.setData("text/plain",n(f).textContent),e.preventDefault()}}})),e=e.default}()}));
|
|
@@ -8,7 +8,7 @@ var defaultCopyDelimiters = {
|
|
|
8
8
|
// Modifies fragment in-place. Useful for writing your own 'copy' handler,
|
|
9
9
|
// as in copy-tex.js.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
function katexReplaceWithTex(fragment, copyDelimiters) {
|
|
12
12
|
if (copyDelimiters === void 0) {
|
|
13
13
|
copyDelimiters = defaultCopyDelimiters;
|
|
14
14
|
}
|
|
@@ -21,8 +21,8 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
|
|
|
21
21
|
var element = katexHtml[i];
|
|
22
22
|
|
|
23
23
|
if (element.remove) {
|
|
24
|
-
element.remove(
|
|
25
|
-
} else {
|
|
24
|
+
element.remove();
|
|
25
|
+
} else if (element.parentNode) {
|
|
26
26
|
element.parentNode.removeChild(element);
|
|
27
27
|
}
|
|
28
28
|
} // Replace .katex-mathml elements with their annotation (TeX source)
|
|
@@ -39,7 +39,7 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
|
|
|
39
39
|
if (texSource) {
|
|
40
40
|
if (_element.replaceWith) {
|
|
41
41
|
_element.replaceWith(texSource);
|
|
42
|
-
} else {
|
|
42
|
+
} else if (_element.parentNode) {
|
|
43
43
|
_element.parentNode.replaceChild(texSource, _element);
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -56,31 +56,50 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
return fragment;
|
|
59
|
-
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function closestKatex(node) {
|
|
62
|
+
// If node is a Text Node, for example, go up to containing Element,
|
|
63
|
+
// where we can apply the `closest` method.
|
|
64
|
+
var element = node instanceof Element ? node : node.parentElement;
|
|
65
|
+
return element && element.closest('.katex');
|
|
66
|
+
} // Global copy handler to modify behavior on/within .katex elements.
|
|
67
|
+
|
|
60
68
|
|
|
61
69
|
document.addEventListener('copy', function (event) {
|
|
62
70
|
var selection = window.getSelection();
|
|
63
71
|
|
|
64
|
-
if (selection.isCollapsed) {
|
|
65
|
-
return; // default action OK if selection is empty
|
|
72
|
+
if (selection.isCollapsed || !event.clipboardData) {
|
|
73
|
+
return; // default action OK if selection is empty or unchangeable
|
|
66
74
|
}
|
|
67
75
|
|
|
68
|
-
var
|
|
76
|
+
var clipboardData = event.clipboardData;
|
|
77
|
+
var range = selection.getRangeAt(0); // When start point is within a formula, expand to entire formula.
|
|
69
78
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
79
|
+
var startKatex = closestKatex(range.startContainer);
|
|
80
|
+
|
|
81
|
+
if (startKatex) {
|
|
82
|
+
range.setStartBefore(startKatex);
|
|
83
|
+
} // Similarly, when end point is within a formula, expand to entire formula.
|
|
73
84
|
|
|
74
85
|
|
|
75
|
-
var
|
|
86
|
+
var endKatex = closestKatex(range.endContainer);
|
|
76
87
|
|
|
77
|
-
|
|
78
|
-
|
|
88
|
+
if (endKatex) {
|
|
89
|
+
range.setEndAfter(endKatex);
|
|
79
90
|
}
|
|
80
91
|
|
|
81
|
-
|
|
92
|
+
var fragment = range.cloneContents();
|
|
93
|
+
|
|
94
|
+
if (!fragment.querySelector('.katex-mathml')) {
|
|
95
|
+
return; // default action OK if no .katex-mathml elements
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
var htmlContents = Array.prototype.map.call(fragment.childNodes, el => el instanceof Text ? el.textContent : el.outerHTML).join(''); // Preserve usual HTML copy/paste behavior.
|
|
99
|
+
|
|
100
|
+
clipboardData.setData('text/html', htmlContents); // Rewrite plain-text version.
|
|
82
101
|
|
|
83
|
-
|
|
102
|
+
clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent); // Prevent normal copy handling.
|
|
84
103
|
|
|
85
104
|
event.preventDefault();
|
|
86
105
|
});
|
package/dist/contrib/mhchem.js
CHANGED
|
@@ -154,7 +154,7 @@ katex__WEBPACK_IMPORTED_MODULE_0___default().__defineMacro("\\tripledash", "{\\v
|
|
|
154
154
|
var chemParse = function chemParse(tokens, stateMachine) {
|
|
155
155
|
// Recreate the argument string from KaTeX's array of tokens.
|
|
156
156
|
var str = "";
|
|
157
|
-
var expectedLoc = tokens[tokens.length - 1].loc.start;
|
|
157
|
+
var expectedLoc = tokens.length && tokens[tokens.length - 1].loc.start;
|
|
158
158
|
|
|
159
159
|
for (var i = tokens.length - 1; i >= 0; i--) {
|
|
160
160
|
if (tokens[i].loc.start > expectedLoc) {
|