Skip to content

Commit fd9d014

Browse files
committed
make toc generation part of the api-documenter
1 parent 4bdc9f3 commit fd9d014

File tree

6 files changed

+168
-137
lines changed

6 files changed

+168
-137
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454
"docgen:exp": "ts-node-script scripts/exp/docgen.ts",
5555
"postinstall": "yarn --cwd repo-scripts/changelog-generator build",
5656
"sa": "ts-node-script repo-scripts/size-analysis/cli.ts",
57-
"api-documenter-devsite": "ts-node-script repo-scripts/api-documenter/src/start.ts",
58-
"toc-devsite": "ts-node-script scripts/exp/generate-devsite-toc.ts -i temp"
57+
"api-documenter-devsite": "ts-node-script repo-scripts/api-documenter/src/start.ts"
5958
},
6059
"repository": {
6160
"type": "git",

repo-scripts/api-documenter/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
"@rushstack/ts-command-line": "4.7.8",
2828
"colors": "~1.2.1",
2929
"resolve": "~1.17.0",
30-
"tslib": "^2.1.0"
30+
"tslib": "^2.1.0",
31+
"js-yaml": "4.0.0"
3132
},
3233
"devDependencies": {
34+
"@types/js-yaml": "4.0.0",
3335
"@types/resolve": "1.17.1",
3436
"mocha-chai-jest-snapshot": "1.1.1"
3537
}

repo-scripts/api-documenter/src/cli/ApiDocumenterCommandLine.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import { CommandLineParser } from '@rushstack/ts-command-line';
2222
import { MarkdownAction } from './MarkdownAction';
23+
import { TocAction } from './TocAction';
2324

2425
export class ApiDocumenterCommandLine extends CommandLineParser {
2526
public constructor() {
@@ -39,5 +40,6 @@ export class ApiDocumenterCommandLine extends CommandLineParser {
3940

4041
private _populateActions(): void {
4142
this.addAction(new MarkdownAction(this));
43+
this.addAction(new TocAction(this));
4244
}
4345
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { CommandLineStringParameter } from '@rushstack/ts-command-line';
19+
import { ApiDocumenterCommandLine } from './ApiDocumenterCommandLine';
20+
import { BaseAction } from './BaseAction';
21+
import { generateToc } from '../toc';
22+
23+
export class TocAction extends BaseAction {
24+
private _g3PathParameter!: CommandLineStringParameter;
25+
public constructor(parser: ApiDocumenterCommandLine) {
26+
super({
27+
actionName: 'toc',
28+
summary: 'Generate documentation as Markdown files (*.md)',
29+
documentation:
30+
'Generates API documentation as a collection of files in' +
31+
' Markdown format, suitable for example for publishing on a GitHub site.'
32+
});
33+
}
34+
35+
protected onDefineParameters(): void {
36+
super.onDefineParameters();
37+
38+
this._g3PathParameter = this.defineStringParameter({
39+
parameterLongName: '--g3-path',
40+
parameterShortName: '-p',
41+
argumentName: 'G3PREFIX',
42+
description: `Specifies the path where the reference docs will be written to in g3.
43+
Used to generate paths in the toc`
44+
});
45+
}
46+
47+
protected async onExecute(): Promise<void> {
48+
// override
49+
const { apiModel, outputFolder, addFileNameSuffix } = this.buildApiModel();
50+
const g3Path: string | undefined = this._g3PathParameter.value;
51+
52+
if (!g3Path) {
53+
throw new Error(
54+
'--g3-path is a required to generate toc, but it is not provided'
55+
);
56+
}
57+
58+
generateToc({
59+
apiModel,
60+
outputFolder,
61+
addFileNameSuffix,
62+
g3Path
63+
});
64+
}
65+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import yaml from 'js-yaml';
19+
import { ApiItem, ApiItemKind, ApiModel } from 'api-extractor-model-me';
20+
import { getFilenameForApiItem } from './documenters/MarkdownDocumenterHelpers';
21+
import { ModuleSource } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference';
22+
import { writeFileSync } from 'fs';
23+
import { resolve } from 'path';
24+
25+
export interface ITocGenerationOptions {
26+
apiModel: ApiModel;
27+
g3Path: string;
28+
outputFolder: string;
29+
addFileNameSuffix: boolean;
30+
}
31+
32+
interface ITocItem {
33+
title: string;
34+
path: string;
35+
section?: ITocItem[];
36+
}
37+
38+
export function generateToc({
39+
apiModel,
40+
g3Path,
41+
outputFolder,
42+
addFileNameSuffix
43+
}: ITocGenerationOptions) {
44+
const js: ITocItem = {
45+
title: 'firebase',
46+
path: `${g3Path}/index`,
47+
section: []
48+
};
49+
const toc = [js];
50+
51+
generateTocRecursively(apiModel, g3Path, addFileNameSuffix, toc);
52+
53+
writeFileSync(
54+
resolve(outputFolder, 'toc.yaml'),
55+
yaml.dump(
56+
{ toc },
57+
{
58+
quotingType: '"'
59+
}
60+
)
61+
);
62+
}
63+
64+
function generateTocRecursively(
65+
apiItem: ApiItem,
66+
g3Path: string,
67+
addFileNameSuffix: boolean,
68+
toc: ITocItem[]
69+
) {
70+
if (apiItem.kind === ApiItemKind.EntryPoint) {
71+
// Entry point
72+
const entryPointName = (apiItem.canonicalReference
73+
.source! as ModuleSource).escapedPath.replace('@firebase/', '');
74+
const entryPointToc: ITocItem = {
75+
title: entryPointName,
76+
path: `${g3Path}/${getFilenameForApiItem(apiItem, addFileNameSuffix)}`,
77+
section: []
78+
};
79+
80+
for (const member of apiItem.members) {
81+
const fileName = getFilenameForApiItem(member, addFileNameSuffix);
82+
83+
if (fileName) {
84+
entryPointToc.section!.push({
85+
title: member.displayName,
86+
path: `${g3Path}/${fileName}`
87+
});
88+
}
89+
}
90+
toc.push(entryPointToc);
91+
} else {
92+
// travel the api tree to find the next entry point
93+
for (const member of apiItem.members) {
94+
generateTocRecursively(member, g3Path, addFileNameSuffix, toc);
95+
}
96+
}
97+
}

scripts/exp/generate-devsite-toc.ts

Lines changed: 0 additions & 134 deletions
This file was deleted.

0 commit comments

Comments
 (0)