@zzish/math-rich-input 0.1.58 → 0.1.59
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 +22 -0
- package/dist/index.js +20 -35
- package/dist/standalone.js +1 -1
- package/package.json +7 -2
- package/scripts/build-katex-stylesheet.js +40 -0
- package/src/katexHelper.js +20 -38
- package/src/katexStylesheet.generated.js +3 -0
- package/src/standalone.js +48 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zzish/math-rich-input",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.59",
|
|
4
4
|
"description": "React component for user to enter rich text with embedded math equations.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,9 +13,13 @@
|
|
|
13
13
|
},
|
|
14
14
|
"main": "dist/index.js",
|
|
15
15
|
"scripts": {
|
|
16
|
-
"
|
|
16
|
+
"pretest": "node scripts/build-katex-stylesheet.js",
|
|
17
|
+
"test": "playwright test tests/caret-regression.spec.js tests/math-only-caret.spec.js tests/math-only-caret-react19.spec.js tests/studio-equation.spec.js tests/release-regression.spec.js tests/first-render.spec.js --workers=1",
|
|
18
|
+
"prebuild": "node scripts/build-katex-stylesheet.js",
|
|
17
19
|
"build": "rollup --config && webpack --config webpack.standalone.config.js",
|
|
20
|
+
"prebuild:legacy": "node scripts/build-katex-stylesheet.js",
|
|
18
21
|
"build:legacy": "rollup --config",
|
|
22
|
+
"prebuild:standalone": "node scripts/build-katex-stylesheet.js",
|
|
19
23
|
"build:standalone": "webpack --config webpack.standalone.config.js",
|
|
20
24
|
"prepublishOnly": "npm test && npm run build"
|
|
21
25
|
},
|
|
@@ -43,6 +47,7 @@
|
|
|
43
47
|
"@rollup/plugin-node-resolve": "^11.2.0",
|
|
44
48
|
"babel-loader": "^8.2.2",
|
|
45
49
|
"css-loader": "^6.7.1",
|
|
50
|
+
"katex-0.11.1": "npm:katex@0.11.1",
|
|
46
51
|
"postcss": "^8.4.38",
|
|
47
52
|
"postcss-loader": "^6.2.1",
|
|
48
53
|
"postcss-nested": "^6.0.1",
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build the KaTeX stylesheet the field carries inside itself, so it never fetches one from a CDN.
|
|
3
|
+
*
|
|
4
|
+
* Up to 0.1.58 the field appended `katex@0.11.1`'s stylesheet from jsdelivr on its first mount. Every
|
|
5
|
+
* host's formulas in and around the field have looked the way that stylesheet draws them ever since —
|
|
6
|
+
* Classic's own KaTeX is 0.3.0, two examples have none — so the SAME stylesheet is kept, byte for byte,
|
|
7
|
+
* and only its delivery changes: read from the pinned `katex-0.11.1` dev dependency, with each font face's
|
|
8
|
+
* woff2 inlined as a data URI. A data URI needs no bundler, public path or server in any consumer
|
|
9
|
+
* (webpack 3 and 5, Vite, SvelteKit, plain script tags). The woff and ttf fallbacks are dropped: every
|
|
10
|
+
* browser the hosts support reads woff2 (KaTeX docs, "Kinds of fonts used").
|
|
11
|
+
*
|
|
12
|
+
* Output: `src/katexStylesheet.generated.js` (ignored by git; `prebuild` and `pretest` recreate it).
|
|
13
|
+
*/
|
|
14
|
+
const fs = require("node:fs");
|
|
15
|
+
const path = require("node:path");
|
|
16
|
+
|
|
17
|
+
const root = path.resolve(__dirname, "..");
|
|
18
|
+
const dist = path.join(root, "node_modules", "katex-0.11.1", "dist");
|
|
19
|
+
const version = require(path.join(root, "node_modules", "katex-0.11.1", "package.json")).version;
|
|
20
|
+
if (version !== "0.11.1") throw new Error(`expected katex 0.11.1 for the bundled stylesheet, found ${version}`);
|
|
21
|
+
|
|
22
|
+
let missing = 0;
|
|
23
|
+
const css = fs
|
|
24
|
+
.readFileSync(path.join(dist, "katex.min.css"), "utf8")
|
|
25
|
+
.replace(/src:url\(fonts\/([^)]+)\.woff2\) format\("woff2"\)(,url\([^)]*\) format\("[^"]*"\))*/g, (_, name) => {
|
|
26
|
+
const file = path.join(dist, "fonts", `${name}.woff2`);
|
|
27
|
+
if (!fs.existsSync(file)) missing += 1;
|
|
28
|
+
const data = fs.readFileSync(file).toString("base64");
|
|
29
|
+
return `src:url(data:font/woff2;base64,${data}) format("woff2")`;
|
|
30
|
+
});
|
|
31
|
+
if (missing || /url\(fonts\//.test(css)) throw new Error("a KaTeX font was left pointing at a relative URL");
|
|
32
|
+
|
|
33
|
+
const out = path.join(root, "src", "katexStylesheet.generated.js");
|
|
34
|
+
fs.writeFileSync(
|
|
35
|
+
out,
|
|
36
|
+
`// Generated by scripts/build-katex-stylesheet.js from katex@${version}; do not edit.\n` +
|
|
37
|
+
`export const KATEX_VERSION = ${JSON.stringify(version)};\n` +
|
|
38
|
+
`export const KATEX_CSS = ${JSON.stringify(css)};\n`
|
|
39
|
+
);
|
|
40
|
+
console.log(`katex ${version} stylesheet: ${css.length} characters, written to ${path.relative(root, out)}`);
|
package/src/katexHelper.js
CHANGED
|
@@ -1,39 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
export const setupKatex = () => {
|
|
3
|
-
let links = document.getElementsByTagName("link")
|
|
4
|
-
for (let i = 0; i < links.length; i++) {
|
|
5
|
-
if (links[i].id === "katex-css")
|
|
6
|
-
return
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
let link = document.createElement("link")
|
|
10
|
-
link.id = "katex-css"
|
|
11
|
-
link.rel = "stylesheet"
|
|
12
|
-
link.href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css"
|
|
13
|
-
// link.integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq"
|
|
14
|
-
link.crossorigin="anonymous"
|
|
15
|
-
document.head.appendChild(link);
|
|
16
|
-
console.log("Added katex css to document head")
|
|
17
|
-
|
|
18
|
-
// The following two scripts are required to have pages auto-render embedded latex.
|
|
19
|
-
// We don't need to do this as we render everything programatically.
|
|
20
|
-
// These are inlcuded here only for reference in case of future need.
|
|
1
|
+
import { KATEX_CSS } from "./katexStylesheet.generated.js";
|
|
21
2
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Put KaTeX's stylesheet on the page, once, from inside the package.
|
|
5
|
+
*
|
|
6
|
+
* The field draws formulas with KaTeX and every host has looked the way `katex@0.11.1`'s stylesheet draws
|
|
7
|
+
* them, because up to 0.1.58 this appended that stylesheet from jsdelivr. It is the same stylesheet now,
|
|
8
|
+
* appended at the same moments under the same id, but carried in the bundle with its fonts inlined
|
|
9
|
+
* (`scripts/build-katex-stylesheet.js`): no request leaves the page, nothing waits on a third party, and
|
|
10
|
+
* there is no late arrival to resize a field that was already drawn.
|
|
11
|
+
*
|
|
12
|
+
* A host that already provides its own KaTeX stylesheet under the id `katex-css` keeps it: this adds
|
|
13
|
+
* nothing then, as before.
|
|
14
|
+
*/
|
|
15
|
+
export const setupKatex = () => {
|
|
16
|
+
if (typeof document === "undefined" || document.getElementById("katex-css")) return;
|
|
17
|
+
const style = document.createElement("style");
|
|
18
|
+
style.id = "katex-css";
|
|
19
|
+
style.textContent = KATEX_CSS;
|
|
20
|
+
document.head.appendChild(style);
|
|
21
|
+
};
|