1
0

order.js 4.1 KB

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