order.js 4.1 KB

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