order.js 4.1 KB

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