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.

68 lines
1.7 KiB

import React from 'react';
import { Route, IndexRedirect } from 'react-router';
import MainContent from '../component/MainContent';
import Article from '../component/Article';
import ComponentDoc from '../component/ComponentDoc';
function dashed(name) {
return name.toLowerCase().trim().replace(/\s+/g, '-');
}
function getMenuItems(data) {
const menuMeta = Object.keys(data)
.map((key) => data[key])
.map((file) => file.meta);
const menuItems = {};
menuMeta.sort((a, b) => {
return parseInt(a.order, 10) - parseInt(b.order, 10);
}).forEach((meta) => {
const category = meta.category || 'topLevel';
if (!menuItems[category]) {
menuItems[category] = [];
}
menuItems[category].push(meta);
});
return menuItems;
}
export function generateContainer(category, data) {
const menuItems = getMenuItems(data);
return (props) => {
return (
<MainContent {...props}
category={category} menuItems={menuItems} />
);
};
}
function getPagesData(data) {
return Object.keys(data)
.map((key) => data[key]);
}
export function generateChildren(data) {
const pagesData = getPagesData(data);
const menuItems = getMenuItems(data);
const children = pagesData.map((pageData, index) => {
const Wrapper = !pageData.meta.hasDemos ?
() => <Article content={pageData} /> :
() => <ComponentDoc doc={pageData} />;
return (
<Route key={index}
path={dashed(pageData.meta.english)}
component={Wrapper} />
);
});
const firstChild = menuItems.topLevel.find((item) => {
return item.disabled !== 'true';
});
children.unshift(
<IndexRedirect key="index"
to={dashed(firstChild.english)} />
);
return children;
}