tagHandler - Handle table - all tags + tests

This commit is contained in:
2019-12-26 11:44:03 +05:30
parent efb2c4af5c
commit 4a084a8f51
2 changed files with 86 additions and 4 deletions
+53 -1
View File
@@ -234,7 +234,8 @@ test('should render break (br)', () => {
expect( expect(
(render( (render(
<p> <p>
text 1<br/>text 2 text 1<br />
text 2
</p> </p>
) as string).trim() ) as string).trim()
).toBe('text 1 \ntext 2'); ).toBe('text 1 \ntext 2');
@@ -250,8 +251,59 @@ test('should render strike (strike, s, del)', () => {
).toBe('This ~~is~~ some ~~text~~'); ).toBe('This ~~is~~ some ~~text~~');
}); });
test('should render tables', () => {
expect(
(render(
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Col 1</td>
<td>Row 1 Col 2</td>
</tr>
<tr>
<td>Row 2 Col 1</td>
<td>Row 2 Col 2</td>
</tr>
</tbody>
</table>
) as string).trim()
).toBe(`\
| Header 1 | Header 2 |
| ----------- | ----------- |
| Row 1 Col 1 | Row 1 Col 2 |
| Row 2 Col 1 | Row 2 Col 2 |\
`);
});
test('should render fallback as html tag', () => { test('should render fallback as html tag', () => {
expect((render(<span>some text</span>) as string).trim()).toBe( expect((render(<span>some text</span>) as string).trim()).toBe(
'<span>some text</span>' '<span>some text</span>'
); );
}); });
/*
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Col 1</td>
<td>Row 1 Col 2</td>
</tr>
<tr>
<td>Row 2 Col 1</td>
<td>Row 2 Col 2</td>
</tr>
</tbody>
</table>
*/
+32 -2
View File
@@ -14,6 +14,9 @@ import {
separator, separator,
strike, strike,
strong, strong,
table,
tableCell,
tableRow,
} from 'mdast-builder'; } from 'mdast-builder';
import { render as renderToHTML } from 'preact-render-to-string'; import { render as renderToHTML } from 'preact-render-to-string';
import { import {
@@ -199,14 +202,41 @@ export default function handleTag(
} }
break; break;
} }
// END - Handle blockquote // END - Handle pre.code
// START - Handle table - all tags
case 'table': {
// TODO: Handle align
newParentASTNode = childASTNode = table();
break;
}
// Passthrough thead and tbody
case 'thead':
case 'tbody': {
newParentASTNode = parentASTNode;
break;
}
// Handle table rows
case 'tr': {
newParentASTNode = childASTNode = tableRow();
break;
}
// Handle table values (th - for header, td - for body)
case 'th':
case 'td': {
newParentASTNode = childASTNode = tableCell();
break;
}
// END - Handle table - all tags
default: default:
isHandled = false; isHandled = false;
break; break;
} }
if (!isHandled) { if (!isHandled) {
// Handle as html // Handle as html
let htmlText = ''; let htmlText = '';