diff --git a/aio/tools/translator/bin/translate-one.ts b/aio/tools/translator/bin/translate-one.ts index 0feb105d71..383a76a20b 100644 --- a/aio/tools/translator/bin/translate-one.ts +++ b/aio/tools/translator/bin/translate-one.ts @@ -3,5 +3,5 @@ import { dirs } from '../dirs'; import { translateFile } from '../translate'; -const filename = 'ajs-quick-reference.md'; +const filename = 'api-page-class.md'; translateFile(__dirname + '/../../../../../content-en/' + 'guide/' + filename, dirs.content + 'guide/' + filename); diff --git a/aio/tools/translator/translate.ts b/aio/tools/translator/translate.ts index e5a427ceb7..1b87ce13e9 100644 --- a/aio/tools/translator/translate.ts +++ b/aio/tools/translator/translate.ts @@ -45,7 +45,7 @@ export function translate(content: string): string[] { export function translateFile(sourceFile: string, targetFile: string): void { const content = fs.readFileSync(sourceFile, 'utf-8'); const result = translate(content); - fs.writeFileSync(targetFile, result.join('\n\n'), 'utf-8'); + fs.writeFileSync(targetFile, result.join('\n\n').trim() + '\n', 'utf-8'); } export function translateDirectory(sourceDir: string, targetDir: string): void { diff --git a/aio/tools/translator/utils.spec.ts b/aio/tools/translator/utils.spec.ts index 927f1b207a..e85963f999 100644 --- a/aio/tools/translator/utils.spec.ts +++ b/aio/tools/translator/utils.spec.ts @@ -3,24 +3,46 @@ import { normalizeLines } from './utils'; describe(' 工具函数', () => { it('把“1. ”列表处理成空行分隔的格式,以便处理', function () { - const lines = normalizeLines('1. abc\n11. def\n'); - expect(lines).eql('1. abc\n\n11. def\n'); + const lines = normalizeLines(`1. abc +11. def +`); + expect(lines).eql(` +1. abc + +11. def +`); }); it('把“- ”列表处理成空行分隔的格式,以便处理', function () { - const lines = normalizeLines('- abc\n- def\n'); - expect(lines).eql('- abc\n\n- def\n'); + const lines = normalizeLines(`- abc +- def +`); + expect(lines).eql(` +- abc + +- def +`); }); it('把“* ”列表处理成空行分隔的格式,以便处理', function () { - const lines = normalizeLines('* abc\n* def\n'); - expect(lines).eql('* abc\n\n* def\n'); + const lines = normalizeLines(`* abc +* def +`); + expect(lines).eql(` +* abc + +* def +`); }); it('把“# ”标题处理成空行分隔的格式,以便处理', function () { const lines = normalizeLines(`\n# abc def`); - expect(lines).eql('\n\n# abc\n\ndef'); + expect(lines).eql(` +# abc + +def +`); }); it('拆解单行的成对 tag', function () { @@ -159,13 +181,11 @@ def`); `); expect(lines).eq(` -
Angular forms don't require a style library
- `); }); @@ -181,7 +201,6 @@ def`); `); expect(lines).eq(` -

a @@ -193,7 +212,6 @@ def`); 一

- `); }); @@ -203,34 +221,64 @@ def`); abc `)).eql(` - abc - `); }); it('拆解独行的 li', function () { expect(normalizeLines(` -
  • abc
  • + + `)).eql(` + `); }); it('不要拆解行内的 html tag', function () { - expect(normalizeLines(`\na c\n\n`)).eql('\na c\n\n'); + expect(normalizeLines(` +a c + +`)).eql(` +a c +`); }); it('把连续的三行及以上空行简化为两个空行', function () { - const lines = normalizeLines(`\n a \n\n\n b `); - expect(lines).eql(`\n a \n\n b `); + const lines = normalizeLines(` + a + + + b`); + expect(lines).eql(` + a + + b +`); }); }); diff --git a/aio/tools/translator/utils.ts b/aio/tools/translator/utils.ts index a7e6046403..c09a30a321 100644 --- a/aio/tools/translator/utils.ts +++ b/aio/tools/translator/utils.ts @@ -46,6 +46,7 @@ export function isHead(line: string): boolean { } export function normalizeLines(text: string): string { + text = '\n' + text + '\n'; // 列表、标题等自带换行含义的markdown const blockElementPattern = /(?=\n *(\d+\.|-|\*) )\n/g; text = text.replace(blockElementPattern, '\n\n'); @@ -55,7 +56,7 @@ export function normalizeLines(text: string): string { 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>( *)\n/g; + 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'); @@ -76,7 +77,7 @@ export function normalizeLines(text: string): string { const multipleBlankLinePattern = /\n\s*\n+/g; text = text.replace(multipleBlankLinePattern, '\n\n'); - return text; + return text.replace(/^\n+/, '\n').replace(/\n+$/, '\n'); } export function indentOf(line): number {