order.js 4.3 KB

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