order.js 4.4 KB

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