order.js 4.4 KB

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