-
Notifications
You must be signed in to change notification settings - Fork 620
/
Copy pathRushStackFeature.ts
83 lines (72 loc) · 2.59 KB
/
RushStackFeature.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'path';
import yaml = require('js-yaml');
import { FileSystem } from '@rushstack/node-core-library';
import { ApiItem } from '@microsoft/api-extractor-model';
import {
MarkdownDocumenterFeature,
IMarkdownDocumenterFeatureOnBeforeWritePageArgs,
IMarkdownDocumenterFeatureOnFinishedArgs
} from '@microsoft/api-documenter';
interface INavigationNode {
title: string;
url?: string;
subitems?: INavigationNode[];
}
interface INavigationFile {
api_nav: INavigationNode[];
}
export class RushStackFeature extends MarkdownDocumenterFeature {
private _apiItemsWithPages: Set<ApiItem> = new Set<ApiItem>();
public onInitialized(): void {
console.log('RushStackFeature: onInitialized()');
}
public onBeforeWritePage(eventArgs: IMarkdownDocumenterFeatureOnBeforeWritePageArgs): void {
// Add the Jekyll header
const header: string = [
'---',
'layout: page',
'navigation_source: api_nav',
'improve_this_button: false',
'---',
''
].join('\n');
eventArgs.pageContent = header + eventArgs.pageContent;
this._apiItemsWithPages.add(eventArgs.apiItem);
}
public onFinished(eventArgs: IMarkdownDocumenterFeatureOnFinishedArgs): void {
const navigationFile: INavigationFile = {
api_nav: [
{
title: 'API Reference',
url: '/pages/api/'
}
]
};
this._buildNavigation(navigationFile.api_nav, this.context.apiModel);
const navFilePath: string = path.join(this.context.outputFolder, '..', 'api_nav.yaml');
const navFileContent: string = yaml.safeDump(navigationFile, { lineWidth: 120 });
FileSystem.writeFile(navFilePath, navFileContent, { ensureFolderExists: true });
}
private _buildNavigation(parentNodes: INavigationNode[], parentApiItem: ApiItem): void {
for (const apiItem of parentApiItem.members) {
if (this._apiItemsWithPages.has(apiItem)) {
const newNode: INavigationNode = {
title: apiItem.displayName,
url: path.posix
.join('/pages/api/', this.context.documenter.getLinkForApiItem(apiItem)!)
.replace(/\.md$/, '')
};
parentNodes.push(newNode);
const newNodeSubitems: INavigationNode[] = [];
this._buildNavigation(newNodeSubitems, apiItem);
if (newNodeSubitems.length > 0) {
newNode.subitems = newNodeSubitems;
}
} else {
this._buildNavigation(parentNodes, apiItem);
}
}
}
}