From 4ae52c5aa6e60b4819554c2fa1f6411bdeb55f7e Mon Sep 17 00:00:00 2001 From: Ika Date: Sun, 12 Nov 2017 00:29:59 +0800 Subject: [PATCH] fix(markdown): do not enable splitText in inlineCode (#3243) --- src/parser-markdown.js | 36 +++------------- src/printer-markdown.js | 41 ++++++++----------- .../__snapshots__/jsfmt.spec.js.snap | 7 ++++ tests/markdown_inlineCode/cjk.md | 1 + 4 files changed, 30 insertions(+), 55 deletions(-) create mode 100644 tests/markdown_inlineCode/cjk.md diff --git a/src/parser-markdown.js b/src/parser-markdown.js index ad4d9662..18255884 100644 --- a/src/parser-markdown.js +++ b/src/parser-markdown.js @@ -10,7 +10,8 @@ const util = require("./util"); * * 1. restore unescaped character (Text) * 2. merge continuous Texts - * 3. transform InlineCode#value into InlineCode#children (Text) + * 3. replace whitespaces in InlineCode#value with one whitespace + * reference: http://spec.commonmark.org/0.25/#example-605 * 4. split Text into Sentence * * interface Word { value: string } @@ -24,7 +25,7 @@ function parse(text /*, parsers, opts*/) { .use(remarkFrontmatter, ["yaml"]) .use(restoreUnescapedCharacter(text)) .use(mergeContinuousTexts) - .use(transformInlineCode(text)) + .use(transformInlineCode) .use(splitText); return processor.runSync(processor.parse(text)); } @@ -41,40 +42,15 @@ function map(ast, handler) { })(ast, null, null); } -function transformInlineCode(originalText) { - return () => ast => +function transformInlineCode() { + return ast => map(ast, node => { if (node.type !== "inlineCode") { return node; } - const rawContent = originalText.slice( - node.position.start.offset, - node.position.end.offset - ); - - const style = rawContent.match(/^`+/)[0]; - return Object.assign({}, node, { - value: node.value.replace(/\s+/g, " "), - children: [ - { - type: "text", - value: node.value, - position: { - start: { - line: node.position.start.line, - column: node.position.start.column + style.length, - offset: node.position.start.offset + style.length - }, - end: { - line: node.position.end.line, - column: node.position.end.column - style.length, - offset: node.position.end.offset - style.length - } - } - } - ] + value: node.value.replace(/\s+/g, " ") }); }); } diff --git a/src/printer-markdown.js b/src/printer-markdown.js index 960f1780..66e44145 100644 --- a/src/printer-markdown.js +++ b/src/printer-markdown.js @@ -28,8 +28,7 @@ const SINGLE_LINE_NODE_TYPES = [ "heading", "tableCell", "footnoteDefinition", - "link", - "inlineCode" + "link" ]; const SIBLING_NODE_TYPES = ["listItem", "definition", "footnoteDefinition"]; @@ -89,21 +88,19 @@ function genericPrint(path, options, print) { case "sentence": return printChildren(path, options, print); case "word": - return getAncestorNode(path, "inlineCode") - ? node.value - : node.value - .replace(/[*]/g, "\\*") // escape all `*` - .replace( - new RegExp( - `(^|${punctuationPattern})(_+)|(_+)(${punctuationPattern}|$)`, - "g" - ), - (_, text1, underscore1, underscore2, text2) => - (underscore1 - ? `${text1}${underscore1}` - : `${underscore2}${text2}` - ).replace(/_/g, "\\_") - ); // escape all `_` except concating with non-punctuation, e.g. `1_2_3` is not considered emphasis + return node.value + .replace(/[*]/g, "\\*") // escape all `*` + .replace( + new RegExp( + `(^|${punctuationPattern})(_+)|(_+)(${punctuationPattern}|$)`, + "g" + ), + (_, text1, underscore1, underscore2, text2) => + (underscore1 + ? `${text1}${underscore1}` + : `${underscore2}${text2}` + ).replace(/_/g, "\\_") + ); // escape all `_` except concating with non-punctuation, e.g. `1_2_3` is not considered emphasis case "whitespace": { const parentNode = path.getParentNode(); const index = parentNode.children.indexOf(node); @@ -147,14 +144,8 @@ function genericPrint(path, options, print) { case "inlineCode": { const backtickCount = util.getMaxContinuousCount(node.value, "`"); const style = backtickCount === 1 ? "``" : "`"; - const gap = backtickCount ? printLine(path, line, options) : ""; - return concat([ - style, - gap, - printChildren(path, options, print), - gap, - style - ]); + const gap = backtickCount ? " " : ""; + return concat([style, gap, node.value, gap, style]); } case "link": switch (options.originalText[node.position.start.offset]) { diff --git a/tests/markdown_inlineCode/__snapshots__/jsfmt.spec.js.snap b/tests/markdown_inlineCode/__snapshots__/jsfmt.spec.js.snap index 0c667dca..29162453 100644 --- a/tests/markdown_inlineCode/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown_inlineCode/__snapshots__/jsfmt.spec.js.snap @@ -23,6 +23,13 @@ exports[`backtick.md 1`] = ` `; +exports[`cjk.md 1`] = ` +\`const x = "中文123"\` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\`const x = "中文123"\` + +`; + exports[`escape.md 1`] = ` \`1*2*3\` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/markdown_inlineCode/cjk.md b/tests/markdown_inlineCode/cjk.md new file mode 100644 index 00000000..83ed7221 --- /dev/null +++ b/tests/markdown_inlineCode/cjk.md @@ -0,0 +1 @@ +`const x = "中文123"`