tagHandler - Handle anchor tag + tests

This commit is contained in:
2019-12-21 23:22:52 +05:30
parent fdd00f9262
commit 6607c1134f
2 changed files with 40 additions and 1 deletions
+26
View File
@@ -129,3 +129,29 @@ test('should render image without attributes', () => {
'![](./test.jpg)' '![](./test.jpg)'
); );
}); });
test('should render link with text and title', () => {
expect(
(render(
<a href="./test" title="some title">
some text
</a>
) as string).trim()
).toBe('[some text](./test "some title")');
});
test('should render link with only text', () => {
expect((render(<a href="./test">some text</a>) as string).trim()).toBe(
'[some text](./test)'
);
});
test('should render link with only title', () => {
expect(
(render(<a href="./test" title="some title" />) as string).trim()
).toBe('[](./test "some title")');
});
test('should render link without title and text', () => {
expect((render(<a href="./test" />) as string).trim()).toBe('[](./test)');
});
+14 -1
View File
@@ -2,11 +2,12 @@ import {
emphasis, emphasis,
heading, heading,
image, image,
link,
list, list,
listItem, listItem,
strong, strong,
} from 'mdast-builder'; } from 'mdast-builder';
import { ImgHTMLAttributes } from 'react'; import { AnchorHTMLAttributes, ImgHTMLAttributes } from 'react';
// tslint:disable-next-line: no-implicit-dependencies // tslint:disable-next-line: no-implicit-dependencies
import { Node as MDASTNode, Parent as MDASTParent } from 'unist'; import { Node as MDASTNode, Parent as MDASTParent } from 'unist';
@@ -76,6 +77,18 @@ export default function handleTag(
} }
// END - Handle img tag - with src and alt // END - Handle img tag - with src and alt
// START - Handle anchor tag (link)
case 'a': {
const props = node.props as AnchorHTMLAttributes<any>;
let href = '';
if (props.href !== undefined) {
href = props.href;
}
newParentASTNode = childASTNode = link(href, props.title);
break;
}
// END - Handle anchor tag (link)
default: default:
newParentASTNode = parentASTNode; newParentASTNode = parentASTNode;
break; break;