order.js 4.2 KB

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