ant-design/site/entry/utils.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-03-02 11:57:37 +08:00
import React from 'react';
import { Route, IndexRedirect } from 'react-router';
2016-03-03 11:12:46 +08:00
import MainContent from '../component/MainContent';
2016-03-02 11:57:37 +08:00
import Article from '../component/Article';
import ComponentDoc from '../component/ComponentDoc';
2016-03-07 11:58:14 +08:00
import demosList from '../../_site/data/demos-list';
2016-03-02 11:57:37 +08:00
2016-03-02 17:12:43 +08:00
function dashed(name) {
return name.toLowerCase().trim().replace(/\s+/g, '-');
}
2016-03-04 15:19:23 +08:00
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]) {
2016-03-07 11:35:23 +08:00
menuItems[category] = {};
2016-03-04 15:19:23 +08:00
}
2016-03-07 11:35:23 +08:00
const type = meta.type || 'topLevel';
if (!menuItems[category][type]) {
menuItems[category][type] = [];
}
menuItems[category][type].push(meta);
2016-03-04 15:19:23 +08:00
});
return menuItems;
}
export function generateContainer(category, data) {
const menuItems = getMenuItems(data);
2016-03-03 11:12:46 +08:00
return (props) => {
return (
<MainContent {...props}
category={category} menuItems={menuItems} />
);
};
}
2016-03-04 15:19:23 +08:00
function getPagesData(data) {
return Object.keys(data)
.map((key) => data[key]);
}
export function generateChildren(data) {
const pagesData = getPagesData(data);
const menuItems = getMenuItems(data);
2016-03-02 11:57:37 +08:00
const children = pagesData.map((pageData, index) => {
2016-03-07 11:58:14 +08:00
const hasDemos = demosList[pageData.meta.fileName];
const Wrapper = !hasDemos ?
() => <Article content={pageData} /> :
2016-03-04 18:06:34 +08:00
() => <ComponentDoc doc={pageData} />;
2016-03-02 11:57:37 +08:00
return (
<Route key={index}
2016-03-02 17:12:43 +08:00
path={dashed(pageData.meta.english)}
component={Wrapper} />
2016-03-02 11:57:37 +08:00
);
});
2016-03-07 11:35:23 +08:00
const firstChild = menuItems.topLevel.topLevel.find((item) => {
2016-03-03 17:23:08 +08:00
return item.disabled !== 'true';
2016-03-02 11:57:37 +08:00
});
children.unshift(
<IndexRedirect key="index"
2016-03-03 17:23:08 +08:00
to={dashed(firstChild.english)} />
2016-03-02 11:57:37 +08:00
);
return children;
}