fix(markdown): do not enable splitText in inlineCode (#3243)
parent
1cde865a11
commit
4ae52c5aa6
|
@ -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, " ")
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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]) {
|
||||
|
|
|
@ -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\`
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
`const x = "中文123"`
|
Loading…
Reference in New Issue