1
1
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
2
// See LICENSE in the project root for license information.
3
3
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
+ }
5
22
6
23
export class RushStackFeature extends MarkdownDocumenterFeature {
24
+ private _apiItemsWithPages : Set < ApiItem > = new Set < ApiItem > ( ) ;
25
+
7
26
public onInitialized ( ) : void {
8
27
console . log ( 'RushStackFeature: onInitialized()' ) ;
9
28
}
10
29
11
30
public onBeforeWritePage ( eventArgs : IMarkdownDocumenterFeatureOnBeforeWritePageArgs ) : void {
31
+ // Add the Jekyll header
12
32
const header : string = [
13
33
'---' ,
14
34
'layout: page' ,
@@ -18,5 +38,39 @@ export class RushStackFeature extends MarkdownDocumenterFeature {
18
38
''
19
39
] . join ( '\n' ) ;
20
40
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
+ }
21
75
}
22
76
}
0 commit comments