@bdocs/plugin-math 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Boltdocs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,7 @@
1
+ @import url("https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css");
2
+
3
+ .math-block {
4
+ overflow-x: auto;
5
+ overflow-y: hidden;
6
+ padding: 0.5rem 0;
7
+ }
@@ -0,0 +1,19 @@
1
+ import * as _$react_jsx_runtime0 from "react/jsx-runtime";
2
+
3
+ //#region src/client/Math.d.ts
4
+ interface MathProps {
5
+ children: string;
6
+ }
7
+ declare function Math({
8
+ children
9
+ }: MathProps): _$react_jsx_runtime0.JSX.Element;
10
+ //#endregion
11
+ //#region src/client/BlockMath.d.ts
12
+ interface BlockMathProps {
13
+ children: string;
14
+ }
15
+ declare function BlockMath({
16
+ children
17
+ }: BlockMathProps): _$react_jsx_runtime0.JSX.Element;
18
+ //#endregion
19
+ export { BlockMath, Math };
@@ -0,0 +1,29 @@
1
+ import { useMemo } from "react";
2
+ import katex from "katex";
3
+ import { jsx } from "react/jsx-runtime";
4
+ //#region src/client/Math.tsx
5
+ function Math({ children }) {
6
+ return /* @__PURE__ */ jsx("span", { dangerouslySetInnerHTML: { __html: useMemo(() => {
7
+ try {
8
+ return katex.renderToString(children, { displayMode: false });
9
+ } catch {
10
+ return children;
11
+ }
12
+ }, [children]) } });
13
+ }
14
+ //#endregion
15
+ //#region src/client/BlockMath.tsx
16
+ function BlockMath({ children }) {
17
+ return /* @__PURE__ */ jsx("div", {
18
+ className: "math-block my-6 flex justify-center overflow-x-auto",
19
+ dangerouslySetInnerHTML: { __html: useMemo(() => {
20
+ try {
21
+ return katex.renderToString(children, { displayMode: true });
22
+ } catch {
23
+ return children;
24
+ }
25
+ }, [children]) }
26
+ });
27
+ }
28
+ //#endregion
29
+ export { BlockMath, Math };
@@ -0,0 +1,6 @@
1
+ import { BoltdocsPlugin } from "boltdocs";
2
+
3
+ //#region src/node/index.d.ts
4
+ declare function mathPlugin(): BoltdocsPlugin;
5
+ //#endregion
6
+ export { mathPlugin as default };
@@ -0,0 +1,47 @@
1
+ //#region src/node/source-transform.ts
2
+ function escapeJsString(content) {
3
+ return content.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n");
4
+ }
5
+ function preprocessMath(source) {
6
+ const placeholders = [];
7
+ let result = source.replace(/(`{3,})[\s\S]*?\1/g, (match) => {
8
+ placeholders.push(match);
9
+ return `\0MATH_PH_${placeholders.length - 1}\0`;
10
+ });
11
+ result = result.replace(/(`[^`\n]+`)/g, (match) => {
12
+ placeholders.push(match);
13
+ return `\0MATH_PH_${placeholders.length - 1}\0`;
14
+ });
15
+ result = result.replace(/^---[\s\S]*?---\n*/m, (match) => {
16
+ placeholders.push(match);
17
+ return `\0MATH_PH_${placeholders.length - 1}\0`;
18
+ });
19
+ result = result.replace(/\$\$([\s\S]*?)\$\$/g, (_, content) => {
20
+ return `<BlockMath>{"${escapeJsString(content.trim())}"}</BlockMath>`;
21
+ });
22
+ result = result.replace(/(?<!\$)\$(?!\$)(.+?)\$(?!\$)/g, (_, content) => {
23
+ return `<Math>{"${escapeJsString(content)}"}</Math>`;
24
+ });
25
+ while (result.includes("\0MATH_PH_")) result = result.replace(/\0MATH_PH_(\d+)\0/g, (_, idx) => {
26
+ return placeholders[parseInt(idx)];
27
+ });
28
+ return result;
29
+ }
30
+ function transformSource(_ctx, params) {
31
+ return { code: preprocessMath(params.code) };
32
+ }
33
+ //#endregion
34
+ //#region src/node/index.ts
35
+ function mathPlugin() {
36
+ return {
37
+ name: "boltdocs-plugin-math",
38
+ version: "0.1.0",
39
+ hooks: { transformSource },
40
+ components: {
41
+ Math: "@bdocs/plugin-math/client",
42
+ BlockMath: "@bdocs/plugin-math/client"
43
+ }
44
+ };
45
+ }
46
+ //#endregion
47
+ export { mathPlugin as default };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@bdocs/plugin-math",
3
+ "version": "1.0.0",
4
+ "description": "KaTeX math plugin for Boltdocs",
5
+ "main": "dist/node/index.js",
6
+ "module": "dist/node/index.mjs",
7
+ "types": "dist/node/index.d.ts",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/node/index.d.mts",
17
+ "import": "./dist/node/index.mjs"
18
+ },
19
+ "./client": {
20
+ "types": "./dist/client/index.d.mts",
21
+ "import": "./dist/client/index.mjs"
22
+ },
23
+ "./style.css": "./dist/client/index.css"
24
+ },
25
+ "author": "Jesus Alcala",
26
+ "license": "MIT",
27
+ "type": "module",
28
+ "dependencies": {
29
+ "katex": "^0.16.11"
30
+ },
31
+ "peerDependencies": {
32
+ "@bdocs/dui": "^0.2.0",
33
+ "react": "^19.0.0",
34
+ "react-dom": "^19.0.0",
35
+ "boltdocs": "2.9.0"
36
+ },
37
+ "devDependencies": {
38
+ "@types/katex": "^0.16.7",
39
+ "@types/node": "^22.0.0",
40
+ "@types/react": "^19.0.0",
41
+ "@types/react-dom": "^19.0.0",
42
+ "react": "^19.0.0",
43
+ "react-dom": "^19.0.0",
44
+ "tsdown": "^0.21.7",
45
+ "typescript": "^5.9.3",
46
+ "boltdocs": "2.9.0"
47
+ },
48
+ "scripts": {
49
+ "build": "tsdown --config-loader unrun && node -e \"require('fs').cpSync('src/client/index.css', 'dist/client/index.css')\"",
50
+ "dev": "tsdown --watch --config-loader unrun",
51
+ "test": "vitest run"
52
+ }
53
+ }