Skip to content

Commit 31360ab

Browse files
committed
Add singleDollarTextMath option
This option can be used to prevent math (text) from forming if only one dollar is used. Related-to: remarkjs/remark-math#63.
1 parent f0e0c1c commit 31360ab

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

Diff for: index.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
66
* @typedef {import('./complex-types').Math} Math
77
* @typedef {import('./complex-types').InlineMath} InlineMath
8+
*
9+
* @typedef ToOptions
10+
* @property {boolean} [singleDollarTextMath=true]
11+
* Whether to support math (text) with a single dollar (`boolean`, default:
12+
* `true`).
13+
* Single dollars work in Pandoc and many other places, but often interfere
14+
* with “normal” dollars in text.
815
*/
916

1017
import {longestStreak} from 'longest-streak'
@@ -111,16 +118,29 @@ export function mathFromMarkdown() {
111118
}
112119

113120
/**
121+
* @param {ToOptions} [options]
114122
* @returns {ToMarkdownExtension}
115123
*/
116-
export function mathToMarkdown() {
124+
export function mathToMarkdown(options = {}) {
125+
let single = options.singleDollarTextMath
126+
127+
if (single === null || single === undefined) {
128+
single = true
129+
}
130+
117131
inlineMath.peek = inlineMathPeek
118132

119133
return {
120134
unsafe: [
121135
{character: '\r', inConstruct: ['mathFlowMeta']},
122136
{character: '\r', inConstruct: ['mathFlowMeta']},
123-
{character: '$', inConstruct: ['mathFlowMeta', 'phrasing']},
137+
single
138+
? {character: '$', inConstruct: ['mathFlowMeta', 'phrasing']}
139+
: {
140+
character: '$',
141+
after: '\\$',
142+
inConstruct: ['mathFlowMeta', 'phrasing']
143+
},
124144
{atBreak: true, character: '$', after: '\\$'}
125145
],
126146
handlers: {math, inlineMath}
@@ -166,6 +186,8 @@ export function mathToMarkdown() {
166186
let size = 1
167187
let pad = ''
168188

189+
if (!single) size++
190+
169191
// If there is a single dollar sign on its own in the math, use a fence of
170192
// two.
171193
// If there are two in a row, use one.

Diff for: readme.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,22 @@ There is no default export.
103103

104104
### `mathFromMarkdown()`
105105

106-
### `mathToMarkdown()`
106+
### `mathToMarkdown(toOptions?)`
107107

108108
Support math.
109109
These exports are functions that create extensions, respectively for
110110
[`mdast-util-from-markdown`][from-markdown] and
111111
[`mdast-util-to-markdown`][to-markdown].
112112

113+
##### `toOptions`
114+
115+
###### `toOptions.singleDollarTextMath`
116+
117+
Whether to support math (text) with a single dollar (`boolean`, default:
118+
`true`).
119+
Single dollars work in Pandoc and many other places, but often interfere with
120+
“normal” dollars in text.
121+
113122
## Related
114123

115124
* [`remarkjs/remark`][remark]

Diff for: test.js

+27
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ test('mdast -> markdown', (t) => {
147147
'should serialize math (text)'
148148
)
149149

150+
t.deepEqual(
151+
toMarkdown(
152+
{type: 'inlineMath', value: 'a'},
153+
{extensions: [mathToMarkdown({singleDollarTextMath: false})]}
154+
),
155+
'$$a$$\n',
156+
'should serialize math (text) with at least 2 dollars w/ `singleDollarTextMath: false`'
157+
)
158+
150159
t.deepEqual(
151160
// @ts-expect-error: `value` missing.
152161
toMarkdown({type: 'inlineMath'}, {extensions: [mathToMarkdown()]}),
@@ -223,6 +232,24 @@ test('mdast -> markdown', (t) => {
223232
'should escape `$` in phrasing'
224233
)
225234

235+
t.deepEqual(
236+
toMarkdown(
237+
{type: 'paragraph', children: [{type: 'text', value: 'a $ b'}]},
238+
{extensions: [mathToMarkdown({singleDollarTextMath: false})]}
239+
),
240+
'a $ b\n',
241+
'should not escape a single dollar in phrasing w/ `singleDollarTextMath: false`'
242+
)
243+
244+
t.deepEqual(
245+
toMarkdown(
246+
{type: 'paragraph', children: [{type: 'text', value: 'a $$ b'}]},
247+
{extensions: [mathToMarkdown({singleDollarTextMath: false})]}
248+
),
249+
'a \\$$ b\n',
250+
'should escape two dollars in phrasing w/ `singleDollarTextMath: false`'
251+
)
252+
226253
t.deepEqual(
227254
toMarkdown(
228255
{

0 commit comments

Comments
 (0)