diff --git a/aio/tools/translator/utils.spec.ts b/aio/tools/translator/utils.spec.ts index e85963f999..3811392c4f 100644 --- a/aio/tools/translator/utils.spec.ts +++ b/aio/tools/translator/utils.spec.ts @@ -100,7 +100,7 @@ def - line + line @@ -138,6 +138,19 @@ def `); }); + it('拆解 code-example', function () { + const lines = normalizeLines(` + +ng generate directive highlight + +`); + expect(lines).eql(` + + +ng generate directive highlight +`); + }); + it('拆解 @a 标记', function () { const lines = normalizeLines(` a @@ -177,13 +190,13 @@ def it('拆解多行的成对 tag', function () { const lines = normalizeLines(`
- Angular forms don't require a style library + Angular forms don't require a style library
`); expect(lines).eq(`
- Angular forms don't require a style library + Angular forms don't require a style library
`); @@ -192,7 +205,7 @@ def it('拆解多行的成对 tag', function () { const lines = normalizeLines(`

- a + a

@@ -203,7 +216,7 @@ def expect(lines).eq(`

- a + a

@@ -241,19 +254,19 @@ def
  • -a + a
  • -b + b
  • -c + c
  • diff --git a/aio/tools/translator/utils.ts b/aio/tools/translator/utils.ts index dd3b11446f..2257955520 100644 --- a/aio/tools/translator/utils.ts +++ b/aio/tools/translator/utils.ts @@ -50,34 +50,35 @@ export function normalizeLines(text: string): string { // 列表、标题等自带换行含义的markdown const blockElementPattern = /(?=\n *(\d+\.|-|\*) )\n/g; text = text.replace(blockElementPattern, '\n\n'); - const hxPattern = /(\n *#.*)\n/g; - text = text.replace(hxPattern, '\n$1\n\n'); - const leadHxPattern = /^( *#.*)\n/g; - text = text.replace(leadHxPattern, '$1\n\n'); - const oneLinePairedTagPattern = /\n( *)<(p|code-example|div|h\d+|a)( ?[^> \n]*)>([^<\n]*)<\/\2>( *)\n/g; - text = text.replace(oneLinePairedTagPattern, '\n\n$1<$2$3>$4$5\n\n'); - const oneLineThTdTagPattern = /\n( *)<(th|td|li)( ?[^> \n]*)>(.*)<\/\2>( *)/g; - text = text.replace(oneLineThTdTagPattern, '\n\n$1<$2$3>\n\n$1 $4\n\n$1$5\n\n'); - const oneLineCommentPattern = /\n( *)()( *)\n/g; - text = text.replace(oneLineCommentPattern, '\n\n$1$2$3\n\n'); - const oneLineBrTagPattern = /\n( *)(
    )( *)\n/g; - text = text.replace(oneLineBrTagPattern, '\n\n$1$2$3\n\n'); - const atTagCommentPattern = /\n( *)({@a.*})( *)\n/g; - text = text.replace(atTagCommentPattern, '\n\n$1$2$3\n\n'); - const oneLineClosedTagPattern = /\n( *)<(hr|p)(\/?)>( *)\n/g; - text = text.replace(oneLineClosedTagPattern, '\n\n$1<$2$3>$4\n\n'); - const multiLinePairedTagPattern = /\n( *)<(header|p)( *[^> \n]*)>\n*(.*?)\n*( *)<\/\2>( *)\n/g; - text = text.replace(multiLinePairedTagPattern, '\n\n$1<$2$3>\n\n$1 $4\n\n$1$6\n\n'); + const hxPattern = /(\n *# .*)(?=\n)/g; + text = text.replace(hxPattern, '\n$1\n'); + const oneLinePairedTagPattern = /\n( *)<(p|div|h\d+|a|code-example)( ?[^>\n]*)>([^<\n]*)<\/\2>( *)(?=\n)/g; + text = text.replace(oneLinePairedTagPattern, '\n\n$1<$2$3>$4$5\n'); + const oneLineThTdTagPattern = /\n( *)<(th|td|li)( ?[^>\n]*)>(.*)<\/\2>( *)(?=\n)/g; + text = text.replace(oneLineThTdTagPattern, '\n\n$1<$2$3>\n\n$1 $4\n\n$1$5\n'); + const oneLineCommentPattern = /\n( *)()( *)(?=\n)/g; + text = text.replace(oneLineCommentPattern, '\n\n$1$2$3\n'); + const oneLineBrTagPattern = /\n( *)(
    )( *)(?=\n)/g; + text = text.replace(oneLineBrTagPattern, '\n\n$1$2$3\n'); + const atTagCommentPattern = /\n( *)({@a.*})( *)(?=\n)/g; + text = text.replace(atTagCommentPattern, '\n\n$1$2$3\n'); + const oneLineClosedTagPattern = /\n( *)<(hr|p)(\/?)>( *)(?=\n)/g; + text = text.replace(oneLineClosedTagPattern, '\n\n$1<$2$3>$4\n'); + const multiLinePairedTagPattern = /\n( *)<(header|p)( *[^>\n]*)>\n*(.*?)\n*( *)<\/\2>( *)(?=\n)/g; + text = text.replace(multiLinePairedTagPattern, '\n\n$1<$2$3>\n\n$4\n\n$5$6\n'); - const blockTagPattern = /\n( *)<(\/?)(td|th|div)( *[^> \n]*)>( *)\n/g; - text = text.replace(blockTagPattern, '\n\n$1<$2$3$4>$5\n\n'); + const blockTagPattern = /\n( *)<(\/?)(td|th|div|code-example)( *[^>\n]*)>( *)(?=\n)/g; + text = text.replace(blockTagPattern, '\n\n$1<$2$3$4>$5\n'); - const multiLineCodePattern = /\n( *)```(\w*)( *)\n/g; - text = text.replace(multiLineCodePattern, '\n\n$1```$2$3\n\n'); + const multiLineCodePattern = /\n( *)```(\w*)( *)(?=\n)/g; + text = text.replace(multiLineCodePattern, '\n\n$1```$2$3\n'); const multipleBlankLinePattern = /\n\s*\n+/g; text = text.replace(multipleBlankLinePattern, '\n\n'); - return text.replace(/^\n+/, '\n').replace(/\n+$/, '\n'); + + text = text.replace(/^\n+/, '\n').replace(/\n+$/, '\n'); + + return text; } export function indentOf(line): number {