feat(markdown): preserve math (#5050)

- preserve inlineMath (`$inline$`) and blockMath:

  ```md
  $$
  block
  $$
  ```
- side effect: `$` always needs to be escaped.
master
Ika 2018-09-05 21:07:37 +08:00 committed by GitHub
parent 7fd346ffa1
commit a1545a835a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 303 additions and 5 deletions

View File

@ -54,6 +54,7 @@
"postcss-selector-parser": "2.2.3",
"postcss-values-parser": "1.5.0",
"regexp-util": "1.2.2",
"remark-math": "1.0.4",
"remark-parse": "5.0.0",
"resolve": "1.5.0",
"semver": "5.4.1",

View File

@ -6,6 +6,7 @@ const pragma = require("./pragma");
const parseFrontMatter = require("../utils/front-matter");
const { mapAst } = require("./utils");
const mdx = require("./mdx");
const remarkMath = require("remark-math");
/**
* based on [MDAST](https://github.com/syntax-tree/mdast) with following modifications:
@ -35,6 +36,7 @@ function createParse({ isMDX }) {
)
)
.use(frontMatter)
.use(remarkMath)
.use(isMDX ? mdx.esSyntax : identity)
.use(liquid)
.use(isMDX ? htmlToJsx : identity);

View File

@ -49,7 +49,8 @@ const INLINE_NODE_TYPES = [
"sentence",
"whitespace",
"word",
"break"
"break",
"inlineMath"
];
const INLINE_NODE_WRAPPER_TYPES = INLINE_NODE_TYPES.concat([
@ -99,7 +100,7 @@ function genericPrint(path, options, print) {
return printChildren(path, options, print);
case "word":
return node.value
.replace(/[*]/g, "\\*") // escape all `*`
.replace(/[*$]/g, "\\$&") // escape all `*` and `$` (math)
.replace(
new RegExp(
[
@ -399,6 +400,17 @@ function genericPrint(path, options, print) {
case "importExport":
case "jsx":
return node.value; // fallback to the original text if multiparser failed
case "math":
return concat([
"$$",
hardline,
node.value
? concat([replaceNewlinesWith(node.value, hardline), hardline])
: "",
"$$"
]);
case "inlineMath":
return concat(["$", node.value, "$"]);
case "tableRow": // handled in "table"
case "listItem": // handled in "list"

View File

@ -0,0 +1,186 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`empty-block.md - markdown-verify 1`] = `
$$
$$
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$$
$$
`;
exports[`remark-math.md - markdown-verify 1`] = `
<!-- tests from https://github.com/Rokt33r/remark-math/blob/9e13e49/specs/remark-math.spec.js -->
$$
\\beta+\\gamma
$$
---
$\\alpha\\$
---
\\$\\alpha\\$
---
\\\\$\\alpha$
---
\`$\`\\alpha$
---
$\\alpha\`$\` foo
---
$\`\\alpha\`$
---
$\\alpha\\$$
---
$$
\\alpha\\$
$$
---
tango
$$
\\alpha
$$
---
$$\\\\alpha$$
---
$$\\alpha$$
$$
\\alpha\\beta
$$
---
> $$
> \\alpha\\beta
> $$
---
$$$
\\alpha
$$$
---
$$ must
\\alpha
$$ be ignored
---
$$
\\alpha
$$
\`\`\`
code fence
\`\`\`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!-- tests from https://github.com/Rokt33r/remark-math/blob/9e13e49/specs/remark-math.spec.js -->
$$
\\beta+\\gamma
$$
---
\\$\\alpha\\$
---
\\\\$\\alpha\\$
---
\\\\$\\alpha$
---
\`$\`\\alpha\\$
---
\\$\\alpha\`$\` foo
---
$\`\\alpha\`$
---
$\\alpha\\$$
---
$$
\\alpha\\$
$$
---
tango
$$
\\alpha
$$
---
$\\\\alpha$
---
$\\alpha$
$$
\\alpha\\beta
$$
---
> $$
> \\alpha\\beta
> $$
---
$$
\\alpha
$$
---
$$
\\alpha
$$
---
$$
\\alpha
$$
\`\`\`
code fence
\`\`\`
`;

View File

@ -0,0 +1,2 @@
$$
$$

View File

@ -0,0 +1 @@
run_spec(__dirname, ["markdown"]);

View File

@ -0,0 +1,84 @@
<!-- tests from https://github.com/Rokt33r/remark-math/blob/9e13e49/specs/remark-math.spec.js -->
$$
\beta+\gamma
$$
---
$\alpha\$
---
\$\alpha\$
---
\\$\alpha$
---
`$`\alpha$
---
$\alpha`$` foo
---
$`\alpha`$
---
$\alpha\$$
---
$$
\alpha\$
$$
---
tango
$$
\alpha
$$
---
$$\\alpha$$
---
$$\alpha$$
$$
\alpha\beta
$$
---
> $$
> \alpha\beta
> $$
---
$$$
\alpha
$$$
---
$$ must
\alpha
$$ be ignored
---
$$
\alpha
$$
```
code fence
```

View File

@ -3306,7 +3306,7 @@ exports[`example-284.md - markdown-verify 1`] = `
exports[`example-285.md - markdown-verify 1`] = `
\\!\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\\`\\{\\|\\}\\~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\\!\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\\`\\{\\|\\}\\~
\\!\\"\\#\\\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\\`\\{\\|\\}\\~
`;
@ -5914,7 +5914,7 @@ foo baz
exports[`example-614.md - markdown-verify 1`] = `
hello $.;'there
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hello $.;'there
hello \\$.;'there
`;

View File

@ -92,7 +92,7 @@ markdown\`
- \\\`
- \\\\\\\`
- \\\\\\\\
- \\$
- \\\\$
- \\u1234
\`;

View File

@ -4856,6 +4856,12 @@ regjsparser@^0.3.0:
dependencies:
jsesc "~0.5.0"
remark-math@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/remark-math/-/remark-math-1.0.4.tgz#ced46473075ff99e4678a154a1a3d0dde403a9f2"
dependencies:
trim-trailing-lines "^1.1.0"
remark-parse@5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/remark-parse/-/remark-parse-5.0.0.tgz#4c077f9e499044d1d5c13f80d7a98cf7b9285d95"
@ -5662,6 +5668,10 @@ trim-trailing-lines@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/trim-trailing-lines/-/trim-trailing-lines-1.1.0.tgz#7aefbb7808df9d669f6da2e438cac8c46ada7684"
trim-trailing-lines@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/trim-trailing-lines/-/trim-trailing-lines-1.1.1.tgz#e0ec0810fd3c3f1730516b45f49083caaf2774d9"
trim@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/trim/-/trim-0.0.1.tgz#5858547f6b290757ee95cccc666fb50084c460dd"