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
+6
View File
@@ -251,3 +251,9 @@ test('should render strike (strike, s, del)', () => {
) as string).trim() ) as string).trim()
).toBe('This ~~is~~ some ~~text~~'); ).toBe('This ~~is~~ some ~~text~~');
}); });
test('should render fallback as html tag', () => {
expect((render(<span>some text</span>) as string).trim()).toBe(
'<span>some text</span>'
);
});
+20
View File
@@ -4254,6 +4254,26 @@
"integrity": "sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=", "integrity": "sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=",
"dev": true "dev": true
}, },
"preact": {
"version": "10.1.1",
"resolved": "https://registry.npmjs.org/preact/-/preact-10.1.1.tgz",
"integrity": "sha512-mKW7Cdn68XMhdes0FjyIbA8+IVPsj3aIuAEQlZVkj9E2VhujWcXZEfwirBoXK6qZYfj1djaTBDCFKjAu1sK93w=="
},
"preact-render-to-string": {
"version": "5.1.3",
"resolved": "https://registry.npmjs.org/preact-render-to-string/-/preact-render-to-string-5.1.3.tgz",
"integrity": "sha512-fVCd/qidYNiVSvN4cP5TVwI5VklMdrHMvcz0Z5mvwrYSKCrX0azInroBrKc5x6N9VtqMViMkYWaYh54cO6psIA==",
"requires": {
"pretty-format": "^3.8.0"
},
"dependencies": {
"pretty-format": {
"version": "3.8.0",
"resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-3.8.0.tgz",
"integrity": "sha1-v77VbV6ad2ZF9LH/eqGjrE+jw4U="
}
}
},
"prelude-ls": { "prelude-ls": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz",
+2
View File
@@ -31,6 +31,8 @@
}, },
"dependencies": { "dependencies": {
"mdast-builder": "^1.1.1", "mdast-builder": "^1.1.1",
"preact": "^10.1.1",
"preact-render-to-string": "^5.1.3",
"react": "^16.12.0", "react": "^16.12.0",
"remark-stringify": "^7.0.4", "remark-stringify": "^7.0.4",
"unified": "^8.4.2" "unified": "^8.4.2"
+2 -8
View File
@@ -48,7 +48,6 @@ function render(
let processChildren = true; let processChildren = true;
// Handle nested function or class components // Handle nested function or class components
{
if (isFunctionalComponent(elementType)) { if (isFunctionalComponent(elementType)) {
const renderOutputRaw = elementType.call(null, props); const renderOutputRaw = elementType.call(null, props);
const renderOutput = ReactChildren.toArray( const renderOutput = ReactChildren.toArray(
@@ -57,9 +56,7 @@ function render(
// Render processed array of children // Render processed array of children
render(renderOutput, parentASTNodeMod); render(renderOutput, parentASTNodeMod);
} } else if (isClassComponent(elementType)) {
if (isClassComponent(elementType)) {
const elementInstance = new elementType(props); const elementInstance = new elementType(props);
const renderOutputRaw = elementInstance.render(); const renderOutputRaw = elementInstance.render();
const renderOutput = ReactChildren.toArray( const renderOutput = ReactChildren.toArray(
@@ -68,10 +65,7 @@ function render(
// Render processed array of children // Render processed array of children
render(renderOutput, parentASTNodeMod); render(renderOutput, parentASTNodeMod);
} } else {
}
{
// Handle intrinsic types of element like div, h1 // Handle intrinsic types of element like div, h1
const newParentASTNode = handleTag(node, parentASTNodeMod); const newParentASTNode = handleTag(node, parentASTNodeMod);
if (newParentASTNode === undefined) { if (newParentASTNode === undefined) {
+16 -1
View File
@@ -4,6 +4,7 @@ import {
code, code,
emphasis, emphasis,
heading, heading,
html,
image, image,
inlineCode, inlineCode,
link, link,
@@ -14,6 +15,7 @@ import {
strike, strike,
strong, strong,
} from 'mdast-builder'; } from 'mdast-builder';
import {render as renderToHTML} from 'preact-render-to-string';
import { import {
AnchorHTMLAttributes, AnchorHTMLAttributes,
Children as ReactChildren, Children as ReactChildren,
@@ -23,6 +25,7 @@ import {
// 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';
import { VNode } from 'preact';
import { import {
IReactElementSubset, IReactElementSubset,
ReactElementSubsetWithPrimitive, ReactElementSubsetWithPrimitive,
@@ -178,6 +181,7 @@ export default function handleTag(
// START - Handle pre.code // START - Handle pre.code
case 'pre': { case 'pre': {
isHandled = false;
// If structure looks like pre.code, call handleTag again on code tag with extraProps having inline: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); const childrenArray = ReactChildren.toArray(node.props.children);
if (childrenArray.length === 1) { if (childrenArray.length === 1) {
@@ -189,6 +193,8 @@ export default function handleTag(
onlyChild = onlyChild as IReactElementSubset; onlyChild = onlyChild as IReactElementSubset;
handleTag(onlyChild, parentASTNode, { inline: false }); handleTag(onlyChild, parentASTNode, { inline: false });
isHandled = true;
} }
} }
break; break;
@@ -196,10 +202,19 @@ export default function handleTag(
// END - Handle blockquote // END - Handle blockquote
default: default:
newParentASTNode = parentASTNode; isHandled = false;
break; break;
} }
if (!isHandled) {
// Handle as html
let htmlText = '';
htmlText = renderToHTML(node as VNode);
childASTNode = html(htmlText);
}
if (childASTNode !== undefined) { if (childASTNode !== undefined) {
parentASTNode.children.push(childASTNode); parentASTNode.children.push(childASTNode);
} }