mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-05 16:42:27 +03:00
refactor(*): convert to babel and generation to lib, es, dist folders (#2)
* refactor(*): convert to babel and generation to lib, es, dist * chore(*): remove generated files * fix(*): lint run command
This commit is contained in:
committed by
GitHub
parent
d22a2895a4
commit
32a7fb51da
@@ -0,0 +1,25 @@
|
||||
const React = require('react');
|
||||
const PropTypes = require('prop-types');
|
||||
|
||||
const { render } = require('./render');
|
||||
|
||||
const content = (children, plugins) => React.Children.map(children, child =>
|
||||
(typeof child === 'string' ? render(child, plugins) : child));
|
||||
|
||||
const Component = props =>
|
||||
React.createElement(props.container, {}, content(props.children, props.plugins));
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
Component.propTypes = {
|
||||
container: PropTypes.node,
|
||||
children: PropTypes.node.isRequired,
|
||||
plugins: PropTypes.arrayOf(Function),
|
||||
};
|
||||
}
|
||||
|
||||
Component.defaultProps = {
|
||||
container: 'span',
|
||||
plugins: [],
|
||||
};
|
||||
|
||||
export default Component;
|
||||
@@ -0,0 +1,2 @@
|
||||
export { default } from './Component';
|
||||
export { render } from './render';
|
||||
@@ -0,0 +1,48 @@
|
||||
import React from 'react';
|
||||
import core from '@bbob/core';
|
||||
import html from '@bbob/html';
|
||||
|
||||
import { isTagNode, isStringNode } from '@bbob/plugin-helper';
|
||||
|
||||
const toAST = (source, plugins) => core(plugins)
|
||||
.process(source, {
|
||||
render: input => html(input, { stripTags: true }),
|
||||
}).tree;
|
||||
|
||||
function tagToReactElement(node) {
|
||||
if (node.content === null) {
|
||||
return React.createElement(
|
||||
node.tag,
|
||||
node.attrs,
|
||||
null,
|
||||
);
|
||||
}
|
||||
|
||||
return React.createElement(
|
||||
node.tag,
|
||||
node.attrs,
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
renderToReactNodes(node.content),
|
||||
);
|
||||
}
|
||||
|
||||
function renderToReactNodes(nodes) {
|
||||
const els = nodes.reduce((arr, node) => {
|
||||
if (isTagNode(node)) {
|
||||
arr.push(tagToReactElement(node));
|
||||
} else if (isStringNode(node)) {
|
||||
arr.push(node);
|
||||
}
|
||||
|
||||
return arr;
|
||||
}, []);
|
||||
|
||||
return els;
|
||||
}
|
||||
|
||||
function render(source, plugins) {
|
||||
return renderToReactNodes(toAST(source, plugins));
|
||||
}
|
||||
|
||||
export { render };
|
||||
export default render;
|
||||
Reference in New Issue
Block a user