rapier-math 3.0.0 → 3.1.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 +5 -3
- package/dist/classic.d.ts +0 -1
- package/dist/cli.mjs +438 -0
- package/dist/delimiters.mjs +56 -17
- package/dist/index.mjs +1 -1
- package/dist/markdown-rules.mjs +22 -5
- package/dist/rapier-math.js +1 -1
- package/docs/api.md +44 -42
- package/docs/font-source.md +22 -22
- package/docs/math-components.json +6 -6
- package/docs/math-resources.json +35 -29
- package/docs/semantics.md +33 -33
- package/examples/README.md +7 -7
- package/package.json +6 -3
- package/skills/rapier-math/SKILL.md +60 -0
- package/src/math/classic.js +1 -1
- package/src/math/package-layout.mjs +2 -2
- package/src/math/package-readme.mjs +5 -3
- package/src/tools/build-math.mjs +2 -2
- package/CHANGELOG.md +0 -24
package/docs/semantics.md
CHANGED
|
@@ -23,7 +23,7 @@ import {renderWithMathML} from 'rapier-math/combined';
|
|
|
23
23
|
const result = renderWithMathML(source, {displayMode: true});
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
Its required `semantics` field contains the same result as `toMathML
|
|
26
|
+
Its required `semantics` field contains the same result as `toMathML`. The combined call admits
|
|
27
27
|
and parses once. SVG errors and semantic status remain independent: MathML can represent a
|
|
28
28
|
Unicode character absent from the embedded SVG font. A successful SVG does not turn a semantic
|
|
29
29
|
refusal into success. `renderToString` returns SVG. The complete classic `RapierMath` object
|
|
@@ -35,7 +35,7 @@ Combined and accessible rendering use the strict parse and never repair incomple
|
|
|
35
35
|
The font-free entry exports `parse` for inspection as well. Parse results are ordinary,
|
|
36
36
|
detached data: changing a returned tree has no effect on subsequent renders. There is no
|
|
37
37
|
public API accepting arbitrary or modified trees for serialization. Local macros are
|
|
38
|
-
expanded by the existing parser
|
|
38
|
+
expanded by the existing parser. Their expanded tree is separate from exact authored TeX.
|
|
39
39
|
|
|
40
40
|
| Result field | Meaning |
|
|
41
41
|
| --- | --- |
|
|
@@ -55,24 +55,24 @@ is the portable structural target. Extensions and lossy cases are reported expli
|
|
|
55
55
|
|
|
56
56
|
| TeX/AST family | Mapping and qualification |
|
|
57
57
|
| --- | --- |
|
|
58
|
-
| Rows and groups | `mrow
|
|
59
|
-
| Identifiers and alphabets | `mi
|
|
60
|
-
| Numbers | `mn` joins Unicode decimal-digit integer/decimal runs within one row and matching style. Unary signs remain `mo`. Explicit groups, spaces and style boundaries stop joining. A final directly scripted digit belongs to the complete preceding numeric token, as in `12^3
|
|
61
|
-
| Scientific notation | Authored `1.2e-3` remains a number, identifier and signed expression
|
|
62
|
-
| Named operators and text | Standard names and plain `operatorname` use one `mo
|
|
63
|
-
| Fractions | `mfrac`, with numerator then denominator
|
|
58
|
+
| Rows and groups | `mrow`. Explicit groups stay separate. No inferred multiplication or function application is inserted. Explicit U+2061–U+2064 invisible operators remain `mo`. |
|
|
59
|
+
| Identifiers and alphabets | `mi`. A single default or italic letter stays plain, so MathML Core italicizes it and assistive technology reads the ordinary letter. Other alphabets reuse the SVG renderer's pure Unicode mapping. Roman identifiers use `mathvariant="normal"`. Text spaces become no-break spaces because MathML trims token whitespace. |
|
|
60
|
+
| Numbers | `mn` joins Unicode decimal-digit integer/decimal runs within one row and matching style. Unary signs remain `mo`. Explicit groups, spaces and style boundaries stop joining. A final directly scripted digit belongs to the complete preceding numeric token, as in `12^3`. `1{2}^3` stays grouped. Multi-dot runs are not reinterpreted as decimals. |
|
|
61
|
+
| Scientific notation | Authored `1.2e-3` remains a number, identifier and signed expression. There is no inferred exponent grammar. `1.2\times10^{-3}` preserves its explicit structure. The existing units parser's lowering remains reported as a domain fallback. |
|
|
62
|
+
| Named operators and text | Standard names and plain `operatorname` use one `mo`. Ordinary multi-character text uses `mtext`. Mixed or styled operator-name content retains its nested structure and gets a fallback notice. |
|
|
63
|
+
| Fractions | `mfrac`, with numerator then denominator. Zero-bar fractions and explicit fences are retained. Continued-fraction numerator alignment is a documented presentation fallback. |
|
|
64
64
|
| Roots | `msqrt` or `mroot`, with radicand before degree. |
|
|
65
|
-
| Scripts and limits | `msub`, `msup`, `msubsup
|
|
65
|
+
| Scripts and limits | `msub`, `msup`, `msubsup`, `munder`, `mover`, `munderover` for applicable explicit/display operator limits. |
|
|
66
66
|
| Fences and accents | Explicit `mo` fences, plus under/over accent relationships. No `mfenced` dependency. |
|
|
67
|
-
| Matrices, aligned equations and cases | `mtable`/`mtr`/`mtd`, the original row/cell order and case/matrix fences. Cell alignment uses fixed CSS. Custom row gaps/baselines are reported as presentation fallback. Array insertion content remains in adjacent cells
|
|
67
|
+
| Matrices, aligned equations and cases | `mtable`/`mtr`/`mtd`, the original row/cell order and case/matrix fences. Cell alignment uses fixed CSS. Custom row gaps/baselines are reported as presentation fallback. Array insertion content remains in adjacent cells. Decorative row/column rules are omitted with a notice. |
|
|
68
68
|
| Styles and spaces | `mstyle`, safe fixed CSS, Unicode alphabets and `mspace`. Text script/fraktur/double-struck requests use a disclosed MathML 3 `mathvariant` extension. |
|
|
69
69
|
| Phantom, smash and raising | `mphantom`/`mpadded`. Overlay-width adjustments retain their content with a fallback notice. Native presentation may differ from SVG. |
|
|
70
70
|
| Cancellation and generic negation overlay | MathML 3 `menclose`, with an explicit extension notice. Core-only hosts may omit the mark. Already-negated Unicode operators stay ordinary `mo`. |
|
|
71
|
-
| Rules | A decorative `mspace` with an explicit presentation notice
|
|
71
|
+
| Rules | A decorative `mspace` with an explicit presentation notice. No additional mathematical relationship is inferred. |
|
|
72
72
|
| Explicit `mmlToken` | The authored admitted `mi`/`mn`/`mo`/`mtext` kind and token text are retained before visual literal lowering. Only the parser's admitted font variant is accepted. |
|
|
73
|
-
| Chemistry, physics and units | Existing fractions, scripts, arrows, fences and rows survive. Origin metadata triggers a domain notice
|
|
73
|
+
| Chemistry, physics and units | Existing fractions, scripts, arrows, fences and rows survive. Origin metadata triggers a domain notice. Chemical elements/bonds/charges, unit identities and specialized physical meaning are not invented. |
|
|
74
74
|
| Tags and labels | Tags remain following authored content with a notice. Labels remain parser-discarded metadata: there is no equation identity, automatic numbering or cross-formula reference support. |
|
|
75
|
-
| HTTP(S) links | A native XHTML `a` inside MathML `mtext`, containing a nested MathML expression. This is a disclosed host integration fallback
|
|
75
|
+
| HTTP(S) links | A native XHTML `a` inside MathML `mtext`, containing a nested MathML expression. This is a disclosed host integration fallback. See the composition contract below. Nested links and links inside phantom content refuse the semantic path. |
|
|
76
76
|
|
|
77
77
|
The parser retains only distinctions with live consumers: explicit token kind/text, operator-name
|
|
78
78
|
origin, and chemistry/physics/units/tag origin. Its visual syntax and SVG layout relationships
|
|
@@ -82,7 +82,7 @@ remain the same. Unsupported commands still produce parser diagnostics.
|
|
|
82
82
|
|
|
83
83
|
The first child of `semantics` is the mathematical expression. For XML-valid source without
|
|
84
84
|
CR, a following `annotation encoding="application/x-tex"` keeps exact authored source
|
|
85
|
-
independently. Escaping includes numeric references for tabs and LF
|
|
85
|
+
independently. Escaping includes numeric references for tabs and LF. BOM and supplementary
|
|
86
86
|
Unicode characters remain intact. The semantic token text is normalized presentation and is
|
|
87
87
|
never described as the original source. The [XML character and newline rules](https://www.w3.org/TR/xml/)
|
|
88
88
|
explain why ordinary escaping alone is insufficient.
|
|
@@ -93,7 +93,7 @@ is a readable alternative, not an exact-source claim. This preserves CRLF throug
|
|
|
93
93
|
HAST-to-HTML-to-HAST pipeline: an HTML stringifier can write a decoded CR literally, which a
|
|
94
94
|
later HTML parser normalizes. The ASCII carrier survives that boundary. CR alone does not
|
|
95
95
|
make an otherwise supported expression a fallback or refusal. Recover exact source from the
|
|
96
|
-
annotation selected by `source.encoding
|
|
96
|
+
annotation selected by `source.encoding`. When only markup remains, prefer its UTF-16
|
|
97
97
|
annotation when present, otherwise its TeX annotation. SVG uses the same carrier for CR-bearing source, preserving exact input through host
|
|
98
98
|
reserialization and later browser fitting. The source is admitted and encoded once per operation.
|
|
99
99
|
|
|
@@ -106,7 +106,7 @@ never becomes an invalid character reference. Invalid characters produced by an
|
|
|
106
106
|
Unicode command likewise refuse while retaining its valid authored command text.
|
|
107
107
|
|
|
108
108
|
Source longer than 1,000,000 UTF-16 code units is refused before scanning, escaping or parsing.
|
|
109
|
-
It is not truncated or embedded
|
|
109
|
+
It is not truncated or embedded. `source.omitted`, the diagnostic's `sourceOmitted`, and
|
|
110
110
|
`data-math-source-omitted` make that explicit. Keep that original input with the host.
|
|
111
111
|
|
|
112
112
|
Existing parser bounds still apply: 100,000 tokens, 50,000 nodes, nesting 192 and 4096 macro
|
|
@@ -116,7 +116,7 @@ repeated insertion content count against the semantic budget. If source carriers
|
|
|
116
116
|
presentation exceed the complete-output bound, the result refuses, retains the admitted
|
|
117
117
|
source carriers and replaces the partial expression with `merror`. SVG retains its existing
|
|
118
118
|
separate limits. Combined accessible HTML has a 40 Mi code-unit bound. All budgets are local
|
|
119
|
-
to one operation
|
|
119
|
+
to one operation. A refusal cannot poison a later render. These bounds do not measure device
|
|
120
120
|
memory or claim constant-size output.
|
|
121
121
|
|
|
122
122
|
## Usable SVG and MathML composition
|
|
@@ -132,14 +132,14 @@ else container.innerHTML = result.html; // only freshly generated output reaches
|
|
|
132
132
|
`renderAccessible` calls `renderWithMathML` once. It returns full standalone `svg`, the
|
|
133
133
|
semantic result, `html`, and `presentation`:
|
|
134
134
|
|
|
135
|
-
- `svg-mathml`: visual SVG is inside an `aria-hidden` wrapper
|
|
135
|
+
- `svg-mathml`: visual SVG is inside an `aria-hidden` wrapper. Native MathML is its clipped
|
|
136
136
|
assistive sibling. There are no authored links in this branch. The semantic tree has no
|
|
137
137
|
image role, raw-TeX aria-label, `aria-hidden`, `display:none` or `visibility:hidden` override.
|
|
138
138
|
- `mathml`: authored links use visible native MathML once. Standalone SVG is still returned
|
|
139
139
|
for consumers choosing that representation, but is not inserted into the combined host.
|
|
140
140
|
Each link uses an HTTP(S) URL, a native HTML anchor, `noopener noreferrer`, and its visible
|
|
141
141
|
content as a nested mathematical expression. The destination title supplies a link-name
|
|
142
|
-
fallback in hosts that do not derive a name from MathML children
|
|
142
|
+
fallback in hosts that do not derive a name from MathML children. It is not generated
|
|
143
143
|
mathematical speech or a claim of a descriptive link purpose. No duplicate or clipped
|
|
144
144
|
focus target is created.
|
|
145
145
|
- `refused`: `html` is empty and `errors` explains the failure. Insert the original source
|
|
@@ -148,23 +148,23 @@ semantic result, `html`, and `presentation`:
|
|
|
148
148
|
This composition supplies one exposed semantic equation surface structurally. Its actual
|
|
149
149
|
announcements and link navigation still require AT acceptance. Putting an interactive link
|
|
150
150
|
under `aria-hidden` violates the [W3C focus rule](https://www.w3.org/WAI/standards-guidelines/act/rules/6cfa84/).
|
|
151
|
-
The published Core target omits native MathML links
|
|
151
|
+
The published Core target omits native MathML links. HTML embedding uses its web integration
|
|
152
152
|
rules. A newer editor draft's MathML anchor does not establish support in a target browser.
|
|
153
|
-
Nested, phantom and empty links refuse
|
|
153
|
+
Nested, phantom and empty links refuse. The serializer also checks explicit transparency
|
|
154
154
|
while allowing an opaque descendant. Unknown or host-specific color names cannot undo an explicitly transparent ancestor. These are structural checks, not a visibility guarantee
|
|
155
155
|
under arbitrary host CSS, colors or backgrounds.
|
|
156
156
|
|
|
157
157
|
The stylesheet must be retained. If it is absent or blocked, both ordinary representations
|
|
158
158
|
can become visible. Hosts that cannot include it can insert `toMathML(source).mathml` visibly
|
|
159
|
-
as their sole representation instead. Native MathML uses the host's mathematical font
|
|
160
|
-
|
|
159
|
+
as their sole representation instead. Native MathML uses the host's mathematical font.
|
|
160
|
+
It does not inherit Rapier's embedded SVG outlines. Linked formulas therefore use native
|
|
161
161
|
typesetting rather than a guarantee of identical SVG appearance.
|
|
162
162
|
|
|
163
163
|
`fit`/`observe` may operate on ordinary combined output because its SVG source carrier remains
|
|
164
|
-
intact
|
|
164
|
+
intact. They adjust only SVG geometry. Linked native MathML has no SVG to fit. Do not change
|
|
165
165
|
source on one side of a combined formula: regenerate the whole result. Stop observations
|
|
166
|
-
before replacement and on unmount
|
|
167
|
-
For SSR, emit the same source/options and stylesheet
|
|
166
|
+
before replacement and on unmount. No semantic timers, IDs or retained formula caches exist.
|
|
167
|
+
For SSR, emit the same source/options and stylesheet. Initial MathML needs no client code.
|
|
168
168
|
|
|
169
169
|
The [accessible browser example](../examples/accessible.html) keeps editable source and generated
|
|
170
170
|
MathML available and demonstrates both representations, replacement and cleanup. The
|
|
@@ -175,19 +175,19 @@ preserving its SVG appearance. Put the anchor outside both composition siblings
|
|
|
175
175
|
an authored link-purpose name, such as `aria-label="Open the derivation"`, or a meaningful
|
|
176
176
|
link title. Native MathML children can leave an otherwise unnamed host link blank in a
|
|
177
177
|
browser's accessibility tree. For accessible output the rehype adapter retains an existing
|
|
178
|
-
`aria-label`, `aria-labelledby` or title
|
|
178
|
+
`aria-label`, `aria-labelledby` or title. Otherwise it supplies the existing destination as
|
|
179
179
|
a title fallback. This preserves the native mathematical subtree without synthesizing
|
|
180
180
|
speech. A URL fallback is not a substitute for the author's meaningful link-purpose name.
|
|
181
181
|
Direct `renderAccessible` users must name any containing host anchor themselves. Do not nest
|
|
182
182
|
an internally linked formula inside another link. The adapters retain the outer host link
|
|
183
183
|
and leave such a formula as source with a `nested-link` diagnostic. Host grammar still applies:
|
|
184
|
-
markdown-it keeps math in link labels literal
|
|
184
|
+
markdown-it keeps math in link labels literal. A remark-math/rehype pipeline can admit it.
|
|
185
185
|
|
|
186
186
|
## Markdown, sanitizer and CSP boundaries
|
|
187
187
|
|
|
188
188
|
The neutral markdown-it and rehype adapters accept `accessible: true`. Include the same CSS.
|
|
189
|
-
Their document output budget counts the complete inserted HTML, including semantic annotations and every cache hit
|
|
190
|
-
|
|
189
|
+
Their document output budget counts the complete inserted HTML, including semantic annotations and every cache hit.
|
|
190
|
+
Semantic refusal leaves source in the host. Original Markdown and diagnostic `mathSource` remain
|
|
191
191
|
the exact fallback authority: HTML text alone can normalize CR or replace invalid Unicode code units. Their parser grammar and lifetime ownership do not
|
|
192
192
|
change. Each inserted occurrence counts against its document's output budget.
|
|
193
193
|
|
|
@@ -200,9 +200,9 @@ authored SVG or HTML. HTML serialization is not a replacement for retaining orig
|
|
|
200
200
|
The example uses an external stylesheet/module and permits generated style attributes. A CSP
|
|
201
201
|
forbidding all inline style attributes cannot reproduce all SVG or MathML presentation. Core
|
|
202
202
|
structure may survive, but that is not a claim of unchanged presentation. The package creates
|
|
203
|
-
no Trusted Types policy
|
|
203
|
+
no Trusted Types policy. String insertion and SVG fitting have the same TrustedHTML limitations
|
|
204
204
|
as the existing browser API. Exact HTTP(S) link destinations carry no fetch authority until
|
|
205
|
-
the person activates them
|
|
205
|
+
the person activates them. No formula rendering request performs a network fetch.
|
|
206
206
|
|
|
207
207
|
## Assistive-technology acceptance
|
|
208
208
|
|
|
@@ -218,7 +218,7 @@ link focus target, link activation and source edit/copy. Repeat after replacemen
|
|
|
218
218
|
These steps are manual acceptance, not an automated pass:
|
|
219
219
|
|
|
220
220
|
- [NVDA's mathematical-content guide](https://download.nvaccess.org/documentation/userGuide.html):
|
|
221
|
-
use the installed provider and recorded browser
|
|
221
|
+
use the installed provider and recorded browser. Enter math from browse mode and inspect
|
|
222
222
|
its nested parts, then return to the surrounding sentence.
|
|
223
223
|
- [JAWS Math Viewer guidance](https://support.freedomscientific.com/SurfsUp/24-MathML.htm):
|
|
224
224
|
enter its viewer, inspect peers and nested structure, return and activate the native link.
|
package/examples/README.md
CHANGED
|
@@ -5,15 +5,15 @@ From a consumer directory, install `rapier-math`, then run
|
|
|
5
5
|
|
|
6
6
|
For Markdown and streaming, also install `markdown-it` and run `markdown.mjs` or `stream.mjs`.
|
|
7
7
|
For unified, install `unified remark-parse remark-math remark-rehype rehype-sanitize rehype-stringify hast-util-from-html`
|
|
8
|
-
and run `unified.mjs`. The remark example uses remark's syntax
|
|
8
|
+
and run `unified.mjs`. The remark example uses remark's syntax. It does not recognize backslash delimiters.
|
|
9
9
|
|
|
10
10
|
Serve the package directory over HTTP and open `examples/browser.html`. Its external module imports the
|
|
11
11
|
installed ESM renderer, fits the output and releases observation before replacement and on page exit.
|
|
12
12
|
Keep both `dist/` and `src/` when serving or copying these native-module examples.
|
|
13
13
|
For a one-file alternative, copy `dist/rapier-math.js`, load it with a classic script tag
|
|
14
|
-
and call `globalThis.RapierMath.renderToString(source)
|
|
14
|
+
and call `globalThis.RapierMath.renderToString(source)`. Do not copy only `dist/index.mjs`.
|
|
15
15
|
Start `new Worker('./worker.mjs', {type: 'module'})` from this directory and post `{id, source}`.
|
|
16
|
-
Use an id owned by the current message and ignore stale replies. Workers render SVG
|
|
16
|
+
Use an id owned by the current message and ignore stale replies. Workers render SVG. They do not call `observe`.
|
|
17
17
|
|
|
18
18
|
`react.tsx` is a small React 19/SSR component to copy into an existing TypeScript application. Install
|
|
19
19
|
`react react-dom` and the development types `@types/react @types/react-dom`. No framework is required by the core.
|
|
@@ -25,14 +25,14 @@ Do not concatenate transport data into HTML. Pass the callback's session through
|
|
|
25
25
|
VFile `data.rapierMathSession` to reuse formulas. On cancellation call `abort`. For replacement, cancel transport
|
|
26
26
|
and use the new handle returned by `stream = stream.reset()`. Each producer captures its own handle, so stale
|
|
27
27
|
writes/finish calls on the old handle refuse. A failed render releases reusable artifacts but keeps accepted
|
|
28
|
-
source for an explicit `finish()` retry
|
|
28
|
+
source for an explicit `finish()` retry. Abort in a final cleanup if the host will not retry.
|
|
29
29
|
|
|
30
30
|
The examples retain the original source as the editable/copyable authority. Markdown hosts can normalize
|
|
31
|
-
newlines or escapes in their syntax nodes
|
|
31
|
+
newlines or escapes in their syntax nodes. An HTML serialization is not an exact source archive.
|
|
32
32
|
|
|
33
33
|
## Structured and accessible formulas
|
|
34
34
|
|
|
35
35
|
Run `node examples/semantic.mjs` for font-free MathML. Serve the package root locally and open
|
|
36
36
|
`examples/accessible.html` for source replacement, generated MathML, cleanup, CSP and the two
|
|
37
|
-
link compositions. The external stylesheet must load. General internal links use native MathML
|
|
38
|
-
|
|
37
|
+
link compositions. The external stylesheet must load. General internal links use native MathML.
|
|
38
|
+
A host link around a complete ordinary formula preserves SVG. See [semantic contracts](../docs/semantics.md).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rapier-math",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Self-contained offline LaTeX-to-SVG rendering with Latin Modern outlines, optional Markdown integrations and streaming helpers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -73,14 +73,17 @@
|
|
|
73
73
|
"optional": true
|
|
74
74
|
}
|
|
75
75
|
},
|
|
76
|
+
"bin": {
|
|
77
|
+
"rapier-math": "./dist/cli.mjs"
|
|
78
|
+
},
|
|
76
79
|
"files": [
|
|
77
80
|
"dist",
|
|
78
81
|
"src",
|
|
79
82
|
"docs",
|
|
80
83
|
"examples",
|
|
84
|
+
"skills",
|
|
81
85
|
"LICENSE",
|
|
82
|
-
"README.md"
|
|
83
|
-
"CHANGELOG.md"
|
|
86
|
+
"README.md"
|
|
84
87
|
],
|
|
85
88
|
"license": "SEE LICENSE IN LICENSE",
|
|
86
89
|
"author": "Jack Skipworth",
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: rapier-math
|
|
3
|
+
description: Typeset TeX math to self-contained SVG or MathML synchronously and offline with npx rapier-math add. Embedded Latin Modern outlines, Markdown and rehype adapters, streaming reuse and responsive browser fitting. Node 22+.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# TeX Math
|
|
7
|
+
|
|
8
|
+
The npm package and command are `rapier-math`. Render TeX formulas synchronously to SVG with embedded Latin Modern outlines or to Presentation MathML with no runtime dependencies, DOM requirement or remote font requests.
|
|
9
|
+
|
|
10
|
+
| Entry | Use |
|
|
11
|
+
| --- | --- |
|
|
12
|
+
| `rapier-math` | `render(tex, options)` (`{svg, width, height, depth, lines, fitted, errors}`), `renderToString(tex, options)`, `parse(tex)`, `fit(container)`, `observe(container)`. |
|
|
13
|
+
| `rapier-math/mathml` | `toMathML(tex, options)` (`{mathml, status, errors, source, metrics}`) and `parse(tex)` with zero font decoding. |
|
|
14
|
+
| `rapier-math/combined` | `renderWithMathML(tex, options)` returning both SVG and MathML from one parse. |
|
|
15
|
+
| `rapier-math/accessible` | `renderAccessible(tex, options)` (`{html, presentation, errors, semantics}`) paired with `rapier-math/accessible.css`. |
|
|
16
|
+
| `rapier-math/markdown-it` | `md.use(rapierMath, options)` for `\(...\)`, `\[...\]` and `$$...$$` (`{singleDollar: true}` opts in `$...$`). |
|
|
17
|
+
| `rapier-math/rehype` | `rehypeRapierMath(options)` placed after `rehype-sanitize` in a `remark-math` + `remark-rehype` pipeline. |
|
|
18
|
+
| `rapier-math/stream` | `createMarkdownStream(renderMarkdown, options)` and `createMathSession(limits)` for streamed model output. |
|
|
19
|
+
| `rapier-math/delimiters` | `createMathMatcher(source, options)` returning exact UTF-16 formula spans in raw text. |
|
|
20
|
+
| `rapier-math/classic` | `dist/rapier-math.js` (`globalThis.RapierMath`), one self-contained browser script with font outlines included. |
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import {render, observe} from 'rapier-math';
|
|
24
|
+
|
|
25
|
+
const source = String.raw`\frac{-b\pm\sqrt{b^2-4ac}}{2a}`;
|
|
26
|
+
const result = render(source, {displayMode: true});
|
|
27
|
+
if (result.errors.length) element.textContent = source;
|
|
28
|
+
else {
|
|
29
|
+
element.innerHTML = result.svg;
|
|
30
|
+
const stop = observe(element); // call stop() before replacing or unmounting
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Keep the original TeX string for editing and copying. Omit `screen` during SSR and initial client hydration so server and client SVG match, then call `observe(container)` after mount to fit wide formulas to narrow columns and snap rules to device pixels. For `markdown-it`, initialize `new MarkdownIt({html: false}).use(rapierMath)`. For `stream`, pass `(text, session) => md.render(text, {rapierMathSession: session})` to `createMarkdownStream`, replace the whole preview whenever `stream.write(chunk)` returns a snapshot, call `stream.finish()` at EOF, and call `stream.abort()` in `finally`.
|
|
35
|
+
|
|
36
|
+
## Mistakes
|
|
37
|
+
|
|
38
|
+
| Wrong | Right |
|
|
39
|
+
| --- | --- |
|
|
40
|
+
| `render('$$x^2$$')` | `render('x^2', {displayMode: true})` (formula APIs take raw TeX without Markdown delimiters) |
|
|
41
|
+
| `"\\frac{1}{2}"` unescaped twice | `String.raw`\frac{1}{2}`` in JavaScript source literals |
|
|
42
|
+
| Inserting `result.svg` without checking `result.errors` | Check `result.errors.length === 0` first and write `source` to `textContent` on error |
|
|
43
|
+
| Passing `screen` during SSR | Omit `screen` on server and initial client render, then run `observe(container)` after hydration |
|
|
44
|
+
| Copying `dist/index.mjs` alone into a static site | Copy `dist/rapier-math.js` (`globalThis.RapierMath`) for one file, or bundle/serve `dist/` and `src/` together |
|
|
45
|
+
| Running `rapier-math/rehype` before `rehype-sanitize` | Run `rehype-sanitize` (allowing `language-math`, `math-inline`, `math-display` on `code`) before `rapier-math/rehype` |
|
|
46
|
+
| Appending `stream.write(chunk)` snapshots | Replace the entire preview with `snapshot.html` and finish with `stream.finish()` |
|
|
47
|
+
|
|
48
|
+
## CLI
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
npx rapier-math add
|
|
52
|
+
npx rapier-math add --json
|
|
53
|
+
npx rapier-math remove --json
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`npx rapier-math add` inspects the current folder (or `--path <dir>`), detects `next`, `astro`, `svelte`, `vue`, `react`, `npm` or plain `html` (or accepts an explicit target name or `agents`), installs `rapier-math` (or copies `dist/rapier-math.js` for plain HTML), writes a ready-to-use `<Equation>` component or `typeset` helper, and copies this skill to `.agents/skills/rapier-math/SKILL.md`. `npx rapier-math remove` removes unmodified generated files and preserves files you edited. With `--json`, `add` and `remove` emit one JSON record to stdout.
|
|
57
|
+
|
|
58
|
+
## Refusals
|
|
59
|
+
|
|
60
|
+
Check `result.errors` on `render` (`{code, message, offset, command?, suggestion?, sourceOmitted?}`). Codes include `invalid-tex`, `unsupported-command` (check `suggestion`, such as `\frac` for `\farc`), `unsupported-package`, `invalid-source-character`, `invalid-screen`, `unsafe-url` and `resource-limit`. On `toMathML` and `renderAccessible`, check `semantics.status` (`'complete'`, `'presentation-fallback'` or `'refused'`). CLI setup errors carry `MATH_OPTIONS`, `MATH_SETUP` or `MATH_INSTALL` with `error.hint`.
|
package/src/math/classic.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
globalThis.RapierMath=Object.freeze({
|
|
3
|
-
version:'3.
|
|
3
|
+
version:'3.1.0', parse:parseMathTex,
|
|
4
4
|
render:renderMath, renderToString:mathRenderToString,
|
|
5
5
|
fit:mathFit, observe:mathObserve,
|
|
6
6
|
toMathML:mathToMathML, renderWithMathML:mathRenderWithMathML,
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
export const MATH_CLASSIC = 'dist/rapier-math.js';
|
|
3
|
+
export const MATH_BIN = Object.freeze({'rapier-math': './dist/cli.mjs'});
|
|
3
4
|
|
|
4
5
|
// One public location per authored input. Runtime formats share dist/.
|
|
5
6
|
export function mathPackagePath(file) {
|
|
6
7
|
const owners = {
|
|
7
8
|
'math/README.md': 'docs/api.md',
|
|
8
|
-
'math/CHANGELOG.md': 'CHANGELOG.md',
|
|
9
9
|
'math/SEMANTICS.md': 'docs/semantics.md',
|
|
10
10
|
'math/semantic-verify.mjs': 'tests/semantics.mjs',
|
|
11
11
|
'math/accessible.css': 'dist/accessible.css',
|
|
@@ -16,7 +16,7 @@ export function mathPackagePath(file) {
|
|
|
16
16
|
'math/typeset-verify.mjs': 'tests/typeset.mjs',
|
|
17
17
|
};
|
|
18
18
|
if (owners[file]) return owners[file];
|
|
19
|
-
if (file.startsWith('math/examples/')) return file.slice(5);
|
|
19
|
+
if (file.startsWith('math/examples/') || file.startsWith('math/skills/')) return file.slice(5);
|
|
20
20
|
if (/^math\/(?!package-).+\.(?:mjs|d\.ts)$/.test(file)) return 'dist/' + file.slice(5);
|
|
21
21
|
return 'src/' + file;
|
|
22
22
|
}
|
|
@@ -5,9 +5,11 @@ export function mathReadme(resources) {
|
|
|
5
5
|
TeX to SVG or MathML, synchronously and offline. Latin Modern outlines are embedded in the SVG, so there are no font requests and no runtime downloads. No dependencies.
|
|
6
6
|
|
|
7
7
|
\`\`\`sh
|
|
8
|
-
|
|
8
|
+
npx rapier-math add
|
|
9
9
|
\`\`\`
|
|
10
10
|
|
|
11
|
+
Installs into npm, plain HTML, React, Vue, Svelte, Astro or Next projects, or run \`npm install rapier-math\`.
|
|
12
|
+
|
|
11
13
|
\`\`\`js
|
|
12
14
|
import {renderToString} from 'rapier-math';
|
|
13
15
|
|
|
@@ -24,13 +26,13 @@ Try it in [Rapier](https://rapier.website): type \\$x^2\\$.
|
|
|
24
26
|
| \`rapier-math/mathml\` | Native MathML, no font |
|
|
25
27
|
| \`rapier-math/combined\` | SVG and MathML from one parse |
|
|
26
28
|
| \`rapier-math/accessible\` | Accessible output for your own layout, with \`rapier-math/accessible.css\` |
|
|
27
|
-
| \`rapier-math/markdown-it\`, \`rapier-math/rehype\` | Markdown integrations
|
|
29
|
+
| \`rapier-math/markdown-it\`, \`rapier-math/rehype\` | Markdown integrations. Your parser stays in charge |
|
|
28
30
|
| \`rapier-math/stream\` | Formulas in streamed text, such as model output |
|
|
29
31
|
| \`rapier-math/classic\` | [${resources.packageBundle}](${resources.packageBundle}): one file, \`globalThis.RapierMath\` |
|
|
30
32
|
|
|
31
33
|
Types ship with every entry. The classic file is the one to copy or inline: ${resources.bytes.toLocaleString('en-US')} bytes, ${resources.gzipBytes.toLocaleString('en-US')} gzipped, font included. The ES modules import \`src/math/\`, so serve or bundle the whole package rather than copying \`dist/index.mjs\` alone.
|
|
32
34
|
|
|
33
|
-
[API](docs/api.md) · [MathML and accessibility](docs/semantics.md) · [Examples](examples/README.md) · [Font](docs/font-source.md) · [Changes](
|
|
35
|
+
[API](docs/api.md) · [MathML and accessibility](docs/semantics.md) · [Examples](examples/README.md) · [Font](docs/font-source.md) · [Changes](https://github.com/jackskip22/rapier-math/releases)
|
|
34
36
|
|
|
35
37
|
## Build
|
|
36
38
|
|
package/src/tools/build-math.mjs
CHANGED
|
@@ -12,10 +12,10 @@ import {mathModules, mathClassicSource} from '../math/package-build.mjs';
|
|
|
12
12
|
import {mathReadme} from '../math/package-readme.mjs';
|
|
13
13
|
|
|
14
14
|
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
15
|
-
export const MATH_VERSION = '3.
|
|
15
|
+
export const MATH_VERSION = '3.1.0';
|
|
16
16
|
export const MATH_BUNDLE = 'rapier-math-' + MATH_VERSION + '.js';
|
|
17
17
|
export const MATH_SOURCES = ['font.js', 'symbols.js', 'parser.js', 'source.js', 'variants.js', 'layout.js', 'fallbacks.js', 'responsive.js', 'api.js', 'browser.js', 'mathml.js', 'semantic-api.js', 'combined.js', 'classic.js'].map(name => 'math/' + name);
|
|
18
|
-
export const MATH_RECIPE = ['math/build-font.py', 'math/FONT-SOURCE.md', 'math/FONT-LICENSE.txt', 'math/package-verify.mjs', 'tools/build-math.mjs', 'math/LICENSE-MIT', 'math/adoption-verify.mjs', 'math/package-build.mjs', 'math/package-layout.mjs', 'math/package-check.mjs', 'math/package-readme.mjs', 'math/README.md', 'math/
|
|
18
|
+
export const MATH_RECIPE = ['math/build-font.py', 'math/FONT-SOURCE.md', 'math/FONT-LICENSE.txt', 'math/package-verify.mjs', 'tools/build-math.mjs', 'math/LICENSE-MIT', 'math/adoption-verify.mjs', 'math/package-build.mjs', 'math/package-layout.mjs', 'math/package-check.mjs', 'math/package-readme.mjs', 'math/README.md', 'math/SEMANTICS.md', 'math/semantic-verify.mjs', 'math/typeset-verify.mjs', 'math/cli.mjs', 'math/skills/rapier-math/SKILL.md'];
|
|
19
19
|
export const MATH_ADOPTION = ['delimiters.mjs', 'markdown-rules.mjs', 'reuse.mjs', 'integration.mjs', 'markdown-it.mjs', 'rehype.mjs', 'stream.mjs',
|
|
20
20
|
'accessible.mjs', 'accessible.css', 'mathml.d.ts', 'combined.d.ts', 'accessible.d.ts',
|
|
21
21
|
'index.d.ts', 'classic.d.ts', 'delimiters.d.ts', 'integration.d.ts', 'markdown-it.d.ts', 'rehype.d.ts', 'stream.d.ts',
|
package/CHANGELOG.md
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## 3.0.0 (2026-10-31)
|
|
4
|
-
|
|
5
|
-
- Breaking: types: `renderWithMathML` and `renderAccessible` take no `preview` or `caret`.
|
|
6
|
-
- Types: `markdown-it` takes extra `env` fields; `rehype` works without a `VFile`.
|
|
7
|
-
- Types: the classic `RapierMath` global is a `var`.
|
|
8
|
-
- `node src/tools/build-math.mjs --package .` refreshes source receipts and checksums.
|
|
9
|
-
|
|
10
|
-
## 2.7.0 (2026-10-20)
|
|
11
|
-
|
|
12
|
-
- `\verb` and `\verb*` set verbatim text.
|
|
13
|
-
- `\mathchoice` picks its branch by display or script style, in SVG and MathML.
|
|
14
|
-
- Prescripts render as `mmultiscripts` in MathML.
|
|
15
|
-
- Monospace punctuation, visible space and the sharp s use Latin Modern Mono outlines.
|
|
16
|
-
|
|
17
|
-
## 2.6.1 (2026-10-18)
|
|
18
|
-
|
|
19
|
-
- Use the font's ellipses and correct extensible arrow sizes.
|
|
20
|
-
- Match amscd row spacing and under-accent attachment points.
|
|
21
|
-
- Correct chemistry labels, state spacing and superscripts.
|
|
22
|
-
- Keep chemistry arrows at least 2 em wide in MathML.
|
|
23
|
-
- Include the Latin Modern Mono sharp s outline for monospace text.
|
|
24
|
-
- Preserve exact math fence source in shared Markdown rules.
|