tagHandler - Handle fallback html tag + basic test

Also added preact dep.
Fixed class or function component flowing into tagHandler
This commit is contained in:
2019-12-22 19:54:46 +05:30
parent 736726805d
commit 84a9b387f0
5 changed files with 60 additions and 23 deletions
+16 -22
View File
@@ -48,30 +48,24 @@ function render(
let processChildren = true;
// Handle nested function or class components
{
if (isFunctionalComponent(elementType)) {
const renderOutputRaw = elementType.call(null, props);
const renderOutput = ReactChildren.toArray(
renderOutputRaw
) as ReactElementSubsetWithPrimitive[];
if (isFunctionalComponent(elementType)) {
const renderOutputRaw = elementType.call(null, props);
const renderOutput = ReactChildren.toArray(
renderOutputRaw
) as ReactElementSubsetWithPrimitive[];
// Render processed array of children
render(renderOutput, parentASTNodeMod);
}
// Render processed array of children
render(renderOutput, parentASTNodeMod);
} else if (isClassComponent(elementType)) {
const elementInstance = new elementType(props);
const renderOutputRaw = elementInstance.render();
const renderOutput = ReactChildren.toArray(
renderOutputRaw
) as ReactElementSubsetWithPrimitive[];
if (isClassComponent(elementType)) {
const elementInstance = new elementType(props);
const renderOutputRaw = elementInstance.render();
const renderOutput = ReactChildren.toArray(
renderOutputRaw
) as ReactElementSubsetWithPrimitive[];
// Render processed array of children
render(renderOutput, parentASTNodeMod);
}
}
{
// Render processed array of children
render(renderOutput, parentASTNodeMod);
} else {
// Handle intrinsic types of element like div, h1
const newParentASTNode = handleTag(node, parentASTNodeMod);
if (newParentASTNode === undefined) {
+16 -1
View File
@@ -4,6 +4,7 @@ import {
code,
emphasis,
heading,
html,
image,
inlineCode,
link,
@@ -14,6 +15,7 @@ import {
strike,
strong,
} from 'mdast-builder';
import {render as renderToHTML} from 'preact-render-to-string';
import {
AnchorHTMLAttributes,
Children as ReactChildren,
@@ -23,6 +25,7 @@ import {
// tslint:disable-next-line: no-implicit-dependencies
import { Node as MDASTNode, Parent as MDASTParent } from 'unist';
import { VNode } from 'preact';
import {
IReactElementSubset,
ReactElementSubsetWithPrimitive,
@@ -178,6 +181,7 @@ export default function handleTag(
// START - Handle pre.code
case 'pre': {
isHandled = false;
// If structure looks like pre.code, call handleTag again on code tag with extraProps having inline:false
const childrenArray = ReactChildren.toArray(node.props.children);
if (childrenArray.length === 1) {
@@ -189,6 +193,8 @@ export default function handleTag(
onlyChild = onlyChild as IReactElementSubset;
handleTag(onlyChild, parentASTNode, { inline: false });
isHandled = true;
}
}
break;
@@ -196,10 +202,19 @@ export default function handleTag(
// END - Handle blockquote
default:
newParentASTNode = parentASTNode;
isHandled = false;
break;
}
if (!isHandled) {
// Handle as html
let htmlText = '';
htmlText = renderToHTML(node as VNode);
childASTNode = html(htmlText);
}
if (childASTNode !== undefined) {
parentASTNode.children.push(childASTNode);
}