ant-design/scripts/build-demos-list.js

62 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-02-29 14:27:11 +08:00
'use strict';
const fs = require('fs');
const path = require('path');
const R = require('ramda');
const devil = require('./devil'); // TODO: extract as a module?
const utils = require('./utils');
const isIntro = function isIntro(element) {
const type = element.type;
return type !== 'h1' && type !== 'ul' && type !== 'code' && type !== 'hr';
};
const isCode = R.whereEq({ type: 'code', props: { lang: 'jsx' } });
const isStyle = R.whereEq({ type: 'code', props: { lang: 'css' } });
const getChildren = R.compose(R.prop('children'), R.defaultTo({}));
const sortByOrder = R.sortBy(R.prop('order'));
const parseDemosList = function parseDemosList(demoList) {
2016-02-29 14:27:11 +08:00
const demos = R.map((fileName) => {
const data = utils.parseFileContent(fileName);
const parts = fileName.split(path.sep);
const demo = {};
demo.order = parseInt(utils.parseMeta(data).order);
2016-03-04 18:06:34 +08:00
const demoIndex = parts.indexOf('demo');
demo.parent = parts.slice(0, demoIndex).join('/') + '/index.md';
2016-02-29 14:27:11 +08:00
demo.id = 'components-' + demo.parent + '-demo-' + path.basename(fileName, '.md');
demo.title = data[0].children;
demo.intro = data.filter(isIntro);
demo.code = getChildren(data.find(isCode));
2016-03-04 18:06:34 +08:00
demo.preview = devil(demo.code, ['React', 'ReactDOM', '_antd', 'BrowserDemo']);
2016-02-29 14:27:11 +08:00
demo.style = getChildren(data.find(isStyle));
return demo;
}, demoList);
const demosList = R.groupBy((demo) => demo.parent.replace('-', ''), demos);
const sortedDemosList = R.mapObjIndexed(sortByOrder, demosList);
return sortedDemosList;
};
module.exports = function buildDemosList(demoList, outputPath) {
2016-03-05 10:45:01 +08:00
const groupedDemos = R.groupBy((fileName) => {
const parts = fileName.split(path.sep);
const demoIndex = parts.indexOf('demo');
const relativeIndex = path.join(parts.slice(0, demoIndex).join(path.sep), 'index.md');
return relativeIndex;
}, demoList);
let content = 'module.exports = {';
Object.keys(groupedDemos).forEach((key) => {
content += `\n '${key}': [`;
groupedDemos[key].forEach((fileName) => {
2016-03-07 15:20:18 +08:00
content += `\n require('antd-md?demo&fileName=${fileName}!../../${fileName}'),`;
2016-03-05 10:45:01 +08:00
});
content += '\n ],'
});
content += '\n};';
2016-02-29 14:27:11 +08:00
fs.writeFile(outputPath, content);
};
module.exports.parse = parseDemosList;