@@ -9,6 +9,8 @@ const typeParser = require('./type-parser.js');
99
1010module . exports = toHTML ;
1111
12+ const STABILITY_TEXT_REG_EXP = / ( .* : ) \s * ( \d ) ( [ \s \S ] * ) / ;
13+
1214// customized heading without id attribute
1315var renderer = new marked . Renderer ( ) ;
1416renderer . heading = function ( text , level ) {
@@ -153,8 +155,11 @@ function parseLists(input) {
153155 var savedState = [ ] ;
154156 var depth = 0 ;
155157 var output = [ ] ;
158+ let headingIndex = - 1 ;
159+ let heading = null ;
160+
156161 output . links = input . links ;
157- input . forEach ( function ( tok ) {
162+ input . forEach ( function ( tok , index ) {
158163 if ( tok . type === 'blockquote_start' ) {
159164 savedState . push ( state ) ;
160165 state = 'MAYBE_STABILITY_BQ' ;
@@ -167,6 +172,17 @@ function parseLists(input) {
167172 if ( ( tok . type === 'paragraph' && state === 'MAYBE_STABILITY_BQ' ) ||
168173 tok . type === 'code' ) {
169174 if ( tok . text . match ( / S t a b i l i t y : .* / g) ) {
175+ const stabilityMatch = tok . text . match ( STABILITY_TEXT_REG_EXP ) ;
176+ const stability = Number ( stabilityMatch [ 2 ] ) ;
177+ const isStabilityIndex =
178+ index - 2 === headingIndex || // general
179+ index - 3 === headingIndex ; // with api_metadata block
180+
181+ if ( heading && isStabilityIndex ) {
182+ heading . stability = stability ;
183+ headingIndex = - 1 ;
184+ heading = null ;
185+ }
170186 tok . text = parseAPIHeader ( tok . text ) ;
171187 output . push ( { type : 'html' , text : tok . text } ) ;
172188 return ;
@@ -178,6 +194,8 @@ function parseLists(input) {
178194 if ( state === null ||
179195 ( state === 'AFTERHEADING' && tok . type === 'heading' ) ) {
180196 if ( tok . type === 'heading' ) {
197+ headingIndex = index ;
198+ heading = tok ;
181199 state = 'AFTERHEADING' ;
182200 }
183201 output . push ( tok ) ;
@@ -280,7 +298,7 @@ function linkJsTypeDocs(text) {
280298
281299function parseAPIHeader ( text ) {
282300 text = text . replace (
283- / ( . * : ) \s ( \d ) ( [ \s \S ] * ) / ,
301+ STABILITY_TEXT_REG_EXP ,
284302 '<pre class="api_stability api_stability_$2">$1 $2$3</pre>'
285303 ) ;
286304 return text ;
@@ -324,8 +342,8 @@ function buildToc(lexed, filename, cb) {
324342 const realFilename = path . basename ( realFilenames [ 0 ] , '.md' ) ;
325343 const id = getId ( realFilename + '_' + tok . text . trim ( ) ) ;
326344 toc . push ( new Array ( ( depth - 1 ) * 2 + 1 ) . join ( ' ' ) +
327- '* <a href="# ' + id + '">' +
328- tok . text + '</a>' ) ;
345+ '* <span class="stability_ ' + tok . stability + '">' +
346+ '<a href="#' + id + '">' + tok . text + '</a></span >' ) ;
329347 tok . text += '<span><a class="mark" href="#' + id + '" ' +
330348 'id="' + id + '">#</a></span>' ;
331349 } ) ;
0 commit comments