mirror of
https://github.com/bendtherules/react-to-markdown.git
synced 2026-08-18 13:53:11 +00:00
tagHandler - Handle code tag as code block + test
This commit is contained in:
@@ -175,3 +175,19 @@ test('should render blockquote with mixed text', () => {
|
|||||||
) as string).trim()
|
) as string).trim()
|
||||||
).toBe('> This is _hello_ **world**');
|
).toBe('> This is _hello_ **world**');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should render code tag with text', () => {
|
||||||
|
expect(
|
||||||
|
(render(
|
||||||
|
<code lang="js">const a = 1;</code>
|
||||||
|
) as string).trim()
|
||||||
|
).toBe('```js\nconst a = 1;\n```');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should render code tag without text', () => {
|
||||||
|
expect(
|
||||||
|
(render(
|
||||||
|
<code lang="js"/>
|
||||||
|
) as string).trim()
|
||||||
|
).toBe('```js\n\n```');
|
||||||
|
});
|
||||||
|
|||||||
+28
-1
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
blockquote,
|
blockquote,
|
||||||
|
code,
|
||||||
emphasis,
|
emphasis,
|
||||||
heading,
|
heading,
|
||||||
image,
|
image,
|
||||||
@@ -9,7 +10,11 @@ import {
|
|||||||
paragraph,
|
paragraph,
|
||||||
strong,
|
strong,
|
||||||
} from 'mdast-builder';
|
} from 'mdast-builder';
|
||||||
import { AnchorHTMLAttributes, ImgHTMLAttributes } from 'react';
|
import {
|
||||||
|
HTMLAttributes,
|
||||||
|
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';
|
||||||
|
|
||||||
@@ -105,6 +110,28 @@ export default function handleTag(
|
|||||||
}
|
}
|
||||||
// END - Handle blockquote
|
// END - Handle blockquote
|
||||||
|
|
||||||
|
// START - Handle code
|
||||||
|
case 'code': {
|
||||||
|
const props = node.props as HTMLAttributes<any>;
|
||||||
|
|
||||||
|
let lang = '';
|
||||||
|
if (props.lang !== undefined) {
|
||||||
|
lang = props.lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
let value = '';
|
||||||
|
if (props.children !== undefined) {
|
||||||
|
const onlyChild = props.children;
|
||||||
|
if (typeof onlyChild === 'string' || typeof onlyChild === 'number') {
|
||||||
|
value = onlyChild.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
childASTNode = code(lang, value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// END - Handle code
|
||||||
|
|
||||||
default:
|
default:
|
||||||
newParentASTNode = parentASTNode;
|
newParentASTNode = parentASTNode;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user