order.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const order = [
  2. 'introduction',
  3. 'getting-started',
  4. 'customize-theme',
  5. 'dark-mode',
  6. 'changelog',
  7. 'update-to-v2',
  8. 'faq',
  9. 'overview',
  10. 'grid',
  11. 'icon',
  12. 'layout',
  13. 'tokens',
  14. 'space',
  15. 'typography',
  16. 'autocomplete',
  17. 'button',
  18. 'cascader',
  19. 'checkbox',
  20. 'datepicker',
  21. 'form',
  22. 'input',
  23. 'inputnumber',
  24. 'radio',
  25. 'rating',
  26. 'select',
  27. 'slider',
  28. 'switch',
  29. 'taginput',
  30. 'timepicker',
  31. 'transfer',
  32. 'treeselect',
  33. 'upload',
  34. 'anchor',
  35. 'backtop',
  36. 'breadcrumb',
  37. 'navigation',
  38. 'pagination',
  39. 'steps',
  40. 'tabs',
  41. 'tree',
  42. 'avatar',
  43. 'badge',
  44. 'calendar',
  45. 'card',
  46. 'collapse',
  47. 'collapsible',
  48. 'descriptions',
  49. 'dropdown',
  50. 'empty',
  51. 'list',
  52. 'modal',
  53. 'overflowlist',
  54. 'popover',
  55. 'scrolllist',
  56. 'sidesheet',
  57. 'table',
  58. 'tag',
  59. 'timeline',
  60. 'tooltip',
  61. 'banner',
  62. 'notification',
  63. 'popconfirm',
  64. 'progress',
  65. 'skeleton',
  66. 'spin',
  67. 'toast',
  68. 'configprovider',
  69. 'locale',
  70. ];
  71. let { exec } = require('child_process');
  72. let fs = require('fs');
  73. const executeShell = (command, callback) => {
  74. exec(command, (error, stdout, stderr) => {
  75. callback(stdout);
  76. });
  77. };
  78. module.exports = () => {
  79. const reOrderSingleMdx = (mdxPath, order) => {
  80. let data = fs.readFileSync(mdxPath, { encoding: 'utf-8' });
  81. let dataArray = data.split('---');
  82. let yaml = dataArray[1];
  83. const yamlArray = yaml.split('\n');
  84. let done = false;
  85. let localeDone = false;
  86. // let title=null;
  87. // let titleIndex=null;
  88. for (let index in yamlArray) {
  89. let info = yamlArray[index];
  90. // //
  91. // if(info.indexOf('subTitle')!==-1){
  92. // title=info.split(':')[1];
  93. // if(titleIndex){
  94. // yamlArray[titleIndex]=`title: ${title}`;
  95. // }
  96. // }
  97. // if(info.match(/title: /)){
  98. // if(title){
  99. // yamlArray[index]=`title: ${title}`;
  100. // }else{
  101. // titleIndex=index;
  102. // }
  103. // }
  104. // //
  105. let localeResult = info.match(/localeCode: /);
  106. if (localeResult) {
  107. localeDone = true;
  108. }
  109. let result = info.match(/order: \d/);
  110. if (result) {
  111. yamlArray[index] = `order: ${order}`;
  112. done = true;
  113. }
  114. }
  115. if (!done) {
  116. yamlArray.splice(1, 0, `order: ${order}`);
  117. }
  118. if (!localeDone) {
  119. const localCode = mdxPath.indexOf('en-US') !== -1 ? 'en-US' : 'zh-CN';
  120. yamlArray.splice(1, 0, `localeCode: ${localCode}`);
  121. console.log(`add localCode ${localCode} into ${mdxPath}'s yaml.`);
  122. }
  123. dataArray[1] = yamlArray.join('\n');
  124. const orderedData = dataArray.join('---');
  125. fs.writeFileSync(mdxPath, orderedData);
  126. };
  127. executeShell(`find ${process.cwd()}/content/* | grep index`, fileListStr => {
  128. let mdxFileList = fileListStr.split('\n').filter(filePath => {
  129. let res = filePath && fs.existsSync(filePath) && fs.statSync(filePath).isFile();
  130. if (!res && filePath) {
  131. console.error(`ERROR: ${filePath} not found or it's not a file.`);
  132. }
  133. return res;
  134. });
  135. mdxFileList.map(filePath => {
  136. const pathArray = filePath.split('/');
  137. const itemName = pathArray[pathArray.length - 2];
  138. const newOrder = order.indexOf(itemName) + 1;
  139. if (newOrder !== -1) {
  140. reOrderSingleMdx(filePath, newOrder);
  141. } else {
  142. console.error(`${itenName} not in order Array. Check your order.js or markdown folder name.`);
  143. }
  144. // console.log(`set ${itemName} to order ${newOrder}`)
  145. });
  146. console.log('ordered mdx');
  147. });
  148. };