diff --git a/changelog_unreleased/markdown/pr-7938.md b/changelog_unreleased/markdown/pr-7938.md new file mode 100644 index 000000000..97e0b9d17 --- /dev/null +++ b/changelog_unreleased/markdown/pr-7938.md @@ -0,0 +1,19 @@ +#### Update remark-math to 3.0.1; escape dollar signs only if originally escaped (#7938 by @fisker and @thorn0) + + +```markdown + +Paragraph with $14 million. + +Paragraph with $14 million. But if more $dollars on the same line... + + +Paragraph with \$14 million. + +Paragraph with $14 million. But if more $dollars on the same line... + + +Paragraph with $14 million. + +Paragraph with $14 million. But if more $dollars on the same line... +``` diff --git a/package.json b/package.json index 00580a2af..e76adcff4 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "postcss-values-parser": "2.0.1", "regexp-util": "1.2.2", "remark-footnotes": "2.0.0", - "remark-math": "1.0.6", + "remark-math": "3.0.1", "remark-parse": "8.0.3", "resolve": "1.19.0", "semver": "7.3.2", diff --git a/src/language-markdown/print-preprocess.js b/src/language-markdown/print-preprocess.js index 5b97c7e42..afde0c065 100644 --- a/src/language-markdown/print-preprocess.js +++ b/src/language-markdown/print-preprocess.js @@ -39,25 +39,21 @@ function transformInlineCode(ast) { } function restoreUnescapedCharacter(ast, options) { - return mapAst(ast, (node) => { - return node.type !== "text" + return mapAst(ast, (node) => + node.type !== "text" || + node.value === "*" || + node.value === "_" || // handle these cases in printer + !isSingleCharRegex.test(node.value) || + node.position.end.offset - node.position.start.offset === node.value.length ? node : { ...node, - value: - node.value !== "*" && - node.value !== "_" && - node.value !== "$" && // handle these cases in printer - isSingleCharRegex.test(node.value) && - node.position.end.offset - node.position.start.offset !== - node.value.length - ? options.originalText.slice( - node.position.start.offset, - node.position.end.offset - ) - : node.value, - }; - }); + value: options.originalText.slice( + node.position.start.offset, + node.position.end.offset + ), + } + ); } function mergeContinuousImportExport(ast) { diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index 17a6373a0..5ea2257b8 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -94,7 +94,7 @@ function genericPrint(path, options, print) { return printChildren(path, options, print); case "word": { let escapedValue = node.value - .replace(/[$*]/g, "\\$&") // escape all `*` and `$` (math) + .replace(/\*/g, "\\$&") // escape all `*` .replace( new RegExp( [ diff --git a/tests/markdown/math/__snapshots__/jsfmt.spec.js.snap b/tests/markdown/math/__snapshots__/jsfmt.spec.js.snap index 04f8107b4..a7f91efd7 100644 --- a/tests/markdown/math/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown/math/__snapshots__/jsfmt.spec.js.snap @@ -15,11 +15,11 @@ $ \\\\\\$ =====================================output===================================== -\\$ +$ \\$ -\\\\\\$ +\\\\$ \\\\\\$ @@ -50,9 +50,13 @@ printWidth: 80 =====================================input====================================== $10 - $20 +Paragraph with $14 million. But if more $dollars on the same line... + =====================================output===================================== $10 - $20 +Paragraph with $14 million. But if more $dollars on the same line... + ================================================================================ `; @@ -136,7 +140,7 @@ $$ $$ must \\alpha -$$ be ignored +$$ --- @@ -156,7 +160,7 @@ $$ --- -\\$\\alpha\\$ +$\\alpha\\$ --- @@ -168,11 +172,11 @@ $$ --- -\`$\`\\alpha\\$ +\`$\`\\alpha$ --- -\\$\\alpha\`$\` foo +$\\alpha\`$\` foo --- @@ -223,6 +227,7 @@ $$ --- $$ +must \\alpha $$ diff --git a/tests/markdown/math/math-like.md b/tests/markdown/math/math-like.md index 832901386..95e606e37 100644 --- a/tests/markdown/math/math-like.md +++ b/tests/markdown/math/math-like.md @@ -1 +1,3 @@ $10 - $20 + +Paragraph with $14 million. But if more $dollars on the same line... diff --git a/tests/markdown/math/remark-math.md b/tests/markdown/math/remark-math.md index 1c1a6ca92..dbf1828d8 100644 --- a/tests/markdown/math/remark-math.md +++ b/tests/markdown/math/remark-math.md @@ -72,7 +72,7 @@ $$ $$ must \alpha -$$ be ignored +$$ --- diff --git a/tests/markdown/spec/__snapshots__/jsfmt.spec.js.snap b/tests/markdown/spec/__snapshots__/jsfmt.spec.js.snap index 5af5a7b02..4be0bcab9 100644 --- a/tests/markdown/spec/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown/spec/__snapshots__/jsfmt.spec.js.snap @@ -10750,7 +10750,7 @@ proseWrap: "always" hello $.;'there =====================================output===================================== -hello \\$.;'there +hello $.;'there ================================================================================ `; diff --git a/yarn.lock b/yarn.lock index 3d6ad5d4a..a9259cae6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6202,12 +6202,10 @@ remark-footnotes@2.0.0: resolved "https://registry.yarnpkg.com/remark-footnotes/-/remark-footnotes-2.0.0.tgz#9001c4c2ffebba55695d2dd80ffb8b82f7e6303f" integrity sha512-3Clt8ZMH75Ayjp9q4CorNeyjwIxHFcTkaektplKGl2A1jNGEUey8cKL0ZC5vJwfcD5GFGsNLImLG/NGzWIzoMQ== -remark-math@1.0.6: - version "1.0.6" - resolved "https://registry.yarnpkg.com/remark-math/-/remark-math-1.0.6.tgz#49eb3dd15d298734c9ae21673115389793af4d1b" - integrity sha512-I43wU/QOQpXvVFXKjA4FHp5xptK65+5F6yolm8+69/JV0EqSOB64wURUZ3JK50JtnTL8FvwLiH2PZ+fvsBxviA== - dependencies: - trim-trailing-lines "^1.1.0" +remark-math@3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/remark-math/-/remark-math-3.0.1.tgz#85a02a15b15cad34b89a27244d4887b3a95185bb" + integrity sha512-epT77R/HK0x7NqrWHdSV75uNLwn8g9qTyMqCRCDujL0vj/6T6+yhdrR7mjELWtkse+Fw02kijAaBuVcHBor1+Q== remark-parse@8.0.3: version "8.0.3" @@ -7083,7 +7081,7 @@ tr46@^2.0.2: dependencies: punycode "^2.1.1" -trim-trailing-lines@^1.0.0, trim-trailing-lines@^1.1.0: +trim-trailing-lines@^1.0.0: version "1.1.3" resolved "https://registry.yarnpkg.com/trim-trailing-lines/-/trim-trailing-lines-1.1.3.tgz#7f0739881ff76657b7776e10874128004b625a94" integrity sha512-4ku0mmjXifQcTVfYDfR5lpgV7zVqPg6zV9rdZmwOPqq0+Zq19xDqEgagqVbc4pOOShbncuAOIs59R3+3gcF3ZA==