88[ ![ Backers] [ backers-badge ]] [ collective ]
99[ ![ Chat] [ chat-badge ]] [ chat ]
1010
11- Extension for [ ` mdast-util-from-markdown ` ] [ from-markdown ] and/or
12- [ ` mdast-util-to-markdown ` ] [ to-markdown ] to support math in ** [ mdast] [ ] ** .
13- When parsing (` from-markdown ` ), must be combined with
14- [ ` micromark-extension-math ` ] [ extension ] .
11+ [ mdast] [ ] extensions to parse and serialize math (` $C_L$ ` ).
12+
13+ ## Contents
14+
15+ * [ What is this?] ( #what-is-this )
16+ * [ When to use this] ( #when-to-use-this )
17+ * [ Install] ( #install )
18+ * [ Use] ( #use )
19+ * [ API] ( #api )
20+ * [ ` mathFromMarkdown() ` ] ( #mathfrommarkdown )
21+ * [ ` mathToMarkdown(options?) ` ] ( #mathtomarkdownoptions )
22+ * [ HTML] ( #html )
23+ * [ Syntax tree] ( #syntax-tree )
24+ * [ ` InlineMath ` ] ( #inlinemath )
25+ * [ Content model] ( #content-model )
26+ * [ Types] ( #types )
27+ * [ Compatibility] ( #compatibility )
28+ * [ Related] ( #related )
29+ * [ Contribute] ( #contribute )
30+ * [ License] ( #license )
31+
32+ ## What is this?
33+
34+ This package contains extensions that add support for math to
35+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] and
36+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
1537
1638## When to use this
1739
18- Use this if you’re dealing with the AST manually.
19- It might be better to use [ ` remark-math ` ] [ remark-math ] with ** [ remark] [ ] ** ,
20- which includes this but provides a nicer interface and makes it easier to
21- combine with hundreds of plugins.
40+ These tools are all rather low-level.
41+ In most cases, you’d want to use [ ` remark-math ` ] [ remark-math ] with remark
42+ instead.
2243
23- ## Install
44+ This project is useful when you want to support math in markdown.
45+ Extending markdown with a syntax extension makes the markdown less portable.
46+ LaTeX equations are also quite hard.
47+ But this mechanism works well when you want authors, that have some LaTeX
48+ experience, to be able to embed rich diagrams of math in scientific text.
49+
50+ When working with ` mdast-util-from-markdown ` , you must combine this package with
51+ [ ` micromark-extension-math ` ] [ extension ] .
52+
53+ This utility adds [ fields on nodes] [ fields ] so that the utility responsible for
54+ turning mdast (markdown) nodes into hast (HTML) nodes,
55+ [ ` mdast-util-to-hast ` ] [ mdast-util-to-hast ] , turns text (inline) math nodes into
56+ ` <span class="math math-inline">…</span> ` and flow (block) math nodes into
57+ ` <div class="math math-display">…</div> ` .
2458
25- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
26- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
59+ ## Install
2760
28- [ npm] [ ] :
61+ This package is [ ESM only] [ esm ] .
62+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
2963
3064``` sh
3165npm install mdast-util-math
3266```
3367
68+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
69+
70+ ``` js
71+ import {mathFromMarkdown , mathToMarkdown } from ' https://esm.sh/mdast-util-math@2'
72+ ```
73+
74+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
75+
76+ ``` html
77+ <script type =" module" >
78+ import {mathFromMarkdown , mathToMarkdown } from ' https://esm.sh/mdast-util-math@2?bundle'
79+ </script >
80+ ```
81+
3482## Use
3583
36- Say we have the following file, ` example.md ` :
84+ Say our document ` example.md ` contains :
3785
3886``` markdown
3987Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation.
@@ -43,16 +91,16 @@ L = \frac{1}{2} \rho v^2 S C_L
4391$$
4492```
4593
46- And our module, ` example.js ` , looks as follows:
94+ …and our module ` example.js ` looks as follows:
4795
4896``` js
49- import fs from ' node:fs'
97+ import fs from ' node:fs/promises '
5098import {fromMarkdown } from ' mdast-util-from-markdown'
5199import {toMarkdown } from ' mdast-util-to-markdown'
52100import {math } from ' micromark-extension-math'
53101import {mathFromMarkdown , mathToMarkdown } from ' mdast-util-math'
54102
55- const doc = fs .readFileSync (' example.md' )
103+ const doc = await fs .readFile (' example.md' )
56104
57105const tree = fromMarkdown (doc, {
58106 extensions: [math ()],
@@ -66,7 +114,8 @@ const out = toMarkdown(tree, {extensions: [mathToMarkdown()]})
66114console .log (out)
67115```
68116
69- Now, running ` node example ` yields (positional info removed for brevity):
117+ …now running ` node example.js ` yields (positional info and data removed for
118+ brevity):
70119
71120``` js
72121{
97146
98147## API
99148
100- This package exports the following identifiers: ` mathFromMarkdown ` ,
101- ` mathToMarkdown ` .
149+ This package exports the identifiers ` mathFromMarkdown ` and ` mathToMarkdown ` .
102150There is no default export.
103151
104152### ` mathFromMarkdown() `
105153
106- ### ` mathToMarkdown(toOptions?) `
154+ Function that can be called to get an extension for
155+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] .
156+
157+ ### ` mathToMarkdown(options?) `
158+
159+ Function that can be called to get an extension for
160+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
107161
108- Support math.
109- These exports are functions that create extensions, respectively for
110- [ ` mdast-util-from-markdown ` ] [ from-markdown ] and
111- [ ` mdast-util-to-markdown ` ] [ to-markdown ] .
162+ ##### ` options `
112163
113- ##### ` toOptions `
164+ Configuration (optional).
114165
115- ###### ` toOptions .singleDollarTextMath`
166+ ###### ` options .singleDollarTextMath`
116167
117- Whether to support math (text ) with a single dollar (` boolean ` , default:
168+ Whether to support text math (inline ) with a single dollar (` boolean ` , default:
118169` true ` ).
119170Single dollars work in Pandoc and many other places, but often interfere with
120171“normal” dollars in text.
121172
173+ ## HTML
174+
175+ This plugin integrates with [ ` mdast-util-to-hast ` ] [ mdast-util-to-hast ] .
176+ When mdast is turned into hast the math nodes are turned into
177+ ` <span class="math math-inline">…</span> ` and
178+ ` <div class="math math-display">…</div> ` elements.
179+
180+ ## Syntax tree
181+
182+ The following interfaces are added to ** [ mdast] [ ] ** by this utility.
183+
184+ #### Nodes
185+
186+ #### ` Math `
187+
188+ ``` idl
189+ interface Math <: Literal {
190+ type: "code"
191+ meta: string?
192+ }
193+ ```
194+
195+ ** Math** ([ ** Literal** ] [ dfn-literal ] ) represents a block of math,
196+ such as LaTeX mathematical expressions.
197+
198+ ** Math** can be used where [ ** flow** ] [ dfn-flow-content ] content is expected.
199+ Its content is represented by its ` value ` field.
200+
201+ This node relates to the [ ** phrasing** ] [ dfn-phrasing-content ] content concept
202+ [ ** InlineMath** ] [ dfn-inline-math ] .
203+
204+ A ` meta ` field can be present.
205+ It represents custom information relating to the node.
206+
207+ For example, the following markdown:
208+
209+ ``` markdown
210+ $$
211+ L = \frac{1}{2} \rho v^2 S C_L
212+ $$
213+ ```
214+
215+ Yields:
216+
217+ ``` js
218+ {
219+ type: ' math' ,
220+ meta: null ,
221+ value: ' L = \\ frac{1}{2} \\ rho v^2 S C_L' ,
222+ data: {/* … */ }
223+ }
224+ ```
225+
226+ ### ` InlineMath `
227+
228+ ``` idl
229+ interface InlineMath <: Literal {
230+ type: "inlineMath"
231+ }
232+ ```
233+
234+ ** InlineMath** ([ ** Literal** ] [ dfn-literal ] ) represents a fragment of computer
235+ code, such as a file name, computer program, or anything a computer could parse.
236+
237+ ** InlineMath** can be used where [ ** phrasing** ] [ dfn-phrasing-content ] content
238+ is expected.
239+ Its content is represented by its ` value ` field.
240+
241+ This node relates to the [ ** flow** ] [ dfn-flow-content ] content concept
242+ [ ** Math** ] [ dfn-math ] .
243+
244+ For example, the following markdown:
245+
246+ ``` markdown
247+ $L$
248+ ```
249+
250+ Yields:
251+
252+ ``` js
253+ {type: ' inlineMath' , value: ' L' , data: {/* … */ }}
254+ ```
255+
256+ ### Content model
257+
258+ #### ` FlowContent ` (math)
259+
260+ ``` idl
261+ type FlowContentMath = Math | FlowContent
262+ ```
263+
264+ #### ` PhrasingContent ` (math)
265+
266+ ``` idl
267+ type PhrasingMath = InlineMath | PhrasingContent
268+ ```
269+
270+ ## Types
271+
272+ This package is fully typed with [ TypeScript] [ ] .
273+ It exports the additional types ` Math ` , ` InlineMath ` , and ` ToOptions ` .
274+
275+ It also registers the node types with ` @types/mdast ` .
276+ If you’re working with the syntax tree, make sure to import this utility
277+ somewhere in your types, as that registers the new node types in the tree.
278+
279+ ``` js
280+ /**
281+ * @typedef {import('mdast-util-math')}
282+ */
283+
284+ import {visit } from ' unist-util-visit'
285+
286+ /** @type {import('mdast').Root} */
287+ const tree = getMdastNodeSomeHow ()
288+
289+ visit (tree, (node ) => {
290+ // `node` can now be one of the nodes for math.
291+ })
292+ ```
293+
294+ ## Compatibility
295+
296+ Projects maintained by the unified collective are compatible with all maintained
297+ versions of Node.js.
298+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
299+ Our projects sometimes work with older versions, but this is not guaranteed.
300+
301+ This plugin works with ` mdast-util-from-markdown ` version 1+ and
302+ ` mdast-util-to-markdown ` version 1+.
303+
122304## Related
123305
124- * [ ` remarkjs/remark ` ] [ remark ]
125- — markdown processor powered by plugins
126306* [ ` remarkjs/remark-math ` ] [ remark-math ]
127307 — remark plugin to support math
128- * [ ` micromark/micromark ` ] [ micromark ]
129- — the smallest commonmark compliant markdown parser that exists
130308* [ ` micromark/micromark-extension-math ` ] [ extension ]
131309 — micromark extension to parse math
132- * [ ` syntax-tree/mdast-util-from-markdown ` ] [ from-markdown ]
133- — mdast parser using ` micromark ` to create mdast from markdown
134- * [ ` syntax-tree/mdast-util-to-markdown ` ] [ to-markdown ]
135- — mdast serializer to create markdown from mdast
136310
137311## Contribute
138312
139- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
140- started.
313+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
314+ ways to get started.
141315See [ ` support.md ` ] [ support ] for ways to get help.
142316
143317This project has a [ code of conduct] [ coc ] .
@@ -178,26 +352,44 @@ abide by its terms.
178352
179353[ npm ] : https://docs.npmjs.com/cli/install
180354
355+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
356+
357+ [ esmsh ] : https://esm.sh
358+
359+ [ typescript ] : https://www.typescriptlang.org
360+
181361[ license ] : license
182362
183363[ author ] : https://wooorm.com
184364
185- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
186-
187- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support.md
365+ [ health ] : https://github.com/syntax-tree/.github
188366
189- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct .md
367+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
190368
191- [ mdast ] : https://github.com/syntax-tree/mdast
369+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support.md
192370
193- [ remark ] : https://github.com/remarkjs/remark
371+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
194372
195373[ remark-math ] : https://github.com/remarkjs/remark-math
196374
197- [ from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
375+ [ mdast ] : https://github.com/syntax-tree/mdast
376+
377+ [ mdast-util-from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
198378
199- [ to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
379+ [ mdast-util- to-markdown] : https://github.com/syntax-tree/mdast-util-to-markdown
200380
201- [ micromark ] : https://github.com/micromark/micromark
381+ [ mdast-util-to-hast ] : https://github.com/syntax-tree/mdast-util-to-hast
202382
203383[ extension ] : https://github.com/micromark/micromark-extension-math
384+
385+ [ fields ] : https://github.com/syntax-tree/mdast-util-to-hast#fields-on-nodes
386+
387+ [ dfn-literal ] : https://github.com/syntax-tree/mdast#literal
388+
389+ [ dfn-flow-content ] : #flowcontent-math
390+
391+ [ dfn-phrasing-content ] : #phrasingcontent-math
392+
393+ [ dfn-inline-math ] : #inlinemath
394+
395+ [ dfn-math ] : #math
0 commit comments