@liqvid/katex 0.0.5 → 0.1.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/dist/cjs/RenderGroup.cjs +76 -0
- package/dist/cjs/fancy.cjs +29 -0
- package/dist/cjs/index.cjs +9 -0
- package/dist/{plain.cjs → cjs/loading.cjs} +4 -55
- package/dist/cjs/plain.cjs +50 -0
- package/dist/esm/RenderGroup.mjs +73 -0
- package/dist/esm/fancy.mjs +26 -0
- package/dist/esm/index.mjs +3 -0
- package/dist/{plain.mjs → esm/loading.mjs} +1 -51
- package/dist/esm/plain.mjs +47 -0
- package/dist/types/RenderGroup.d.ts +19 -0
- package/dist/types/fancy.d.ts +17 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/loading.d.ts +6 -0
- package/dist/{plain.d.ts → types/plain.d.ts} +3 -6
- package/package.json +18 -15
- package/dist/index.cjs +0 -212
- package/dist/index.d.ts +0 -67
- package/dist/index.mjs +0 -206
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RenderGroup = void 0;
|
|
4
|
+
const react_1 = require("@liqvid/utils/react");
|
|
5
|
+
const liqvid_1 = require("liqvid");
|
|
6
|
+
const react_2 = require("react");
|
|
7
|
+
const fancy_1 = require("./fancy.cjs");
|
|
8
|
+
const plain_1 = require("./plain.cjs");
|
|
9
|
+
/**
|
|
10
|
+
* Wait for several things to be rendered
|
|
11
|
+
*/
|
|
12
|
+
exports.RenderGroup = (0, react_2.forwardRef)(function RenderGroup(props, ref) {
|
|
13
|
+
const [ready, resolve] = (0, react_1.usePromise)();
|
|
14
|
+
// handle
|
|
15
|
+
(0, react_2.useImperativeHandle)(ref, () => ({ ready }));
|
|
16
|
+
const elements = (0, react_2.useRef)([]);
|
|
17
|
+
const promises = (0, react_2.useRef)([]);
|
|
18
|
+
// reparsing
|
|
19
|
+
const player = (0, liqvid_1.usePlayer)();
|
|
20
|
+
(0, react_2.useEffect)(() => {
|
|
21
|
+
// promises
|
|
22
|
+
Promise.all(promises.current).then(() => {
|
|
23
|
+
// reparse
|
|
24
|
+
if (props.reparse) {
|
|
25
|
+
player.reparseTree(leastCommonAncestor(elements.current));
|
|
26
|
+
}
|
|
27
|
+
// ready()
|
|
28
|
+
resolve();
|
|
29
|
+
});
|
|
30
|
+
}, []);
|
|
31
|
+
return (0, react_1.recursiveMap)(props.children, node => {
|
|
32
|
+
if (shouldInspect(node)) {
|
|
33
|
+
const originalRef = node.ref;
|
|
34
|
+
return (0, react_2.cloneElement)(node, {
|
|
35
|
+
ref: (ref) => {
|
|
36
|
+
if (!ref)
|
|
37
|
+
return;
|
|
38
|
+
elements.current.push(ref.domElement);
|
|
39
|
+
promises.current.push(ref.ready);
|
|
40
|
+
// pass along original ref
|
|
41
|
+
if (typeof originalRef === "function") {
|
|
42
|
+
originalRef(ref);
|
|
43
|
+
}
|
|
44
|
+
else if (originalRef && typeof originalRef === "object") {
|
|
45
|
+
originalRef.current = ref;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return node;
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
/**
|
|
54
|
+
* Determine whether the node is a <KTX> element
|
|
55
|
+
* @param node Element to check
|
|
56
|
+
*/
|
|
57
|
+
function shouldInspect(node) {
|
|
58
|
+
return (0, react_2.isValidElement)(node) && typeof node.type === "object" && (node.type === fancy_1.KTX || node.type === plain_1.KTX);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Find least common ancestor of an array of elements
|
|
62
|
+
* @param elements Elements
|
|
63
|
+
* @returns Deepest node containing all passed elements
|
|
64
|
+
*/
|
|
65
|
+
function leastCommonAncestor(elements) {
|
|
66
|
+
if (elements.length === 0) {
|
|
67
|
+
throw new Error("Must pass at least one element");
|
|
68
|
+
}
|
|
69
|
+
let ancestor = elements[0];
|
|
70
|
+
let failing = elements.slice(1);
|
|
71
|
+
while (failing.length > 0) {
|
|
72
|
+
ancestor = ancestor.parentElement;
|
|
73
|
+
failing = failing.filter(node => !ancestor.contains(node));
|
|
74
|
+
}
|
|
75
|
+
return ancestor;
|
|
76
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KTX = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("@liqvid/utils/react");
|
|
6
|
+
const liqvid_1 = require("liqvid");
|
|
7
|
+
const react_2 = require("react");
|
|
8
|
+
const plain_1 = require("./plain.cjs");
|
|
9
|
+
/** Component for KaTeX code */
|
|
10
|
+
exports.KTX = (0, react_2.forwardRef)(function KTX(props, ref) {
|
|
11
|
+
const { obstruct = "canplay canplaythrough", reparse = false, ...attrs } = props;
|
|
12
|
+
const plain = (0, react_2.useRef)();
|
|
13
|
+
const combined = (0, react_1.combineRefs)(plain, ref);
|
|
14
|
+
const player = (0, liqvid_1.usePlayer)();
|
|
15
|
+
(0, react_2.useEffect)(() => {
|
|
16
|
+
// obstruction
|
|
17
|
+
if (obstruct.match(/\bcanplay\b/)) {
|
|
18
|
+
player.obstruct("canplay", plain.current.ready);
|
|
19
|
+
}
|
|
20
|
+
if (obstruct.match("canplaythrough")) {
|
|
21
|
+
player.obstruct("canplaythrough", plain.current.ready);
|
|
22
|
+
}
|
|
23
|
+
// reparsing
|
|
24
|
+
if (reparse) {
|
|
25
|
+
plain.current.ready.then(() => player.reparseTree(plain.current.domElement));
|
|
26
|
+
}
|
|
27
|
+
}, []);
|
|
28
|
+
return ((0, jsx_runtime_1.jsx)(plain_1.KTX, { ref: combined, ...attrs }));
|
|
29
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RenderGroup = exports.KaTeXReady = exports.KTX = void 0;
|
|
4
|
+
var fancy_1 = require("./fancy.cjs");
|
|
5
|
+
Object.defineProperty(exports, "KTX", { enumerable: true, get: function () { return fancy_1.KTX; } });
|
|
6
|
+
var loading_1 = require("./loading.cjs");
|
|
7
|
+
Object.defineProperty(exports, "KaTeXReady", { enumerable: true, get: function () { return loading_1.KaTeXReady; } });
|
|
8
|
+
var RenderGroup_1 = require("./RenderGroup.cjs");
|
|
9
|
+
Object.defineProperty(exports, "RenderGroup", { enumerable: true, get: function () { return RenderGroup_1.RenderGroup; } });
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var jsxRuntime_js = require('react/jsx-runtime.js');
|
|
6
|
-
var react$1 = require('@liqvid/utils/react');
|
|
7
|
-
var react = require('react');
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KaTeXReady = void 0;
|
|
9
4
|
// option of loading KaTeX asynchronously
|
|
10
5
|
const KaTeXLoad = new Promise((resolve) => {
|
|
11
6
|
const script = document.querySelector("script[src*=\"katex.js\"], script[src*=\"katex.min.js\"]");
|
|
@@ -35,7 +30,7 @@ const KaTeXMacros = new Promise((resolve) => {
|
|
|
35
30
|
/**
|
|
36
31
|
* Ready Promise
|
|
37
32
|
*/
|
|
38
|
-
|
|
33
|
+
exports.KaTeXReady = Promise.all([KaTeXLoad, KaTeXMacros]);
|
|
39
34
|
/**
|
|
40
35
|
* Parse \newcommand macros in a file.
|
|
41
36
|
* Also supports \ktxnewcommand (for use in conjunction with MathJax).
|
|
@@ -70,49 +65,3 @@ function parseMacros(file) {
|
|
|
70
65
|
}
|
|
71
66
|
return macros;
|
|
72
67
|
}
|
|
73
|
-
|
|
74
|
-
/** Component for KaTeX code */
|
|
75
|
-
const KTX = react.forwardRef(function KTX(props, ref) {
|
|
76
|
-
const spanRef = react.useRef();
|
|
77
|
-
const { children, display = false, ...attrs } = props;
|
|
78
|
-
const [ready, resolve] = react$1.usePromise();
|
|
79
|
-
// handle
|
|
80
|
-
react.useImperativeHandle(ref, () => ({
|
|
81
|
-
domElement: spanRef.current,
|
|
82
|
-
ready
|
|
83
|
-
}));
|
|
84
|
-
react.useEffect(() => {
|
|
85
|
-
KaTeXReady.then(([katex, macros]) => {
|
|
86
|
-
katex.render(children.toString(), spanRef.current, {
|
|
87
|
-
displayMode: !!display,
|
|
88
|
-
macros,
|
|
89
|
-
strict: "ignore",
|
|
90
|
-
throwOnError: false,
|
|
91
|
-
trust: true
|
|
92
|
-
});
|
|
93
|
-
/* move katex into placeholder element */
|
|
94
|
-
const child = spanRef.current.firstElementChild;
|
|
95
|
-
// copy classes
|
|
96
|
-
for (let i = 0, len = child.classList.length; i < len; ++i) {
|
|
97
|
-
spanRef.current.classList.add(child.classList.item(i));
|
|
98
|
-
}
|
|
99
|
-
// move children
|
|
100
|
-
while (child.childNodes.length > 0) {
|
|
101
|
-
spanRef.current.appendChild(child.firstChild);
|
|
102
|
-
}
|
|
103
|
-
// delete child
|
|
104
|
-
child.remove();
|
|
105
|
-
// resolve promise
|
|
106
|
-
resolve();
|
|
107
|
-
});
|
|
108
|
-
}, [children]);
|
|
109
|
-
// Google Chrome fails without this
|
|
110
|
-
if (display) {
|
|
111
|
-
if (!attrs.style)
|
|
112
|
-
attrs.style = {};
|
|
113
|
-
attrs.style.display = "block";
|
|
114
|
-
}
|
|
115
|
-
return (jsxRuntime_js.jsx("span", { ...attrs, ref: spanRef }));
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
exports.KTX = KTX;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KTX = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("@liqvid/utils/react");
|
|
6
|
+
const react_2 = require("react");
|
|
7
|
+
const loading_1 = require("./loading.cjs");
|
|
8
|
+
/** Component for KaTeX code */
|
|
9
|
+
exports.KTX = (0, react_2.forwardRef)(function KTX(props, ref) {
|
|
10
|
+
const spanRef = (0, react_2.useRef)();
|
|
11
|
+
const { children, display = false, ...attrs } = props;
|
|
12
|
+
const [ready, resolve] = (0, react_1.usePromise)();
|
|
13
|
+
// handle
|
|
14
|
+
(0, react_2.useImperativeHandle)(ref, () => ({
|
|
15
|
+
domElement: spanRef.current,
|
|
16
|
+
ready
|
|
17
|
+
}));
|
|
18
|
+
(0, react_2.useEffect)(() => {
|
|
19
|
+
loading_1.KaTeXReady.then(([katex, macros]) => {
|
|
20
|
+
katex.render(children.toString(), spanRef.current, {
|
|
21
|
+
displayMode: !!display,
|
|
22
|
+
macros,
|
|
23
|
+
strict: "ignore",
|
|
24
|
+
throwOnError: false,
|
|
25
|
+
trust: true
|
|
26
|
+
});
|
|
27
|
+
/* move katex into placeholder element */
|
|
28
|
+
const child = spanRef.current.firstElementChild;
|
|
29
|
+
// copy classes
|
|
30
|
+
for (let i = 0, len = child.classList.length; i < len; ++i) {
|
|
31
|
+
spanRef.current.classList.add(child.classList.item(i));
|
|
32
|
+
}
|
|
33
|
+
// move children
|
|
34
|
+
while (child.childNodes.length > 0) {
|
|
35
|
+
spanRef.current.appendChild(child.firstChild);
|
|
36
|
+
}
|
|
37
|
+
// delete child
|
|
38
|
+
child.remove();
|
|
39
|
+
// resolve promise
|
|
40
|
+
resolve();
|
|
41
|
+
});
|
|
42
|
+
}, [children]);
|
|
43
|
+
// Google Chrome fails without this
|
|
44
|
+
if (display) {
|
|
45
|
+
if (!attrs.style)
|
|
46
|
+
attrs.style = {};
|
|
47
|
+
attrs.style.display = "block";
|
|
48
|
+
}
|
|
49
|
+
return ((0, jsx_runtime_1.jsx)("span", { ...attrs, ref: spanRef }));
|
|
50
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { recursiveMap, usePromise } from "@liqvid/utils/react";
|
|
2
|
+
import { usePlayer } from "liqvid";
|
|
3
|
+
import { cloneElement, forwardRef, isValidElement, useEffect, useImperativeHandle, useRef } from "react";
|
|
4
|
+
import { KTX } from "./fancy.mjs";
|
|
5
|
+
import { KTX as KTXPlain } from "./plain.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* Wait for several things to be rendered
|
|
8
|
+
*/
|
|
9
|
+
export const RenderGroup = forwardRef(function RenderGroup(props, ref) {
|
|
10
|
+
const [ready, resolve] = usePromise();
|
|
11
|
+
// handle
|
|
12
|
+
useImperativeHandle(ref, () => ({ ready }));
|
|
13
|
+
const elements = useRef([]);
|
|
14
|
+
const promises = useRef([]);
|
|
15
|
+
// reparsing
|
|
16
|
+
const player = usePlayer();
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
// promises
|
|
19
|
+
Promise.all(promises.current).then(() => {
|
|
20
|
+
// reparse
|
|
21
|
+
if (props.reparse) {
|
|
22
|
+
player.reparseTree(leastCommonAncestor(elements.current));
|
|
23
|
+
}
|
|
24
|
+
// ready()
|
|
25
|
+
resolve();
|
|
26
|
+
});
|
|
27
|
+
}, []);
|
|
28
|
+
return recursiveMap(props.children, node => {
|
|
29
|
+
if (shouldInspect(node)) {
|
|
30
|
+
const originalRef = node.ref;
|
|
31
|
+
return cloneElement(node, {
|
|
32
|
+
ref: (ref) => {
|
|
33
|
+
if (!ref)
|
|
34
|
+
return;
|
|
35
|
+
elements.current.push(ref.domElement);
|
|
36
|
+
promises.current.push(ref.ready);
|
|
37
|
+
// pass along original ref
|
|
38
|
+
if (typeof originalRef === "function") {
|
|
39
|
+
originalRef(ref);
|
|
40
|
+
}
|
|
41
|
+
else if (originalRef && typeof originalRef === "object") {
|
|
42
|
+
originalRef.current = ref;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return node;
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* Determine whether the node is a <KTX> element
|
|
52
|
+
* @param node Element to check
|
|
53
|
+
*/
|
|
54
|
+
function shouldInspect(node) {
|
|
55
|
+
return isValidElement(node) && typeof node.type === "object" && (node.type === KTX || node.type === KTXPlain);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Find least common ancestor of an array of elements
|
|
59
|
+
* @param elements Elements
|
|
60
|
+
* @returns Deepest node containing all passed elements
|
|
61
|
+
*/
|
|
62
|
+
function leastCommonAncestor(elements) {
|
|
63
|
+
if (elements.length === 0) {
|
|
64
|
+
throw new Error("Must pass at least one element");
|
|
65
|
+
}
|
|
66
|
+
let ancestor = elements[0];
|
|
67
|
+
let failing = elements.slice(1);
|
|
68
|
+
while (failing.length > 0) {
|
|
69
|
+
ancestor = ancestor.parentElement;
|
|
70
|
+
failing = failing.filter(node => !ancestor.contains(node));
|
|
71
|
+
}
|
|
72
|
+
return ancestor;
|
|
73
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { combineRefs } from "@liqvid/utils/react";
|
|
3
|
+
import { usePlayer } from "liqvid";
|
|
4
|
+
import { forwardRef, useEffect, useRef } from "react";
|
|
5
|
+
import { KTX as KTXPlain } from "./plain.mjs";
|
|
6
|
+
/** Component for KaTeX code */
|
|
7
|
+
export const KTX = forwardRef(function KTX(props, ref) {
|
|
8
|
+
const { obstruct = "canplay canplaythrough", reparse = false, ...attrs } = props;
|
|
9
|
+
const plain = useRef();
|
|
10
|
+
const combined = combineRefs(plain, ref);
|
|
11
|
+
const player = usePlayer();
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
// obstruction
|
|
14
|
+
if (obstruct.match(/\bcanplay\b/)) {
|
|
15
|
+
player.obstruct("canplay", plain.current.ready);
|
|
16
|
+
}
|
|
17
|
+
if (obstruct.match("canplaythrough")) {
|
|
18
|
+
player.obstruct("canplaythrough", plain.current.ready);
|
|
19
|
+
}
|
|
20
|
+
// reparsing
|
|
21
|
+
if (reparse) {
|
|
22
|
+
plain.current.ready.then(() => player.reparseTree(plain.current.domElement));
|
|
23
|
+
}
|
|
24
|
+
}, []);
|
|
25
|
+
return (_jsx(KTXPlain, { ref: combined, ...attrs }));
|
|
26
|
+
});
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import { jsx } from 'react/jsx-runtime.js';
|
|
2
|
-
import { usePromise } from '@liqvid/utils/react';
|
|
3
|
-
import { forwardRef, useRef, useImperativeHandle, useEffect } from 'react';
|
|
4
|
-
|
|
5
1
|
// option of loading KaTeX asynchronously
|
|
6
2
|
const KaTeXLoad = new Promise((resolve) => {
|
|
7
3
|
const script = document.querySelector("script[src*=\"katex.js\"], script[src*=\"katex.min.js\"]");
|
|
@@ -31,7 +27,7 @@ const KaTeXMacros = new Promise((resolve) => {
|
|
|
31
27
|
/**
|
|
32
28
|
* Ready Promise
|
|
33
29
|
*/
|
|
34
|
-
const KaTeXReady = Promise.all([KaTeXLoad, KaTeXMacros]);
|
|
30
|
+
export const KaTeXReady = Promise.all([KaTeXLoad, KaTeXMacros]);
|
|
35
31
|
/**
|
|
36
32
|
* Parse \newcommand macros in a file.
|
|
37
33
|
* Also supports \ktxnewcommand (for use in conjunction with MathJax).
|
|
@@ -66,49 +62,3 @@ function parseMacros(file) {
|
|
|
66
62
|
}
|
|
67
63
|
return macros;
|
|
68
64
|
}
|
|
69
|
-
|
|
70
|
-
/** Component for KaTeX code */
|
|
71
|
-
const KTX = forwardRef(function KTX(props, ref) {
|
|
72
|
-
const spanRef = useRef();
|
|
73
|
-
const { children, display = false, ...attrs } = props;
|
|
74
|
-
const [ready, resolve] = usePromise();
|
|
75
|
-
// handle
|
|
76
|
-
useImperativeHandle(ref, () => ({
|
|
77
|
-
domElement: spanRef.current,
|
|
78
|
-
ready
|
|
79
|
-
}));
|
|
80
|
-
useEffect(() => {
|
|
81
|
-
KaTeXReady.then(([katex, macros]) => {
|
|
82
|
-
katex.render(children.toString(), spanRef.current, {
|
|
83
|
-
displayMode: !!display,
|
|
84
|
-
macros,
|
|
85
|
-
strict: "ignore",
|
|
86
|
-
throwOnError: false,
|
|
87
|
-
trust: true
|
|
88
|
-
});
|
|
89
|
-
/* move katex into placeholder element */
|
|
90
|
-
const child = spanRef.current.firstElementChild;
|
|
91
|
-
// copy classes
|
|
92
|
-
for (let i = 0, len = child.classList.length; i < len; ++i) {
|
|
93
|
-
spanRef.current.classList.add(child.classList.item(i));
|
|
94
|
-
}
|
|
95
|
-
// move children
|
|
96
|
-
while (child.childNodes.length > 0) {
|
|
97
|
-
spanRef.current.appendChild(child.firstChild);
|
|
98
|
-
}
|
|
99
|
-
// delete child
|
|
100
|
-
child.remove();
|
|
101
|
-
// resolve promise
|
|
102
|
-
resolve();
|
|
103
|
-
});
|
|
104
|
-
}, [children]);
|
|
105
|
-
// Google Chrome fails without this
|
|
106
|
-
if (display) {
|
|
107
|
-
if (!attrs.style)
|
|
108
|
-
attrs.style = {};
|
|
109
|
-
attrs.style.display = "block";
|
|
110
|
-
}
|
|
111
|
-
return (jsx("span", { ...attrs, ref: spanRef }));
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
export { KTX };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { usePromise } from "@liqvid/utils/react";
|
|
3
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
4
|
+
import { KaTeXReady } from "./loading.mjs";
|
|
5
|
+
/** Component for KaTeX code */
|
|
6
|
+
export const KTX = forwardRef(function KTX(props, ref) {
|
|
7
|
+
const spanRef = useRef();
|
|
8
|
+
const { children, display = false, ...attrs } = props;
|
|
9
|
+
const [ready, resolve] = usePromise();
|
|
10
|
+
// handle
|
|
11
|
+
useImperativeHandle(ref, () => ({
|
|
12
|
+
domElement: spanRef.current,
|
|
13
|
+
ready
|
|
14
|
+
}));
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
KaTeXReady.then(([katex, macros]) => {
|
|
17
|
+
katex.render(children.toString(), spanRef.current, {
|
|
18
|
+
displayMode: !!display,
|
|
19
|
+
macros,
|
|
20
|
+
strict: "ignore",
|
|
21
|
+
throwOnError: false,
|
|
22
|
+
trust: true
|
|
23
|
+
});
|
|
24
|
+
/* move katex into placeholder element */
|
|
25
|
+
const child = spanRef.current.firstElementChild;
|
|
26
|
+
// copy classes
|
|
27
|
+
for (let i = 0, len = child.classList.length; i < len; ++i) {
|
|
28
|
+
spanRef.current.classList.add(child.classList.item(i));
|
|
29
|
+
}
|
|
30
|
+
// move children
|
|
31
|
+
while (child.childNodes.length > 0) {
|
|
32
|
+
spanRef.current.appendChild(child.firstChild);
|
|
33
|
+
}
|
|
34
|
+
// delete child
|
|
35
|
+
child.remove();
|
|
36
|
+
// resolve promise
|
|
37
|
+
resolve();
|
|
38
|
+
});
|
|
39
|
+
}, [children]);
|
|
40
|
+
// Google Chrome fails without this
|
|
41
|
+
if (display) {
|
|
42
|
+
if (!attrs.style)
|
|
43
|
+
attrs.style = {};
|
|
44
|
+
attrs.style.display = "block";
|
|
45
|
+
}
|
|
46
|
+
return (_jsx("span", { ...attrs, ref: spanRef }));
|
|
47
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
/** RenderGroup element API */
|
|
3
|
+
interface Handle {
|
|
4
|
+
/** Promise that resolves once all KTX descendants have finished typesetting */
|
|
5
|
+
ready: Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
interface Props {
|
|
8
|
+
children?: React.ReactNode;
|
|
9
|
+
/**
|
|
10
|
+
* Whether to reparse descendants for `during()` and `from()`
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
reparse?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Wait for several things to be rendered
|
|
17
|
+
*/
|
|
18
|
+
export declare const RenderGroup: React.ForwardRefExoticComponent<Props & React.RefAttributes<Handle>>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { Handle, KTX as KTXPlain } from "./plain";
|
|
3
|
+
interface Props extends React.ComponentProps<typeof KTXPlain> {
|
|
4
|
+
/**
|
|
5
|
+
* Player events to obstruct
|
|
6
|
+
* @default "canplay canplaythrough"
|
|
7
|
+
*/
|
|
8
|
+
obstruct?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Whether to reparse descendants for `during()` and `from()`
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
reparse?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** Component for KaTeX code */
|
|
16
|
+
export declare const KTX: import("react").ForwardRefExoticComponent<Pick<Props, "key" | "id" | "color" | "display" | "translate" | "hidden" | "dir" | "slot" | "style" | "title" | "accessKey" | "draggable" | "lang" | "className" | "prefix" | "children" | "contentEditable" | "inputMode" | "tabIndex" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | "suppressHydrationWarning" | "contextMenu" | "placeholder" | "spellCheck" | "radioGroup" | "role" | "about" | "datatype" | "inlist" | "property" | "resource" | "typeof" | "vocab" | "autoCapitalize" | "autoCorrect" | "autoSave" | "itemProp" | "itemScope" | "itemType" | "itemID" | "itemRef" | "results" | "security" | "unselectable" | "is" | "aria-activedescendant" | "aria-atomic" | "aria-autocomplete" | "aria-busy" | "aria-checked" | "aria-colcount" | "aria-colindex" | "aria-colspan" | "aria-controls" | "aria-current" | "aria-describedby" | "aria-details" | "aria-disabled" | "aria-dropeffect" | "aria-errormessage" | "aria-expanded" | "aria-flowto" | "aria-grabbed" | "aria-haspopup" | "aria-hidden" | "aria-invalid" | "aria-keyshortcuts" | "aria-label" | "aria-labelledby" | "aria-level" | "aria-live" | "aria-modal" | "aria-multiline" | "aria-multiselectable" | "aria-orientation" | "aria-owns" | "aria-placeholder" | "aria-posinset" | "aria-pressed" | "aria-readonly" | "aria-relevant" | "aria-required" | "aria-roledescription" | "aria-rowcount" | "aria-rowindex" | "aria-rowspan" | "aria-selected" | "aria-setsize" | "aria-sort" | "aria-valuemax" | "aria-valuemin" | "aria-valuenow" | "aria-valuetext" | "dangerouslySetInnerHTML" | "onCopy" | "onCopyCapture" | "onCut" | "onCutCapture" | "onPaste" | "onPasteCapture" | "onCompositionEnd" | "onCompositionEndCapture" | "onCompositionStart" | "onCompositionStartCapture" | "onCompositionUpdate" | "onCompositionUpdateCapture" | "onFocus" | "onFocusCapture" | "onBlur" | "onBlurCapture" | "onChange" | "onChangeCapture" | "onBeforeInput" | "onBeforeInputCapture" | "onInput" | "onInputCapture" | "onReset" | "onResetCapture" | "onSubmit" | "onSubmitCapture" | "onInvalid" | "onInvalidCapture" | "onLoad" | "onLoadCapture" | "onError" | "onErrorCapture" | "onKeyDown" | "onKeyDownCapture" | "onKeyPress" | "onKeyPressCapture" | "onKeyUp" | "onKeyUpCapture" | "onAbort" | "onAbortCapture" | "onCanPlay" | "onCanPlayCapture" | "onCanPlayThrough" | "onCanPlayThroughCapture" | "onDurationChange" | "onDurationChangeCapture" | "onEmptied" | "onEmptiedCapture" | "onEncrypted" | "onEncryptedCapture" | "onEnded" | "onEndedCapture" | "onLoadedData" | "onLoadedDataCapture" | "onLoadedMetadata" | "onLoadedMetadataCapture" | "onLoadStart" | "onLoadStartCapture" | "onPause" | "onPauseCapture" | "onPlay" | "onPlayCapture" | "onPlaying" | "onPlayingCapture" | "onProgress" | "onProgressCapture" | "onRateChange" | "onRateChangeCapture" | "onSeeked" | "onSeekedCapture" | "onSeeking" | "onSeekingCapture" | "onStalled" | "onStalledCapture" | "onSuspend" | "onSuspendCapture" | "onTimeUpdate" | "onTimeUpdateCapture" | "onVolumeChange" | "onVolumeChangeCapture" | "onWaiting" | "onWaitingCapture" | "onAuxClick" | "onAuxClickCapture" | "onClick" | "onClickCapture" | "onContextMenu" | "onContextMenuCapture" | "onDoubleClick" | "onDoubleClickCapture" | "onDrag" | "onDragCapture" | "onDragEnd" | "onDragEndCapture" | "onDragEnter" | "onDragEnterCapture" | "onDragExit" | "onDragExitCapture" | "onDragLeave" | "onDragLeaveCapture" | "onDragOver" | "onDragOverCapture" | "onDragStart" | "onDragStartCapture" | "onDrop" | "onDropCapture" | "onMouseDown" | "onMouseDownCapture" | "onMouseEnter" | "onMouseLeave" | "onMouseMove" | "onMouseMoveCapture" | "onMouseOut" | "onMouseOutCapture" | "onMouseOver" | "onMouseOverCapture" | "onMouseUp" | "onMouseUpCapture" | "onSelect" | "onSelectCapture" | "onTouchCancel" | "onTouchCancelCapture" | "onTouchEnd" | "onTouchEndCapture" | "onTouchMove" | "onTouchMoveCapture" | "onTouchStart" | "onTouchStartCapture" | "onPointerDown" | "onPointerDownCapture" | "onPointerMove" | "onPointerMoveCapture" | "onPointerUp" | "onPointerUpCapture" | "onPointerCancel" | "onPointerCancelCapture" | "onPointerEnter" | "onPointerEnterCapture" | "onPointerLeave" | "onPointerLeaveCapture" | "onPointerOver" | "onPointerOverCapture" | "onPointerOut" | "onPointerOutCapture" | "onGotPointerCapture" | "onGotPointerCaptureCapture" | "onLostPointerCapture" | "onLostPointerCaptureCapture" | "onScroll" | "onScrollCapture" | "onWheel" | "onWheelCapture" | "onAnimationStart" | "onAnimationStartCapture" | "onAnimationEnd" | "onAnimationEndCapture" | "onAnimationIteration" | "onAnimationIterationCapture" | "onTransitionEnd" | "onTransitionEndCapture" | "obstruct" | "reparse"> & import("react").RefAttributes<Handle>>;
|
|
17
|
+
export {};
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import * as react from 'react';
|
|
3
|
-
|
|
4
2
|
/**
|
|
5
3
|
* KTX element API
|
|
6
4
|
*/
|
|
7
|
-
interface Handle {
|
|
5
|
+
export interface Handle {
|
|
8
6
|
/** The underlying <span> element */
|
|
9
7
|
domElement: HTMLSpanElement;
|
|
10
8
|
/** Promise that resolves once typesetting is finished */
|
|
@@ -18,6 +16,5 @@ interface Props extends React.HTMLAttributes<HTMLSpanElement> {
|
|
|
18
16
|
display?: boolean;
|
|
19
17
|
}
|
|
20
18
|
/** Component for KaTeX code */
|
|
21
|
-
declare const KTX: react.ForwardRefExoticComponent<Props & react.RefAttributes<Handle>>;
|
|
22
|
-
|
|
23
|
-
export { Handle, KTX };
|
|
19
|
+
export declare const KTX: import("react").ForwardRefExoticComponent<Props & import("react").RefAttributes<Handle>>;
|
|
20
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liqvid/katex",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "KaTeX integration for Liqvid",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist/*"
|
|
7
7
|
],
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"import": "./dist/index.mjs",
|
|
11
|
-
"require": "./dist/index.cjs"
|
|
10
|
+
"import": "./dist/esm/index.mjs",
|
|
11
|
+
"require": "./dist/cjs/index.cjs"
|
|
12
12
|
},
|
|
13
13
|
"./plain": {
|
|
14
|
-
"import": "./dist/plain.mjs",
|
|
15
|
-
"require": "./dist/plain.cjs"
|
|
14
|
+
"import": "./dist/esm/plain.mjs",
|
|
15
|
+
"require": "./dist/cjs/plain.cjs"
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"typesVersions": {
|
|
19
19
|
"*": {
|
|
20
20
|
"*": [
|
|
21
|
-
"./dist/*.d.ts"
|
|
21
|
+
"./dist/types/*.d.ts"
|
|
22
22
|
]
|
|
23
23
|
}
|
|
24
24
|
},
|
|
@@ -37,10 +37,10 @@
|
|
|
37
37
|
"homepage": "https://github.com/liqvidjs/liqvid/tree/main/packages/katex",
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@types/katex": "
|
|
41
|
-
"@types/react": ">=
|
|
42
|
-
"liqvid": "^2.1.
|
|
43
|
-
"react": ">=
|
|
40
|
+
"@types/katex": ">=0.14.0",
|
|
41
|
+
"@types/react": ">=18.0.0",
|
|
42
|
+
"liqvid": "^2.1.7",
|
|
43
|
+
"react": ">=18.1.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependenciesMeta": {
|
|
46
46
|
"liqvid": {
|
|
@@ -48,16 +48,19 @@
|
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"liqvid": "^2.1.
|
|
51
|
+
"liqvid": "^2.1.7"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@liqvid/utils": "^1.
|
|
54
|
+
"@liqvid/utils": "^1.6.0"
|
|
55
55
|
},
|
|
56
|
+
"sideEffects": false,
|
|
57
|
+
"type": "module",
|
|
56
58
|
"scripts": {
|
|
57
59
|
"build": "pnpm build:clean && pnpm build:js && pnpm build:postclean",
|
|
58
60
|
"build:clean": "rm -rf dist",
|
|
59
|
-
"build:js": "tsc
|
|
60
|
-
"build:postclean": "rm
|
|
61
|
+
"build:js": "tsc --module esnext --outDir dist/esm; tsc --module commonjs --outDir dist/cjs; node ../../build.mjs",
|
|
62
|
+
"build:postclean": "rm dist/tsconfig.tsbuildinfo",
|
|
61
63
|
"lint": "eslint --ext ts,tsx --fix src"
|
|
62
|
-
}
|
|
64
|
+
},
|
|
65
|
+
"readme": "# @liqvid/katex\n\n[KaTeX](https://katex.org/) integration for [Liqvid](https://liqvidjs.org). See https://liqvidjs.org/docs/integrations/katex/ for documentation.\n"
|
|
63
66
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var jsxRuntime_js = require('react/jsx-runtime.js');
|
|
6
|
-
var react$1 = require('@liqvid/utils/react');
|
|
7
|
-
var liqvid = require('liqvid');
|
|
8
|
-
var react = require('react');
|
|
9
|
-
|
|
10
|
-
// option of loading KaTeX asynchronously
|
|
11
|
-
const KaTeXLoad = new Promise((resolve) => {
|
|
12
|
-
const script = document.querySelector("script[src*=\"katex.js\"], script[src*=\"katex.min.js\"]");
|
|
13
|
-
if (!script)
|
|
14
|
-
return;
|
|
15
|
-
if (window.hasOwnProperty("katex")) {
|
|
16
|
-
resolve(katex);
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
script.addEventListener("load", () => resolve(katex));
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
// load macros from <head>
|
|
23
|
-
const KaTeXMacros = new Promise((resolve) => {
|
|
24
|
-
const macros = {};
|
|
25
|
-
const scripts = Array.from(document.querySelectorAll("head > script[type='math/tex']"));
|
|
26
|
-
return Promise.all(scripts.map(script => fetch(script.src)
|
|
27
|
-
.then(res => {
|
|
28
|
-
if (res.ok)
|
|
29
|
-
return res.text();
|
|
30
|
-
throw new Error(`${res.status} ${res.statusText}: ${script.src}`);
|
|
31
|
-
})
|
|
32
|
-
.then(tex => {
|
|
33
|
-
Object.assign(macros, parseMacros(tex));
|
|
34
|
-
}))).then(() => resolve(macros));
|
|
35
|
-
});
|
|
36
|
-
/**
|
|
37
|
-
* Ready Promise
|
|
38
|
-
*/
|
|
39
|
-
const KaTeXReady = Promise.all([KaTeXLoad, KaTeXMacros]);
|
|
40
|
-
/**
|
|
41
|
-
* Parse \newcommand macros in a file.
|
|
42
|
-
* Also supports \ktxnewcommand (for use in conjunction with MathJax).
|
|
43
|
-
* @param file TeX file to parse
|
|
44
|
-
*/
|
|
45
|
-
function parseMacros(file) {
|
|
46
|
-
const macros = {};
|
|
47
|
-
const rgx = /\\(?:ktx)?newcommand\{(.+?)\}(?:\[\d+\])?\{/g;
|
|
48
|
-
let match;
|
|
49
|
-
while (match = rgx.exec(file)) {
|
|
50
|
-
let body = "";
|
|
51
|
-
const macro = match[1];
|
|
52
|
-
let braceCount = 1;
|
|
53
|
-
for (let i = match.index + match[0].length; (braceCount > 0) && (i < file.length); ++i) {
|
|
54
|
-
const char = file[i];
|
|
55
|
-
if (char === "{") {
|
|
56
|
-
braceCount++;
|
|
57
|
-
}
|
|
58
|
-
else if (char === "}") {
|
|
59
|
-
braceCount--;
|
|
60
|
-
if (braceCount === 0)
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
else if (char === "\\") {
|
|
64
|
-
body += file.slice(i, i + 2);
|
|
65
|
-
++i;
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
body += char;
|
|
69
|
-
}
|
|
70
|
-
macros[macro] = body;
|
|
71
|
-
}
|
|
72
|
-
return macros;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** Component for KaTeX code */
|
|
76
|
-
const KTX$1 = react.forwardRef(function KTX(props, ref) {
|
|
77
|
-
const spanRef = react.useRef();
|
|
78
|
-
const { children, display = false, ...attrs } = props;
|
|
79
|
-
const [ready, resolve] = react$1.usePromise();
|
|
80
|
-
// handle
|
|
81
|
-
react.useImperativeHandle(ref, () => ({
|
|
82
|
-
domElement: spanRef.current,
|
|
83
|
-
ready
|
|
84
|
-
}));
|
|
85
|
-
react.useEffect(() => {
|
|
86
|
-
KaTeXReady.then(([katex, macros]) => {
|
|
87
|
-
katex.render(children.toString(), spanRef.current, {
|
|
88
|
-
displayMode: !!display,
|
|
89
|
-
macros,
|
|
90
|
-
strict: "ignore",
|
|
91
|
-
throwOnError: false,
|
|
92
|
-
trust: true
|
|
93
|
-
});
|
|
94
|
-
/* move katex into placeholder element */
|
|
95
|
-
const child = spanRef.current.firstElementChild;
|
|
96
|
-
// copy classes
|
|
97
|
-
for (let i = 0, len = child.classList.length; i < len; ++i) {
|
|
98
|
-
spanRef.current.classList.add(child.classList.item(i));
|
|
99
|
-
}
|
|
100
|
-
// move children
|
|
101
|
-
while (child.childNodes.length > 0) {
|
|
102
|
-
spanRef.current.appendChild(child.firstChild);
|
|
103
|
-
}
|
|
104
|
-
// delete child
|
|
105
|
-
child.remove();
|
|
106
|
-
// resolve promise
|
|
107
|
-
resolve();
|
|
108
|
-
});
|
|
109
|
-
}, [children]);
|
|
110
|
-
// Google Chrome fails without this
|
|
111
|
-
if (display) {
|
|
112
|
-
if (!attrs.style)
|
|
113
|
-
attrs.style = {};
|
|
114
|
-
attrs.style.display = "block";
|
|
115
|
-
}
|
|
116
|
-
return (jsxRuntime_js.jsx("span", { ...attrs, ref: spanRef }));
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
/** Component for KaTeX code */
|
|
120
|
-
const KTX = react.forwardRef(function KTX(props, ref) {
|
|
121
|
-
const { obstruct = "canplay canplaythrough", reparse = false, ...attrs } = props;
|
|
122
|
-
const plain = react.useRef();
|
|
123
|
-
const combined = react$1.combineRefs(plain, ref);
|
|
124
|
-
const player = liqvid.usePlayer();
|
|
125
|
-
react.useEffect(() => {
|
|
126
|
-
// obstruction
|
|
127
|
-
if (obstruct.match(/\bcanplay\b/)) {
|
|
128
|
-
player.obstruct("canplay", plain.current.ready);
|
|
129
|
-
}
|
|
130
|
-
if (obstruct.match("canplaythrough")) {
|
|
131
|
-
player.obstruct("canplaythrough", plain.current.ready);
|
|
132
|
-
}
|
|
133
|
-
// reparsing
|
|
134
|
-
if (reparse) {
|
|
135
|
-
plain.current.ready.then(() => player.reparseTree(plain.current.domElement));
|
|
136
|
-
}
|
|
137
|
-
}, []);
|
|
138
|
-
return (jsxRuntime_js.jsx(KTX$1, { ref: combined, ...attrs }));
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Wait for several things to be rendered
|
|
143
|
-
*/
|
|
144
|
-
const RenderGroup = react.forwardRef(function RenderGroup(props, ref) {
|
|
145
|
-
const [ready, resolve] = react$1.usePromise();
|
|
146
|
-
// handle
|
|
147
|
-
react.useImperativeHandle(ref, () => ({ ready }));
|
|
148
|
-
const elements = react.useRef([]);
|
|
149
|
-
const promises = react.useRef([]);
|
|
150
|
-
// reparsing
|
|
151
|
-
const player = liqvid.usePlayer();
|
|
152
|
-
react.useEffect(() => {
|
|
153
|
-
// promises
|
|
154
|
-
Promise.all(promises.current).then(() => {
|
|
155
|
-
// reparse
|
|
156
|
-
if (props.reparse) {
|
|
157
|
-
player.reparseTree(leastCommonAncestor(elements.current));
|
|
158
|
-
}
|
|
159
|
-
// ready()
|
|
160
|
-
resolve();
|
|
161
|
-
});
|
|
162
|
-
}, []);
|
|
163
|
-
return react$1.recursiveMap(props.children, node => {
|
|
164
|
-
if (shouldInspect(node)) {
|
|
165
|
-
const originalRef = node.ref;
|
|
166
|
-
return react.cloneElement(node, {
|
|
167
|
-
ref: (ref) => {
|
|
168
|
-
if (!ref)
|
|
169
|
-
return;
|
|
170
|
-
elements.current.push(ref.domElement);
|
|
171
|
-
promises.current.push(ref.ready);
|
|
172
|
-
// pass along original ref
|
|
173
|
-
if (typeof originalRef === "function") {
|
|
174
|
-
originalRef(ref);
|
|
175
|
-
}
|
|
176
|
-
else if (originalRef && typeof originalRef === "object") {
|
|
177
|
-
originalRef.current = ref;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return node;
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
/**
|
|
186
|
-
* Determine whether the node is a <KTX> element
|
|
187
|
-
* @param node Element to check
|
|
188
|
-
*/
|
|
189
|
-
function shouldInspect(node) {
|
|
190
|
-
return react.isValidElement(node) && typeof node.type === "object" && (node.type === KTX || node.type === KTX$1);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Find least common ancestor of an array of elements
|
|
194
|
-
* @param elements Elements
|
|
195
|
-
* @returns Deepest node containing all passed elements
|
|
196
|
-
*/
|
|
197
|
-
function leastCommonAncestor(elements) {
|
|
198
|
-
if (elements.length === 0) {
|
|
199
|
-
throw new Error("Must pass at least one element");
|
|
200
|
-
}
|
|
201
|
-
let ancestor = elements[0];
|
|
202
|
-
let failing = elements.slice(1);
|
|
203
|
-
while (failing.length > 0) {
|
|
204
|
-
ancestor = ancestor.parentElement;
|
|
205
|
-
failing = failing.filter(node => !ancestor.contains(node));
|
|
206
|
-
}
|
|
207
|
-
return ancestor;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
exports.KTX = KTX;
|
|
211
|
-
exports.KaTeXReady = KaTeXReady;
|
|
212
|
-
exports.RenderGroup = RenderGroup;
|
package/dist/index.d.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
import * as react from 'react';
|
|
3
|
-
import * as katex from 'katex';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* KTX element API
|
|
7
|
-
*/
|
|
8
|
-
interface Handle$1 {
|
|
9
|
-
/** The underlying <span> element */
|
|
10
|
-
domElement: HTMLSpanElement;
|
|
11
|
-
/** Promise that resolves once typesetting is finished */
|
|
12
|
-
ready: Promise<void>;
|
|
13
|
-
}
|
|
14
|
-
interface Props$2 extends React.HTMLAttributes<HTMLSpanElement> {
|
|
15
|
-
/**
|
|
16
|
-
* Whether to render in display style
|
|
17
|
-
* @default false
|
|
18
|
-
*/
|
|
19
|
-
display?: boolean;
|
|
20
|
-
}
|
|
21
|
-
/** Component for KaTeX code */
|
|
22
|
-
declare const KTX$1: react.ForwardRefExoticComponent<Props$2 & react.RefAttributes<Handle$1>>;
|
|
23
|
-
|
|
24
|
-
interface Props$1 extends React.ComponentProps<typeof KTX$1> {
|
|
25
|
-
/**
|
|
26
|
-
* Player events to obstruct
|
|
27
|
-
* @default "canplay canplaythrough"
|
|
28
|
-
*/
|
|
29
|
-
obstruct?: string;
|
|
30
|
-
/**
|
|
31
|
-
* Whether to reparse descendants for `during()` and `from()`
|
|
32
|
-
* @default false
|
|
33
|
-
*/
|
|
34
|
-
reparse?: boolean;
|
|
35
|
-
}
|
|
36
|
-
/** Component for KaTeX code */
|
|
37
|
-
declare const KTX: react.ForwardRefExoticComponent<Pick<Props$1, "hidden" | "color" | "style" | "display" | "translate" | "slot" | "title" | "children" | "key" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | "suppressHydrationWarning" | "accessKey" | "className" | "contentEditable" | "contextMenu" | "dir" | "draggable" | "id" | "lang" | "placeholder" | "spellCheck" | "tabIndex" | "radioGroup" | "role" | "about" | "datatype" | "inlist" | "prefix" | "property" | "resource" | "typeof" | "vocab" | "autoCapitalize" | "autoCorrect" | "autoSave" | "itemProp" | "itemScope" | "itemType" | "itemID" | "itemRef" | "results" | "security" | "unselectable" | "inputMode" | "is" | "aria-activedescendant" | "aria-atomic" | "aria-autocomplete" | "aria-busy" | "aria-checked" | "aria-colcount" | "aria-colindex" | "aria-colspan" | "aria-controls" | "aria-current" | "aria-describedby" | "aria-details" | "aria-disabled" | "aria-dropeffect" | "aria-errormessage" | "aria-expanded" | "aria-flowto" | "aria-grabbed" | "aria-haspopup" | "aria-hidden" | "aria-invalid" | "aria-keyshortcuts" | "aria-label" | "aria-labelledby" | "aria-level" | "aria-live" | "aria-modal" | "aria-multiline" | "aria-multiselectable" | "aria-orientation" | "aria-owns" | "aria-placeholder" | "aria-posinset" | "aria-pressed" | "aria-readonly" | "aria-relevant" | "aria-required" | "aria-roledescription" | "aria-rowcount" | "aria-rowindex" | "aria-rowspan" | "aria-selected" | "aria-setsize" | "aria-sort" | "aria-valuemax" | "aria-valuemin" | "aria-valuenow" | "aria-valuetext" | "dangerouslySetInnerHTML" | "onCopy" | "onCopyCapture" | "onCut" | "onCutCapture" | "onPaste" | "onPasteCapture" | "onCompositionEnd" | "onCompositionEndCapture" | "onCompositionStart" | "onCompositionStartCapture" | "onCompositionUpdate" | "onCompositionUpdateCapture" | "onFocus" | "onFocusCapture" | "onBlur" | "onBlurCapture" | "onChange" | "onChangeCapture" | "onBeforeInput" | "onBeforeInputCapture" | "onInput" | "onInputCapture" | "onReset" | "onResetCapture" | "onSubmit" | "onSubmitCapture" | "onInvalid" | "onInvalidCapture" | "onLoad" | "onLoadCapture" | "onError" | "onErrorCapture" | "onKeyDown" | "onKeyDownCapture" | "onKeyPress" | "onKeyPressCapture" | "onKeyUp" | "onKeyUpCapture" | "onAbort" | "onAbortCapture" | "onCanPlay" | "onCanPlayCapture" | "onCanPlayThrough" | "onCanPlayThroughCapture" | "onDurationChange" | "onDurationChangeCapture" | "onEmptied" | "onEmptiedCapture" | "onEncrypted" | "onEncryptedCapture" | "onEnded" | "onEndedCapture" | "onLoadedData" | "onLoadedDataCapture" | "onLoadedMetadata" | "onLoadedMetadataCapture" | "onLoadStart" | "onLoadStartCapture" | "onPause" | "onPauseCapture" | "onPlay" | "onPlayCapture" | "onPlaying" | "onPlayingCapture" | "onProgress" | "onProgressCapture" | "onRateChange" | "onRateChangeCapture" | "onSeeked" | "onSeekedCapture" | "onSeeking" | "onSeekingCapture" | "onStalled" | "onStalledCapture" | "onSuspend" | "onSuspendCapture" | "onTimeUpdate" | "onTimeUpdateCapture" | "onVolumeChange" | "onVolumeChangeCapture" | "onWaiting" | "onWaitingCapture" | "onAuxClick" | "onAuxClickCapture" | "onClick" | "onClickCapture" | "onContextMenu" | "onContextMenuCapture" | "onDoubleClick" | "onDoubleClickCapture" | "onDrag" | "onDragCapture" | "onDragEnd" | "onDragEndCapture" | "onDragEnter" | "onDragEnterCapture" | "onDragExit" | "onDragExitCapture" | "onDragLeave" | "onDragLeaveCapture" | "onDragOver" | "onDragOverCapture" | "onDragStart" | "onDragStartCapture" | "onDrop" | "onDropCapture" | "onMouseDown" | "onMouseDownCapture" | "onMouseEnter" | "onMouseLeave" | "onMouseMove" | "onMouseMoveCapture" | "onMouseOut" | "onMouseOutCapture" | "onMouseOver" | "onMouseOverCapture" | "onMouseUp" | "onMouseUpCapture" | "onSelect" | "onSelectCapture" | "onTouchCancel" | "onTouchCancelCapture" | "onTouchEnd" | "onTouchEndCapture" | "onTouchMove" | "onTouchMoveCapture" | "onTouchStart" | "onTouchStartCapture" | "onPointerDown" | "onPointerDownCapture" | "onPointerMove" | "onPointerMoveCapture" | "onPointerUp" | "onPointerUpCapture" | "onPointerCancel" | "onPointerCancelCapture" | "onPointerEnter" | "onPointerEnterCapture" | "onPointerLeave" | "onPointerLeaveCapture" | "onPointerOver" | "onPointerOverCapture" | "onPointerOut" | "onPointerOutCapture" | "onGotPointerCapture" | "onGotPointerCaptureCapture" | "onLostPointerCapture" | "onLostPointerCaptureCapture" | "onScroll" | "onScrollCapture" | "onWheel" | "onWheelCapture" | "onAnimationStart" | "onAnimationStartCapture" | "onAnimationEnd" | "onAnimationEndCapture" | "onAnimationIteration" | "onAnimationIterationCapture" | "onTransitionEnd" | "onTransitionEndCapture" | "obstruct" | "reparse"> & react.RefAttributes<Handle$1>>;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Ready Promise
|
|
41
|
-
*/
|
|
42
|
-
declare const KaTeXReady: Promise<[typeof katex, {
|
|
43
|
-
[key: string]: string;
|
|
44
|
-
}]>;
|
|
45
|
-
|
|
46
|
-
/** RenderGroup element API */
|
|
47
|
-
interface Handle {
|
|
48
|
-
/** Promise that resolves once all KTX descendants have finished typesetting */
|
|
49
|
-
ready: Promise<void>;
|
|
50
|
-
}
|
|
51
|
-
interface Props {
|
|
52
|
-
/**
|
|
53
|
-
* Whether to reparse descendants for `during()` and `from()`
|
|
54
|
-
* @default false
|
|
55
|
-
*/
|
|
56
|
-
reparse?: boolean;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Wait for several things to be rendered
|
|
60
|
-
*/
|
|
61
|
-
declare const RenderGroup: react.ForwardRefExoticComponent<Props & react.RefAttributes<Handle>>;
|
|
62
|
-
|
|
63
|
-
declare global {
|
|
64
|
-
const katex: typeof katex;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export { Handle$1 as Handle, KTX, KaTeXReady, RenderGroup };
|
package/dist/index.mjs
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { jsx } from 'react/jsx-runtime.js';
|
|
2
|
-
import { usePromise, combineRefs, recursiveMap } from '@liqvid/utils/react';
|
|
3
|
-
import { usePlayer } from 'liqvid';
|
|
4
|
-
import { forwardRef, useRef, useImperativeHandle, useEffect, isValidElement, cloneElement } from 'react';
|
|
5
|
-
|
|
6
|
-
// option of loading KaTeX asynchronously
|
|
7
|
-
const KaTeXLoad = new Promise((resolve) => {
|
|
8
|
-
const script = document.querySelector("script[src*=\"katex.js\"], script[src*=\"katex.min.js\"]");
|
|
9
|
-
if (!script)
|
|
10
|
-
return;
|
|
11
|
-
if (window.hasOwnProperty("katex")) {
|
|
12
|
-
resolve(katex);
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
script.addEventListener("load", () => resolve(katex));
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
// load macros from <head>
|
|
19
|
-
const KaTeXMacros = new Promise((resolve) => {
|
|
20
|
-
const macros = {};
|
|
21
|
-
const scripts = Array.from(document.querySelectorAll("head > script[type='math/tex']"));
|
|
22
|
-
return Promise.all(scripts.map(script => fetch(script.src)
|
|
23
|
-
.then(res => {
|
|
24
|
-
if (res.ok)
|
|
25
|
-
return res.text();
|
|
26
|
-
throw new Error(`${res.status} ${res.statusText}: ${script.src}`);
|
|
27
|
-
})
|
|
28
|
-
.then(tex => {
|
|
29
|
-
Object.assign(macros, parseMacros(tex));
|
|
30
|
-
}))).then(() => resolve(macros));
|
|
31
|
-
});
|
|
32
|
-
/**
|
|
33
|
-
* Ready Promise
|
|
34
|
-
*/
|
|
35
|
-
const KaTeXReady = Promise.all([KaTeXLoad, KaTeXMacros]);
|
|
36
|
-
/**
|
|
37
|
-
* Parse \newcommand macros in a file.
|
|
38
|
-
* Also supports \ktxnewcommand (for use in conjunction with MathJax).
|
|
39
|
-
* @param file TeX file to parse
|
|
40
|
-
*/
|
|
41
|
-
function parseMacros(file) {
|
|
42
|
-
const macros = {};
|
|
43
|
-
const rgx = /\\(?:ktx)?newcommand\{(.+?)\}(?:\[\d+\])?\{/g;
|
|
44
|
-
let match;
|
|
45
|
-
while (match = rgx.exec(file)) {
|
|
46
|
-
let body = "";
|
|
47
|
-
const macro = match[1];
|
|
48
|
-
let braceCount = 1;
|
|
49
|
-
for (let i = match.index + match[0].length; (braceCount > 0) && (i < file.length); ++i) {
|
|
50
|
-
const char = file[i];
|
|
51
|
-
if (char === "{") {
|
|
52
|
-
braceCount++;
|
|
53
|
-
}
|
|
54
|
-
else if (char === "}") {
|
|
55
|
-
braceCount--;
|
|
56
|
-
if (braceCount === 0)
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
else if (char === "\\") {
|
|
60
|
-
body += file.slice(i, i + 2);
|
|
61
|
-
++i;
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
body += char;
|
|
65
|
-
}
|
|
66
|
-
macros[macro] = body;
|
|
67
|
-
}
|
|
68
|
-
return macros;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Component for KaTeX code */
|
|
72
|
-
const KTX$1 = forwardRef(function KTX(props, ref) {
|
|
73
|
-
const spanRef = useRef();
|
|
74
|
-
const { children, display = false, ...attrs } = props;
|
|
75
|
-
const [ready, resolve] = usePromise();
|
|
76
|
-
// handle
|
|
77
|
-
useImperativeHandle(ref, () => ({
|
|
78
|
-
domElement: spanRef.current,
|
|
79
|
-
ready
|
|
80
|
-
}));
|
|
81
|
-
useEffect(() => {
|
|
82
|
-
KaTeXReady.then(([katex, macros]) => {
|
|
83
|
-
katex.render(children.toString(), spanRef.current, {
|
|
84
|
-
displayMode: !!display,
|
|
85
|
-
macros,
|
|
86
|
-
strict: "ignore",
|
|
87
|
-
throwOnError: false,
|
|
88
|
-
trust: true
|
|
89
|
-
});
|
|
90
|
-
/* move katex into placeholder element */
|
|
91
|
-
const child = spanRef.current.firstElementChild;
|
|
92
|
-
// copy classes
|
|
93
|
-
for (let i = 0, len = child.classList.length; i < len; ++i) {
|
|
94
|
-
spanRef.current.classList.add(child.classList.item(i));
|
|
95
|
-
}
|
|
96
|
-
// move children
|
|
97
|
-
while (child.childNodes.length > 0) {
|
|
98
|
-
spanRef.current.appendChild(child.firstChild);
|
|
99
|
-
}
|
|
100
|
-
// delete child
|
|
101
|
-
child.remove();
|
|
102
|
-
// resolve promise
|
|
103
|
-
resolve();
|
|
104
|
-
});
|
|
105
|
-
}, [children]);
|
|
106
|
-
// Google Chrome fails without this
|
|
107
|
-
if (display) {
|
|
108
|
-
if (!attrs.style)
|
|
109
|
-
attrs.style = {};
|
|
110
|
-
attrs.style.display = "block";
|
|
111
|
-
}
|
|
112
|
-
return (jsx("span", { ...attrs, ref: spanRef }));
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
/** Component for KaTeX code */
|
|
116
|
-
const KTX = forwardRef(function KTX(props, ref) {
|
|
117
|
-
const { obstruct = "canplay canplaythrough", reparse = false, ...attrs } = props;
|
|
118
|
-
const plain = useRef();
|
|
119
|
-
const combined = combineRefs(plain, ref);
|
|
120
|
-
const player = usePlayer();
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
// obstruction
|
|
123
|
-
if (obstruct.match(/\bcanplay\b/)) {
|
|
124
|
-
player.obstruct("canplay", plain.current.ready);
|
|
125
|
-
}
|
|
126
|
-
if (obstruct.match("canplaythrough")) {
|
|
127
|
-
player.obstruct("canplaythrough", plain.current.ready);
|
|
128
|
-
}
|
|
129
|
-
// reparsing
|
|
130
|
-
if (reparse) {
|
|
131
|
-
plain.current.ready.then(() => player.reparseTree(plain.current.domElement));
|
|
132
|
-
}
|
|
133
|
-
}, []);
|
|
134
|
-
return (jsx(KTX$1, { ref: combined, ...attrs }));
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Wait for several things to be rendered
|
|
139
|
-
*/
|
|
140
|
-
const RenderGroup = forwardRef(function RenderGroup(props, ref) {
|
|
141
|
-
const [ready, resolve] = usePromise();
|
|
142
|
-
// handle
|
|
143
|
-
useImperativeHandle(ref, () => ({ ready }));
|
|
144
|
-
const elements = useRef([]);
|
|
145
|
-
const promises = useRef([]);
|
|
146
|
-
// reparsing
|
|
147
|
-
const player = usePlayer();
|
|
148
|
-
useEffect(() => {
|
|
149
|
-
// promises
|
|
150
|
-
Promise.all(promises.current).then(() => {
|
|
151
|
-
// reparse
|
|
152
|
-
if (props.reparse) {
|
|
153
|
-
player.reparseTree(leastCommonAncestor(elements.current));
|
|
154
|
-
}
|
|
155
|
-
// ready()
|
|
156
|
-
resolve();
|
|
157
|
-
});
|
|
158
|
-
}, []);
|
|
159
|
-
return recursiveMap(props.children, node => {
|
|
160
|
-
if (shouldInspect(node)) {
|
|
161
|
-
const originalRef = node.ref;
|
|
162
|
-
return cloneElement(node, {
|
|
163
|
-
ref: (ref) => {
|
|
164
|
-
if (!ref)
|
|
165
|
-
return;
|
|
166
|
-
elements.current.push(ref.domElement);
|
|
167
|
-
promises.current.push(ref.ready);
|
|
168
|
-
// pass along original ref
|
|
169
|
-
if (typeof originalRef === "function") {
|
|
170
|
-
originalRef(ref);
|
|
171
|
-
}
|
|
172
|
-
else if (originalRef && typeof originalRef === "object") {
|
|
173
|
-
originalRef.current = ref;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
return node;
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
/**
|
|
182
|
-
* Determine whether the node is a <KTX> element
|
|
183
|
-
* @param node Element to check
|
|
184
|
-
*/
|
|
185
|
-
function shouldInspect(node) {
|
|
186
|
-
return isValidElement(node) && typeof node.type === "object" && (node.type === KTX || node.type === KTX$1);
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Find least common ancestor of an array of elements
|
|
190
|
-
* @param elements Elements
|
|
191
|
-
* @returns Deepest node containing all passed elements
|
|
192
|
-
*/
|
|
193
|
-
function leastCommonAncestor(elements) {
|
|
194
|
-
if (elements.length === 0) {
|
|
195
|
-
throw new Error("Must pass at least one element");
|
|
196
|
-
}
|
|
197
|
-
let ancestor = elements[0];
|
|
198
|
-
let failing = elements.slice(1);
|
|
199
|
-
while (failing.length > 0) {
|
|
200
|
-
ancestor = ancestor.parentElement;
|
|
201
|
-
failing = failing.filter(node => !ancestor.contains(node));
|
|
202
|
-
}
|
|
203
|
-
return ancestor;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export { KTX, KaTeXReady, RenderGroup };
|