From ce0ba8c76da3853aabce5464de8d2bc34fba915c Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 25 Aug 2021 11:38:46 +0200 Subject: [PATCH] Add `singleDollarTextMath` option This option can be used to prevent math (text) from forming if only one dollar is used. Closes GH-63. --- packages/remark-math/index.js | 11 ++++++----- packages/remark-math/package.json | 4 ++-- packages/remark-math/readme.md | 9 +++++++++ packages/remark-math/test.js | 23 +++++++++++++++++++++++ readme.md | 2 +- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/packages/remark-math/index.js b/packages/remark-math/index.js index 266a061..85fcd02 100644 --- a/packages/remark-math/index.js +++ b/packages/remark-math/index.js @@ -1,5 +1,6 @@ /** * @typedef {import('mdast').Root} Root + * @typedef {import('mdast-util-math').ToOptions} Options * * @typedef {import('mdast-util-math')} DoNotTouchAsThisImportIncludesMathInTree */ @@ -10,14 +11,14 @@ import {mathFromMarkdown, mathToMarkdown} from 'mdast-util-math' /** * Plugin to support math. * - * @type {import('unified').Plugin} + * @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 diff --git a/packages/remark-math/package.json b/packages/remark-math/package.json index dc6341b..a5e4a65 100644 --- a/packages/remark-math/package.json +++ b/packages/remark-math/package.json @@ -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": { diff --git a/packages/remark-math/readme.md b/packages/remark-math/readme.md index f192465..6849090 100644 --- a/packages/remark-math/readme.md +++ b/packages/remark-math/readme.md @@ -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 diff --git a/packages/remark-math/test.js b/packages/remark-math/test.js index e174d7c..2e8c532 100644 --- a/packages/remark-math/test.js +++ b/packages/remark-math/test.js @@ -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(), + '

Math $\\alpha$

\n

\\beta+\\gamma

', + 'should support `singleDollarTextMath: false` (2)' + ) + t.deepEqual( unified() .use(remarkParse) diff --git a/readme.md b/readme.md index d3727bd..8238d86 100644 --- a/readme.md +++ b/readme.md @@ -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