fix: 处理单 html tag 行的翻译问题

This commit is contained in:
Zhicheng Wang 2018-03-03 21:05:33 +08:00
parent 757526a6ed
commit 585e377fa8
8 changed files with 137 additions and 1644 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -74,7 +74,6 @@ export function gatherFromMarkdownFiles(directory: string): DictEntry[] {
export function purifyText(text): string {
return text
.replace(/^<(\w+)[\s\S]*?>([\s\S]*)<\/\1>$/, '$2')
.replace(/^(.*)<code-example .*$/, '$1')
.trim();
}

View File

@ -11,4 +11,8 @@ describe('根据字典进行翻译', () => {
it('查字典', () => {
expect(lookup('# Forms')[0].translation).eql('# 表单');
});
it('对包裹在 a 标签中的内容查字典', () => {
expect(lookup('<a href=""># Forms</a>')[0].translation).eql('# 表单');
});
});

View File

@ -8,7 +8,7 @@ import { indentOf, normalizeLines, repeat } from './utils';
export const dict = require('./dict-3.json') as DictEntry[];
export function lookup(english: string, filename: RegExp = /.*/): DictEntry[] {
let entries = dict
const entries = dict
.filter(entry => filename.test(entry.sourceFile))
.filter(entry => kernelText(entry.original) === kernelText(english));
return _.uniqBy(entries, 'translation');

View File

@ -35,7 +35,6 @@ def`);
Angular forms don't require a style library
</header>
`);

View File

@ -53,9 +53,15 @@ export function normalizeLines(text: string): string {
text = text.replace(hxPattern, '\n$1\n\n');
const leadHxPattern = /^( *#.*)\n/g;
text = text.replace(leadHxPattern, '$1\n\n');
const htmlTagPattern = /(\s*<.*?>\s*?)\n/g;
text = text.replace(htmlTagPattern, '\n\n$1\n\n');
text = text.replace(/\n\n+/, '\n\n');
const htmlTagPattern = /(\s*)<(\/?\w+)(.*?)>(\s*?)\n/g;
text = text.replace(htmlTagPattern, (line, _1, _2, _3, _4) => {
if (_2 === 'a') {
return line;
} else {
return `\n\n${line}\n\n`;
}
});
text = text.replace(/\n\s*\n+/g, '\n\n');
return text;
}