forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-api-table.js
137 lines (116 loc) Β· 3.05 KB
/
sort-api-table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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;
}
// from small to large
const sizeBreakPoints = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'];
const groups = {
isDynamic: val => /^on[A-Z]/.test(val),
isSize: val => sizeBreakPoints.indexOf(val) > -1,
};
function asciiSort(prev, next) {
if (prev > next) {
return 1;
}
if (prev < next) {
return -1;
}
return 0;
}
// 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 => (
sizeBreakPoints.indexOf(getCellValue(val).toLowerCase())
))
);
});
}
function sort(ast) {
ast.children.forEach((child) => {
const staticProps = [];
// prefix with `on`
const dynamicProps = [];
// one of ['xs', 'sm', 'md', 'lg', 'xl']
const sizeProps = [];
// 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);
if (groups.isDynamic(value)) {
dynamicProps.push(node);
} else if (groups.isSize(value)) {
sizeProps.push(node);
} else {
staticProps.push(node);
}
});
child.children = [
child.children[0],
...alphabetSort(staticProps),
...sizeSort(sizeProps),
...alphabetSort(dynamicProps),
];
}
});
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 */
});
module.exports = {
getCellValue,
sort,
};