1
0

order.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. 'carousel',
  49. 'collapse',
  50. 'collapsible',
  51. 'descriptions',
  52. 'dropdown',
  53. 'empty',
  54. 'list',
  55. 'modal',
  56. 'overflowlist',
  57. 'popover',
  58. 'scrolllist',
  59. 'sidesheet',
  60. 'table',
  61. 'tag',
  62. 'timeline',
  63. 'tooltip',
  64. 'banner',
  65. 'notification',
  66. 'popconfirm',
  67. 'progress',
  68. 'skeleton',
  69. 'spin',
  70. 'toast',
  71. 'configprovider',
  72. 'locale',
  73. ];
  74. let { exec } = require('child_process');
  75. let fs = require('fs');
  76. const executeShell = (command, callback) => {
  77. exec(command, (error, stdout, stderr) => {
  78. callback(stdout);
  79. });
  80. };
  81. module.exports = () => {
  82. const reOrderSingleMdx = (mdxPath, order) => {
  83. let data = fs.readFileSync(mdxPath, { encoding: 'utf-8' });
  84. let dataArray = data.split('---');
  85. let yaml = dataArray[1];
  86. const yamlArray = yaml.split('\n');
  87. let done = false;
  88. let localeDone = false;
  89. // let title=null;
  90. // let titleIndex=null;
  91. for (let index in yamlArray) {
  92. let info = yamlArray[index];
  93. // //
  94. // if(info.indexOf('subTitle')!==-1){
  95. // title=info.split(':')[1];
  96. // if(titleIndex){
  97. // yamlArray[titleIndex]=`title: ${title}`;
  98. // }
  99. // }
  100. // if(info.match(/title: /)){
  101. // if(title){
  102. // yamlArray[index]=`title: ${title}`;
  103. // }else{
  104. // titleIndex=index;
  105. // }
  106. // }
  107. // //
  108. let localeResult = info.match(/localeCode: /);
  109. if (localeResult) {
  110. localeDone = true;
  111. }
  112. let result = info.match(/order: \d/);
  113. if (result) {
  114. yamlArray[index] = `order: ${order}`;
  115. done = true;
  116. }
  117. }
  118. if (!done) {
  119. yamlArray.splice(1, 0, `order: ${order}`);
  120. }
  121. if (!localeDone) {
  122. const localCode = mdxPath.indexOf('en-US') !== -1 ? 'en-US' : 'zh-CN';
  123. yamlArray.splice(1, 0, `localeCode: ${localCode}`);
  124. console.log(`add localCode ${localCode} into ${mdxPath}'s yaml.`);
  125. }
  126. dataArray[1] = yamlArray.join('\n');
  127. const orderedData = dataArray.join('---');
  128. fs.writeFileSync(mdxPath, orderedData);
  129. };
  130. executeShell(`find ${process.cwd()}/content/* | grep index`, fileListStr => {
  131. let mdxFileList = fileListStr.split('\n').filter(filePath => {
  132. let res = filePath && fs.existsSync(filePath) && fs.statSync(filePath).isFile();
  133. if (!res && filePath) {
  134. console.error(`ERROR: ${filePath} not found or it's not a file.`);
  135. }
  136. return res;
  137. });
  138. mdxFileList.map(filePath => {
  139. const pathArray = filePath.split('/');
  140. const itemName = pathArray[pathArray.length - 2];
  141. const newOrder = order.indexOf(itemName) + 1;
  142. if (newOrder !== -1) {
  143. reOrderSingleMdx(filePath, newOrder);
  144. } else {
  145. console.error(`${itenName} not in order Array. Check your order.js or markdown folder name.`);
  146. }
  147. // console.log(`set ${itemName} to order ${newOrder}`)
  148. });
  149. console.log('ordered mdx');
  150. });
  151. };