|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Build stability table to documentation.html/json/md by generated all.json |
| 4 | + |
| 5 | +constfs=require('fs'); |
| 6 | +constpath=require('path'); |
| 7 | +constunified=require('unified'); |
| 8 | +constraw=require('rehype-raw'); |
| 9 | +constmarkdown=require('remark-parse'); |
| 10 | +consthtmlStringify=require('rehype-stringify'); |
| 11 | +constgfm=require('remark-gfm'); |
| 12 | +constremark2rehype=require('remark-rehype'); |
| 13 | +constvisit=require('unist-util-visit'); |
| 14 | + |
| 15 | +constsource=`${__dirname}/../../out/doc/api`; |
| 16 | +constdata=require(path.join(source,'all.json')); |
| 17 | +constmark='<!-- STABILITY_OVERVIEW_SLOT -->'; |
| 18 | + |
| 19 | +constoutput={ |
| 20 | +json: path.join(source,'stability.json'), |
| 21 | +docHTML: path.join(source,'documentation.html'), |
| 22 | +docJSON: path.join(source,'documentation.json'), |
| 23 | +docMarkdown: path.join(source,'documentation.md'), |
| 24 | +}; |
| 25 | + |
| 26 | +functioncollectStability(data){ |
| 27 | +conststability=[]; |
| 28 | + |
| 29 | +for(constmodofdata.modules){ |
| 30 | +if(mod.displayName&&mod.stability>=0){ |
| 31 | +constlink=mod.source.replace('doc/api/','').replace('.md','.html'); |
| 32 | +// const link = re.exec(toc)[1] |
| 33 | +stability.push({ |
| 34 | +api: mod.name, |
| 35 | +link: link, |
| 36 | +stability: mod.stability, |
| 37 | +stabilityText: `(${mod.stability}) ${mod.stabilityText}`, |
| 38 | +}); |
| 39 | +} |
| 40 | +} |
| 41 | + |
| 42 | +stability.sort((a,b)=>a.api.localeCompare(b.api)); |
| 43 | +returnstability; |
| 44 | +} |
| 45 | + |
| 46 | +functioncreateMarkdownTable(data){ |
| 47 | +constmd=['| API | Stability |','| --- | --------- |']; |
| 48 | + |
| 49 | +for(constmodofdata){ |
| 50 | +md.push(`| [${mod.api}](${mod.link}) | ${mod.stabilityText} |`); |
| 51 | +} |
| 52 | + |
| 53 | +returnmd.join('\n'); |
| 54 | +} |
| 55 | + |
| 56 | +functioncreateHTML(md){ |
| 57 | +constfile=unified() |
| 58 | +.use(markdown) |
| 59 | +.use(gfm) |
| 60 | +.use(remark2rehype,{allowDangerousHtml: true}) |
| 61 | +.use(raw) |
| 62 | +.use(htmlStringify) |
| 63 | +.use(processStability) |
| 64 | +.processSync(md); |
| 65 | + |
| 66 | +returnfile.contents.trim(); |
| 67 | +} |
| 68 | + |
| 69 | +functionprocessStability(){ |
| 70 | +return(tree)=>{ |
| 71 | +visit(tree,{type: 'element',tagName: 'tr'},(node)=>{ |
| 72 | +const[api,stability]=node.children; |
| 73 | +if(api.tagName!=='td'){ |
| 74 | +return; |
| 75 | +} |
| 76 | + |
| 77 | +api.properties.class='module_stability'; |
| 78 | + |
| 79 | +constlevel=stability.children[0].value[1]; |
| 80 | +stability.properties.class=`api_stability api_stability_${level}`; |
| 81 | +}); |
| 82 | +}; |
| 83 | +} |
| 84 | + |
| 85 | +functionupdateStabilityMark(file,value,mark){ |
| 86 | +constfd=fs.openSync(file,'r+'); |
| 87 | +constcontent=fs.readFileSync(fd); |
| 88 | + |
| 89 | +// Find the position of the `mark`. |
| 90 | +constindex=content.indexOf(mark); |
| 91 | + |
| 92 | +// Overwrite the mark with `value` parameter. |
| 93 | +constoffset=fs.writeSync(fd,value,index,'utf-8'); |
| 94 | + |
| 95 | +// Re-write the end of the file after `value`. |
| 96 | +fs.writeSync(fd,content,index+mark.length,undefined,index+offset); |
| 97 | +fs.closeSync(fd); |
| 98 | +} |
| 99 | + |
| 100 | +conststability=collectStability(data); |
| 101 | + |
| 102 | +// add markdown |
| 103 | +constmarkdownTable=createMarkdownTable(stability); |
| 104 | +updateStabilityMark(output.docMarkdown,markdownTable,mark); |
| 105 | + |
| 106 | +// add html table |
| 107 | +consthtml=createHTML(markdownTable); |
| 108 | +updateStabilityMark(output.docHTML,html,mark); |
| 109 | + |
| 110 | +// add json output |
| 111 | +updateStabilityMark(output.docJSON,JSON.stringify(html),JSON.stringify(mark)); |
0 commit comments