Skip to content

Commit 98d6eeb

Browse files
committed
RushStackFeature now generates a Jekyll navigation tree
1 parent 01913b8 commit 98d6eeb

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

Diff for: repo-scripts/doc-plugin-rush-stack/.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"--input-folder",
1616
"../../common/temp/api",
1717
"--output-folder",
18-
"./dist"
18+
"./dist/api"
1919
]
2020
},
2121
]

Diff for: repo-scripts/doc-plugin-rush-stack/src/RushStackFeature.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
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

623
export 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(/\.md$/, '')
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
}

Diff for: repo-scripts/generate-api-docs/generate-api-docs.cmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
@echo off
2-
.\node_modules\.bin\api-documenter generate --input-folder ..\..\common\temp\api --output-folder .\dist
2+
.\node_modules\.bin\api-documenter generate --input-folder ..\..\common\temp\api --output-folder .\dist\api

0 commit comments

Comments
 (0)