Skip to content

Commit

Permalink
Add singleDollarTextMath option
Browse files Browse the repository at this point in the history
This option can be used to prevent math (text) from forming if only one
dollar is used.

Related-to: remarkjs/remark-math#63.
  • Loading branch information
wooorm committed Aug 25, 2021
1 parent f0e0c1c commit 31360ab
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
26 changes: 24 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
* @typedef {import('./complex-types').Math} Math
* @typedef {import('./complex-types').InlineMath} InlineMath
*
* @typedef ToOptions
* @property {boolean} [singleDollarTextMath=true]
* Whether to support math (text) with a single dollar (`boolean`, default:
* `true`).
* Single dollars work in Pandoc and many other places, but often interfere
* with “normal” dollars in text.
*/

import {longestStreak} from 'longest-streak'
Expand Down Expand Up @@ -111,16 +118,29 @@ export function mathFromMarkdown() {
}

/**
* @param {ToOptions} [options]
* @returns {ToMarkdownExtension}
*/
export function mathToMarkdown() {
export function mathToMarkdown(options = {}) {
let single = options.singleDollarTextMath

if (single === null || single === undefined) {
single = true
}

inlineMath.peek = inlineMathPeek

return {
unsafe: [
{character: '\r', inConstruct: ['mathFlowMeta']},
{character: '\r', inConstruct: ['mathFlowMeta']},
{character: '$', inConstruct: ['mathFlowMeta', 'phrasing']},
single
? {character: '$', inConstruct: ['mathFlowMeta', 'phrasing']}
: {
character: '$',
after: '\\$',
inConstruct: ['mathFlowMeta', 'phrasing']
},
{atBreak: true, character: '$', after: '\\$'}
],
handlers: {math, inlineMath}
Expand Down Expand Up @@ -166,6 +186,8 @@ export function mathToMarkdown() {
let size = 1
let pad = ''

if (!single) size++

// If there is a single dollar sign on its own in the math, use a fence of
// two.
// If there are two in a row, use one.
Expand Down
11 changes: 10 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,22 @@ There is no default export.

### `mathFromMarkdown()`

### `mathToMarkdown()`
### `mathToMarkdown(toOptions?)`

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

##### `toOptions`

###### `toOptions.singleDollarTextMath`

Whether to support math (text) with a single dollar (`boolean`, default:
`true`).
Single dollars work in Pandoc and many other places, but often interfere with
“normal” dollars in text.

## Related

* [`remarkjs/remark`][remark]
Expand Down
27 changes: 27 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ test('mdast -> markdown', (t) => {
'should serialize math (text)'
)

t.deepEqual(
toMarkdown(
{type: 'inlineMath', value: 'a'},
{extensions: [mathToMarkdown({singleDollarTextMath: false})]}
),
'$$a$$\n',
'should serialize math (text) with at least 2 dollars w/ `singleDollarTextMath: false`'
)

t.deepEqual(
// @ts-expect-error: `value` missing.
toMarkdown({type: 'inlineMath'}, {extensions: [mathToMarkdown()]}),
Expand Down Expand Up @@ -223,6 +232,24 @@ test('mdast -> markdown', (t) => {
'should escape `$` in phrasing'
)

t.deepEqual(
toMarkdown(
{type: 'paragraph', children: [{type: 'text', value: 'a $ b'}]},
{extensions: [mathToMarkdown({singleDollarTextMath: false})]}
),
'a $ b\n',
'should not escape a single dollar in phrasing w/ `singleDollarTextMath: false`'
)

t.deepEqual(
toMarkdown(
{type: 'paragraph', children: [{type: 'text', value: 'a $$ b'}]},
{extensions: [mathToMarkdown({singleDollarTextMath: false})]}
),
'a \\$$ b\n',
'should escape two dollars in phrasing w/ `singleDollarTextMath: false`'
)

t.deepEqual(
toMarkdown(
{
Expand Down

0 comments on commit 31360ab

Please sign in to comment.