order.js 4.0 KB

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