1
0

order.js 4.0 KB

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