You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

57 lines
1.3 KiB

import React from 'react';
import hljs from 'highlight.js';
import { Menu } from '../../';
export function objectToComponent(object, index) {
if (object === null) return;
if (typeof object === 'string') {
return <span key={index}>{ object }</span>;
}
const children = object.children;
if (object.type === 'html') {
return React.createElement('div', {
key: index,
dangerouslySetInnerHTML: { __html: children }
});
}
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>
);
}
if (typeof children === 'string') {
return React.createElement(object.type, {
key: index,
dangerouslySetInnerHTML: { __html: children }
});
}
return React.createElement(
object.type, { key: index },
children && children.map(objectToComponent) // `hr` has no children
);
}
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);
}