generator.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. const fs = require("fs");
  2. // function decycle(object, replacer) {
  3. // let objects = new WeakMap(); // object to path mappings
  4. // return (function derez(value, path) {
  5. // let old_path; // The path of an earlier occurance of value
  6. // let nu; // The new object or array
  7. // if (replacer !== undefined) {
  8. // value = replacer(value);
  9. // }
  10. // if (typeof value === "object" && value !== null && !(value instanceof Boolean) && !(value instanceof Date) && !(value instanceof Number) && !(value instanceof RegExp) && !(value instanceof String)) {
  11. // old_path = objects.get(value);
  12. // if (old_path !== undefined) {
  13. // return { $ref: old_path };
  14. // }
  15. // objects.set(value, path);
  16. // if (Array.isArray(value)) {
  17. // nu = [];
  18. // value.forEach(function(element, i) {
  19. // nu[i] = derez(element, path + "[" + i + "]");
  20. // });
  21. // } else {
  22. // nu = {};
  23. // Object.keys(value).forEach(function(name) {
  24. // nu[name] = derez(value[name], path + "[" + JSON.stringify(name) + "]");
  25. // });
  26. // }
  27. // return nu;
  28. // }
  29. // return value;
  30. // })(object, "$");
  31. // }
  32. // function retrocycle($) {
  33. // let px = /^\$(?:\[(?:\d+|"(?:[^\\"\u0000-\u001f]|\\(?:[\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*")\])*$/;
  34. // (function rez(value) {
  35. // if (value && typeof value === "object") {
  36. // if (Array.isArray(value)) {
  37. // value.forEach(function(element, i) {
  38. // if (typeof element === "object" && element !== null) {
  39. // let path = element.$ref;
  40. // if (typeof path === "string" && px.test(path)) {
  41. // value[i] = eval(path);
  42. // } else {
  43. // rez(element);
  44. // }
  45. // }
  46. // });
  47. // } else {
  48. // Object.keys(value).forEach(function(name) {
  49. // let item = value[name];
  50. // if (typeof item === "object" && item !== null) {
  51. // let path = item.$ref;
  52. // if (typeof path === "string" && px.test(path)) {
  53. // value[name] = eval(path);
  54. // } else {
  55. // rez(item);
  56. // }
  57. // }
  58. // });
  59. // }
  60. // }
  61. // })($);
  62. // return $;
  63. // }
  64. class SingleMdxProcessor {
  65. constructor(mdxNode) {
  66. this.belong=`component`;
  67. this.mdxID = mdxNode.id;
  68. this.slug = mdxNode.fields.slug;
  69. this.headingContext=mdxNode.tableOfContents.items;
  70. this.locale = this.slug.indexOf("en-US") === -1 ? "zh-CN" : "en-US";
  71. this.nodeMap={}
  72. this.mdxInfo = {
  73. slug: this.slug,
  74. belong:this.belong,
  75. brief: mdxNode.frontmatter.brief,
  76. title: mdxNode.frontmatter.title,
  77. folder:mdxNode.fields.type,
  78. nodeUniqueIDList: [],
  79. };
  80. this.closestAnchorAncestorUniqueID=null;
  81. this.headingNodeMap={}
  82. this.processChildNode(mdxNode.mdxAST,null,null);
  83. this.processHeadingParent();
  84. }
  85. getMdxInfo(){
  86. //暂时无用 删除以缩小文件体积
  87. delete this.mdxInfo['nodeUniqueIDList'];
  88. return {mdxInfo:this.mdxInfo,locale:this.locale,nodeMap:this.nodeMap};
  89. }
  90. processHeadingParent(ancestor=null,items=this.headingContext){
  91. if(!items)
  92. return;
  93. for(let item of items){
  94. let headingNode=this.headingNodeMap[item.title];
  95. if(!headingNode)
  96. continue;
  97. headingNode.parent=ancestor?ancestor.uniqueID:null;
  98. //使用 heading context 修正 anchor
  99. headingNode.anchor=item.url?item.url:headingNode.value;
  100. if(item.items){
  101. this.processHeadingParent(headingNode,item.items);
  102. }
  103. }
  104. }
  105. processChildNode(childNode, meaningfulType) {
  106. const belong=this.belong;
  107. const mdxID = this.mdxID;
  108. const mdxInfo = this.mdxInfo;
  109. const type=childNode.type;
  110. const uniqueID = `${mdxID}#${type}#${Math.random()}`;
  111. if (type === "heading") {
  112. const value = childNode.children[0].value;
  113. const anchor = '#'+value;
  114. const node = {
  115. uniqueID,
  116. belong,
  117. type,
  118. value,
  119. parent:null,
  120. anchor,
  121. mdxInfo,
  122. meaningfulType: "heading",
  123. };
  124. this.nodeMap[uniqueID] = node;
  125. this.closestAnchorAncestorUniqueID=node.uniqueID;
  126. this.mdxInfo.nodeUniqueIDList.push(uniqueID);
  127. this.headingNodeMap[value]=node;
  128. return;
  129. }
  130. if (type === "text") {
  131. const value = childNode.value;
  132. const node = {
  133. uniqueID,
  134. belong,
  135. type,
  136. value,
  137. parent:this.closestAnchorAncestorUniqueID,
  138. mdxInfo,
  139. meaningfulType,
  140. };
  141. this.nodeMap[uniqueID] = node;
  142. this.mdxInfo.nodeUniqueIDList.push(uniqueID);
  143. return;
  144. }
  145. if (type === "code" || type === "jsx") {
  146. return;
  147. const value = childNode.value;
  148. const node = {
  149. uniqueID,
  150. belong,
  151. type,
  152. value,
  153. parent:this.closestAnchorAncestorUniqueID,
  154. mdxInfo,
  155. meaningfulType: type,
  156. };
  157. this.nodeMap[uniqueID] = node;
  158. this.mdxInfo.nodeUniqueIDList.push(uniqueID);
  159. return;
  160. }
  161. if (type === "list"||type==='listItem'||type==='root'||type==='strong') {
  162. for (let child of childNode.children) {
  163. this.processChildNode(child,type);
  164. }
  165. return;
  166. }
  167. if (type === "paragraph" || type === "table" || type === "tableCell") {
  168. for (let child of childNode.children) {
  169. this.processChildNode(child, type === "paragraph" ? "paragraph" : "table");
  170. }
  171. return;
  172. }
  173. }
  174. }
  175. function processGraphQLData(rawGraphQLData,extraDataCallback) {
  176. const {data}=rawGraphQLData;
  177. // let data = null;
  178. // try {
  179. // data = JSON.parse(fs.readFileSync("search/data_graphQL.json")).data;
  180. // if (!data) {
  181. // console.error("FATAL ERROR: GraphQL data in empty. Data:", data);
  182. // process.exit(1);
  183. // }
  184. // } catch (e) {
  185. // console.error("FATAL ERROR: Run GraphQL for searching failed! Error message: " + e);
  186. // process.exit(1);
  187. // }
  188. let dataToClient = {
  189. 'zh-CN': { mdxInfoList: [], nodeMap: {} },
  190. 'en-US': { mdxInfoList: [], nodeMap: {} },
  191. };
  192. const nodeArray = data.allMdx.nodes;
  193. //开始解析mdxNode
  194. nodeArray.map(mdxNode=>{
  195. const {mdxInfo,locale,nodeMap}=(new SingleMdxProcessor(mdxNode,dataToClient)).getMdxInfo();
  196. dataToClient[locale].mdxInfoList.push(mdxInfo);
  197. Object.assign(dataToClient[locale].nodeMap,nodeMap);
  198. })
  199. //暂时无用 删除以缩小文件体积
  200. dataToClient['zh-CN'].mdxInfoList;
  201. dataToClient['en-US'].mdxInfoList;
  202. extraDataCallback&&extraDataCallback(dataToClient);
  203. try {
  204. fs.writeFileSync("search/data_client.json",JSON.stringify(dataToClient));
  205. } catch (e) {
  206. console.error("FATAL ERROR: Can not write search data to disk.");
  207. process.exit(1);
  208. }
  209. }
  210. module.exports=processGraphQLData;