tagHandler - Handle code tag as code block + test

This commit is contained in:
2019-12-22 03:32:49 +05:30
parent 3a279e9710
commit 4b9bfffbb5
2 changed files with 44 additions and 1 deletions
+28 -1
View File
@@ -1,5 +1,6 @@
import {
blockquote,
code,
emphasis,
heading,
image,
@@ -9,7 +10,11 @@ import {
paragraph,
strong,
} from 'mdast-builder';
import { AnchorHTMLAttributes, ImgHTMLAttributes } from 'react';
import {
HTMLAttributes,
AnchorHTMLAttributes,
ImgHTMLAttributes,
} from 'react';
// tslint:disable-next-line: no-implicit-dependencies
import { Node as MDASTNode, Parent as MDASTParent } from 'unist';
@@ -105,6 +110,28 @@ export default function handleTag(
}
// 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:
newParentASTNode = parentASTNode;
break;