ant-design/site/entry/utils.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-03-02 11:57:37 +08:00
import React from 'react';
2016-03-21 10:33:27 +08:00
import { 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-22 11:32:49 +08:00
import demosList from '../../_data/demos-list';
2016-03-21 10:33:27 +08:00
import { redirects } from '../website.config';
2016-03-02 11:57:37 +08:00
2016-03-21 16:54:20 +08:00
if (module.hot) {
2016-03-22 11:32:49 +08:00
module.hot.accept('../../_data/demos-list', () => {});
2016-03-21 16:54:20 +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-21 10:33:27 +08:00
export function generateIndex(data) {
2016-03-08 16:12:04 +08:00
const menuItems = getMenuItems(data);
const firstChild = menuItems.topLevel.topLevel.filter((item) => !item.disabled)[0];
2016-03-21 10:33:27 +08:00
return (
2016-03-02 11:57:37 +08:00
<IndexRedirect key="index"
2016-03-10 16:56:17 +08:00
to={fileNameToPath(firstChild.fileName)} />
2016-03-02 11:57:37 +08:00
);
2016-03-21 10:33:27 +08:00
}
const pathToFile = {};
Object.keys(redirects).forEach((key) => pathToFile[redirects[key]] = key);
2016-05-06 14:27:51 +08:00
pathToFile['docs/react/changelog'] = './CHANGELOG'; // TODO
2016-03-21 10:33:27 +08:00
export function getChildrenWrapper(data) {
return function childrenWrapper(props) {
const trimedPathname = props.location.pathname.replace(/^\//, '');
const processedPathname = pathToFile[trimedPathname] || trimedPathname;
const doc = data[`${processedPathname}.md`] ||
data[`${processedPathname}/index.md`];
const hasDemos = demosList[doc.meta.fileName];
return !hasDemos ?
<Article {...props} content={doc} /> :
<ComponentDoc {...props} doc={doc} />;
};
2016-03-02 11:57:37 +08:00
}