Skip to content

Support toc generation in the api-documenter #4931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
"docgen:exp": "ts-node-script scripts/exp/docgen.ts",
"postinstall": "yarn --cwd repo-scripts/changelog-generator build",
"sa": "ts-node-script repo-scripts/size-analysis/cli.ts",
"api-documenter-devsite": "ts-node-script repo-scripts/api-documenter/src/start.ts",
"toc-devsite": "ts-node-script scripts/exp/generate-devsite-toc.ts -i temp"
"api-documenter-devsite": "ts-node-script repo-scripts/api-documenter/src/start.ts"
},
"repository": {
"type": "git",
Expand Down
9 changes: 9 additions & 0 deletions repo-scripts/api-documenter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
It is a fork of [API Documenter](https://github.com/microsoft/rushstack/tree/master/apps/api-documenter)
It reads the *.api.json data files produced by [API Extractor](https://api-extractor.com/),
and then generates files in [Markdown](https://en.wikipedia.org/wiki/Markdown) format suitable for displaying in Firebase Devsite.

## Generate toc for Firebase devsite
`api-documenter-fire toc -i temp -p "/docs/reference/js/v9"`

`-i` and `-p` are required parameters.
Use `-i` to specify the folder that contains api.json files.
Use `-p` to specify the g3 path that contains the reference docs.

By default, the command will create `toc.yaml` in folder `/toc`. To change the output folder, use the flag `-o`.
4 changes: 3 additions & 1 deletion repo-scripts/api-documenter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
"@rushstack/ts-command-line": "4.7.8",
"colors": "~1.2.1",
"resolve": "~1.17.0",
"tslib": "^2.1.0"
"tslib": "^2.1.0",
"js-yaml": "4.0.0"
},
"devDependencies": {
"@types/js-yaml": "4.0.0",
"@types/resolve": "1.17.1",
"mocha-chai-jest-snapshot": "1.1.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import { CommandLineParser } from '@rushstack/ts-command-line';
import { MarkdownAction } from './MarkdownAction';
import { TocAction } from './TocAction';

export class ApiDocumenterCommandLine extends CommandLineParser {
public constructor() {
Expand All @@ -39,5 +40,6 @@ export class ApiDocumenterCommandLine extends CommandLineParser {

private _populateActions(): void {
this.addAction(new MarkdownAction(this));
this.addAction(new TocAction(this));
}
}
65 changes: 65 additions & 0 deletions repo-scripts/api-documenter/src/cli/TocAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { CommandLineStringParameter } from '@rushstack/ts-command-line';
import { ApiDocumenterCommandLine } from './ApiDocumenterCommandLine';
import { BaseAction } from './BaseAction';
import { generateToc } from '../toc';

export class TocAction extends BaseAction {
private _g3PathParameter!: CommandLineStringParameter;
public constructor(parser: ApiDocumenterCommandLine) {
super({
actionName: 'toc',
summary: 'Generate documentation as Markdown files (*.md)',
documentation:
'Generates API documentation as a collection of files in' +
' Markdown format, suitable for example for publishing on a GitHub site.'
});
}

protected onDefineParameters(): void {
super.onDefineParameters();

this._g3PathParameter = this.defineStringParameter({
parameterLongName: '--g3-path',
parameterShortName: '-p',
argumentName: 'G3PREFIX',
description: `Specifies the path where the reference docs will be written to in g3.
Used to generate paths in the toc`
});
}

protected async onExecute(): Promise<void> {
// override
const { apiModel, outputFolder, addFileNameSuffix } = this.buildApiModel();
const g3Path: string | undefined = this._g3PathParameter.value;

if (!g3Path) {
throw new Error(
'--g3-path is a required to generate toc, but it is not provided'
);
}

generateToc({
apiModel,
outputFolder,
addFileNameSuffix,
g3Path
});
}
}
97 changes: 97 additions & 0 deletions repo-scripts/api-documenter/src/toc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import yaml from 'js-yaml';
import { ApiItem, ApiItemKind, ApiModel } from 'api-extractor-model-me';
import { getFilenameForApiItem } from './documenters/MarkdownDocumenterHelpers';
import { ModuleSource } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference';
import { writeFileSync } from 'fs';
import { resolve } from 'path';

export interface ITocGenerationOptions {
apiModel: ApiModel;
g3Path: string;
outputFolder: string;
addFileNameSuffix: boolean;
}

interface ITocItem {
title: string;
path: string;
section?: ITocItem[];
}

export function generateToc({
apiModel,
g3Path,
outputFolder,
addFileNameSuffix
}: ITocGenerationOptions) {
const js: ITocItem = {
title: 'firebase',
path: `${g3Path}/index`,
section: []
};
const toc = [js];

generateTocRecursively(apiModel, g3Path, addFileNameSuffix, toc);

writeFileSync(
resolve(outputFolder, 'toc.yaml'),
yaml.dump(
{ toc },
{
quotingType: '"'
}
)
);
}

function generateTocRecursively(
apiItem: ApiItem,
g3Path: string,
addFileNameSuffix: boolean,
toc: ITocItem[]
) {
if (apiItem.kind === ApiItemKind.EntryPoint) {
// Entry point
const entryPointName = (apiItem.canonicalReference
.source! as ModuleSource).escapedPath.replace('@firebase/', '');
const entryPointToc: ITocItem = {
title: entryPointName,
path: `${g3Path}/${getFilenameForApiItem(apiItem, addFileNameSuffix)}`,
section: []
};

for (const member of apiItem.members) {
const fileName = getFilenameForApiItem(member, addFileNameSuffix);

if (fileName) {
entryPointToc.section!.push({
title: member.displayName,
path: `${g3Path}/${fileName}`
});
}
}
toc.push(entryPointToc);
} else {
// travel the api tree to find the next entry point
for (const member of apiItem.members) {
generateTocRecursively(member, g3Path, addFileNameSuffix, toc);
}
}
}
134 changes: 0 additions & 134 deletions scripts/exp/generate-devsite-toc.ts

This file was deleted.