fix(markdown): no extra whitespace between ascii punc and CJK char (#3249)

* fix(markdown): no extra whitespace between ascii punctuation and CJK character

* test: update snapshot
This commit is contained in:
Ika authored and GitHub committed 2017-11-12 00:38:18 +08:00
1 parent 4ae52c5aa6
commit bddbd1914d
5 files changed
+26 -8

No files matched your search

+1 -6
View File
@@ -11,14 +11,9 @@ const fill = docBuilders.fill;
const align = docBuilders.align;
const docPrinter = require("./doc-printer");
const printDocToString = docPrinter.printDocToString;
const escapeStringRegexp = require("escape-string-regexp");
const asciiPunctuationPattern = util.asciiPunctuationPattern;
const getCjkRegex = require("cjk-regex");
// http://spec.commonmark.org/0.25/#ascii-punctuation-character
const asciiPunctuationPattern = escapeStringRegexp(
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
);
const punctuationPattern = `(?:${[
getCjkRegex.punctuations().source,
`[${asciiPunctuationPattern}]`
+14 -1
View File
@@ -11,6 +11,11 @@ const cjkRegex = getCjkRegex();
// https://stackoverflow.com/a/21373261
const cjkPunctuationRegex = new RegExp(getCjkRegex.punctuations().source, "");
// http://spec.commonmark.org/0.25/#ascii-punctuation-character
const asciiPunctuationPattern = escapeStringRegexp(
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
);
function isExportDeclaration(node) {
if (node) {
switch (node.type) {
@@ -721,7 +726,14 @@ function splitText(text) {
function appendNode(node) {
const lastNode = nodes[nodes.length - 1];
if (lastNode && lastNode.type === "word") {
if (isBetween(KIND_NON_CJK, KIND_CJK_CHARACTER)) {
if (
(lastNode.kind === KIND_NON_CJK &&
node.kind === KIND_CJK_CHARACTER &&
!new RegExp(`[${asciiPunctuationPattern}]$`).test(lastNode.value)) ||
(lastNode.kind === KIND_CJK_CHARACTER &&
node.kind === KIND_NON_CJK &&
!new RegExp(`^[${asciiPunctuationPattern}]`).test(node.value))
) {
nodes.push({ type: "whitespace", value: " " });
} else if (
!isBetween(KIND_NON_CJK, KIND_CJK_PUNCTUATION) &&
@@ -754,6 +766,7 @@ function getStringWidth(text) {
}
module.exports = {
asciiPunctuationPattern,
getStringWidth,
splitText,
mapDoc,
@@ -7,6 +7,8 @@ exports[`cjk.md 1`] = `
全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白
This ia an english paragraph with a CJK quote "中文".
扩展运算符(spread)是三个点(\`...\`)。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
這是一段很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長
@@ -26,6 +28,8 @@ English 混合著中文的一段 Paragraph!
全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白
全  形 空白全  形 空白全  形 空白
This ia an english paragraph with a CJK quote "中文".
扩展运算符(spread)是三个点(\`...\`)。
`;
@@ -37,6 +41,8 @@ exports[`cjk.md 2`] = `
全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白
This ia an english paragraph with a CJK quote "中文".
扩展运算符(spread)是三个点(\`...\`)。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
這是一段很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長很長的段落
@@ -45,6 +51,8 @@ exports[`cjk.md 2`] = `
全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白
This ia an english paragraph with a CJK quote "中文".
扩展运算符(spread)是三个点(\`...\`)。
`;
+2
View File
@@ -4,4 +4,6 @@
全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白全  形 空白
This ia an english paragraph with a CJK quote "中文".
扩展运算符(spread)是三个点(`...`)。
@@ -35,6 +35,6 @@ asd &amp; asd &#132; 123
123 _ 123 _ 123
## 类的 prototype 属性和 \\_\\_proto\\_\\_ 属性
## 类的 prototype 属性和\\_\\_proto\\_\\_属性
`;