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'));
|
2016-03-03 12:07:28 +08:00
|
|
|
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);
|
2016-03-03 12:07:28 +08:00
|
|
|
return sortedDemosList;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = function buildDemosList(demoList, outputPath) {
|
|
|
|
const parsedDemosList = parseDemosList(demoList);
|
2016-03-04 18:06:34 +08:00
|
|
|
const content = 'module.exports = ' + utils.stringify(parsedDemosList) + ';';
|
2016-02-29 14:27:11 +08:00
|
|
|
|
|
|
|
fs.writeFile(outputPath, content);
|
|
|
|
};
|
2016-03-03 12:07:28 +08:00
|
|
|
module.exports.parse = parseDemosList;
|