@notatki/format 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Aliaksandr Radzivanovich
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,3 @@
1
+ import { type MathRenderer } from "./math-renderer.js";
2
+ export declare function formatField(value: string, renderer?: MathRenderer): string;
3
+ //# sourceMappingURL=format-field.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format-field.d.ts","sourceRoot":"","sources":["../src/format-field.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,YAAY,GAAG,MAAM,CAI1E"}
@@ -0,0 +1,9 @@
1
+ import { Marked } from "marked";
2
+ import { mathExtension } from "./marked-math.js";
3
+ import {} from "./math-renderer.js";
4
+ export function formatField(value, renderer) {
5
+ const parser = new Marked();
6
+ parser.use(mathExtension(renderer));
7
+ return parser.parse(value.trim(), { async: false }).trim();
8
+ }
9
+ //# sourceMappingURL=format-field.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format-field.js","sourceRoot":"","sources":["../src/format-field.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAqB,MAAM,oBAAoB,CAAC;AAEvD,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,QAAuB;IAChE,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;IAC5B,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC;IACpC,OAAO,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;AAC7D,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./format-field.js";
2
+ export * from "./marked-math.js";
3
+ export * from "./math-renderer.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./format-field.js";
2
+ export * from "./marked-math.js";
3
+ export * from "./math-renderer.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { type KatexOptions } from "katex";
2
+ export declare function katexBlock(value: string, options: KatexOptions): string;
3
+ export declare function katexInline(value: string, options: KatexOptions): string;
4
+ //# sourceMappingURL=katex.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"katex.d.ts","sourceRoot":"","sources":["../src/katex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAkB,MAAM,OAAO,CAAC;AAQ1D,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CAavE;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CAaxE"}
package/dist/katex.js ADDED
@@ -0,0 +1,39 @@
1
+ import { renderToString } from "katex";
2
+ const defaultOptions = {
3
+ output: "html",
4
+ strict: true,
5
+ throwOnError: false,
6
+ };
7
+ export function katexBlock(value, options) {
8
+ options = { ...defaultOptions, ...options, displayMode: true };
9
+ try {
10
+ return renderToString(value, options) + "\n";
11
+ }
12
+ catch (err) {
13
+ // Sometimes KaTeX throws errors even if throwOnError is false.
14
+ // In such a case we should handle the errors ourselves.
15
+ if (options.throwOnError) {
16
+ throw err;
17
+ }
18
+ else {
19
+ return `<span style="color:red">${err.message}</span>`;
20
+ }
21
+ }
22
+ }
23
+ export function katexInline(value, options) {
24
+ options = { ...defaultOptions, ...options, displayMode: false };
25
+ try {
26
+ return renderToString(value, options);
27
+ }
28
+ catch (err) {
29
+ // Sometimes KaTeX throws errors even if throwOnError is false.
30
+ // In such a case we should handle the errors ourselves.
31
+ if (options.throwOnError) {
32
+ throw err;
33
+ }
34
+ else {
35
+ return `<span style="color:red">${err.message}</span>`;
36
+ }
37
+ }
38
+ }
39
+ //# sourceMappingURL=katex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"katex.js","sourceRoot":"","sources":["../src/katex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,cAAc,EAAE,MAAM,OAAO,CAAC;AAE1D,MAAM,cAAc,GAAG;IACrB,MAAM,EAAE,MAAM;IACd,MAAM,EAAE,IAAI;IACZ,YAAY,EAAE,KAAK;CACY,CAAC;AAElC,MAAM,UAAU,UAAU,CAAC,KAAa,EAAE,OAAqB;IAC7D,OAAO,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;IAC/D,IAAI,CAAC;QACH,OAAO,cAAc,CAAC,KAAK,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAC/C,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,+DAA+D;QAC/D,wDAAwD;QACxD,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;YACzB,MAAM,GAAG,CAAC;QACZ,CAAC;aAAM,CAAC;YACN,OAAO,2BAA4B,GAAa,CAAC,OAAO,SAAS,CAAC;QACpE,CAAC;IACH,CAAC;AACH,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,OAAqB;IAC9D,OAAO,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC;IAChE,IAAI,CAAC;QACH,OAAO,cAAc,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACxC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,+DAA+D;QAC/D,wDAAwD;QACxD,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;YACzB,MAAM,GAAG,CAAC;QACZ,CAAC;aAAM,CAAC;YACN,OAAO,2BAA4B,GAAa,CAAC,OAAO,SAAS,CAAC;QACpE,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { type MarkedExtension } from "marked";
2
+ import { type MathRenderer } from "./math-renderer.js";
3
+ export declare function mathExtension(renderer?: MathRenderer): MarkedExtension;
4
+ //# sourceMappingURL=marked-math.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"marked-math.d.ts","sourceRoot":"","sources":["../src/marked-math.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAmD,MAAM,QAAQ,CAAC;AAC/F,OAAO,EAAE,KAAK,YAAY,EAAa,MAAM,oBAAoB,CAAC;AAqOlE,wBAAgB,aAAa,CAAC,QAAQ,GAAE,YAA0B,GAAG,eAAe,CA2HnF"}
@@ -0,0 +1,305 @@
1
+ import {} from "marked";
2
+ import { renderTex } from "./math-renderer.js";
3
+ /**
4
+ * Tests if the given text begins with "\[".
5
+ */
6
+ function isBlockStart(src) {
7
+ return (src.length >= 4 && //
8
+ src.charCodeAt(0) === /* "\" */ 0x005c &&
9
+ src.charCodeAt(1) === /* "[" */ 0x005b);
10
+ }
11
+ /**
12
+ * Finds the position of the opening "\[" in the given text.
13
+ */
14
+ function findBlockStart(src, start) {
15
+ for (let i = start; i < src.length - 1; i += 1) {
16
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
17
+ if (src.charCodeAt(i + 1) === /* "[" */ 0x005b) {
18
+ return i;
19
+ }
20
+ i += 1; // Skip the escaped character.
21
+ }
22
+ }
23
+ return -1;
24
+ }
25
+ /**
26
+ * Finds the position of the closing "\]" in the given text.
27
+ */
28
+ function findBlockEnd(src, start) {
29
+ let braceLevel = 0;
30
+ for (let i = start; i < src.length - 1; i += 1) {
31
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
32
+ if (braceLevel <= 0 && //
33
+ src.charCodeAt(i + 1) === /* "]" */ 0x005d) {
34
+ return i;
35
+ }
36
+ i += 1; // Skip the escaped character.
37
+ continue;
38
+ }
39
+ if (src.charCodeAt(i) === /* "{" */ 0x007b) {
40
+ braceLevel += 1;
41
+ continue;
42
+ }
43
+ if (src.charCodeAt(i) === /* "}" */ 0x007d) {
44
+ braceLevel -= 1;
45
+ continue;
46
+ }
47
+ }
48
+ return -1;
49
+ }
50
+ /**
51
+ * Tests if the given text begins with "\(".
52
+ */
53
+ function isInlineStart(src) {
54
+ return (src.length >= 4 && //
55
+ src.charCodeAt(0) === /* "\" */ 0x005c &&
56
+ src.charCodeAt(1) === /* "(" */ 0x0028);
57
+ }
58
+ /**
59
+ * Finds the position of the opening "\(" in the given text.
60
+ */
61
+ function findInlineStart(src, start) {
62
+ for (let i = start; i < src.length - 1; i += 1) {
63
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
64
+ if (src.charCodeAt(i + 1) === /* "(" */ 0x0028) {
65
+ return i;
66
+ }
67
+ i += 1; // Skip the escaped character.
68
+ }
69
+ }
70
+ return -1;
71
+ }
72
+ /**
73
+ * Finds the position of the closing "\)" in the given text.
74
+ */
75
+ function findInlineEnd(src, start) {
76
+ let braceLevel = 0;
77
+ for (let i = start; i < src.length - 1; i += 1) {
78
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
79
+ if (braceLevel <= 0 && //
80
+ src.charCodeAt(i + 1) === /* ")" */ 0x0029) {
81
+ return i;
82
+ }
83
+ i += 1; // Skip the escaped character.
84
+ continue;
85
+ }
86
+ if (src.charCodeAt(i) === /* "{" */ 0x007b) {
87
+ braceLevel += 1;
88
+ continue;
89
+ }
90
+ if (src.charCodeAt(i) === /* "}" */ 0x007d) {
91
+ braceLevel -= 1;
92
+ continue;
93
+ }
94
+ }
95
+ return -1;
96
+ }
97
+ /**
98
+ * Tests if the given text begins with "$$".
99
+ */
100
+ function isBlockStartAlt(src) {
101
+ return (src.length >= 5 && // $$x$$
102
+ src.charCodeAt(0) === /* "$" */ 0x0024 &&
103
+ src.charCodeAt(1) === /* "$" */ 0x0024 &&
104
+ src.charCodeAt(2) !== /* "$" */ 0x0024);
105
+ }
106
+ /**
107
+ * Finds the position of the opening "$$" in the given text.
108
+ */
109
+ function findBlockStartAlt(src, start) {
110
+ for (let i = start; i < src.length - 4; i += 1) {
111
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
112
+ i += 1; // Skip the escaped character.
113
+ continue;
114
+ }
115
+ if (src.charCodeAt(i) === /* "$" */ 0x0024 &&
116
+ src.charCodeAt(i + 1) === /* "$" */ 0x0024 &&
117
+ src.charCodeAt(i + 2) !== /* "$" */ 0x0024) {
118
+ return i;
119
+ }
120
+ }
121
+ return -1;
122
+ }
123
+ /**
124
+ * Finds the position of the closing "$$" in the given text.
125
+ */
126
+ function findBlockEndAlt(src, start) {
127
+ let braceLevel = 0;
128
+ for (let i = start; i < src.length - 1; i += 1) {
129
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
130
+ i += 1; // Skip the escaped character.
131
+ continue;
132
+ }
133
+ if (src.charCodeAt(i) === /* "{" */ 0x007b) {
134
+ braceLevel += 1;
135
+ continue;
136
+ }
137
+ if (src.charCodeAt(i) === /* "}" */ 0x007d) {
138
+ braceLevel -= 1;
139
+ continue;
140
+ }
141
+ if (braceLevel <= 0 && //
142
+ src.charCodeAt(i) === /* "$" */ 0x0024 &&
143
+ src.charCodeAt(i + 1) === /* "$" */ 0x0024) {
144
+ return i;
145
+ }
146
+ }
147
+ return -1;
148
+ }
149
+ /**
150
+ * Tests if the given text begins with "$".
151
+ */
152
+ function isInlineStartAlt(src) {
153
+ return (src.length >= 3 && // $x$
154
+ src.charCodeAt(0) === /* "$" */ 0x0024 &&
155
+ src.charCodeAt(1) !== /* "$" */ 0x0024);
156
+ }
157
+ /**
158
+ * Finds the position of the opening "$" in the given text.
159
+ */
160
+ function findInlineStartAlt(src, start) {
161
+ for (let i = start; i < src.length - 2; i += 1) {
162
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
163
+ i += 1; // Skip the escaped character.
164
+ continue;
165
+ }
166
+ if (src.charCodeAt(i) === /* "$" */ 0x0024 && //
167
+ src.charCodeAt(i + 1) !== /* "$" */ 0x0024) {
168
+ return i;
169
+ }
170
+ }
171
+ return -1;
172
+ }
173
+ /**
174
+ * Finds the position of the closing "$" in the given text.
175
+ */
176
+ function findInlineEndAlt(src, start) {
177
+ let braceLevel = 0;
178
+ for (let i = 1; i < src.length; i += 1) {
179
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
180
+ i += 1; // Skip the escaped character.
181
+ continue;
182
+ }
183
+ if (src.charCodeAt(i) === /* "{" */ 0x007b) {
184
+ braceLevel += 1;
185
+ continue;
186
+ }
187
+ if (src.charCodeAt(i) === /* "}" */ 0x007d) {
188
+ braceLevel -= 1;
189
+ continue;
190
+ }
191
+ if (braceLevel <= 0 && //
192
+ src.charCodeAt(i) === /* "$" */ 0x0024) {
193
+ return i;
194
+ }
195
+ }
196
+ return -1;
197
+ }
198
+ export function mathExtension(renderer = renderTex()) {
199
+ // Common syntax.
200
+ // Block LaTeX: \[ ... \]
201
+ const blockLatex = {
202
+ name: "blockLatex",
203
+ level: "block",
204
+ start(src) {
205
+ return findBlockStart(src, 0);
206
+ },
207
+ tokenizer(src) {
208
+ if (isBlockStart(src)) {
209
+ const end = findBlockEnd(src, 2);
210
+ if (end >= 2) {
211
+ return {
212
+ type: "blockLatex",
213
+ raw: src.substring(0, end + 2),
214
+ code: src.substring(2, end),
215
+ };
216
+ }
217
+ }
218
+ return undefined;
219
+ },
220
+ renderer(token) {
221
+ return renderer.block(token.code);
222
+ },
223
+ };
224
+ // Inline LaTeX: \( ... \)
225
+ const inlineLatex = {
226
+ name: "inlineLatex",
227
+ level: "inline",
228
+ start(src) {
229
+ return findInlineStart(src, 0);
230
+ },
231
+ tokenizer(src) {
232
+ if (isInlineStart(src)) {
233
+ const end = findInlineEnd(src, 2);
234
+ if (end >= 2) {
235
+ return {
236
+ type: "inlineLatex",
237
+ raw: src.substring(0, end + 2),
238
+ code: src.substring(2, end),
239
+ };
240
+ }
241
+ }
242
+ return undefined;
243
+ },
244
+ renderer(token) {
245
+ return renderer.inline(token.code);
246
+ },
247
+ };
248
+ // Block LaTeX: $$ ... $$
249
+ const blockLatexAlt = {
250
+ name: "blockLatexAlt",
251
+ level: "block",
252
+ start(src) {
253
+ return findBlockStartAlt(src, 0);
254
+ },
255
+ tokenizer(src) {
256
+ if (isBlockStartAlt(src)) {
257
+ const end = findBlockEndAlt(src, 2);
258
+ if (end >= 2) {
259
+ return {
260
+ type: "blockLatexAlt",
261
+ raw: src.substring(0, end + 2),
262
+ code: src.substring(2, end),
263
+ };
264
+ }
265
+ }
266
+ return undefined;
267
+ },
268
+ renderer(token) {
269
+ return renderer.block(token.code);
270
+ },
271
+ };
272
+ // Inline LaTeX: $ ... $
273
+ const inlineLatexAlt = {
274
+ name: "inlineLatexAlt",
275
+ level: "inline",
276
+ start(src) {
277
+ return findInlineStartAlt(src, 0);
278
+ },
279
+ tokenizer(src) {
280
+ if (isInlineStartAlt(src)) {
281
+ const end = findInlineEndAlt(src, 1);
282
+ if (end >= 1) {
283
+ return {
284
+ type: "inlineLatexAlt",
285
+ raw: src.substring(0, end + 1),
286
+ code: src.substring(1, end),
287
+ };
288
+ }
289
+ }
290
+ return undefined;
291
+ },
292
+ renderer(token) {
293
+ return renderer.inline(token.code);
294
+ },
295
+ };
296
+ return {
297
+ extensions: [
298
+ blockLatex, //
299
+ inlineLatex,
300
+ blockLatexAlt,
301
+ inlineLatexAlt,
302
+ ],
303
+ };
304
+ }
305
+ //# sourceMappingURL=marked-math.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"marked-math.js","sourceRoot":"","sources":["../src/marked-math.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyE,MAAM,QAAQ,CAAC;AAC/F,OAAO,EAAqB,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAElE;;GAEG;AACH,SAAS,YAAY,CAAC,GAAW;IAC/B,OAAO,CACL,GAAG,CAAC,MAAM,IAAI,CAAC,IAAI,EAAE;QACrB,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,MAAM;QACtC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,MAAM,CACvC,CAAC;AACJ,CAAC;AAED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ACrB,CAAC;YACH,CAAC;YACD,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,QAAQ,CAAC,KAAK;YACZ,OAAO,QAAQ,CAAC,KAAK,CAAE,KAAuB,CAAC,IAAI,CAAC,CAAC;QACvD,CAAC;KACF,CAAC;IAEF,wBAAwB;IACxB,MAAM,cAAc,GAAkC;QACpD,IAAI,EAAE,gBAAgB;QACtB,KAAK,EAAE,QAAQ;QACf,KAAK,CAAC,GAAG;YACP,OAAO,kBAAkB,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QACpC,CAAC;QACD,SAAS,CAAC,GAAG;YACX,IAAI,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC1B,MAAM,GAAG,GAAG,gBAAgB,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;gBACrC,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC;oBACb,OAAO;wBACL,IAAI,EAAE,gBAAgB;wBACtB,GAAG,EAAE,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC;wBAC9B,IAAI,EAAE,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC;qBACX,CAAC;gBACrB,CAAC;YACH,CAAC;YACD,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,QAAQ,CAAC,KAAK;YACZ,OAAO,QAAQ,CAAC,MAAM,CAAE,KAAuB,CAAC,IAAI,CAAC,CAAC;QACxD,CAAC;KACF,CAAC;IAEF,OAAO;QACL,UAAU,EAAE;YACV,UAAU,EAAE,EAAE;YACd,WAAW;YACX,aAAa;YACb,cAAc;SACf;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { type KatexOptions } from "katex";
2
+ export type MathRenderer = {
3
+ block: (code: string) => string;
4
+ inline: (code: string) => string;
5
+ };
6
+ export declare const renderTex: () => MathRenderer;
7
+ export declare const renderHtml: (options?: KatexOptions) => MathRenderer;
8
+ //# sourceMappingURL=math-renderer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"math-renderer.d.ts","sourceRoot":"","sources":["../src/math-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,OAAO,CAAC;AAG1C,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;IAChC,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;CAClC,CAAC;AAEF,eAAO,MAAM,SAAS,QAAO,YAS5B,CAAC;AAEF,eAAO,MAAM,UAAU,GAAI,UAAS,YAAiB,KAAG,YASvD,CAAC"}
@@ -0,0 +1,23 @@
1
+ import {} from "katex";
2
+ import { katexBlock, katexInline } from "./katex.js";
3
+ export const renderTex = () => {
4
+ return {
5
+ block: (code) => {
6
+ return `\\[ ${code.trim()} \\]\n`;
7
+ },
8
+ inline: (code) => {
9
+ return `\\( ${code.trim()} \\)`;
10
+ },
11
+ };
12
+ };
13
+ export const renderHtml = (options = {}) => {
14
+ return {
15
+ block: (code) => {
16
+ return katexBlock(code, options);
17
+ },
18
+ inline: (code) => {
19
+ return katexInline(code, options);
20
+ },
21
+ };
22
+ };
23
+ //# sourceMappingURL=math-renderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"math-renderer.js","sourceRoot":"","sources":["../src/math-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,MAAM,OAAO,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAOrD,MAAM,CAAC,MAAM,SAAS,GAAG,GAAiB,EAAE;IAC1C,OAAO;QACL,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE;YACd,OAAO,OAAO,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC;QACpC,CAAC;QACD,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YACf,OAAO,OAAO,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC;QAClC,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,UAAwB,EAAE,EAAgB,EAAE;IACrE,OAAO;QACL,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE;YACd,OAAO,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YACf,OAAO,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACpC,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@notatki/format",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "dependencies": {
7
+ "katex": "^0.16.22",
8
+ "marked": "^16.2.1"
9
+ },
10
+ "scripts": {
11
+ "compile": "rm -fr dist && tsc",
12
+ "pretest": "npm run compile",
13
+ "test": "node --test 'dist/**/*.test.js'"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "gitHead": "545474a85a5cc649ff9ce50f36e87ccf5c57073a"
19
+ }
@@ -0,0 +1,9 @@
1
+ import { Marked } from "marked";
2
+ import { mathExtension } from "./marked-math.js";
3
+ import { type MathRenderer } from "./math-renderer.js";
4
+
5
+ export function formatField(value: string, renderer?: MathRenderer): string {
6
+ const parser = new Marked();
7
+ parser.use(mathExtension(renderer));
8
+ return parser.parse(value.trim(), { async: false }).trim();
9
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./format-field.js";
2
+ export * from "./marked-math.js";
3
+ export * from "./math-renderer.js";
package/src/katex.ts ADDED
@@ -0,0 +1,37 @@
1
+ import { type KatexOptions, renderToString } from "katex";
2
+
3
+ const defaultOptions = {
4
+ output: "html",
5
+ strict: true,
6
+ throwOnError: false,
7
+ } as const satisfies KatexOptions;
8
+
9
+ export function katexBlock(value: string, options: KatexOptions): string {
10
+ options = { ...defaultOptions, ...options, displayMode: true };
11
+ try {
12
+ return renderToString(value, options) + "\n";
13
+ } catch (err) {
14
+ // Sometimes KaTeX throws errors even if throwOnError is false.
15
+ // In such a case we should handle the errors ourselves.
16
+ if (options.throwOnError) {
17
+ throw err;
18
+ } else {
19
+ return `<span style="color:red">${(err as Error).message}</span>`;
20
+ }
21
+ }
22
+ }
23
+
24
+ export function katexInline(value: string, options: KatexOptions): string {
25
+ options = { ...defaultOptions, ...options, displayMode: false };
26
+ try {
27
+ return renderToString(value, options);
28
+ } catch (err) {
29
+ // Sometimes KaTeX throws errors even if throwOnError is false.
30
+ // In such a case we should handle the errors ourselves.
31
+ if (options.throwOnError) {
32
+ throw err;
33
+ } else {
34
+ return `<span style="color:red">${(err as Error).message}</span>`;
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,354 @@
1
+ import { type MarkedExtension, type TokenizerAndRendererExtension, type Tokens } from "marked";
2
+ import { type MathRenderer, renderTex } from "./math-renderer.js";
3
+
4
+ /**
5
+ * Tests if the given text begins with "\[".
6
+ */
7
+ function isBlockStart(src: string): boolean {
8
+ return (
9
+ src.length >= 4 && //
10
+ src.charCodeAt(0) === /* "\" */ 0x005c &&
11
+ src.charCodeAt(1) === /* "[" */ 0x005b
12
+ );
13
+ }
14
+
15
+ /**
16
+ * Finds the position of the opening "\[" in the given text.
17
+ */
18
+ function findBlockStart(src: string, start: number): number {
19
+ for (let i = start; i < src.length - 1; i += 1) {
20
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
21
+ if (src.charCodeAt(i + 1) === /* "[" */ 0x005b) {
22
+ return i;
23
+ }
24
+ i += 1; // Skip the escaped character.
25
+ }
26
+ }
27
+ return -1;
28
+ }
29
+
30
+ /**
31
+ * Finds the position of the closing "\]" in the given text.
32
+ */
33
+ function findBlockEnd(src: string, start: number): number {
34
+ let braceLevel = 0;
35
+ for (let i = start; i < src.length - 1; i += 1) {
36
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
37
+ if (
38
+ braceLevel <= 0 && //
39
+ src.charCodeAt(i + 1) === /* "]" */ 0x005d
40
+ ) {
41
+ return i;
42
+ }
43
+ i += 1; // Skip the escaped character.
44
+ continue;
45
+ }
46
+ if (src.charCodeAt(i) === /* "{" */ 0x007b) {
47
+ braceLevel += 1;
48
+ continue;
49
+ }
50
+ if (src.charCodeAt(i) === /* "}" */ 0x007d) {
51
+ braceLevel -= 1;
52
+ continue;
53
+ }
54
+ }
55
+ return -1;
56
+ }
57
+
58
+ /**
59
+ * Tests if the given text begins with "\(".
60
+ */
61
+ function isInlineStart(src: string): boolean {
62
+ return (
63
+ src.length >= 4 && //
64
+ src.charCodeAt(0) === /* "\" */ 0x005c &&
65
+ src.charCodeAt(1) === /* "(" */ 0x0028
66
+ );
67
+ }
68
+
69
+ /**
70
+ * Finds the position of the opening "\(" in the given text.
71
+ */
72
+ function findInlineStart(src: string, start: number): number {
73
+ for (let i = start; i < src.length - 1; i += 1) {
74
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
75
+ if (src.charCodeAt(i + 1) === /* "(" */ 0x0028) {
76
+ return i;
77
+ }
78
+ i += 1; // Skip the escaped character.
79
+ }
80
+ }
81
+ return -1;
82
+ }
83
+
84
+ /**
85
+ * Finds the position of the closing "\)" in the given text.
86
+ */
87
+ function findInlineEnd(src: string, start: number): number {
88
+ let braceLevel = 0;
89
+ for (let i = start; i < src.length - 1; i += 1) {
90
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
91
+ if (
92
+ braceLevel <= 0 && //
93
+ src.charCodeAt(i + 1) === /* ")" */ 0x0029
94
+ ) {
95
+ return i;
96
+ }
97
+ i += 1; // Skip the escaped character.
98
+ continue;
99
+ }
100
+ if (src.charCodeAt(i) === /* "{" */ 0x007b) {
101
+ braceLevel += 1;
102
+ continue;
103
+ }
104
+ if (src.charCodeAt(i) === /* "}" */ 0x007d) {
105
+ braceLevel -= 1;
106
+ continue;
107
+ }
108
+ }
109
+ return -1;
110
+ }
111
+
112
+ /**
113
+ * Tests if the given text begins with "$$".
114
+ */
115
+ function isBlockStartAlt(src: string): boolean {
116
+ return (
117
+ src.length >= 5 && // $$x$$
118
+ src.charCodeAt(0) === /* "$" */ 0x0024 &&
119
+ src.charCodeAt(1) === /* "$" */ 0x0024 &&
120
+ src.charCodeAt(2) !== /* "$" */ 0x0024
121
+ );
122
+ }
123
+
124
+ /**
125
+ * Finds the position of the opening "$$" in the given text.
126
+ */
127
+ function findBlockStartAlt(src: string, start: number): number {
128
+ for (let i = start; i < src.length - 4; i += 1) {
129
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
130
+ i += 1; // Skip the escaped character.
131
+ continue;
132
+ }
133
+ if (
134
+ src.charCodeAt(i) === /* "$" */ 0x0024 &&
135
+ src.charCodeAt(i + 1) === /* "$" */ 0x0024 &&
136
+ src.charCodeAt(i + 2) !== /* "$" */ 0x0024
137
+ ) {
138
+ return i;
139
+ }
140
+ }
141
+ return -1;
142
+ }
143
+
144
+ /**
145
+ * Finds the position of the closing "$$" in the given text.
146
+ */
147
+ function findBlockEndAlt(src: string, start: number): number {
148
+ let braceLevel = 0;
149
+ for (let i = start; i < src.length - 1; i += 1) {
150
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
151
+ i += 1; // Skip the escaped character.
152
+ continue;
153
+ }
154
+ if (src.charCodeAt(i) === /* "{" */ 0x007b) {
155
+ braceLevel += 1;
156
+ continue;
157
+ }
158
+ if (src.charCodeAt(i) === /* "}" */ 0x007d) {
159
+ braceLevel -= 1;
160
+ continue;
161
+ }
162
+ if (
163
+ braceLevel <= 0 && //
164
+ src.charCodeAt(i) === /* "$" */ 0x0024 &&
165
+ src.charCodeAt(i + 1) === /* "$" */ 0x0024
166
+ ) {
167
+ return i;
168
+ }
169
+ }
170
+ return -1;
171
+ }
172
+
173
+ /**
174
+ * Tests if the given text begins with "$".
175
+ */
176
+ function isInlineStartAlt(src: string): boolean {
177
+ return (
178
+ src.length >= 3 && // $x$
179
+ src.charCodeAt(0) === /* "$" */ 0x0024 &&
180
+ src.charCodeAt(1) !== /* "$" */ 0x0024
181
+ );
182
+ }
183
+
184
+ /**
185
+ * Finds the position of the opening "$" in the given text.
186
+ */
187
+ function findInlineStartAlt(src: string, start: number): number {
188
+ for (let i = start; i < src.length - 2; i += 1) {
189
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
190
+ i += 1; // Skip the escaped character.
191
+ continue;
192
+ }
193
+ if (
194
+ src.charCodeAt(i) === /* "$" */ 0x0024 && //
195
+ src.charCodeAt(i + 1) !== /* "$" */ 0x0024
196
+ ) {
197
+ return i;
198
+ }
199
+ }
200
+ return -1;
201
+ }
202
+
203
+ /**
204
+ * Finds the position of the closing "$" in the given text.
205
+ */
206
+ function findInlineEndAlt(src: string, start: number): number {
207
+ let braceLevel = 0;
208
+ for (let i = 1; i < src.length; i += 1) {
209
+ if (src.charCodeAt(i) === /* "\" */ 0x005c) {
210
+ i += 1; // Skip the escaped character.
211
+ continue;
212
+ }
213
+ if (src.charCodeAt(i) === /* "{" */ 0x007b) {
214
+ braceLevel += 1;
215
+ continue;
216
+ }
217
+ if (src.charCodeAt(i) === /* "}" */ 0x007d) {
218
+ braceLevel -= 1;
219
+ continue;
220
+ }
221
+ if (
222
+ braceLevel <= 0 && //
223
+ src.charCodeAt(i) === /* "$" */ 0x0024
224
+ ) {
225
+ return i;
226
+ }
227
+ }
228
+ return -1;
229
+ }
230
+
231
+ export function mathExtension(renderer: MathRenderer = renderTex()): MarkedExtension {
232
+ // Common syntax.
233
+
234
+ type LatexToken = Tokens.Generic & {
235
+ type: "blockLatex" | "inlineLatex";
236
+ code: string;
237
+ };
238
+
239
+ // Block LaTeX: \[ ... \]
240
+ const blockLatex: TokenizerAndRendererExtension = {
241
+ name: "blockLatex",
242
+ level: "block",
243
+ start(src) {
244
+ return findBlockStart(src, 0);
245
+ },
246
+ tokenizer(src) {
247
+ if (isBlockStart(src)) {
248
+ const end = findBlockEnd(src, 2);
249
+ if (end >= 2) {
250
+ return {
251
+ type: "blockLatex",
252
+ raw: src.substring(0, end + 2),
253
+ code: src.substring(2, end),
254
+ } as LatexToken;
255
+ }
256
+ }
257
+ return undefined;
258
+ },
259
+ renderer(token) {
260
+ return renderer.block((token as LatexToken).code);
261
+ },
262
+ };
263
+
264
+ // Inline LaTeX: \( ... \)
265
+ const inlineLatex: TokenizerAndRendererExtension = {
266
+ name: "inlineLatex",
267
+ level: "inline",
268
+ start(src) {
269
+ return findInlineStart(src, 0);
270
+ },
271
+ tokenizer(src) {
272
+ if (isInlineStart(src)) {
273
+ const end = findInlineEnd(src, 2);
274
+ if (end >= 2) {
275
+ return {
276
+ type: "inlineLatex",
277
+ raw: src.substring(0, end + 2),
278
+ code: src.substring(2, end),
279
+ } as LatexToken;
280
+ }
281
+ }
282
+ return undefined;
283
+ },
284
+ renderer(token) {
285
+ return renderer.inline((token as LatexToken).code);
286
+ },
287
+ };
288
+
289
+ // Alternate syntax.
290
+
291
+ type LatexTokenAlt = Tokens.Generic & {
292
+ type: "blockLatexAlt" | "inlineLatexAlt";
293
+ code: string;
294
+ };
295
+
296
+ // Block LaTeX: $$ ... $$
297
+ const blockLatexAlt: TokenizerAndRendererExtension = {
298
+ name: "blockLatexAlt",
299
+ level: "block",
300
+ start(src) {
301
+ return findBlockStartAlt(src, 0);
302
+ },
303
+ tokenizer(src) {
304
+ if (isBlockStartAlt(src)) {
305
+ const end = findBlockEndAlt(src, 2);
306
+ if (end >= 2) {
307
+ return {
308
+ type: "blockLatexAlt",
309
+ raw: src.substring(0, end + 2),
310
+ code: src.substring(2, end),
311
+ } as LatexTokenAlt;
312
+ }
313
+ }
314
+ return undefined;
315
+ },
316
+ renderer(token) {
317
+ return renderer.block((token as LatexTokenAlt).code);
318
+ },
319
+ };
320
+
321
+ // Inline LaTeX: $ ... $
322
+ const inlineLatexAlt: TokenizerAndRendererExtension = {
323
+ name: "inlineLatexAlt",
324
+ level: "inline",
325
+ start(src) {
326
+ return findInlineStartAlt(src, 0);
327
+ },
328
+ tokenizer(src) {
329
+ if (isInlineStartAlt(src)) {
330
+ const end = findInlineEndAlt(src, 1);
331
+ if (end >= 1) {
332
+ return {
333
+ type: "inlineLatexAlt",
334
+ raw: src.substring(0, end + 1),
335
+ code: src.substring(1, end),
336
+ } as LatexTokenAlt;
337
+ }
338
+ }
339
+ return undefined;
340
+ },
341
+ renderer(token) {
342
+ return renderer.inline((token as LatexTokenAlt).code);
343
+ },
344
+ };
345
+
346
+ return {
347
+ extensions: [
348
+ blockLatex, //
349
+ inlineLatex,
350
+ blockLatexAlt,
351
+ inlineLatexAlt,
352
+ ],
353
+ };
354
+ }
@@ -0,0 +1,29 @@
1
+ import { type KatexOptions } from "katex";
2
+ import { katexBlock, katexInline } from "./katex.js";
3
+
4
+ export type MathRenderer = {
5
+ block: (code: string) => string;
6
+ inline: (code: string) => string;
7
+ };
8
+
9
+ export const renderTex = (): MathRenderer => {
10
+ return {
11
+ block: (code) => {
12
+ return `\\[ ${code.trim()} \\]\n`;
13
+ },
14
+ inline: (code) => {
15
+ return `\\( ${code.trim()} \\)`;
16
+ },
17
+ };
18
+ };
19
+
20
+ export const renderHtml = (options: KatexOptions = {}): MathRenderer => {
21
+ return {
22
+ block: (code) => {
23
+ return katexBlock(code, options);
24
+ },
25
+ inline: (code) => {
26
+ return katexInline(code, options);
27
+ },
28
+ };
29
+ };