fix: 在正文首尾添加换行,以便统一处理

fix: 拆解 li
This commit is contained in:
Zhicheng Wang 2018-03-07 09:41:45 +08:00
parent 4c6b614064
commit 400a6d0b8e
4 changed files with 71 additions and 22 deletions

View File

@ -3,5 +3,5 @@
import { dirs } from '../dirs'; import { dirs } from '../dirs';
import { translateFile } from '../translate'; 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); translateFile(__dirname + '/../../../../../content-en/' + 'guide/' + filename, dirs.content + 'guide/' + filename);

View File

@ -45,7 +45,7 @@ export function translate(content: string): string[] {
export function translateFile(sourceFile: string, targetFile: string): void { export function translateFile(sourceFile: string, targetFile: string): void {
const content = fs.readFileSync(sourceFile, 'utf-8'); const content = fs.readFileSync(sourceFile, 'utf-8');
const result = translate(content); 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 { export function translateDirectory(sourceDir: string, targetDir: string): void {

View File

@ -3,24 +3,46 @@ import { normalizeLines } from './utils';
describe(' 工具函数', () => { describe(' 工具函数', () => {
it('把“1. ”列表处理成空行分隔的格式,以便处理', function () { it('把“1. ”列表处理成空行分隔的格式,以便处理', function () {
const lines = normalizeLines('1. abc\n11. def\n'); const lines = normalizeLines(`1. abc
expect(lines).eql('1. abc\n\n11. def\n'); 11. def
`);
expect(lines).eql(`
1. abc
11. def
`);
}); });
it('把“- ”列表处理成空行分隔的格式,以便处理', function () { it('把“- ”列表处理成空行分隔的格式,以便处理', function () {
const lines = normalizeLines('- abc\n- def\n'); const lines = normalizeLines(`- abc
expect(lines).eql('- abc\n\n- def\n'); - def
`);
expect(lines).eql(`
- abc
- def
`);
}); });
it('把“* ”列表处理成空行分隔的格式,以便处理', function () { it('把“* ”列表处理成空行分隔的格式,以便处理', function () {
const lines = normalizeLines('* abc\n* def\n'); const lines = normalizeLines(`* abc
expect(lines).eql('* abc\n\n* def\n'); * def
`);
expect(lines).eql(`
* abc
* def
`);
}); });
it('把“# ”标题处理成空行分隔的格式,以便处理', function () { it('把“# ”标题处理成空行分隔的格式,以便处理', function () {
const lines = normalizeLines(`\n# abc const lines = normalizeLines(`\n# abc
def`); def`);
expect(lines).eql('\n\n# abc\n\ndef'); expect(lines).eql(`
# abc
def
`);
}); });
it('拆解单行的成对 tag', function () { it('拆解单行的成对 tag', function () {
@ -159,13 +181,11 @@ def`);
</header> </header>
`); `);
expect(lines).eq(` expect(lines).eq(`
<header> <header>
Angular forms don't require a style library Angular forms don't require a style library
</header> </header>
`); `);
}); });
@ -181,7 +201,6 @@ def`);
`); `);
expect(lines).eq(` expect(lines).eq(`
<p> <p>
a a
@ -193,7 +212,6 @@ def`);
</p> </p>
`); `);
}); });
@ -203,34 +221,64 @@ def`);
abc abc
</td> </td>
`)).eql(` `)).eql(`
<td> <td>
abc abc
</td> </td>
`); `);
}); });
it('拆解独行的 li', function () { it('拆解独行的 li', function () {
expect(normalizeLines(` expect(normalizeLines(`
<li><span>abc</span></li> <ul>
<li><a href="#">a</a></li>
<li><a href="#">b</a></li>
<li><a href="#">c</a></li>
</ul>
`)).eql(` `)).eql(`
<ul>
<li> <li>
<span>abc</span> <a href="#">a</a>
</li> </li>
<li>
<a href="#">b</a>
</li>
<li>
<a href="#">c</a>
</li>
</ul>
`); `);
}); });
it('不要拆解行内的 html tag', function () { it('不要拆解行内的 html tag', function () {
expect(normalizeLines(`\na <b> c\n\n`)).eql('\na <b> c\n\n'); expect(normalizeLines(`
a <b> c
`)).eql(`
a <b> c
`);
}); });
it('把连续的三行及以上空行简化为两个空行', function () { it('把连续的三行及以上空行简化为两个空行', function () {
const lines = normalizeLines(`\n a \n\n\n b `); const lines = normalizeLines(`
expect(lines).eql(`\n a \n\n b `); a
b`);
expect(lines).eql(`
a
b
`);
}); });
}); });

View File

@ -46,6 +46,7 @@ export function isHead(line: string): boolean {
} }
export function normalizeLines(text: string): string { export function normalizeLines(text: string): string {
text = '\n' + text + '\n';
// 列表、标题等自带换行含义的markdown // 列表、标题等自带换行含义的markdown
const blockElementPattern = /(?=\n *(\d+\.|-|\*) )\n/g; const blockElementPattern = /(?=\n *(\d+\.|-|\*) )\n/g;
text = text.replace(blockElementPattern, '\n\n'); text = text.replace(blockElementPattern, '\n\n');
@ -55,7 +56,7 @@ export function normalizeLines(text: string): string {
text = text.replace(leadHxPattern, '$1\n\n'); text = text.replace(leadHxPattern, '$1\n\n');
const oneLinePairedTagPattern = /\n( *)<(p|code-example|div|h\d+|a)( ?[^> \n]*)>([^<\n]*)<\/\2>( *)\n/g; 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</$2>$5\n\n'); text = text.replace(oneLinePairedTagPattern, '\n\n$1<$2$3>$4</$2>$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</$2>$5\n\n'); text = text.replace(oneLineThTdTagPattern, '\n\n$1<$2$3>\n\n$1$4\n\n$1</$2>$5\n\n');
const oneLineCommentPattern = /\n( *)(<!--.*-->)( *)\n/g; const oneLineCommentPattern = /\n( *)(<!--.*-->)( *)\n/g;
text = text.replace(oneLineCommentPattern, '\n\n$1$2$3\n\n'); 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; const multipleBlankLinePattern = /\n\s*\n+/g;
text = text.replace(multipleBlankLinePattern, '\n\n'); text = text.replace(multipleBlankLinePattern, '\n\n');
return text; return text.replace(/^\n+/, '\n').replace(/\n+$/, '\n');
} }
export function indentOf(line): number { export function indentOf(line): number {