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) * 1. restore unescaped character (Text)
* 2. merge continuous Texts * 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 * 4. split Text into Sentence
* *
* interface Word { value: string } * interface Word { value: string }
@ -24,7 +25,7 @@ function parse(text /*, parsers, opts*/) {
.use(remarkFrontmatter, ["yaml"]) .use(remarkFrontmatter, ["yaml"])
.use(restoreUnescapedCharacter(text)) .use(restoreUnescapedCharacter(text))
.use(mergeContinuousTexts) .use(mergeContinuousTexts)
.use(transformInlineCode(text)) .use(transformInlineCode)
.use(splitText); .use(splitText);
return processor.runSync(processor.parse(text)); return processor.runSync(processor.parse(text));
} }
@ -41,40 +42,15 @@ function map(ast, handler) {
})(ast, null, null); })(ast, null, null);
} }
function transformInlineCode(originalText) { function transformInlineCode() {
return () => ast => return ast =>
map(ast, node => { map(ast, node => {
if (node.type !== "inlineCode") { if (node.type !== "inlineCode") {
return node; return node;
} }
const rawContent = originalText.slice(
node.position.start.offset,
node.position.end.offset
);
const style = rawContent.match(/^`+/)[0];
return Object.assign({}, node, { return Object.assign({}, node, {
value: node.value.replace(/\s+/g, " "), 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
}
}
}
]
}); });
}); });
} }

View File

@ -28,8 +28,7 @@ const SINGLE_LINE_NODE_TYPES = [
"heading", "heading",
"tableCell", "tableCell",
"footnoteDefinition", "footnoteDefinition",
"link", "link"
"inlineCode"
]; ];
const SIBLING_NODE_TYPES = ["listItem", "definition", "footnoteDefinition"]; const SIBLING_NODE_TYPES = ["listItem", "definition", "footnoteDefinition"];
@ -89,21 +88,19 @@ function genericPrint(path, options, print) {
case "sentence": case "sentence":
return printChildren(path, options, print); return printChildren(path, options, print);
case "word": case "word":
return getAncestorNode(path, "inlineCode") return node.value
? node.value .replace(/[*]/g, "\\*") // escape all `*`
: node.value .replace(
.replace(/[*]/g, "\\*") // escape all `*` new RegExp(
.replace( `(^|${punctuationPattern})(_+)|(_+)(${punctuationPattern}|$)`,
new RegExp( "g"
`(^|${punctuationPattern})(_+)|(_+)(${punctuationPattern}|$)`, ),
"g" (_, text1, underscore1, underscore2, text2) =>
), (underscore1
(_, text1, underscore1, underscore2, text2) => ? `${text1}${underscore1}`
(underscore1 : `${underscore2}${text2}`
? `${text1}${underscore1}` ).replace(/_/g, "\\_")
: `${underscore2}${text2}` ); // escape all `_` except concating with non-punctuation, e.g. `1_2_3` is not considered emphasis
).replace(/_/g, "\\_")
); // escape all `_` except concating with non-punctuation, e.g. `1_2_3` is not considered emphasis
case "whitespace": { case "whitespace": {
const parentNode = path.getParentNode(); const parentNode = path.getParentNode();
const index = parentNode.children.indexOf(node); const index = parentNode.children.indexOf(node);
@ -147,14 +144,8 @@ function genericPrint(path, options, print) {
case "inlineCode": { case "inlineCode": {
const backtickCount = util.getMaxContinuousCount(node.value, "`"); const backtickCount = util.getMaxContinuousCount(node.value, "`");
const style = backtickCount === 1 ? "``" : "`"; const style = backtickCount === 1 ? "``" : "`";
const gap = backtickCount ? printLine(path, line, options) : ""; const gap = backtickCount ? " " : "";
return concat([ return concat([style, gap, node.value, gap, style]);
style,
gap,
printChildren(path, options, print),
gap,
style
]);
} }
case "link": case "link":
switch (options.originalText[node.position.start.offset]) { 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`] = ` exports[`escape.md 1`] = `
\`1*2*3\` \`1*2*3\`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

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