mirror of
https://github.com/bendtherules/react-to-markdown.git
synced 2026-08-18 13:53:11 +00:00
tagHandler - Handle table - all tags + tests
This commit is contained in:
@@ -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>
|
||||||
|
*/
|
||||||
|
|||||||
+33
-3
@@ -14,8 +14,11 @@ 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 {
|
||||||
AnchorHTMLAttributes,
|
AnchorHTMLAttributes,
|
||||||
Children as ReactChildren,
|
Children as ReactChildren,
|
||||||
@@ -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 = '';
|
||||||
|
|||||||
Reference in New Issue
Block a user