@@ -93,6 +93,7 @@ export interface IMarkdownDocumenterOptions {
93
93
outputFolder : string ;
94
94
addFileNameSuffix : boolean ;
95
95
projectName : string ;
96
+ sortFunctions : string ;
96
97
}
97
98
98
99
/**
@@ -108,13 +109,15 @@ export class MarkdownDocumenter {
108
109
private readonly _pluginLoader : PluginLoader ;
109
110
private readonly _addFileNameSuffix : boolean ;
110
111
private readonly _projectName : string ;
112
+ private readonly _sortFunctions : string ;
111
113
112
114
public constructor ( options : IMarkdownDocumenterOptions ) {
113
115
this . _apiModel = options . apiModel ;
114
116
this . _documenterConfig = options . documenterConfig ;
115
117
this . _outputFolder = options . outputFolder ;
116
118
this . _addFileNameSuffix = options . addFileNameSuffix ;
117
119
this . _projectName = options . projectName ;
120
+ this . _sortFunctions = options . sortFunctions ;
118
121
this . _tsdocConfiguration = CustomDocNodes . configuration ;
119
122
this . _markdownEmitter = new CustomMarkdownEmitter ( this . _apiModel ) ;
120
123
@@ -834,11 +837,13 @@ page_type: reference
834
837
headerTitles : [ 'Enumeration' , 'Description' ]
835
838
} ) ;
836
839
837
- const functionsTable : DocTable = new DocTable ( {
840
+ const finalFunctionsTable : DocTable = new DocTable ( {
838
841
configuration,
839
842
headerTitles : [ 'Function' , 'Description' ]
840
843
} ) ;
841
844
845
+ const functionsRowGroup : Record < string , DocTableRow [ ] > = { } ;
846
+
842
847
const interfacesTable : DocTable = new DocTable ( {
843
848
configuration,
844
849
headerTitles : [ 'Interface' , 'Description' ]
@@ -859,7 +864,8 @@ page_type: reference
859
864
headerTitles : [ 'Type Alias' , 'Description' ]
860
865
} ) ;
861
866
862
- const functionsDefinitions : DocNode [ ] = [ ] ;
867
+ const functionsDefinitionsGroup : Record < string , DocNode [ ] > = { } ;
868
+ const finalFunctionsDefinitions : DocNode [ ] = [ ] ;
863
869
const variablesDefinitions : DocNode [ ] = [ ] ;
864
870
const typeAliasDefinitions : DocNode [ ] = [ ] ;
865
871
const enumsDefinitions : DocNode [ ] = [ ] ;
@@ -899,10 +905,29 @@ page_type: reference
899
905
break ;
900
906
901
907
case ApiItemKind . Function :
902
- functionsTable . addRow ( row ) ;
903
- functionsDefinitions . push (
904
- ...this . _createCompleteOutputForApiItem ( apiMember )
905
- ) ;
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 : '' } ;
915
+ if ( ! functionsRowGroup [ firstParam . name ] ) {
916
+ functionsRowGroup [ firstParam . name ] = [ ] ;
917
+ }
918
+ if ( ! functionsDefinitionsGroup [ firstParam . name ] ) {
919
+ functionsDefinitionsGroup [ firstParam . name ] = [ ] ;
920
+ }
921
+ functionsRowGroup [ firstParam . name ] . push ( row ) ;
922
+ functionsDefinitionsGroup [ firstParam . name ] . push (
923
+ ...this . _createCompleteOutputForApiItem ( apiMember )
924
+ ) ;
925
+ } else {
926
+ finalFunctionsTable . addRow ( row ) ;
927
+ finalFunctionsDefinitions . push (
928
+ ...this . _createCompleteOutputForApiItem ( apiMember )
929
+ ) ;
930
+ }
906
931
break ;
907
932
908
933
case ApiItemKind . TypeAlias :
@@ -921,9 +946,59 @@ page_type: reference
921
946
}
922
947
}
923
948
924
- if ( functionsTable . rows . length > 0 ) {
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 ( priorityParams . includes ( a ) && priorityParams . includes ( b ) ) {
964
+ return priorityParams . indexOf ( a ) - priorityParams . indexOf ( b ) ;
965
+ } else if ( priorityParams . includes ( a ) ) {
966
+ return - 1 ;
967
+ } else if ( priorityParams . includes ( b ) ) {
968
+ return 1 ;
969
+ }
970
+ return a . localeCompare ( b ) ;
971
+ }
972
+ ) ;
973
+
974
+ for ( const paramKey of sortedFunctionsFirstParamKeys ) {
975
+ // Header for each group of functions grouped by first param.
976
+ // Doesn't make sense if there's only one group.
977
+ const headerText = paramKey ? `function(${ paramKey } ...)` : 'function()' ;
978
+ const formattedHeaderText = `<strong>${ headerText } </strong>` ;
979
+ if ( sortedFunctionsFirstParamKeys . length > 1 ) {
980
+ finalFunctionsTable . addRow (
981
+ new DocTableRow ( { configuration } , [
982
+ new DocTableCell ( { configuration } , [
983
+ new DocParagraph ( { configuration } , [
984
+ new DocPlainText ( { configuration, text : formattedHeaderText } )
985
+ ] )
986
+ ] )
987
+ ] )
988
+ ) ;
989
+ }
990
+ for ( const functionsRow of functionsRowGroup [ paramKey ] ) {
991
+ finalFunctionsTable . addRow ( functionsRow ) ;
992
+ }
993
+ for ( const functionDefinition of functionsDefinitionsGroup [ paramKey ] ) {
994
+ finalFunctionsDefinitions . push ( functionDefinition ) ;
995
+ }
996
+ }
997
+ }
998
+
999
+ if ( finalFunctionsTable . rows . length > 0 ) {
925
1000
output . push ( new DocHeading ( { configuration, title : 'Functions' } ) ) ;
926
- output . push ( functionsTable ) ;
1001
+ output . push ( finalFunctionsTable ) ;
927
1002
}
928
1003
929
1004
if ( classesTable . rows . length > 0 ) {
@@ -956,8 +1031,8 @@ page_type: reference
956
1031
output . push ( typeAliasesTable ) ;
957
1032
}
958
1033
959
- if ( functionsDefinitions . length > 0 ) {
960
- output . push ( ...functionsDefinitions ) ;
1034
+ if ( finalFunctionsDefinitions . length > 0 ) {
1035
+ output . push ( ...finalFunctionsDefinitions ) ;
961
1036
}
962
1037
963
1038
if ( variablesDefinitions . length > 0 ) {
0 commit comments