fix: 拆解其它单行块标签

This commit is contained in:
Zhicheng Wang 2018-03-06 17:32:21 +08:00
parent fee2468d41
commit f89d728818
2 changed files with 100 additions and 0 deletions

View File

@ -52,6 +52,95 @@ def`);
b
`);
});
it('拆解单行的 h\\d 标签', function () {
const lines = normalizeLines(`
a
<h3 id="abc">line</h3>
b
`);
expect(lines).eql(`
a
<h3 id="abc">
line
</h3>
b
`);
});
it('拆解单行的 th 标签', function () {
const lines = normalizeLines(`
a
<th>line</th>
b
`);
expect(lines).eql(`
a
<th>
line
</th>
b
`);
});
it('拆解单行注释', function () {
const lines = normalizeLines(`
a
<!-- no -->
b
`);
expect(lines).eql(`
a
<!-- no -->
b
`);
});
it('拆解 @a 标记', function () {
const lines = normalizeLines(`
a
{@a test}
b
`);
expect(lines).eql(`
a
{@a test}
b
`);
});
it('拆解多行代码', function () {
const lines = normalizeLines(`
a
\`\`\`
var a = 1
\`\`\`
b
`);
expect(lines).eql(`
a
\`\`\`
var a = 1
\`\`\`
b
`);
});
it('拆解多行的成对 tag', function () {
const lines = normalizeLines(`
<header>
@ -76,4 +165,5 @@ def`);
const lines = normalizeLines(`\n a \n\n\n b `);
expect(lines).eql(`\n a \n\n b `);
});
});

View File

@ -55,10 +55,20 @@ export function normalizeLines(text: string): string {
text = text.replace(leadHxPattern, '$1\n\n');
const oneLinePairedTagPattern = /\n( *)<(p|code-example|div)( ?[^> \n]*)>([^<\n]*)<\/\2>( *)\n/g;
text = text.replace(oneLinePairedTagPattern, '\n\n$1<$2$3>$4</$2>$5\n\n');
const oneLineHnTagPattern = /\n( *)<(h\d+|th|td)( ?[^> \n]*)>([^<\n]*)<\/\2>( *)\n/g;
text = text.replace(oneLineHnTagPattern, '\n\n$1<$2$3>\n\n$1$4\n\n$1</$2>$5\n\n');
const oneLineCommentPattern = /\n( *)(<!--.*-->)( *)\n/g;
text = text.replace(oneLineCommentPattern, '\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)( *[^> \n]*)>\n?(.*?)\n?( *)<\/\2>( *)\n/g;
text = text.replace(multiLinePairedTagPattern, '\n\n$1<$2$3>\n\n$4\n\n$5</$2>$6\n\n');
const multiLineCodePattern = /\n( *)```( *)\n/g;
text = text.replace(multiLineCodePattern, '\n\n$1```$2\n\n');
const multipleBlankLinePattern = /\n\s*\n+/g;
text = text.replace(multipleBlankLinePattern, '\n\n');
return text;