Skip to content

Commit d0c77c7

Browse files
committed
Add section headers
1 parent d44ecab commit d0c77c7

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
import { ApiDocumenterCommandLine } from './ApiDocumenterCommandLine';
2222
import { BaseAction } from './BaseAction';
2323
import { MarkdownDocumenter } from '../documenters/MarkdownDocumenter';
24-
import { CommandLineFlagParameter } from '@rushstack/ts-command-line';
24+
import { CommandLineStringParameter } from '@rushstack/ts-command-line';
2525

2626
export class MarkdownAction extends BaseAction {
27-
private _shouldSortFunctions!: CommandLineFlagParameter;
27+
private _sortFunctions!: CommandLineStringParameter;
2828
public constructor(parser: ApiDocumenterCommandLine) {
2929
super({
3030
actionName: 'markdown',
@@ -38,18 +38,21 @@ export class MarkdownAction extends BaseAction {
3838
protected onDefineParameters(): void {
3939
super.onDefineParameters();
4040

41-
this._shouldSortFunctions = this.defineFlagParameter({
41+
this._sortFunctions = this.defineStringParameter({
4242
parameterLongName: '--sort-functions',
43+
argumentName: 'PRIORITY_PARAMS',
4344
description:
44-
`Sorts functions tables and listings by first parameter.`
45+
`Sorts functions tables and listings by first parameter.` +
46+
` Provide comma-separated strings for preferred params to be ` +
47+
`ordered first. Alphabetical otherwise.`
4548
});
4649
}
4750

4851
protected async onExecute(): Promise<void> {
4952
// override
5053
const { apiModel, outputFolder, addFileNameSuffix, projectName } =
5154
this.buildApiModel();
52-
const shouldSortFunctions: boolean = this._shouldSortFunctions.value;
55+
const sortFunctions: string = this._sortFunctions.value || '';
5356

5457
if (!projectName) {
5558
throw new Error('No project name provided. Use --project.');
@@ -61,7 +64,7 @@ export class MarkdownAction extends BaseAction {
6164
outputFolder,
6265
addFileNameSuffix,
6366
projectName,
64-
shouldSortFunctions
67+
sortFunctions
6568
});
6669
markdownDocumenter.generateFiles();
6770
}

repo-scripts/api-documenter/src/documenters/MarkdownDocumenter.ts

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export interface IMarkdownDocumenterOptions {
9393
outputFolder: string;
9494
addFileNameSuffix: boolean;
9595
projectName: string;
96-
shouldSortFunctions: boolean;
96+
sortFunctions: string;
9797
}
9898

9999
/**
@@ -109,15 +109,15 @@ export class MarkdownDocumenter {
109109
private readonly _pluginLoader: PluginLoader;
110110
private readonly _addFileNameSuffix: boolean;
111111
private readonly _projectName: string;
112-
private readonly _shouldSortFunctions: boolean;
112+
private readonly _sortFunctions: string;
113113

114114
public constructor(options: IMarkdownDocumenterOptions) {
115115
this._apiModel = options.apiModel;
116116
this._documenterConfig = options.documenterConfig;
117117
this._outputFolder = options.outputFolder;
118118
this._addFileNameSuffix = options.addFileNameSuffix;
119119
this._projectName = options.projectName;
120-
this._shouldSortFunctions = options.shouldSortFunctions;
120+
this._sortFunctions = options.sortFunctions;
121121
this._tsdocConfiguration = CustomDocNodes.configuration;
122122
this._markdownEmitter = new CustomMarkdownEmitter(this._apiModel);
123123

@@ -905,8 +905,13 @@ page_type: reference
905905
break;
906906

907907
case ApiItemKind.Function:
908-
if (this._shouldSortFunctions) {
909-
const firstParam = (apiMember as ApiParameterListMixin).parameters[0] || { name : '' };
908+
/**
909+
* If this option is set, group functions by first param.
910+
* Organize using a map where the key is the first param.
911+
*/
912+
if (this._sortFunctions) {
913+
const firstParam = (apiMember as ApiParameterListMixin)
914+
.parameters[0] || { name: '' };
910915
if (!functionsRowGroup[firstParam.name]) {
911916
functionsRowGroup[firstParam.name] = [];
912917
}
@@ -941,17 +946,48 @@ page_type: reference
941946
}
942947
}
943948

944-
if (this._shouldSortFunctions) {
945-
const sortedFunctionsFirstParamKeys = Object.keys(functionsRowGroup).sort((a, b) => {
946-
if (a === 'app') {
947-
return -1;
949+
/**
950+
* Sort the functions groups by first param. If priority params were
951+
* provided to --sort-functions, will put them first in the order
952+
* given.
953+
*/
954+
if (this._sortFunctions) {
955+
let priorityParams: string[] = [];
956+
if (this._sortFunctions.includes(',')) {
957+
priorityParams = this._sortFunctions.split(',');
958+
} else {
959+
priorityParams = [this._sortFunctions];
960+
}
961+
const sortedFunctionsFirstParamKeys = Object.keys(functionsRowGroup).sort(
962+
(a, b) => {
963+
if (!a || !b) {
964+
console.log(`Missing one. a:${a}, b:${b}`)
965+
}
966+
if (priorityParams.includes(a) && priorityParams.includes(b)) {
967+
return priorityParams.indexOf(a) - priorityParams.indexOf(b);
968+
} else if (priorityParams.includes(a)) {
969+
return -1;
970+
} else if (priorityParams.includes(b)) {
971+
return 1;
972+
}
973+
return a.localeCompare(b);
948974
}
949-
return (a.localeCompare(b));
950-
});
951-
975+
);
976+
952977
for (const paramKey of sortedFunctionsFirstParamKeys) {
953-
if (finalFunctionsTable.rows.length > 0) {
954-
finalFunctionsTable.createAndAddRow();
978+
// Header for each group of functions grouped by first param.
979+
// Doesn't make sense if there's only one group.
980+
const headerText = paramKey ? `function(${paramKey}...)` : 'function()';
981+
if (sortedFunctionsFirstParamKeys.length > 1) {
982+
finalFunctionsTable.addRow(
983+
new DocTableRow({ configuration }, [
984+
new DocTableCell({ configuration }, [
985+
new DocParagraph({ configuration }, [
986+
new DocPlainText({ configuration, text: headerText })
987+
])
988+
])
989+
])
990+
);
955991
}
956992
for (const functionsRow of functionsRowGroup[paramKey]) {
957993
finalFunctionsTable.addRow(functionsRow);

scripts/docgen/docgen.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ async function generateDocs(
182182
outputFolder,
183183
'--project',
184184
'js',
185-
'--sort-functions'
185+
'--sort-functions',
186+
'app,appCheck,auth,analytics,database,firestore,functions,storage,installations,messaging,performance,remoteConfig'
186187
],
187188
{ stdio: 'inherit' }
188189
);

0 commit comments

Comments
 (0)