mirror of
https://github.com/bendtherules/react-to-markdown.git
synced 2026-08-18 13:53:11 +00:00
tagHandler - Handle img tags + tests
This commit is contained in:
@@ -99,3 +99,35 @@ test('should render plain ordered list with text', () => {
|
||||
) as string).trim()
|
||||
).toBe('1. a\n2. b\n3. c');
|
||||
});
|
||||
|
||||
test('should render image with alt text and title', () => {
|
||||
expect(
|
||||
(render(
|
||||
<img src="./test.jpg" title="some title" alt="some alt" />
|
||||
) as string).trim()
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
test('should render image with only alt text', () => {
|
||||
expect(
|
||||
(render(
|
||||
<img src="./test.jpg" alt="some alt" />
|
||||
) as string).trim()
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
test('should render image with only title', () => {
|
||||
expect(
|
||||
(render(
|
||||
<img src="./test.jpg" title="some title" />
|
||||
) as string).trim()
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
test('should render image without attributes', () => {
|
||||
expect(
|
||||
(render(
|
||||
<img src="./test.jpg" />
|
||||
) as string).trim()
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
+22
-2
@@ -1,6 +1,15 @@
|
||||
import { emphasis, heading, strong, list, listItem } from 'mdast-builder';
|
||||
import {
|
||||
emphasis,
|
||||
heading,
|
||||
image,
|
||||
list,
|
||||
listItem,
|
||||
strong,
|
||||
} from 'mdast-builder';
|
||||
import { ImgHTMLAttributes } from 'react';
|
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import { Node as MDASTNode, Parent as MDASTParent } from 'unist';
|
||||
|
||||
import { IReactElementSubset } from './helpers';
|
||||
|
||||
export default function handleTag(
|
||||
@@ -50,9 +59,20 @@ export default function handleTag(
|
||||
case 'li':
|
||||
newParentASTNode = childASTNode = listItem([]);
|
||||
break;
|
||||
|
||||
// END - Handle lists - ul, ol, li
|
||||
|
||||
// START - Handle img tag - with src and alt
|
||||
case 'img': {
|
||||
const props = node.props as ImgHTMLAttributes<any>;
|
||||
let source = '';
|
||||
if (props.src !== undefined) {
|
||||
source = props.src;
|
||||
}
|
||||
newParentASTNode = childASTNode = image(source, props.title, props.alt);
|
||||
break;
|
||||
}
|
||||
// END - Handle img tag - with src and alt
|
||||
|
||||
default:
|
||||
newParentASTNode = parentASTNode;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user