diff --git a/src/printer-markdown.js b/src/printer-markdown.js index d9bcee338..4a6cfe3a6 100644 --- a/src/printer-markdown.js +++ b/src/printer-markdown.js @@ -11,6 +11,12 @@ const fill = docBuilders.fill; const align = docBuilders.align; const docPrinter = require("./doc-printer"); const printDocToString = docPrinter.printDocToString; +const escapeStringRegexp = require("escape-string-regexp"); + +// http://spec.commonmark.org/0.25/#ascii-punctuation-character +const asciiPunctuationPattern = escapeStringRegexp( + "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" +); const SINGLE_LINE_NODE_TYPES = ["heading", "tableCell", "footnoteDefinition"]; @@ -97,11 +103,17 @@ function genericPrint(path, options, print) { (prevNode && prevNode.type === "sentence" && prevNode.children.length > 0 && - prevNode.children[prevNode.children.length - 1].type === "word") || + prevNode.children[prevNode.children.length - 1].type === "word" && + prevNode.children[prevNode.children.length - 1].value.match( + new RegExp(`[^${asciiPunctuationPattern}]$`) + )) || (nextNode && nextNode.type === "sentence" && nextNode.children.length > 0 && - nextNode.children[0].type === "word"); + nextNode.children[0].type === "word" && + nextNode.children[0].value.match( + new RegExp(`^[^${asciiPunctuationPattern}]`) + )); const style = hasPrevOrNextWord || getAncestorNode(path, "emphasis") ? "*" : "_"; return concat([style, printChildren(path, options, print), style]); diff --git a/tests/markdown_emphasis/__snapshots__/jsfmt.spec.js.snap b/tests/markdown_emphasis/__snapshots__/jsfmt.spec.js.snap index 4047a530b..552365365 100644 --- a/tests/markdown_emphasis/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown_emphasis/__snapshots__/jsfmt.spec.js.snap @@ -7,6 +7,17 @@ _123_ `; +exports[`complex.md 1`] = ` +**Do you want to request a *feature* or report a *bug*?** + +*bug*? +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +**Do you want to request a _feature_ or report a _bug_?** + +_bug_? + +`; + exports[`underscore.md 1`] = ` _123_ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/markdown_emphasis/complex.md b/tests/markdown_emphasis/complex.md new file mode 100644 index 000000000..0ff470e36 --- /dev/null +++ b/tests/markdown_emphasis/complex.md @@ -0,0 +1,3 @@ +**Do you want to request a *feature* or report a *bug*?** + +*bug*? diff --git a/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap b/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap index 0abe52677..2336f30f1 100644 --- a/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap @@ -3333,7 +3333,7 @@ list \\* not a list \\# not a heading \\[foo]: /url "not a reference" exports[`example-288.md 1`] = ` \\\\*emphasis* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -\\\\*emphasis* +\\\\_emphasis_ `; @@ -3565,7 +3565,7 @@ exports[`example-316.md 1`] = ` exports[`example-317.md 1`] = ` *foo\`*\` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*foo\`*\` +_foo\`_\` `; @@ -3705,7 +3705,7 @@ aa_"bb"_cc exports[`example-338.md 1`] = ` foo-_(bar)_ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -foo-*(bar)* +foo-_(bar)_ `; @@ -3769,7 +3769,7 @@ _(_foo) exports[`example-347.md 1`] = ` _(_foo_)_ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*(_foo*)_ +_(_foo_)_ `; @@ -3797,7 +3797,7 @@ _foo_bar_baz_ exports[`example-351.md 1`] = ` _(bar)_. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*(bar)*. +_(bar)_. `; @@ -3918,14 +3918,14 @@ exports[`example-368.md 1`] = ` **Gomphocarpus (*Gomphocarpus physocarpus*, syn. *Asclepias physocarpa*)** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -**Gomphocarpus (*Gomphocarpus physocarpus*, syn. *Asclepias physocarpa*)** +**Gomphocarpus (_Gomphocarpus physocarpus_, syn. _Asclepias physocarpa_)** `; exports[`example-369.md 1`] = ` **foo "*bar*" foo** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -**foo "*bar*" foo** +**foo "_bar_" foo** `; @@ -4017,7 +4017,7 @@ _foo _bar_ baz_ exports[`example-382.md 1`] = ` __foo_ bar_ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -\\_*foo* bar_ +\\__foo_ bar_ `; @@ -4222,7 +4222,7 @@ foo **\\_** exports[`example-413.md 1`] = ` **foo* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -\\**foo* +\\*_foo_ `; @@ -4299,7 +4299,7 @@ foo **\\*** exports[`example-425.md 1`] = ` __foo_ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -\\_*foo* +\\__foo_ `; @@ -4320,7 +4320,7 @@ ___foo__ exports[`example-428.md 1`] = ` ____foo_ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -\\_*__foo* +\\____foo_ `; @@ -4439,21 +4439,21 @@ exports[`example-444.md 1`] = ` exports[`example-445.md 1`] = ` *[bar*](/url) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*[bar*](/url) +_[bar_](/url) `; exports[`example-446.md 1`] = ` _foo [bar_](/url) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*foo [bar*](/url) +_foo [bar_](/url) `; exports[`example-447.md 1`] = ` * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -* +_ `; @@ -4474,14 +4474,14 @@ __ exports[`example-450.md 1`] = ` *a \`*\`* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*a \`*\`\\* +_a \`_\`\\* `; exports[`example-451.md 1`] = ` _a \`_\`_ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*a \`*\`_ +_a \`_\`_ `; @@ -4747,7 +4747,7 @@ exports[`example-486.md 1`] = ` exports[`example-487.md 1`] = ` *[foo*](/uri) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*[foo*](/uri) +_[foo_](/uri) `; @@ -4868,7 +4868,7 @@ exports[`example-500.md 1`] = ` [ref]: /uri ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*[foo*][ref] +_[foo_][ref] [ref]: /uri @@ -5208,7 +5208,7 @@ exports[`example-530.md 1`] = ` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [foo*]: /url -*[foo*] +_[foo_] `;