@ziloen/micromark-extension-math 3.1.0-patch.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.
@@ -0,0 +1,339 @@
1
+ /**
2
+ * @import {Options} from '@ziloen/micromark-extension-math'
3
+ * @import {Construct, Previous, Resolver, State, Token, TokenizeContext, Tokenizer} from 'micromark-util-types'
4
+ */
5
+
6
+ // To do: next major: clean spaces in HTML compiler.
7
+ // This has to be coordinated together with `mdast-util-math`.
8
+
9
+ import { asciiDigit, markdownLineEnding, markdownSpace } from 'micromark-util-character';
10
+ /**
11
+ * @param {Options | null | undefined} [options={}]
12
+ * Configuration (default: `{}`).
13
+ * @returns {Construct}
14
+ * Construct.
15
+ */
16
+ export function mathText(options) {
17
+ const options_ = options || {};
18
+ let single = options_.singleDollarTextMath;
19
+ if (single === null || single === undefined) {
20
+ single = true;
21
+ }
22
+ return {
23
+ tokenize: tokenizeMathText,
24
+ resolve: resolveMathText,
25
+ previous: previousCode,
26
+ name: 'mathText'
27
+ };
28
+
29
+ /**
30
+ * @this {TokenizeContext}
31
+ * @type {Tokenizer}
32
+ */
33
+ function tokenizeMathText(effects, ok, nok) {
34
+ const self = this;
35
+ /** @type {number} */
36
+ let codeOpen = 36;
37
+ /** @type {number} */
38
+ let codeClose = 36;
39
+ /** @type {'mathText' | 'mathTextDisplay'} */
40
+ let type = 'mathText';
41
+ let sizeOpen = 0;
42
+ /** @type {number} */
43
+ let size;
44
+ /** @type {number | null | undefined} */
45
+ let previous;
46
+ /** @type {Token} */
47
+ let tokenType;
48
+ /** @type {Token} */
49
+ let token;
50
+ let dataSeen = false;
51
+ return start;
52
+
53
+ /**
54
+ * Start of math (text).
55
+ *
56
+ * ```markdown
57
+ * > | $a$
58
+ * ^
59
+ * > | \$a$
60
+ * ^
61
+ * ```
62
+ *
63
+ * @type {State}
64
+ */
65
+ function start(code) {
66
+ if (code === 92) {
67
+ codeOpen = 40;
68
+ codeClose = 41;
69
+ }
70
+ tokenType = effects.enter(type);
71
+ effects.enter('mathTextSequence');
72
+ effects.consume(code);
73
+ sizeOpen++;
74
+ return code === 92 ? sequenceOpenBackslash : sequenceOpen;
75
+ }
76
+
77
+ /**
78
+ * In opening sequence.
79
+ *
80
+ * ```markdown
81
+ * > | $a$
82
+ * ^
83
+ * ```
84
+ *
85
+ * @type {State}
86
+ */
87
+
88
+ function sequenceOpen(code) {
89
+ if (code === 36) {
90
+ effects.consume(code);
91
+ sizeOpen++;
92
+ return sequenceOpen;
93
+ }
94
+
95
+ // Not enough markers in the sequence.
96
+ if (sizeOpen < 2 && !single || sizeOpen === 1 && !validSingleDollarOpen(code)) {
97
+ return nok(code);
98
+ }
99
+ effects.exit('mathTextSequence');
100
+ return between(code);
101
+ }
102
+
103
+ /**
104
+ * After the backslash in an opening sequence.
105
+ *
106
+ * ```markdown
107
+ * > | \(a\)
108
+ * ^
109
+ * > | \[a\]
110
+ * ^
111
+ * ```
112
+ *
113
+ * @type {State}
114
+ */
115
+ function sequenceOpenBackslash(code) {
116
+ if (code !== 40 && code !== 91) {
117
+ return nok(code);
118
+ }
119
+ codeOpen = code;
120
+ codeClose = code === 40 ? 41 : 93;
121
+ type = code === 40 ? 'mathText' : 'mathTextDisplay';
122
+ tokenType.type = type;
123
+ effects.consume(code);
124
+ effects.exit('mathTextSequence');
125
+ return between;
126
+ }
127
+
128
+ /**
129
+ * Between something and something else.
130
+ *
131
+ * ```markdown
132
+ * > | $a$
133
+ * ^^
134
+ * ```
135
+ *
136
+ * @type {State}
137
+ */
138
+ function between(code) {
139
+ if (code === null) {
140
+ return nok(code);
141
+ }
142
+ if (code === 36 && codeOpen === 36) {
143
+ token = effects.enter('mathTextSequence');
144
+ size = 0;
145
+ return sequenceClose(code);
146
+ }
147
+ if (code === 92 && codeOpen !== 36) {
148
+ token = effects.enter('mathTextSequence');
149
+ return sequenceCloseBackslash(code);
150
+ }
151
+
152
+ // Tabs don’t work, and virtual spaces don’t make sense.
153
+ if (code === 32) {
154
+ effects.enter('space');
155
+ effects.consume(code);
156
+ effects.exit('space');
157
+ previous = code;
158
+ return between;
159
+ }
160
+ if (markdownLineEnding(code)) {
161
+ effects.enter("lineEnding");
162
+ effects.consume(code);
163
+ effects.exit("lineEnding");
164
+ previous = code;
165
+ return between;
166
+ }
167
+
168
+ // Data.
169
+ effects.enter('mathTextData');
170
+ return data(code);
171
+ }
172
+
173
+ /**
174
+ * In data.
175
+ *
176
+ * ```markdown
177
+ * > | $a$
178
+ * ^
179
+ * ```
180
+ *
181
+ * @type {State}
182
+ */
183
+ function data(code) {
184
+ if (code === null || code === 32 || code === 36 && codeOpen === 36 || code === 92 && codeOpen !== 36 || markdownLineEnding(code)) {
185
+ effects.exit('mathTextData');
186
+ return between(code);
187
+ }
188
+ effects.consume(code);
189
+ previous = code;
190
+ dataSeen = true;
191
+ return data;
192
+ }
193
+
194
+ /**
195
+ * In closing sequence.
196
+ *
197
+ * ```markdown
198
+ * > | `a`
199
+ * ^
200
+ * ```
201
+ *
202
+ * @type {State}
203
+ */
204
+
205
+ function sequenceClose(code) {
206
+ // More.
207
+ if (code === 36) {
208
+ effects.consume(code);
209
+ size++;
210
+ return sequenceClose;
211
+ }
212
+
213
+ // Done!
214
+ if (size === sizeOpen && (sizeOpen !== 1 || validSingleDollarClose(previous, code))) {
215
+ effects.exit('mathTextSequence');
216
+ effects.exit(type);
217
+ return ok(code);
218
+ }
219
+
220
+ // More or less accents: mark as data.
221
+ token.type = 'mathTextData';
222
+ return data(code);
223
+ }
224
+
225
+ /**
226
+ * In a closing backslash sequence.
227
+ *
228
+ * ```markdown
229
+ * > | \(a\)
230
+ * ^
231
+ * ```
232
+ *
233
+ * @type {State}
234
+ */
235
+ function sequenceCloseBackslash(code) {
236
+ effects.consume(code);
237
+ return sequenceCloseBackslashEnd;
238
+ }
239
+
240
+ /**
241
+ * After the backslash in a closing sequence.
242
+ *
243
+ * @type {State}
244
+ */
245
+ function sequenceCloseBackslashEnd(code) {
246
+ if (code === codeClose) {
247
+ if (!dataSeen) {
248
+ return nok(code);
249
+ }
250
+ effects.consume(code);
251
+ effects.exit('mathTextSequence');
252
+ effects.exit(type);
253
+ return ok;
254
+ }
255
+ token.type = 'mathTextData';
256
+ previous = 92;
257
+ return data(code);
258
+ }
259
+ }
260
+ }
261
+
262
+ /** @type {Resolver} */
263
+ function resolveMathText(events) {
264
+ let tailExitIndex = events.length - 4;
265
+ let headEnterIndex = 3;
266
+ /** @type {number} */
267
+ let index;
268
+ /** @type {number | undefined} */
269
+ let enter;
270
+
271
+ // If we start and end with an EOL or a space.
272
+ if ((events[headEnterIndex][1].type === "lineEnding" || events[headEnterIndex][1].type === 'space') && (events[tailExitIndex][1].type === "lineEnding" || events[tailExitIndex][1].type === 'space')) {
273
+ index = headEnterIndex;
274
+
275
+ // And we have data.
276
+ while (++index < tailExitIndex) {
277
+ if (events[index][1].type === 'mathTextData') {
278
+ // Then we have padding.
279
+ events[tailExitIndex][1].type = 'mathTextPadding';
280
+ events[headEnterIndex][1].type = 'mathTextPadding';
281
+ headEnterIndex += 2;
282
+ tailExitIndex -= 2;
283
+ break;
284
+ }
285
+ }
286
+ }
287
+
288
+ // Merge adjacent spaces and data.
289
+ index = headEnterIndex - 1;
290
+ tailExitIndex++;
291
+ while (++index <= tailExitIndex) {
292
+ if (enter === undefined) {
293
+ if (index !== tailExitIndex && events[index][1].type !== "lineEnding") {
294
+ enter = index;
295
+ }
296
+ } else if (index === tailExitIndex || events[index][1].type === "lineEnding") {
297
+ events[enter][1].type = 'mathTextData';
298
+ if (index !== enter + 2) {
299
+ events[enter][1].end = events[index - 1][1].end;
300
+ events.splice(enter + 2, index - enter - 2);
301
+ tailExitIndex -= index - enter - 2;
302
+ index = enter + 2;
303
+ }
304
+ enter = undefined;
305
+ }
306
+ }
307
+ return events;
308
+ }
309
+
310
+ /**
311
+ * @this {TokenizeContext}
312
+ * @type {Previous}
313
+ */
314
+ function previousCode(code) {
315
+ // If there is a previous code, there will always be a tail.
316
+ return code !== 36 || this.events[this.events.length - 1][1].type === "characterEscape";
317
+ }
318
+
319
+ /**
320
+ * @param {number | null} code
321
+ * Code.
322
+ * @returns {boolean}
323
+ * Whether `code` can follow an opening single dollar.
324
+ */
325
+ function validSingleDollarOpen(code) {
326
+ return code !== null && !markdownLineEnding(code) && !markdownSpace(code);
327
+ }
328
+
329
+ /**
330
+ * @param {number | null | undefined} before
331
+ * Code before the closing dollar.
332
+ * @param {number | null} after
333
+ * Code after the closing dollar.
334
+ * @returns {boolean}
335
+ * Whether a single dollar can close.
336
+ */
337
+ function validSingleDollarClose(before, after) {
338
+ return before !== undefined && before !== null && !markdownLineEnding(before) && !markdownSpace(before) && !asciiDigit(after);
339
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Create an extension for `micromark` to enable math syntax.
3
+ *
4
+ * @param {Options | null | undefined} [options={}]
5
+ * Configuration (default: `{}`).
6
+ * @returns {Extension}
7
+ * Extension for `micromark` that can be passed in `extensions`, to
8
+ * enable math syntax.
9
+ */
10
+ export function math(options?: Options | null | undefined): Extension;
11
+ import type { Options } from '@ziloen/micromark-extension-math';
12
+ import type { Extension } from 'micromark-util-types';
package/lib/syntax.js ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @import {Options} from '@ziloen/micromark-extension-math'
3
+ * @import {Extension} from 'micromark-util-types'
4
+ */
5
+
6
+ import { mathFlow } from './math-flow.js';
7
+ import { mathText } from './math-text.js';
8
+
9
+ /**
10
+ * Create an extension for `micromark` to enable math syntax.
11
+ *
12
+ * @param {Options | null | undefined} [options={}]
13
+ * Configuration (default: `{}`).
14
+ * @returns {Extension}
15
+ * Extension for `micromark` that can be passed in `extensions`, to
16
+ * enable math syntax.
17
+ */
18
+ export function math(options) {
19
+ const text = mathText(options);
20
+ return {
21
+ flow: {
22
+ [36]: mathFlow,
23
+ [92]: mathFlow
24
+ },
25
+ text: {
26
+ [36]: text,
27
+ [92]: text
28
+ }
29
+ };
30
+ }
package/license ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2020 Titus Wormer <tituswormer@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,124 @@
1
+ {
2
+ "name": "@ziloen/micromark-extension-math",
3
+ "version": "3.1.0-patch.0",
4
+ "description": "micromark extension to support math (`$C_L$`)",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "micromark",
8
+ "micromark-extension",
9
+ "math",
10
+ "katex",
11
+ "latex",
12
+ "tex",
13
+ "markdown",
14
+ "unified"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/micromark/micromark-extension-math.git"
19
+ },
20
+ "bugs": "https://github.com/micromark/micromark-extension-math/issues",
21
+ "funding": {
22
+ "type": "opencollective",
23
+ "url": "https://opencollective.com/unified"
24
+ },
25
+ "author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
26
+ "contributors": [
27
+ "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
28
+ ],
29
+ "sideEffects": false,
30
+ "type": "module",
31
+ "exports": {
32
+ "development": "./dev/index.js",
33
+ "default": "./index.js"
34
+ },
35
+ "files": [
36
+ "dev/",
37
+ "lib/",
38
+ "index.d.ts",
39
+ "index.js"
40
+ ],
41
+ "dependencies": {
42
+ "@types/katex": "^0.16.0",
43
+ "devlop": "^1.0.0",
44
+ "katex": "^0.16.0",
45
+ "micromark-factory-space": "^2.0.0",
46
+ "micromark-util-character": "^2.0.0",
47
+ "micromark-util-symbol": "^2.0.0",
48
+ "micromark-util-types": "^2.0.0"
49
+ },
50
+ "devDependencies": {
51
+ "@types/node": "^22.0.0",
52
+ "c8": "^10.0.0",
53
+ "micromark": "^4.0.0",
54
+ "micromark-build": "^2.0.0",
55
+ "prettier": "^3.0.0",
56
+ "remark-cli": "^12.0.0",
57
+ "remark-preset-wooorm": "^10.0.0",
58
+ "type-coverage": "^2.0.0",
59
+ "typescript": "^5.0.0",
60
+ "xo": "^0.59.0"
61
+ },
62
+ "scripts": {
63
+ "prepack": "npm run build && npm run format",
64
+ "build": "tsc --build --clean && tsc --build && type-coverage && micromark-build",
65
+ "format": "remark . -qfo && prettier . -w --log-level warn && xo --fix",
66
+ "test-api-prod": "node --conditions production test/index.js",
67
+ "test-api-dev": "node --conditions development test/index.js",
68
+ "test-api": "npm run test-api-dev && npm run test-api-prod",
69
+ "test-coverage": "c8 --100 --reporter lcov npm run test-api",
70
+ "test": "npm run build && npm run format && npm run test-coverage"
71
+ },
72
+ "prettier": {
73
+ "bracketSpacing": false,
74
+ "semi": false,
75
+ "singleQuote": true,
76
+ "tabWidth": 2,
77
+ "trailingComma": "none",
78
+ "useTabs": false
79
+ },
80
+ "remarkConfig": {
81
+ "plugins": [
82
+ "remark-preset-wooorm"
83
+ ]
84
+ },
85
+ "typeCoverage": {
86
+ "atLeast": 100,
87
+ "detail": true,
88
+ "ignoreCatch": true,
89
+ "strict": true
90
+ },
91
+ "xo": {
92
+ "overrides": [
93
+ {
94
+ "files": [
95
+ "**/*.d.ts"
96
+ ],
97
+ "rules": {
98
+ "@typescript-eslint/array-type": [
99
+ "error",
100
+ {
101
+ "default": "generic"
102
+ }
103
+ ],
104
+ "@typescript-eslint/ban-types": [
105
+ "error",
106
+ {
107
+ "extendDefaults": true
108
+ }
109
+ ],
110
+ "@typescript-eslint/consistent-type-definitions": [
111
+ "error",
112
+ "interface"
113
+ ]
114
+ }
115
+ }
116
+ ],
117
+ "prettier": true,
118
+ "rules": {
119
+ "logical-assignment-operators": "off",
120
+ "unicorn/no-this-assignment": "off",
121
+ "unicorn/prefer-at": "off"
122
+ }
123
+ }
124
+ }