order.js 4.2 KB

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