order.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. ];
  95. let { exec } = require('child_process');
  96. let fs = require('fs');
  97. const executeShell = (command, callback) => {
  98. exec(command, (error, stdout, stderr) => {
  99. callback(stdout);
  100. });
  101. };
  102. module.exports = () => {
  103. const reOrderSingleMdx = (mdxPath, order) => {
  104. let data = fs.readFileSync(mdxPath, { encoding: 'utf-8' });
  105. let dataArray = data.split('---');
  106. let yaml = dataArray[1];
  107. const yamlArray = yaml.split('\n');
  108. let done = false;
  109. let localeDone = false;
  110. // let title=null;
  111. // let titleIndex=null;
  112. for (let index in yamlArray) {
  113. let info = yamlArray[index];
  114. // //
  115. // if(info.indexOf('subTitle')!==-1){
  116. // title=info.split(':')[1];
  117. // if(titleIndex){
  118. // yamlArray[titleIndex]=`title: ${title}`;
  119. // }
  120. // }
  121. // if(info.match(/title: /)){
  122. // if(title){
  123. // yamlArray[index]=`title: ${title}`;
  124. // }else{
  125. // titleIndex=index;
  126. // }
  127. // }
  128. // //
  129. let localeResult = info.match(/localeCode: /);
  130. if (localeResult) {
  131. localeDone = true;
  132. }
  133. let result = info.match(/order: \d/);
  134. if (result) {
  135. yamlArray[index] = `order: ${order}`;
  136. done = true;
  137. }
  138. }
  139. if (!done) {
  140. yamlArray.splice(1, 0, `order: ${order}`);
  141. }
  142. if (!localeDone) {
  143. const localCode = mdxPath.indexOf('en-US') !== -1 ? 'en-US' : 'zh-CN';
  144. yamlArray.splice(1, 0, `localeCode: ${localCode}`);
  145. console.log(`add localCode ${localCode} into ${mdxPath}'s yaml.`);
  146. }
  147. dataArray[1] = yamlArray.join('\n');
  148. const orderedData = dataArray.join('---');
  149. fs.writeFileSync(mdxPath, orderedData);
  150. };
  151. executeShell(`find ${process.cwd()}/content/* | grep index`, fileListStr => {
  152. let mdxFileList = fileListStr.split('\n').filter(filePath => {
  153. let res = filePath && fs.existsSync(filePath) && fs.statSync(filePath).isFile();
  154. if (!res && filePath) {
  155. console.error(`ERROR: ${filePath} not found or it's not a file.`);
  156. }
  157. return res;
  158. });
  159. mdxFileList.map(filePath => {
  160. const pathArray = filePath.split('/');
  161. const itemName = pathArray[pathArray.length - 2];
  162. const newOrder = order.indexOf(itemName) + 1;
  163. if (newOrder !== -1) {
  164. reOrderSingleMdx(filePath, newOrder);
  165. } else {
  166. console.error(`${itenName} not in order Array. Check your order.js or markdown folder name.`);
  167. }
  168. // console.log(`set ${itemName} to order ${newOrder}`)
  169. });
  170. console.log('ordered mdx');
  171. });
  172. };