@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.
package/readme.md ADDED
@@ -0,0 +1,463 @@
1
+ # micromark-extension-math
2
+
3
+ [![Build][build-badge]][build]
4
+ [![Coverage][coverage-badge]][coverage]
5
+ [![Downloads][downloads-badge]][downloads]
6
+ [![Size][size-badge]][size]
7
+ [![Sponsors][sponsors-badge]][collective]
8
+ [![Backers][backers-badge]][collective]
9
+ [![Chat][chat-badge]][chat]
10
+
11
+ [micromark][] extensions to support math (`$C_L$`).
12
+
13
+ This is a fork of [`micromark/micromark-extension-math`][upstream].
14
+ It adds Pandoc-style backslash math delimiters and tightens single-dollar
15
+ delimiter handling while keeping existing multi-dollar inline compatibility.
16
+
17
+ ## Contents
18
+
19
+ * [What is this?](#what-is-this)
20
+ * [Fork changes](#fork-changes)
21
+ * [When to use this](#when-to-use-this)
22
+ * [Install](#install)
23
+ * [Use](#use)
24
+ * [API](#api)
25
+ * [`math(options?)`](#mathoptions)
26
+ * [`mathHtml(options?)`](#mathhtmloptions)
27
+ * [`HtmlOptions`](#htmloptions)
28
+ * [`Options`](#options)
29
+ * [Authoring](#authoring)
30
+ * [HTML](#html)
31
+ * [CSS](#css)
32
+ * [Syntax](#syntax)
33
+ * [Types](#types)
34
+ * [Compatibility](#compatibility)
35
+ * [Security](#security)
36
+ * [Related](#related)
37
+ * [Contribute](#contribute)
38
+ * [License](#license)
39
+
40
+ ## What is this?
41
+
42
+ This package contains two extensions that add support for math syntax
43
+ in markdown to [`micromark`][micromark].
44
+
45
+ As there is no spec for math in markdown, this extension follows how code
46
+ (fenced and text) works in Commonmark, but uses dollars.
47
+
48
+ ## Fork changes
49
+
50
+ This fork adds support for `\(...\)` as inline math and `\[...\]` as display
51
+ math.
52
+ Display math with `\[...\]` is supported both as a block and inside text.
53
+
54
+ Single-dollar inline math follows the Pandoc delimiter convention: the opening
55
+ `$` must be followed by a non-space character, and the closing `$` must be
56
+ preceded by a non-space character and must not be followed by a digit.
57
+ This prevents currency such as `$20,000 and $30,000` from being parsed as math.
58
+
59
+ For compatibility with the original package, inline math with two or more
60
+ dollars, such as `$$x$$` and `$$$x$$$`, is still supported.
61
+
62
+ ## When to use this
63
+
64
+ This project is useful when you want to support math in markdown.
65
+ Extending markdown with a syntax extension makes the markdown less portable.
66
+ LaTeX equations are also quite hard.
67
+ But this mechanism works well when you want authors, that have some LaTeX
68
+ experience, to be able to embed rich diagrams of math in scientific text.
69
+
70
+ You can use these extensions when you are working with [`micromark`][micromark]
71
+ already.
72
+
73
+ When you need a syntax tree, you can combine this package with
74
+ [`mdast-util-math`][mdast-util-math].
75
+
76
+ All these packages are used [`remark-math`][remark-math], which focusses on
77
+ making it easier to transform content by abstracting these internals away.
78
+
79
+ ## Install
80
+
81
+ This package is [ESM only][esm].
82
+ In Node.js (version 16+), install with [npm][]:
83
+
84
+ [npm][]:
85
+
86
+ ```sh
87
+ npm install micromark-extension-math
88
+ ```
89
+
90
+ In Deno with [`esm.sh`][esmsh]:
91
+
92
+ ```js
93
+ import {math, mathHtml} from 'https://esm.sh/micromark-extension-math@3'
94
+ ```
95
+
96
+ In browsers with [`esm.sh`][esmsh]:
97
+
98
+ ```html
99
+ <script type="module">
100
+ import {math, mathHtml} from 'https://esm.sh/micromark-extension-math@3?bundle'
101
+ </script>
102
+ ```
103
+
104
+ ## Use
105
+
106
+ Say our document `example.md` contains:
107
+
108
+ ```markdown
109
+ Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation.
110
+
111
+ $$
112
+ L = \frac{1}{2} \rho v^2 S C_L
113
+ $$
114
+
115
+ The same inline and display math can also be written with backslash delimiters:
116
+ \(C_L\) and \[L = \frac{1}{2} \rho v^2 S C_L\].
117
+ ```
118
+
119
+ …and our module `example.js` looks as follows:
120
+
121
+ ```js
122
+ import fs from 'node:fs/promises'
123
+ import {micromark} from 'micromark'
124
+ import {math, mathHtml} from 'micromark-extension-math'
125
+
126
+ const output = micromark(await fs.readFile('example.md'), {
127
+ extensions: [math()],
128
+ htmlExtensions: [mathHtml()]
129
+ })
130
+
131
+ console.log(output)
132
+ ```
133
+
134
+ …now running `node example.js` yields (abbreviated):
135
+
136
+ ```html
137
+ <p>Lift(<span class="math math-inline"><span class="katex">…</span></span>) can be determined by Lift Coefficient (<span class="math math-inline"><span class="katex">…</span></span>) like the following equation.</p>
138
+ <div class="math math-display"><span class="katex-display"><span class="katex">…</span></span></div>
139
+ ```
140
+
141
+ ## API
142
+
143
+ This package exports the identifiers [`math`][api-math] and
144
+ [`mathHtml`][api-math-html].
145
+ There is no default export.
146
+
147
+ The export map supports the [`development` condition][development].
148
+ Run `node --conditions development module.js` to get instrumented dev code.
149
+ Without this condition, production code is loaded.
150
+
151
+ ### `math(options?)`
152
+
153
+ Create an extension for `micromark` to enable math syntax.
154
+
155
+ ###### Parameters
156
+
157
+ * `options` ([`Options`][api-options], default: `{}`)
158
+ — configuration
159
+
160
+ ###### Returns
161
+
162
+ Extension for `micromark` that can be passed in `extensions`, to enable math
163
+ syntax ([`Extension`][micromark-extension]).
164
+
165
+ ### `mathHtml(options?)`
166
+
167
+ Create an extension for `micromark` to support math when serializing to HTML.
168
+
169
+ > 👉 **Note**: this uses KaTeX to render math.
170
+
171
+ ###### Parameters
172
+
173
+ * `options` ([`HtmlOptions`][api-html-options], default: `{}`)
174
+ — configuration
175
+
176
+ ###### Returns
177
+
178
+ Extension for `micromark` that can be passed in `htmlExtensions`, to support
179
+ math when serializing to HTML ([`HtmlExtension`][micromark-html-extension]).
180
+
181
+ ### `HtmlOptions`
182
+
183
+ Configuration for HTML output (optional).
184
+
185
+ > 👉 **Note**: passed to [`katex.renderToString`][katex-options].
186
+ > `displayMode` is overwritten by this plugin, to `false` for math in text
187
+ > (inline), and `true` for math in flow (block).
188
+
189
+ ###### Type
190
+
191
+ ```ts
192
+ type Options = Omit<import('katex').KatexOptions, 'displayMode'>
193
+ ```
194
+
195
+ ### `Options`
196
+
197
+ Configuration (TypeScript type).
198
+
199
+ ###### Fields
200
+
201
+ * `singleDollarTextMath` (`boolean`, default: `true`)
202
+ — whether to support math (text, inline) with a single dollar.
203
+ Single dollars work in Pandoc and many other places, but often interfere
204
+ with “normal” dollars in text.
205
+ If you turn this off, you use two or more dollars for text math.
206
+
207
+ ## Authoring
208
+
209
+ When authoring markdown with math, keep in mind that math doesn’t work in most
210
+ places.
211
+ Notably, GitHub currently has a really weird crappy client-side regex-based
212
+ thing.
213
+ But on your own (math-heavy?) site it can be great!
214
+ You can use code (fenced) with an info string of `math` to improve this, as
215
+ that works in many places.
216
+
217
+ ## HTML
218
+
219
+ Math (flow) does not relate to HTML elements.
220
+ `MathML`, which is sort of like SVG but for math, exists but it doesn’t work
221
+ well and isn’t widely supported.
222
+ Instead, this uses [KaTeX][], which generates MathML as a fallback but also
223
+ generates a bunch of divs and spans so math look pretty.
224
+ The KaTeX result is wrapped in `<div>` (for flow, block) and `<span>` (for text,
225
+ inline) elements, with two classes: `math` and either `math-display` or
226
+ `math-inline`.
227
+
228
+ When turning markdown into HTML, each line ending in math (text) is turned
229
+ into a space.
230
+
231
+ ## CSS
232
+
233
+ The HTML produced by KaTeX requires CSS to render correctly.
234
+ You should use `katex.css` somewhere on the page where the math is shown to
235
+ style it properly.
236
+ At the time of writing, the last version is:
237
+
238
+ <!-- To do: update and copy paste the one from: https://katex.org/docs/browser -->
239
+
240
+ ```html
241
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css">
242
+ ```
243
+
244
+ ## Syntax
245
+
246
+ This fork supports both dollar and backslash math delimiters:
247
+
248
+ * `$...$` for inline math, with Pandoc-style single-dollar delimiter rules.
249
+ * `$$...$$` and longer dollar sequences for existing inline compatibility.
250
+ * `\(...\)` for inline math.
251
+ * `\[...\]` for display math.
252
+
253
+ As in Pandoc, enabling `\(...\)` and `\[...\]` means paired `\(` and `\[`
254
+ sequences are treated as math delimiters instead of simple escapes.
255
+
256
+ Math forms with the following BNF:
257
+
258
+ ```abnf
259
+ ; Restriction: the number of markers in the closing sequence must be equal
260
+ ; to the number of markers in the opening sequence.
261
+ mathText ::= sequenceText 1*byte sequenceText
262
+ mathFlow ::= fenceOpen *( eol *line ) [ eol fenceClose ]
263
+
264
+ ; Restriction: not preceded or followed by the marker.
265
+ sequenceText ::= 1*"$"
266
+
267
+ fenceOpen ::= sequenceFlow meta
268
+ ; Restriction: the number of markers in the closing fence sequence must be
269
+ ; equal to or greater than the number of markers in the opening fence
270
+ ; sequence.
271
+ fenceClose ::= sequenceFlow *spaceOrTab
272
+ sequenceFlow ::= 2*"$"
273
+ ; Restriction: the marker cannot occur in `meta`
274
+ meta ::= 1*line
275
+
276
+ ; Character groups for informational purposes.
277
+ byte ::= %x00-FFFF
278
+ eol ::= "\n" | "\r" | "\r\n"
279
+ line ::= byte - eol
280
+ ```
281
+
282
+ The above grammar shows that it is not possible to create empty math (text).
283
+ It is possible to include the sequence marker (dollar) in math (text), by
284
+ wrapping it in bigger or smaller sequences:
285
+
286
+ ```markdown
287
+ Include more: $a$$b$ or include less: $$a$b$$.
288
+ ```
289
+
290
+ It is also possible to include just one marker:
291
+
292
+ ```markdown
293
+ Include just one: $$ $ $$.
294
+ ```
295
+
296
+ Sequences are “greedy”, in that they cannot be preceded or followed by more
297
+ markers.
298
+ To illustrate:
299
+
300
+ ```markdown
301
+ Not math: $$x$.
302
+
303
+ Not math: $x$$.
304
+
305
+ Escapes work, this is math: \$$x$.
306
+
307
+ Escapes work, this is math: $x$\$.
308
+ ```
309
+
310
+ Yields:
311
+
312
+ ```html
313
+ <p>Not math: $$x$.</p>
314
+ <p>Not math: $x$$.</p>
315
+ <p>Escapes work, this is math: $<span>…</span>.</p>
316
+ <p>Escapes work, this is math: <span>…</span>$.</p>
317
+ ```
318
+
319
+ That is because, when turning markdown into HTML, the first and last space,
320
+ if both exist and there is also a non-space in the math, are removed.
321
+ Line endings, at that stage, are considered as spaces.
322
+
323
+ As the math (flow) construct occurs in flow, like all flow constructs, it must
324
+ be followed by an eol (line ending) or eof (end of file).
325
+
326
+ The above grammar does not show how indentation of each line is handled.
327
+ To parse math (flow), let `x` be the number of `space_or_tab` characters
328
+ before the opening fence sequence, after interpreting tabs based on how many
329
+ virtual spaces they represent.
330
+ Each line of text is then allowed (not required) to be indented with up
331
+ to `x` spaces or tabs, which are then ignored as an indent instead of being
332
+ considered as part of the content.
333
+ This indent does not affect the closing fence.
334
+ It can be indented up to a separate 3 real or virtual spaces.
335
+ A bigger indent makes it part of the content instead of a fence.
336
+
337
+ The `meta` part is interpreted as the [string][micromark-content-types] content
338
+ type.
339
+ That means that character escapes and character references are allowed.
340
+
341
+ The optional `meta` part is ignored: it is not used when parsing or
342
+ rendering.
343
+
344
+ ## Types
345
+
346
+ This package is fully typed with [TypeScript][].
347
+ It exports the additional types [`HtmlOptions`][api-html-options]
348
+ and [`Options`][api-options].
349
+
350
+ ## Compatibility
351
+
352
+ Projects maintained by the unified collective are compatible with maintained
353
+ versions of Node.js.
354
+
355
+ When we cut a new major release, we drop support for unmaintained versions of
356
+ Node.
357
+ This means we try to keep the current release line,
358
+ `micromark-extension-math@^3`, compatible with Node.js 16.
359
+
360
+ This package works with `micromark` version `3` and later.
361
+
362
+ ## Security
363
+
364
+ This package is safe assuming that you trust KaTeX.
365
+ Any vulnerability in it could open you to a [cross-site scripting (XSS)][xss]
366
+ attack.
367
+
368
+ ## Related
369
+
370
+ * [`remark-math`][remark-math]
371
+ — remark (and rehype) plugins to support math
372
+ * [`mdast-util-math`][mdast-util-math]
373
+ — mdast utility to support math
374
+
375
+ ## Contribute
376
+
377
+ See [`contributing.md` in `micromark/.github`][contributing] for ways to get
378
+ started.
379
+ See [`support.md`][support] for ways to get help.
380
+
381
+ This project has a [code of conduct][coc].
382
+ By interacting with this repository, organization, or community you agree to
383
+ abide by its terms.
384
+
385
+ ## License
386
+
387
+ [MIT][license] © [Titus Wormer][author]
388
+
389
+ <!-- Definitions -->
390
+
391
+ [build-badge]: https://github.com/micromark/micromark-extension-math/workflows/main/badge.svg
392
+
393
+ [build]: https://github.com/micromark/micromark-extension-math/actions
394
+
395
+ [coverage-badge]: https://img.shields.io/codecov/c/github/micromark/micromark-extension-math.svg
396
+
397
+ [coverage]: https://codecov.io/github/micromark/micromark-extension-math
398
+
399
+ [downloads-badge]: https://img.shields.io/npm/dm/micromark-extension-math.svg
400
+
401
+ [downloads]: https://www.npmjs.com/package/micromark-extension-math
402
+
403
+ [size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=micromark-extension-math
404
+
405
+ [size]: https://bundlejs.com/?q=micromark-extension-math
406
+
407
+ [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
408
+
409
+ [backers-badge]: https://opencollective.com/unified/backers/badge.svg
410
+
411
+ [collective]: https://opencollective.com/unified
412
+
413
+ [chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
414
+
415
+ [chat]: https://github.com/micromark/micromark/discussions
416
+
417
+ [npm]: https://docs.npmjs.com/cli/install
418
+
419
+ [esmsh]: https://esm.sh
420
+
421
+ [license]: license
422
+
423
+ [author]: https://wooorm.com
424
+
425
+ [contributing]: https://github.com/micromark/.github/blob/main/contributing.md
426
+
427
+ [support]: https://github.com/micromark/.github/blob/main/support.md
428
+
429
+ [coc]: https://github.com/micromark/.github/blob/main/code-of-conduct.md
430
+
431
+ [esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
432
+
433
+ [typescript]: https://www.typescriptlang.org
434
+
435
+ [development]: https://nodejs.org/api/packages.html#packages_resolving_user_conditions
436
+
437
+ [micromark]: https://github.com/micromark/micromark
438
+
439
+ [upstream]: https://github.com/micromark/micromark-extension-math
440
+
441
+ [micromark-content-types]: https://github.com/micromark/micromark#content-types
442
+
443
+ [micromark-html-extension]: https://github.com/micromark/micromark#htmlextension
444
+
445
+ [micromark-extension]: https://github.com/micromark/micromark#syntaxextension
446
+
447
+ [mdast-util-math]: https://github.com/syntax-tree/mdast-util-math
448
+
449
+ [remark-math]: https://github.com/remarkjs/remark-math
450
+
451
+ [katex]: https://katex.org
452
+
453
+ [katex-options]: https://katex.org/docs/options.html
454
+
455
+ [xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
456
+
457
+ [api-math]: #mathoptions
458
+
459
+ [api-math-html]: #mathhtmloptions
460
+
461
+ [api-options]: #options
462
+
463
+ [api-html-options]: #htmloptions