11// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22// See LICENSE in the project root for license information.
33
4- import { MarkdownDocumenterFeature , IMarkdownDocumenterFeatureOnBeforeWritePageArgs } from '@microsoft/api-documenter' ;
4+ import * as path from 'path' ;
5+ import yaml = require( 'js-yaml' ) ;
6+ import { FileSystem } from '@microsoft/node-core-library' ;
7+ import { ApiItem } from '@microsoft/api-extractor-model' ;
8+ import {
9+ MarkdownDocumenterFeature ,
10+ IMarkdownDocumenterFeatureOnBeforeWritePageArgs ,
11+ IMarkdownDocumenterFeatureOnFinishedArgs
12+ } from '@microsoft/api-documenter' ;
13+
14+ interface INavigationNode {
15+ title : string ;
16+ url ?: string ;
17+ subitems ?: INavigationNode [ ] ;
18+ }
19+ interface INavigationFile {
20+ api_nav : INavigationNode [ ] ;
21+ }
522
623export class RushStackFeature extends MarkdownDocumenterFeature {
24+ private _apiItemsWithPages : Set < ApiItem > = new Set < ApiItem > ( ) ;
25+
726 public onInitialized ( ) : void {
827 console . log ( 'RushStackFeature: onInitialized()' ) ;
928 }
1029
1130 public onBeforeWritePage ( eventArgs : IMarkdownDocumenterFeatureOnBeforeWritePageArgs ) : void {
31+ // Add the Jekyll header
1232 const header : string = [
1333 '---' ,
1434 'layout: page' ,
@@ -18,5 +38,39 @@ export class RushStackFeature extends MarkdownDocumenterFeature {
1838 ''
1939 ] . join ( '\n' ) ;
2040 eventArgs . pageContent = header + eventArgs . pageContent ;
41+
42+ this . _apiItemsWithPages . add ( eventArgs . apiItem ) ;
43+ }
44+
45+ public onFinished ( eventArgs : IMarkdownDocumenterFeatureOnFinishedArgs ) : void {
46+ const navigationFile : INavigationFile = {
47+ api_nav : [ ]
48+ } ;
49+ this . _buildNavigation ( navigationFile . api_nav , this . context . apiModel ) ;
50+
51+ const navFilePath : string = path . join ( this . context . outputFolder , '..' , 'api_nav.yaml' ) ;
52+ const navFileContent : string = yaml . safeDump ( navigationFile , { lineWidth : 120 } ) ;
53+
54+ FileSystem . writeFile ( navFilePath , navFileContent , { ensureFolderExists : true } ) ;
55+ }
56+
57+ private _buildNavigation ( parentNodes : INavigationNode [ ] , parentApiItem : ApiItem ) : void {
58+ for ( const apiItem of parentApiItem . members ) {
59+ if ( this . _apiItemsWithPages . has ( apiItem ) ) {
60+ const newNode : INavigationNode = {
61+ title : apiItem . displayName ,
62+ url : path . posix . join ( '/pages/api/' , this . context . documenter . getLinkForApiItem ( apiItem ) ! ) . replace ( / \. m d $ / , '' )
63+ } ;
64+ parentNodes . push ( newNode ) ;
65+
66+ const newNodeSubitems : INavigationNode [ ] = [ ] ;
67+ this . _buildNavigation ( newNodeSubitems , apiItem ) ;
68+ if ( newNodeSubitems . length > 0 ) {
69+ newNode . subitems = newNodeSubitems ;
70+ }
71+ } else {
72+ this . _buildNavigation ( parentNodes , apiItem ) ;
73+ }
74+ }
2175 }
2276}
0 commit comments