@liqvid/katex 0.0.1
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/LICENSE +9 -0
- package/README.md +31 -0
- package/dist/Blocking.d.ts +2 -0
- package/dist/Blocking.js +32 -0
- package/dist/NonBlocking.d.ts +9 -0
- package/dist/NonBlocking.js +60 -0
- package/dist/RenderGroup.d.ts +10 -0
- package/dist/RenderGroup.js +76 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +4 -0
- package/dist/loading.d.ts +3 -0
- package/dist/loading.js +54 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Yuri Sulyma
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @liqvid/katex
|
|
2
|
+
|
|
3
|
+
[KaTeX](https://katex.org/) integration for [Liqvid](https://liqvidjs.org).
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import {KTX} from "@liqvid/katex";
|
|
9
|
+
|
|
10
|
+
function Quadratic() {
|
|
11
|
+
return (
|
|
12
|
+
<div>
|
|
13
|
+
The value of <KTX>x</KTX> is given by the quadratic formula
|
|
14
|
+
<KTX display>{String.raw`x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`}</KTX>
|
|
15
|
+
</div>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Macros
|
|
21
|
+
|
|
22
|
+
For convenience, this module supports loading macro definitions from a file. Simply include a `<script type="math/tex">` tag in the `<head>` of your html, pointing to a tex file containing `\newcommand`s.
|
|
23
|
+
|
|
24
|
+
```html
|
|
25
|
+
<!-- this has to go in <head> -->
|
|
26
|
+
<script src="./macros.tex" type="math/tex"></script>
|
|
27
|
+
```
|
|
28
|
+
```tex
|
|
29
|
+
% macros.tex
|
|
30
|
+
\newcommand{\C}{\mathbb C}
|
|
31
|
+
```
|
package/dist/Blocking.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { usePlayer } from "liqvid";
|
|
3
|
+
import { forwardRef, useEffect, useMemo, useRef } from "react";
|
|
4
|
+
import { KTXNonBlocking } from "./NonBlocking";
|
|
5
|
+
export const KTXBlocking = forwardRef(function KTX(props, ref) {
|
|
6
|
+
const player = usePlayer();
|
|
7
|
+
const innerRef = useRef();
|
|
8
|
+
if (typeof ref === "function") {
|
|
9
|
+
ref(innerRef.current);
|
|
10
|
+
}
|
|
11
|
+
else if (ref) {
|
|
12
|
+
ref.current = innerRef.current;
|
|
13
|
+
}
|
|
14
|
+
const resolve = useRef(null);
|
|
15
|
+
useMemo(() => {
|
|
16
|
+
const promise = new Promise((res) => {
|
|
17
|
+
resolve.current = res;
|
|
18
|
+
});
|
|
19
|
+
player.obstruct("canplay", promise);
|
|
20
|
+
player.obstruct("canplaythrough", promise);
|
|
21
|
+
}, []);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (typeof ref === "function") {
|
|
24
|
+
ref(innerRef.current);
|
|
25
|
+
}
|
|
26
|
+
else if (ref) {
|
|
27
|
+
ref.current = innerRef.current;
|
|
28
|
+
}
|
|
29
|
+
innerRef.current.ready.then(() => resolve.current());
|
|
30
|
+
}, []);
|
|
31
|
+
return (_jsx(KTXNonBlocking, Object.assign({ ref: innerRef }, props)));
|
|
32
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export interface Handle {
|
|
3
|
+
domElement: HTMLSpanElement;
|
|
4
|
+
ready: Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
export declare const KTXNonBlocking: import("react").ForwardRefExoticComponent<{
|
|
7
|
+
display?: boolean;
|
|
8
|
+
reparse?: boolean;
|
|
9
|
+
} & import("react").HTMLAttributes<HTMLSpanElement> & import("react").RefAttributes<Handle>>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
import { usePlayer } from "liqvid";
|
|
14
|
+
import { forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from "react";
|
|
15
|
+
import { KaTeXReady } from "./loading";
|
|
16
|
+
const implementation = function KTX(props, ref) {
|
|
17
|
+
const spanRef = useRef();
|
|
18
|
+
const { children, display, reparse } = props, attrs = __rest(props, ["children", "display", "reparse"]);
|
|
19
|
+
const resolveRef = useRef();
|
|
20
|
+
const player = usePlayer();
|
|
21
|
+
const ready = useMemo(() => {
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
resolveRef.current = resolve;
|
|
24
|
+
});
|
|
25
|
+
}, []);
|
|
26
|
+
useImperativeHandle(ref, () => ({
|
|
27
|
+
domElement: spanRef.current,
|
|
28
|
+
ready
|
|
29
|
+
}));
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
KaTeXReady.then(([katex, macros]) => {
|
|
32
|
+
katex.render(children.toString(), spanRef.current, {
|
|
33
|
+
displayMode: !!display,
|
|
34
|
+
macros,
|
|
35
|
+
strict: "ignore",
|
|
36
|
+
throwOnError: false,
|
|
37
|
+
trust: true
|
|
38
|
+
});
|
|
39
|
+
const child = spanRef.current.firstElementChild;
|
|
40
|
+
for (let i = 0, len = child.classList.length; i < len; ++i) {
|
|
41
|
+
spanRef.current.classList.add(child.classList.item(i));
|
|
42
|
+
}
|
|
43
|
+
while (child.childNodes.length > 0) {
|
|
44
|
+
spanRef.current.appendChild(child.firstChild);
|
|
45
|
+
}
|
|
46
|
+
child.remove();
|
|
47
|
+
if (reparse) {
|
|
48
|
+
player.reparseTree(spanRef.current);
|
|
49
|
+
}
|
|
50
|
+
resolveRef.current();
|
|
51
|
+
});
|
|
52
|
+
}, [children]);
|
|
53
|
+
if (display) {
|
|
54
|
+
if (!attrs.style)
|
|
55
|
+
attrs.style = {};
|
|
56
|
+
attrs.style.display = "block";
|
|
57
|
+
}
|
|
58
|
+
return (_jsx("span", Object.assign({}, attrs, { ref: spanRef })));
|
|
59
|
+
};
|
|
60
|
+
export const KTXNonBlocking = forwardRef(implementation);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
interface Handle {
|
|
3
|
+
ready: Promise<void>;
|
|
4
|
+
}
|
|
5
|
+
interface Props {
|
|
6
|
+
reparse?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare const RenderGroup: import("react").ForwardRefExoticComponent<Props & import("react").RefAttributes<Handle>>;
|
|
9
|
+
export declare function recursiveMap(children: React.ReactNode, fn: (child: React.ReactNode) => React.ReactNode): React.ReactNode;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Children, cloneElement, forwardRef, isValidElement, useEffect, useMemo, useRef, useImperativeHandle } from "react";
|
|
2
|
+
import { usePlayer } from "liqvid";
|
|
3
|
+
import { KTXNonBlocking } from "./NonBlocking";
|
|
4
|
+
import { KTXBlocking } from "./Blocking";
|
|
5
|
+
export const RenderGroup = forwardRef(function RenderGroup(props, ref) {
|
|
6
|
+
const [ready, resolve] = usePromise();
|
|
7
|
+
useImperativeHandle(ref, () => ({ ready }));
|
|
8
|
+
const elements = useRef([]);
|
|
9
|
+
const promises = useRef([]);
|
|
10
|
+
const player = usePlayer();
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
Promise.all(promises.current).then(() => {
|
|
13
|
+
if (props.reparse) {
|
|
14
|
+
player.reparseTree(leastCommonAncestor(elements.current));
|
|
15
|
+
}
|
|
16
|
+
resolve();
|
|
17
|
+
});
|
|
18
|
+
}, []);
|
|
19
|
+
return recursiveMap(props.children, node => {
|
|
20
|
+
if (shouldInspect(node)) {
|
|
21
|
+
const originalRef = node.ref;
|
|
22
|
+
return cloneElement(node, {
|
|
23
|
+
ref: (ref) => {
|
|
24
|
+
if (!ref)
|
|
25
|
+
return;
|
|
26
|
+
elements.current.push(ref.domElement);
|
|
27
|
+
promises.current.push(ref.ready);
|
|
28
|
+
if (typeof originalRef === "function") {
|
|
29
|
+
originalRef(ref);
|
|
30
|
+
}
|
|
31
|
+
else if (originalRef && typeof originalRef === "object") {
|
|
32
|
+
originalRef.current = ref;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return node;
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
function shouldInspect(node) {
|
|
41
|
+
return isValidElement(node) && typeof node.type === "object" && (node.type === KTXBlocking || node.type === KTXNonBlocking);
|
|
42
|
+
}
|
|
43
|
+
export function recursiveMap(children, fn) {
|
|
44
|
+
return Children.map(children, (child) => {
|
|
45
|
+
if (!isValidElement(child)) {
|
|
46
|
+
return child;
|
|
47
|
+
}
|
|
48
|
+
if ("children" in child.props) {
|
|
49
|
+
child = cloneElement(child, {
|
|
50
|
+
children: recursiveMap(child.props.children, fn)
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return fn(child);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function usePromise(deps = []) {
|
|
57
|
+
const resolve = useRef();
|
|
58
|
+
const reject = useRef();
|
|
59
|
+
const promise = useMemo(() => new Promise((res, rej) => {
|
|
60
|
+
resolve.current = res;
|
|
61
|
+
reject.current = rej;
|
|
62
|
+
}), deps);
|
|
63
|
+
return [promise, resolve.current, reject.current];
|
|
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
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/loading.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const KaTeXLoad = new Promise((resolve) => {
|
|
2
|
+
const script = document.querySelector(`script[src*="katex.js"], script[src*="katex.min.js"]`);
|
|
3
|
+
if (!script)
|
|
4
|
+
return;
|
|
5
|
+
if (window.hasOwnProperty("katex")) {
|
|
6
|
+
resolve(katex);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
script.addEventListener("load", () => resolve(katex));
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
const KaTeXMacros = new Promise((resolve) => {
|
|
13
|
+
const macros = {};
|
|
14
|
+
const scripts = Array.from(document.querySelectorAll("head > script[type='math/tex']"));
|
|
15
|
+
return Promise.all(scripts.map(script => fetch(script.src)
|
|
16
|
+
.then(res => {
|
|
17
|
+
if (res.ok)
|
|
18
|
+
return res.text();
|
|
19
|
+
throw new Error(`${res.status} ${res.statusText}: ${script.src}`);
|
|
20
|
+
})
|
|
21
|
+
.then(tex => {
|
|
22
|
+
Object.assign(macros, parseMacros(tex));
|
|
23
|
+
}))).then(() => resolve(macros));
|
|
24
|
+
});
|
|
25
|
+
export const KaTeXReady = Promise.all([KaTeXLoad, KaTeXMacros]);
|
|
26
|
+
function parseMacros(file) {
|
|
27
|
+
const macros = {};
|
|
28
|
+
const rgx = /\\(?:ktx)?newcommand\{(.+?)\}(?:\[\d+\])?\{/g;
|
|
29
|
+
let match;
|
|
30
|
+
while (match = rgx.exec(file)) {
|
|
31
|
+
let body = "";
|
|
32
|
+
const macro = match[1];
|
|
33
|
+
let braceCount = 1;
|
|
34
|
+
for (let i = match.index + match[0].length; (braceCount > 0) && (i < file.length); ++i) {
|
|
35
|
+
const char = file[i];
|
|
36
|
+
if (char === "{") {
|
|
37
|
+
braceCount++;
|
|
38
|
+
}
|
|
39
|
+
else if (char === "}") {
|
|
40
|
+
braceCount--;
|
|
41
|
+
if (braceCount === 0)
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
else if (char === "\\") {
|
|
45
|
+
body += file.slice(i, i + 2);
|
|
46
|
+
++i;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
body += char;
|
|
50
|
+
}
|
|
51
|
+
macros[macro] = body;
|
|
52
|
+
}
|
|
53
|
+
return macros;
|
|
54
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@liqvid/katex",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "KaTeX integration for Liqvid",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist/*"
|
|
7
|
+
],
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"typesVersions": {
|
|
12
|
+
"*": {
|
|
13
|
+
"*": [
|
|
14
|
+
"./dist/*"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"author": "Yuri Sulyma <yuri@liqvidjs.org>",
|
|
19
|
+
"keywords": [
|
|
20
|
+
"liqvid",
|
|
21
|
+
"katex"
|
|
22
|
+
],
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/liqvidjs/liqvid.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/liqvidjs/liqvid/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/liqvidjs/liqvid/tree/main/packages/katex",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@types/react": "^17.0.39",
|
|
34
|
+
"liqvid": "2.1.0-beta.3",
|
|
35
|
+
"react": "^17.0.2"
|
|
36
|
+
},
|
|
37
|
+
"peerDependenciesMeta": {
|
|
38
|
+
"liqvid": {
|
|
39
|
+
"optional": true
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/react": "^17.0.11",
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "^5.13.0",
|
|
45
|
+
"@typescript-eslint/parser": "^5.13.0",
|
|
46
|
+
"@yuri/eslint-config": "^1.0.1",
|
|
47
|
+
"eslint": "^8.10.0",
|
|
48
|
+
"eslint-plugin-react": "^7.29.3",
|
|
49
|
+
"liqvid": "2.1.0-beta.3",
|
|
50
|
+
"react": "^17.0.2",
|
|
51
|
+
"typescript": "^4.6.2"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@types/events": "^3.0.0",
|
|
55
|
+
"@types/katex": "^0.11.1"
|
|
56
|
+
}
|
|
57
|
+
}
|