Skip to content

Commit ce0ba8c

Browse files
committed
Add singleDollarTextMath option
This option can be used to prevent math (text) from forming if only one dollar is used. Closes GH-63.
1 parent a98996f commit ce0ba8c

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

packages/remark-math/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @typedef {import('mdast').Root} Root
3+
* @typedef {import('mdast-util-math').ToOptions} Options
34
*
45
* @typedef {import('mdast-util-math')} DoNotTouchAsThisImportIncludesMathInTree
56
*/
@@ -10,14 +11,14 @@ import {mathFromMarkdown, mathToMarkdown} from 'mdast-util-math'
1011
/**
1112
* Plugin to support math.
1213
*
13-
* @type {import('unified').Plugin<void[], Root>}
14+
* @type {import('unified').Plugin<[Options?] | void[], Root, Root>}
1415
*/
15-
export default function remarkMath() {
16+
export default function remarkMath(options = {}) {
1617
const data = this.data()
1718

18-
add('micromarkExtensions', math)
19-
add('fromMarkdownExtensions', mathFromMarkdown)
20-
add('toMarkdownExtensions', mathToMarkdown)
19+
add('micromarkExtensions', math(options))
20+
add('fromMarkdownExtensions', mathFromMarkdown())
21+
add('toMarkdownExtensions', mathToMarkdown(options))
2122

2223
/**
2324
* @param {string} field

packages/remark-math/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
],
3737
"dependencies": {
3838
"@types/mdast": "^3.0.0",
39-
"mdast-util-math": "^1.0.0",
40-
"micromark-extension-math": "^1.0.0",
39+
"mdast-util-math": "^2.0.0",
40+
"micromark-extension-math": "^2.0.0",
4141
"unified": "^10.0.0"
4242
},
4343
"scripts": {

packages/remark-math/readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ Get’s useful when combined with [`rehype-katex`][rehype-katex] or
8888
See [`micromark/micromark-extension-math`][extension-math] for more info on what
8989
syntax is supported.
9090

91+
##### `options`
92+
93+
###### `options.singleDollarTextMath`
94+
95+
Whether to support math (text) with a single dollar (`boolean`, default:
96+
`true`).
97+
Single dollars work in Pandoc and many other places, but often interfere with
98+
“normal” dollars in text.
99+
91100
#### Notes
92101

93102
##### Escaping

packages/remark-math/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,29 @@ test('remarkMath', (t) => {
318318
'should stringify inline and block math'
319319
)
320320

321+
t.deepEqual(
322+
unified()
323+
.use(remarkParse)
324+
.use(remarkStringify)
325+
.use(remarkMath, {singleDollarTextMath: false})
326+
.processSync('Math $\\alpha$\n\n$$\\beta+\\gamma$$\n')
327+
.toString(),
328+
'Math $\\alpha$\n\n$$\\beta+\\gamma$$\n',
329+
'should support `singleDollarTextMath: false` (1)'
330+
)
331+
332+
t.deepEqual(
333+
unified()
334+
.use(remarkParse)
335+
.use(remarkMath, {singleDollarTextMath: false})
336+
.use(remarkRehype)
337+
.use(rehypeStringify)
338+
.processSync('Math $\\alpha$\n\n$$\\beta+\\gamma$$\n')
339+
.toString(),
340+
'<p>Math $\\alpha$</p>\n<p><span class="math math-inline">\\beta+\\gamma</span></p>',
341+
'should support `singleDollarTextMath: false` (2)'
342+
)
343+
321344
t.deepEqual(
322345
unified()
323346
.use(remarkParse)

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ But in a browser, that looks something like this:
8080
8181
## Packages
8282
83-
This repo houses four packages:
83+
This repo houses three packages:
8484
8585
* [`remark-math`][remark-math]
8686
— Parses `$` as `inlineMath` and `$$` as `math` nodes

0 commit comments

Comments
 (0)