tagHandler - Handle inline and block code separately + tests

This commit is contained in:
2019-12-22 04:34:48 +05:30
parent 4b9bfffbb5
commit d86075f999
2 changed files with 61 additions and 9 deletions
+41 -5
View File
@@ -4,6 +4,7 @@ import {
emphasis,
heading,
image,
inlineCode,
link,
list,
listItem,
@@ -11,18 +12,23 @@ import {
strong,
} from 'mdast-builder';
import {
HTMLAttributes,
AnchorHTMLAttributes,
Children as ReactChildren,
HTMLAttributes,
ImgHTMLAttributes,
} from 'react';
// tslint:disable-next-line: no-implicit-dependencies
import { Node as MDASTNode, Parent as MDASTParent } from 'unist';
import { IReactElementSubset } from './helpers';
import {
IReactElementSubset,
ReactElementSubsetWithPrimitive,
} from './helpers';
export default function handleTag(
node: IReactElementSubset,
parentASTNode: MDASTParent
parentASTNode: MDASTParent,
extraProps = {}
) {
const { type: tagType } = node;
@@ -112,7 +118,10 @@ export default function handleTag(
// START - Handle code
case 'code': {
const props = node.props as HTMLAttributes<any>;
// tslint:disable-next-line: no-object-literal-type-assertion
const props = { ...node.props, ...extraProps } as HTMLAttributes<any> & {
inline?: boolean;
};
let lang = '';
if (props.lang !== undefined) {
@@ -127,11 +136,38 @@ export default function handleTag(
}
}
childASTNode = code(lang, value);
let isInline = true;
if (props.inline !== undefined) {
isInline = !!props.inline;
}
if (isInline) {
childASTNode = inlineCode(value);
} else {
childASTNode = code(lang, value);
}
break;
}
// END - Handle code
// START - Handle pre.code
case 'pre': {
const childrenArray = ReactChildren.toArray(node.props.children);
if (childrenArray.length === 1) {
let onlyChild = childrenArray[0] as ReactElementSubsetWithPrimitive;
if (
onlyChild instanceof Object &&
(onlyChild as IReactElementSubset).type === 'code'
) {
onlyChild = onlyChild as IReactElementSubset;
handleTag(onlyChild, parentASTNode, { inline: false });
}
}
break;
}
// END - Handle blockquote
default:
newParentASTNode = parentASTNode;
break;