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.

Closes GH-63.
  • Loading branch information
wooorm committed Aug 25, 2021
1 parent a98996f commit ce0ba8c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
11 changes: 6 additions & 5 deletions packages/remark-math/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast-util-math').ToOptions} Options
*
* @typedef {import('mdast-util-math')} DoNotTouchAsThisImportIncludesMathInTree
*/
Expand All @@ -10,14 +11,14 @@ import {mathFromMarkdown, mathToMarkdown} from 'mdast-util-math'
/**
* Plugin to support math.
*
* @type {import('unified').Plugin<void[], Root>}
* @type {import('unified').Plugin<[Options?] | void[], Root, Root>}
*/
export default function remarkMath() {
export default function remarkMath(options = {}) {
const data = this.data()

add('micromarkExtensions', math)
add('fromMarkdownExtensions', mathFromMarkdown)
add('toMarkdownExtensions', mathToMarkdown)
add('micromarkExtensions', math(options))
add('fromMarkdownExtensions', mathFromMarkdown())
add('toMarkdownExtensions', mathToMarkdown(options))

/**
* @param {string} field
Expand Down
4 changes: 2 additions & 2 deletions packages/remark-math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
],
"dependencies": {
"@types/mdast": "^3.0.0",
"mdast-util-math": "^1.0.0",
"micromark-extension-math": "^1.0.0",
"mdast-util-math": "^2.0.0",
"micromark-extension-math": "^2.0.0",
"unified": "^10.0.0"
},
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions packages/remark-math/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ Get’s useful when combined with [`rehype-katex`][rehype-katex] or
See [`micromark/micromark-extension-math`][extension-math] for more info on what
syntax is supported.

##### `options`

###### `options.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.

#### Notes

##### Escaping
Expand Down
23 changes: 23 additions & 0 deletions packages/remark-math/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,29 @@ test('remarkMath', (t) => {
'should stringify inline and block math'
)

t.deepEqual(
unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkMath, {singleDollarTextMath: false})
.processSync('Math $\\alpha$\n\n$$\\beta+\\gamma$$\n')
.toString(),
'Math $\\alpha$\n\n$$\\beta+\\gamma$$\n',
'should support `singleDollarTextMath: false` (1)'
)

t.deepEqual(
unified()
.use(remarkParse)
.use(remarkMath, {singleDollarTextMath: false})
.use(remarkRehype)
.use(rehypeStringify)
.processSync('Math $\\alpha$\n\n$$\\beta+\\gamma$$\n')
.toString(),
'<p>Math $\\alpha$</p>\n<p><span class="math math-inline">\\beta+\\gamma</span></p>',
'should support `singleDollarTextMath: false` (2)'
)

t.deepEqual(
unified()
.use(remarkParse)
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ But in a browser, that looks something like this:
## Packages
This repo houses four packages:
This repo houses three packages:
* [`remark-math`][remark-math]
— Parses `$` as `inlineMath` and `$$` as `math` nodes
Expand Down

0 comments on commit ce0ba8c

Please sign in to comment.