ant-design/site/entry/utils.js

77 lines
2.0 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-10 16:56:17 +08:00
function fileNameToPath(fileName) {
const snippets = fileName.replace(/(\/index)?\.md$/i, '').split('/');
return snippets[snippets.length - 1];
2016-03-02 17:12:43 +08:00
}
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;
}
2016-03-11 09:42:04 +08:00
export function generateContainer(data) {
2016-03-04 15:19:23 +08:00
const menuItems = getMenuItems(data);
2016-03-03 11:12:46 +08:00
return (props) => {
return (
2016-03-11 09:42:04 +08:00
<MainContent {...props} menuItems={menuItems} />
2016-03-03 11:12:46 +08:00
);
};
}
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);
2016-03-02 11:57:37 +08:00
const children = pagesData.map((pageData, index) => {
2016-03-08 16:12:04 +08:00
const meta = pageData.meta;
const hasDemos = demosList[meta.fileName];
2016-03-07 11:58:14 +08:00
const Wrapper = !hasDemos ?
2016-03-07 17:33:38 +08:00
(props) => <Article {...props} content={pageData} /> :
2016-03-08 16:12:04 +08:00
(props) => <ComponentDoc {...props} doc={pageData} />;
2016-03-02 11:57:37 +08:00
return (
<Route key={index}
2016-03-10 16:56:17 +08:00
path={fileNameToPath(meta.fileName)}
component={Wrapper} />
2016-03-02 11:57:37 +08:00
);
});
2016-03-08 16:12:04 +08:00
const menuItems = getMenuItems(data);
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-10 16:56:17 +08:00
to={fileNameToPath(firstChild.fileName)} />
2016-03-02 11:57:37 +08:00
);
return children;
}