fix(markdown): do not enable splitText in inlineCode (#3243)

master
Ika 2017-11-12 00:29:59 +08:00 committed by GitHub
parent 1cde865a11
commit 4ae52c5aa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 55 deletions

View File

@ -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, " ")
});
});
}

View File

@ -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]) {

View File

@ -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\`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -0,0 +1 @@
`const x = "中文123"`