order.js 4.2 KB

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