ant-design/scripts/sort-api-table.js

138 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-10-17 11:38:21 +08:00
const program = require('commander');
const majo = require('majo');
const style = require('ansi-styles');
const unified = require('unified');
const parse = require('remark-parse');
const stringify = require('remark-stringify');
const yamlConfig = require('remark-yaml-config');
const frontmatter = require('remark-frontmatter');
const remarkWithYaml = unified()
.use(parse)
.use(stringify, {
paddedTable: false,
listItemIndent: 1,
})
.use(frontmatter)
.use(yamlConfig);
const stream = majo();
function getCellValue(node) {
return node.children[0].children[0].value;
}
2017-10-24 14:34:58 +08:00
// from small to large
const sizeBreakPoints = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'];
2017-10-17 11:38:21 +08:00
2017-10-24 14:34:58 +08:00
const groups = {
isDynamic: val => /^on[A-Z]/.test(val),
isSize: val => sizeBreakPoints.indexOf(val) > -1,
2017-10-24 14:34:58 +08:00
};
function asciiSort(prev, next) {
if (prev > next) {
return 1;
}
if (prev < next) {
return -1;
}
return 0;
}
2017-10-17 11:38:21 +08:00
2017-10-24 14:34:58 +08:00
// follow the alphabet order
function alphabetSort(nodes) {
// use toLowerCase to keep `case insensitive`
return nodes.sort((...comparison) => {
return asciiSort(...comparison.map(val => getCellValue(val).toLowerCase()));
});
}
function sizeSort(nodes) {
return nodes.sort((...comparison) => {
return asciiSort(
...comparison.map(val => (
2017-10-24 14:34:58 +08:00
sizeBreakPoints.indexOf(getCellValue(val).toLowerCase())
))
2017-10-24 14:34:58 +08:00
);
2017-10-17 11:38:21 +08:00
});
}
function sort(ast) {
ast.children.forEach((child) => {
2017-10-24 14:34:58 +08:00
const staticProps = [];
2017-10-17 11:38:21 +08:00
// prefix with `on`
2017-10-24 14:34:58 +08:00
const dynamicProps = [];
// one of ['xs', 'sm', 'md', 'lg', 'xl']
const sizeProps = [];
2017-10-17 11:38:21 +08:00
// find table markdown type
if (child.type === 'table') {
// slice will create new array, so sort can affect the original array.
// slice(1) cut down the thead
child.children.slice(1).forEach((node) => {
const value = getCellValue(node);
2017-10-24 14:34:58 +08:00
if (groups.isDynamic(value)) {
dynamicProps.push(node);
} else if (groups.isSize(value)) {
sizeProps.push(node);
2017-10-17 11:38:21 +08:00
} else {
2017-10-24 14:34:58 +08:00
staticProps.push(node);
2017-10-17 11:38:21 +08:00
}
});
child.children = [
child.children[0],
2017-10-24 14:34:58 +08:00
...alphabetSort(staticProps),
...sizeSort(sizeProps),
...alphabetSort(dynamicProps),
2017-10-17 11:38:21 +08:00
];
}
});
return ast;
}
function sortAPI(md) {
return remarkWithYaml.stringify(sort(remarkWithYaml.parse(md)));
}
program
.version('0.1.0')
.option(
'-f, --file [file]',
'Specify which file to be transformed',
// default value
'components/**/index.+(zh-CN|en-US).md'
)
.parse(process.argv);
function sortMiddleware(ctx) {
Object.keys(ctx.files).forEach((filename) => {
const content = ctx.fileContents(filename);
ctx.writeContents(filename, sortAPI(content));
});
}
// Get the markdown file all need to be transformed
stream
.source(program.file)
.use(sortMiddleware)
.dest('.')
.then(() => {
/* eslint-disable no-console */
console.log(
`${style.green.open}sort ant-design api successfully!${style.green.close}`
);
/* eslint-enable no-console */
});
2017-10-24 14:34:58 +08:00
module.exports = {
getCellValue,
sort,
};