micromark / micromark-extension-math

micromark extension to support math (`$C_L$`)
https://unifiedjs.com
MIT License
15 stars 7 forks source link

"$20,000 and $30,000" should’t parse as math (if remark-math and micromark-extension-math follows Pandoc as remark-math 3.0 did) #6

Closed seiyab closed 9 months ago

seiyab commented 9 months ago

Older remark-math handled it same as pandoc: https://github.com/remarkjs/remark-math/pull/35. I consider this a regression.

Initial checklist

Affected packages and versions

remark-math 6.0.0

Link to runnable example

Codesandbox

Steps to reproduce

Codesandbox

import remarkParse from "remark-parse";
import { unified } from "unified";
import remarkMath from "remark-math";

const sourceMarkdown = `$20,000 and $30,000`;

document.getElementById("source").textContent = sourceMarkdown;

const processor = unified().use(remarkParse).use(remarkMath);

processor
  .run(processor.parse(sourceMarkdown))
  .then((file) => {
    document.getElementById("result").textContent = JSON.stringify(
      file,
      null,
      2
    );
  })
  .catch((err) => (document.getElementById("error").textContent = err));

Expected behavior

It shouldn't parse as math.

Actual behavior

parses as inline math.

{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "inlineMath",
          "value": "20,000 and ",
          "data": {
            "hName": "code",
            "hProperties": {
              "className": [
                "language-math",
                "math-inline"
              ]
            },
            "hChildren": [
              {
                "type": "text",
                "value": "20,000 and "
              }
            ]
          },
          "position": {
            "start": {
              "line": 1,
              "column": 1,
              "offset": 0
            },
            "end": {
              "line": 1,
              "column": 14,
              "offset": 13
            }
          }
        },
        {
          "type": "text",
          "value": "30,000",
          "position": {
            "start": {
              "line": 1,
              "column": 14,
              "offset": 13
            },
            "end": {
              "line": 1,
              "column": 20,
              "offset": 19
            }
          }
        }
      ],
      "position": {
        "start": {
          "line": 1,
          "column": 1,
          "offset": 0
        },
        "end": {
          "line": 1,
          "column": 20,
          "offset": 19
        }
      }
    }
  ],
  "position": {
    "start": {
      "line": 1,
      "column": 1,
      "offset": 0
    },
    "end": {
      "line": 1,
      "column": 20,
      "offset": 19
    }
  }
}

Runtime

No response

Package manager

No response

OS

No response

Build and bundle tools

No response

wooorm commented 9 months ago

Hi

The behavior is intended. Pandoc is not a reference. How code works in CommonMark is a reference.

`20,000 and `30,000

20,000 and30,000

You can escape dollars and backticks in markdown if you want:

\`20,000 and `30,000

`20,000 and `30,000

You can also turn off singleDollarTextMath in this extension. This is what I would recommend to everyone. With that, math is unlikely to interfere with regular text. That is also how I implemented syntax highlighting, which is used in places where math may or may not work, such as right here:

$math?$

$$math?$$
github-actions[bot] commented 9 months ago

Hi! This was closed. Team: If this was fixed, please add phase/solved. Otherwise, please add one of the no/* labels.

wooorm commented 9 months ago

The PR you reference also says what I say now. Pandoc is not a reference. https://github.com/remarkjs/remark-math/pull/35

seiyab commented 9 months ago

Thank you for your response. I understand. I'm working on Prettier and will suggest turning off singleDollarTextMath with breaking change or having our own tokenizer.