@@ -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 ) ;
0 commit comments