order.js 4.1 KB

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