Skip to content

Commit 8fa352d

Browse files
committed
Add improved docs
1 parent f41f6af commit 8fa352d

File tree

2 files changed

+135
-38
lines changed

2 files changed

+135
-38
lines changed

Diff for: index.d.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,30 @@ export {mathFromMarkdown, mathToMarkdown} from './lib/index.js'
44

55
export type {ToOptions} from './lib/index.js'
66

7+
/**
8+
* Math (flow).
9+
*/
710
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
811
export interface Math extends Literal {
12+
/**
13+
* Node type.
14+
*/
915
type: 'math'
10-
meta?: string | null
16+
17+
/**
18+
* Custom information relating to the node.
19+
*/
20+
meta?: string | undefined | null
1121
}
1222

23+
/**
24+
* Math (text).
25+
*/
1326
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
1427
export interface InlineMath extends Literal {
28+
/**
29+
* Node type.
30+
*/
1531
type: 'inlineMath'
1632
}
1733

Diff for: readme.md

+118-37
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
* [API](#api)
2020
* [`mathFromMarkdown()`](#mathfrommarkdown)
2121
* [`mathToMarkdown(options?)`](#mathtomarkdownoptions)
22+
* [`InlineMath`](#inlinemath)
23+
* [`Math`](#math)
24+
* [`ToOptions`](#tooptions)
2225
* [HTML](#html)
26+
* [Syntax](#syntax)
2327
* [Syntax tree](#syntax-tree)
2428
* [Nodes](#nodes)
2529
* [Content model](#content-model)
@@ -31,24 +35,35 @@
3135

3236
## What is this?
3337

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].
38+
This package contains two extensions that add support for math syntax in
39+
markdown to [mdast][].
40+
These extensions plug into
41+
[`mdast-util-from-markdown`][mdast-util-from-markdown] (to support parsing
42+
math in markdown into a syntax tree) and
43+
[`mdast-util-to-markdown`][mdast-util-to-markdown] (to support serializing
44+
math in syntax trees to markdown).
3745

3846
## When to use this
3947

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.
43-
4448
This project is useful when you want to support math in markdown.
4549
Extending markdown with a syntax extension makes the markdown less portable.
4650
LaTeX equations are also quite hard.
4751
But this mechanism works well when you want authors, that have some LaTeX
4852
experience, to be able to embed rich diagrams of math in scientific text.
4953

50-
When working with `mdast-util-from-markdown`, you must combine this package with
51-
[`micromark-extension-math`][extension].
54+
You can use these extensions when you are working with
55+
`mdast-util-from-markdown` and `mdast-util-to-markdown` already.
56+
57+
When working with `mdast-util-from-markdown`, you must combine this package
58+
with [`micromark-extension-math`][micromark-extension-math].
59+
60+
When you don’t need a syntax tree, you can use [`micromark`][micromark]
61+
directly with
62+
[`micromark-extension-math`][micromark-extension-math].
63+
64+
All these packages are used [`remark-math`][remark-math], which
65+
focusses on making it easier to transform content by abstracting these
66+
internals away.
5267

5368
This utility adds [fields on nodes][fields] so that the utility responsible for
5469
turning mdast (markdown) nodes into hast (HTML) nodes,
@@ -59,7 +74,7 @@ turning mdast (markdown) nodes into hast (HTML) nodes,
5974
## Install
6075

6176
This package is [ESM only][esm].
62-
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
77+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
6378

6479
```sh
6580
npm install mdast-util-math
@@ -146,30 +161,73 @@ $$
146161

147162
## API
148163

149-
This package exports the identifiers `mathFromMarkdown` and `mathToMarkdown`.
164+
This package exports the identifiers [`mathFromMarkdown`][api-mathfrommarkdown]
165+
and [`mathToMarkdown`][api-mathtomarkdown].
150166
There is no default export.
151167

152168
### `mathFromMarkdown()`
153169

154-
Function that can be called to get an extension for
155-
[`mdast-util-from-markdown`][mdast-util-from-markdown].
170+
Create an extension for [`mdast-util-from-markdown`][mdast-util-from-markdown].
171+
172+
###### Returns
173+
174+
Extension for `mdast-util-from-markdown`
175+
([`FromMarkdownExtension`][frommarkdownextension]).
156176

157177
### `mathToMarkdown(options?)`
158178

159-
Function that can be called to get an extension for
160-
[`mdast-util-to-markdown`][mdast-util-to-markdown].
179+
Create an extension for [`mdast-util-to-markdown`][mdast-util-to-markdown].
180+
181+
###### Parameters
182+
183+
* `options` ([`ToOptions`][api-tooptions], optional)
184+
— configuration
185+
186+
###### Returns
187+
188+
Extension for `mdast-util-to-markdown`
189+
([`ToMarkdownExtension`][tomarkdownextension]).
190+
191+
### `InlineMath`
192+
193+
Math (text) (TypeScript type).
194+
195+
###### Type
196+
197+
```ts
198+
import type {Literal} from 'mdast'
199+
200+
interface InlineMath extends Literal {
201+
type: 'inlineMath'
202+
}
203+
```
204+
205+
### `Math`
206+
207+
Math (flow) (TypeScript type).
208+
209+
###### Type
161210

162-
##### `options`
211+
```ts
212+
import type {Literal} from 'mdast'
163213

164-
Configuration (optional).
214+
interface Math extends Literal {
215+
type: 'math'
216+
meta?: string | undefined | null
217+
}
218+
```
165219

166-
###### `options.singleDollarTextMath`
220+
### `ToOptions`
167221

168-
Whether to support text math (inline) with a single dollar (`boolean`, default:
169-
`true`).
222+
Configuration (TypeScript type).
170223

171-
Single dollars work in Pandoc and many other places, but often interfere with
172-
“normal” dollars in text.
224+
###### Fields
225+
226+
* `singleDollarTextMath` (`boolean`, default: `true`)
227+
— whether to support math (text) with a single dollar.
228+
Single dollars work in Pandoc and many other places, but often interfere
229+
with “normal” dollars in text.
230+
If you turn this off, you can still use two or more dollars for text math
173231

174232
## HTML
175233

@@ -178,6 +236,10 @@ When mdast is turned into hast the math nodes are turned into
178236
`<span class="math math-inline">…</span>` and
179237
`<div class="math math-display">…</div>` elements.
180238

239+
## Syntax
240+
241+
See [Syntax in `micromark-extension-frontmatter`][syntax].
242+
181243
## Syntax tree
182244

183245
The following interfaces are added to **[mdast][]** by this utility.
@@ -193,14 +255,14 @@ interface Math <: Literal {
193255
}
194256
```
195257

196-
**Math** ([**Literal**][dfn-literal]) represents a block of math,
258+
**Math** (**[Literal][dfn-literal]**) represents a block of math,
197259
such as LaTeX mathematical expressions.
198260

199-
**Math** can be used where [**flow**][dfn-flow-content] content is expected.
261+
**Math** can be used where **[flow][dfn-flow-content]** content is expected.
200262
Its content is represented by its `value` field.
201263

202-
This node relates to the [**phrasing**][dfn-phrasing-content] content concept
203-
[**InlineMath**][dfn-inline-math].
264+
This node relates to the **[phrasing][dfn-phrasing-content]** content concept
265+
**[InlineMath][dfn-inline-math]**.
204266

205267
A `meta` field can be present.
206268
It represents custom information relating to the node.
@@ -232,15 +294,15 @@ interface InlineMath <: Literal {
232294
}
233295
```
234296

235-
**InlineMath** ([**Literal**][dfn-literal]) represents a fragment of computer
297+
**InlineMath** (**[Literal][dfn-literal]**) represents a fragment of computer
236298
code, such as a file name, computer program, or anything a computer could parse.
237299

238-
**InlineMath** can be used where [**phrasing**][dfn-phrasing-content] content
300+
**InlineMath** can be used where **[phrasing][dfn-phrasing-content]** content
239301
is expected.
240302
Its content is represented by its `value` field.
241303

242-
This node relates to the [**flow**][dfn-flow-content] content concept
243-
[**Math**][dfn-math].
304+
This node relates to the **[flow][dfn-flow-content]** content concept
305+
**[Math][dfn-math]**.
244306

245307
For example, the following markdown:
246308

@@ -271,7 +333,8 @@ type PhrasingMath = InlineMath | PhrasingContent
271333
## Types
272334

273335
This package is fully typed with [TypeScript][].
274-
It exports the additional types `Math`, `InlineMath`, and `ToOptions`.
336+
It exports the additional types [`InlineMath`][api-inlinemath],
337+
[`Math`][api-math], and [`ToOptions`][api-tooptions].
275338

276339
It also registers the node types with `@types/mdast`.
277340
If you’re working with the syntax tree, make sure to import this utility
@@ -296,17 +359,17 @@ visit(tree, (node) => {
296359

297360
Projects maintained by the unified collective are compatible with all maintained
298361
versions of Node.js.
299-
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
362+
As of now, that is Node.js 14.14+ and 16.0+.
300363
Our projects sometimes work with older versions, but this is not guaranteed.
301364

302365
This plugin works with `mdast-util-from-markdown` version 1+ and
303366
`mdast-util-to-markdown` version 1+.
304367

305368
## Related
306369

307-
* [`remarkjs/remark-math`][remark-math]
370+
* [`remark-math`][remark-math]
308371
— remark plugin to support math
309-
* [`micromark/micromark-extension-math`][extension]
372+
* [`micromark-extension-math`][micromark-extension-math]
310373
— micromark extension to parse math
311374

312375
## Contribute
@@ -381,16 +444,34 @@ abide by its terms.
381444

382445
[mdast-util-to-hast]: https://github.com/syntax-tree/mdast-util-to-hast
383446

384-
[extension]: https://github.com/micromark/micromark-extension-math
447+
[micromark]: https://github.com/micromark/micromark
448+
449+
[micromark-extension-math]: https://github.com/micromark/micromark-extension-math
450+
451+
[syntax]: https://github.com/micromark/micromark-extension-math#syntax
385452

386453
[fields]: https://github.com/syntax-tree/mdast-util-to-hast#fields-on-nodes
387454

388455
[dfn-literal]: https://github.com/syntax-tree/mdast#literal
389456

457+
[frommarkdownextension]: https://github.com/syntax-tree/mdast-util-from-markdown#extension
458+
459+
[tomarkdownextension]: https://github.com/syntax-tree/mdast-util-to-markdown#options
460+
461+
[api-mathfrommarkdown]: #mathfrommarkdown
462+
463+
[api-mathtomarkdown]: #mathtomarkdownoptions
464+
465+
[api-math]: #math
466+
467+
[api-inlinemath]: #inlinemath
468+
469+
[api-tooptions]: #tooptions
470+
390471
[dfn-flow-content]: #flowcontent-math
391472

392473
[dfn-phrasing-content]: #phrasingcontent-math
393474

394-
[dfn-inline-math]: #inlinemath
475+
[dfn-inline-math]: #inlinemath-1
395476

396-
[dfn-math]: #math
477+
[dfn-math]: #math-1

0 commit comments

Comments
 (0)