2016-02-29 14:08:40 +08:00
|
|
|
import React from 'react';
|
2016-03-01 11:10:57 +08:00
|
|
|
import hljs from 'highlight.js';
|
2016-03-01 11:46:18 +08:00
|
|
|
import { Menu } from '../../';
|
2016-02-29 14:08:40 +08:00
|
|
|
|
|
|
|
export function objectToComponent(object, index) {
|
2016-03-01 11:10:57 +08:00
|
|
|
if (object === null) return;
|
2016-02-29 14:08:40 +08:00
|
|
|
|
2016-03-01 12:06:01 +08:00
|
|
|
if (typeof object === 'string') {
|
|
|
|
return <span key={index}>{ object }</span>;
|
|
|
|
}
|
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
const children = object.children;
|
|
|
|
|
|
|
|
if (object.type === 'html') {
|
|
|
|
return React.createElement('div', {
|
|
|
|
key: index,
|
|
|
|
dangerouslySetInnerHTML: { __html: children }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-01 11:10:57 +08:00
|
|
|
if (object.type === 'code') {
|
|
|
|
const highlightedCode = hljs.highlight('javascript', children).value;
|
|
|
|
return (
|
|
|
|
<div className="highlight" key={index}>
|
|
|
|
<pre>
|
|
|
|
<code className={object.props.lang}
|
|
|
|
dangerouslySetInnerHTML={{ __html: highlightedCode }} />
|
|
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
if (typeof children === 'string') {
|
|
|
|
return React.createElement(object.type, {
|
|
|
|
key: index,
|
|
|
|
dangerouslySetInnerHTML: { __html: children }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return React.createElement(
|
2016-03-01 11:10:57 +08:00
|
|
|
object.type, { key: index },
|
2016-02-29 14:08:40 +08:00
|
|
|
children && children.map(objectToComponent) // `hr` has no children
|
|
|
|
);
|
|
|
|
}
|
2016-03-01 11:46:18 +08:00
|
|
|
|
|
|
|
export function flattenMenu(menu) {
|
|
|
|
if (menu.type === Menu.Item) {
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(menu)) {
|
|
|
|
return menu.reduce((acc, item) => {
|
|
|
|
return acc.concat(flattenMenu(item));
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
return flattenMenu(menu.props.children);
|
|
|
|
}
|