order.js 4.0 KB

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