forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableHelpers.ts
More file actions
818 lines (758 loc) · 24 KB
/
TableHelpers.ts
File metadata and controls
818 lines (758 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import {
ApiDocumentedItem,
type ApiItem,
ApiItemKind,
type ApiPackage,
type ApiPropertyItem,
ApiReturnTypeMixin,
type Excerpt,
type Parameter,
type TypeParameter,
type ApiVariable,
} from "@microsoft/api-extractor-model";
import type { DocSection } from "@microsoft/tsdoc";
import { toHtml } from "hast-util-to-html";
import type { BlockContent, Html, PhrasingContent, Table, TableCell } from "mdast";
import { toHast } from "mdast-util-to-hast";
import type { Section } from "../../mdast/index.js";
import {
type ApiFunctionLike,
type ApiModifier,
getDefaultValueBlock,
getModifiers,
injectSeparator,
} from "../../utilities/index.js";
import { transformTsdoc } from "../TsdocNodeTransforms.js";
import type { ApiItemTransformationConfiguration } from "../configuration/index.js";
import { getLinkForApiItem } from "../utilities/index.js";
import { createExcerptSpanWithHyperlinks } from "./Helpers.js";
import { createTableFromItems } from "./TableCreation.js";
/**
* Input properties for creating a table of API members
*/
export interface MemberTableProperties {
/**
* Heading text to display above the table contents.
*/
headingTitle: string;
/**
* The kind of API item.
*/
itemKind: ApiItemKind;
/**
* The items to be displayed as rows in the table.
*/
items: readonly ApiItem[];
/**
* Creation options for the table.
*/
options?: TableCreationOptions;
}
/**
* Content / formatting options for table creation.
*/
export interface TableCreationOptions {
/**
* A list of modifiers to omit from table creation.
*
* @defaultValue No modifier kinds will be excluded.
*/
modifiersToOmit?: ApiModifier[];
}
/**
* Creates a simple section containing a series of headings and tables, representing the API members of some parent
* item, organized by kind.
*
* @param memberTableProperties - List of table configurations.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
export function createMemberTables(
memberTableProperties: readonly MemberTableProperties[],
config: ApiItemTransformationConfiguration,
): Section[] | undefined {
const sections: Section[] = [];
for (const member of memberTableProperties) {
const table = createTableWithHeading(member, config);
if (table !== undefined) {
sections.push(table);
}
}
return sections.length === 0 ? undefined : sections;
}
/**
* Creates a simple section containing a heading and a table, based on the provided properties.
*
* @param memberTableProperties - The table configuration.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
function createTableWithHeading(
memberTableProperties: MemberTableProperties,
config: ApiItemTransformationConfiguration,
): Section | undefined {
const table = createSummaryTable(
memberTableProperties.items,
memberTableProperties.itemKind,
config,
memberTableProperties.options,
);
return table === undefined
? undefined
: {
type: "section",
children: [table],
heading: {
type: "sectionHeading",
title: memberTableProperties.headingTitle,
},
};
}
/**
* Creates a simple summary table for API items of the specified kind.
* This is intended to represent a simple overview of the items.
*
* @remarks General use-case is to display a summary of child items of a given kind for some parent API item.
*
* @param apiItems - The items to be displayed. All of these items must be of the kind specified via `itemKind`.
* @param itemKind - The kind of items being displayed in the table. Used to determine the semantic shape of the table.
* @param config - See {@link ApiItemTransformationConfiguration}.
* @param options - Table content / formatting options.
*/
function createSummaryTable(
apiItems: readonly ApiItem[],
itemKind: ApiItemKind,
config: ApiItemTransformationConfiguration,
options?: TableCreationOptions,
): Table | undefined {
if (itemKind === ApiItemKind.Model || itemKind === ApiItemKind.EntryPoint) {
throw new Error(
`Summary table creation does not support provided API item kind: "${itemKind}".`,
);
}
if (apiItems.length === 0) {
return undefined;
}
switch (itemKind) {
case ApiItemKind.CallSignature:
case ApiItemKind.ConstructSignature:
case ApiItemKind.Constructor:
case ApiItemKind.Function:
case ApiItemKind.Method:
case ApiItemKind.MethodSignature: {
return createFunctionLikeSummaryTable(
apiItems as ApiFunctionLike[],
getTableHeadingTitleForApiKind(itemKind),
config,
options,
);
}
case ApiItemKind.Property:
case ApiItemKind.PropertySignature: {
return createPropertiesTable(apiItems as ApiPropertyItem[], config, options);
}
case ApiItemKind.Variable: {
return createVariablesTable(apiItems as ApiVariable[], config, options);
}
case ApiItemKind.Package: {
return createPackagesTable(apiItems as ApiPackage[], config);
}
default: {
return createDefaultSummaryTable(
apiItems,
getTableHeadingTitleForApiKind(itemKind),
config,
options,
);
}
}
}
// TODO: Remove this
/**
* Default summary table generation. Displays each item's name, modifiers, and description (summary) comment.
*
* @param apiItems - The items to be displayed. All of these items must be of the kind specified via `itemKind`.
* @param nameColumnLabel - The label for the "name" column in the table.
* @param config - See {@link ApiItemTransformationConfiguration}.
* @param options - Table content / formatting options.
*/
export function createDefaultSummaryTable(
apiItems: readonly ApiItem[],
nameColumnLabel: string,
config: ApiItemTransformationConfiguration,
options?: TableCreationOptions,
): Table | undefined {
if (apiItems.length === 0) {
return undefined;
}
return createTableFromItems(apiItems, {
columnOptions: [
{
title: { type: "text", value: nameColumnLabel },
columnKind: "required",
createCellContent: (item) => createNameCell(item, config),
},
{
title: { type: "text", value: "Alerts" },
columnKind: "optional",
createCellContent: (item) => createAlertsCell(config.getAlertsForItem(item)),
},
{
title: { type: "text", value: "Modifiers" },
columnKind: "optional",
createCellContent: (item) => createModifiersCell(item, options?.modifiersToOmit),
},
{
title: { type: "text", value: "Description" },
columnKind: "required",
createCellContent: (item) => createDescriptionCell(item, config),
},
],
});
}
/**
* Creates a simple summary table for a series of parameters.
* Displays each parameter's name, type, and description ({@link https://tsdoc.org/pages/tags/param/ | @param}) comment.
*
* @param apiParameters - The items to be displayed. All of these items must be of the kind specified via `itemKind`.
* @param contextApiItem - The API item with which the parameter is associated.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
export function createParametersSummaryTable(
apiParameters: readonly Parameter[],
contextApiItem: ApiItem,
config: ApiItemTransformationConfiguration,
): Table | undefined {
if (apiParameters.length === 0) {
return undefined;
}
function createModifierCell(apiParameter: Parameter): TableCell | undefined {
return apiParameter.isOptional ? createPlainTextTableCell("optional") : undefined;
}
function createParameterTypeCell(apiParameter: Parameter): TableCell | undefined {
return createTypeExcerptCell(apiParameter.parameterTypeExcerpt, config);
}
return createTableFromItems(apiParameters, {
columnOptions: [
{
title: { type: "text", value: "Parameter" },
columnKind: "required",
createCellContent: (item) => createParameterTitleCell(item),
},
{
title: { type: "text", value: "Modifiers" },
columnKind: "optional",
createCellContent: (item) => createModifierCell(item),
},
{
title: { type: "text", value: "Type" },
columnKind: "required",
createCellContent: (item) => createParameterTypeCell(item),
},
{
title: { type: "text", value: "Description" },
columnKind: "required",
createCellContent: (item) => createParameterSummaryCell(item, contextApiItem, config),
},
],
});
}
/**
* Creates a simple summary table for a series of type parameters.
* Displays each parameter's name, type, and description ({@link https://tsdoc.org/pages/tags/typeparam/ | @typeParam}) comment.
*
* @param apiTypeParameters - The items to be displayed. All of these items must be of the kind specified via `itemKind`.
* @param contextApiItem - The API item with which the parameter is associated.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
export function createTypeParametersSummaryTable(
apiTypeParameters: readonly TypeParameter[],
contextApiItem: ApiItem,
config: ApiItemTransformationConfiguration,
): Table | undefined {
if (apiTypeParameters.length === 0) {
return undefined;
}
function createTypeConstraintCell(apiParameter: TypeParameter): TableCell | undefined {
const constraintSpan = createExcerptSpanWithHyperlinks(
apiParameter.constraintExcerpt,
config,
);
return constraintSpan.length === 0
? undefined
: {
type: "tableCell",
children: constraintSpan,
};
}
function createTypeDefaultCell(apiParameter: TypeParameter): TableCell | undefined {
const excerptSpan = createExcerptSpanWithHyperlinks(
apiParameter.defaultTypeExcerpt,
config,
);
return excerptSpan.length === 0
? undefined
: {
type: "tableCell",
children: excerptSpan,
};
}
return createTableFromItems(apiTypeParameters, {
columnOptions: [
{
title: { type: "text", value: "Parameter" },
columnKind: "required",
createCellContent: (item) => createPlainTextTableCell(item.name),
},
{
title: { type: "text", value: "Constraint" },
columnKind: "optional",
createCellContent: (item) => createTypeConstraintCell(item),
},
{
title: { type: "text", value: "Default" },
columnKind: "optional",
createCellContent: (item) => createTypeDefaultCell(item),
},
{
title: { type: "text", value: "Description" },
columnKind: "required",
createCellContent: (item) =>
createTypeParameterSummaryCell(item, contextApiItem, config),
},
],
});
}
/**
* Creates a simple summary table for function-like API items (constructors, functions, methods).
* Displays each item's name, modifiers, return type, and description (summary) comment.
*
* @param apiItems - The function-like items to be displayed.
* @param nameColumnLabel - The label for the "name" column in the table.
* @param config - See {@link ApiItemTransformationConfiguration}.
* @param options - Table content / formatting options.
*/
export function createFunctionLikeSummaryTable(
apiItems: readonly ApiFunctionLike[],
nameColumnLabel: string,
config: ApiItemTransformationConfiguration,
options?: TableCreationOptions,
): Table | undefined {
if (apiItems.length === 0) {
return undefined;
}
function createReturnTypeCell(apiItem: ApiFunctionLike): TableCell | undefined {
return ApiReturnTypeMixin.isBaseClassOf(apiItem)
? createTypeExcerptCell(apiItem.returnTypeExcerpt, config)
: undefined;
}
return createTableFromItems(apiItems, {
columnOptions: [
{
title: { type: "text", value: nameColumnLabel },
columnKind: "required",
createCellContent: (item) => createNameCell(item, config),
},
{
title: { type: "text", value: "Alerts" },
columnKind: "optional",
createCellContent: (item) => createAlertsCell(config.getAlertsForItem(item)),
},
{
title: { type: "text", value: "Modifiers" },
columnKind: "optional",
createCellContent: (item) => createModifiersCell(item, options?.modifiersToOmit),
},
{
title: { type: "text", value: "Return Type" },
columnKind: "optional",
createCellContent: (item) => createReturnTypeCell(item),
},
{
title: { type: "text", value: "Description" },
columnKind: "required",
createCellContent: (item) => createDescriptionCell(item, config),
},
],
});
}
/**
* Creates a simple summary table for a series of properties.
* Displays each property's name, modifiers, type, and description (summary) comment.
*
* @param apiProperties - The `Property` items to be displayed.
* @param config - See {@link ApiItemTransformationConfiguration}.
* @param options - Table content / formatting options.
*/
export function createPropertiesTable(
apiProperties: readonly ApiPropertyItem[],
config: ApiItemTransformationConfiguration,
options?: TableCreationOptions,
): Table | undefined {
if (apiProperties.length === 0) {
return undefined;
}
function createDefaultValueCell(apiItem: ApiItem): TableCell | undefined {
const defaultValueSection = getDefaultValueBlock(apiItem, config.logger);
return defaultValueSection === undefined
? undefined
: createTableCellFromTsdocSection(defaultValueSection, apiItem, config);
}
return createTableFromItems(apiProperties, {
columnOptions: [
{
title: { type: "text", value: "Property" },
columnKind: "required",
createCellContent: (item) => createNameCell(item, config),
},
{
title: { type: "text", value: "Alerts" },
columnKind: "optional",
createCellContent: (item) => createAlertsCell(config.getAlertsForItem(item)),
},
{
title: { type: "text", value: "Modifiers" },
columnKind: "optional",
createCellContent: (item) => createModifiersCell(item, options?.modifiersToOmit),
},
{
title: { type: "text", value: "Default Value" },
columnKind: "optional",
createCellContent: (item) => createDefaultValueCell(item),
},
{
title: { type: "text", value: "Type" },
columnKind: "required",
createCellContent: (item) => createTypeExcerptCell(item.propertyTypeExcerpt, config),
},
{
title: { type: "text", value: "Description" },
columnKind: "required",
createCellContent: (item) => createDescriptionCell(item, config),
},
],
});
}
/**
* Creates a simple summary table for a series of variable items.
* Displays each variable's name, modifiers, type, and description (summary) comment.
*
* @param apiVariables - The `Variable` items to be displayed.
* @param config - See {@link ApiItemTransformationConfiguration}.
* @param options - Table content / formatting options.
*/
export function createVariablesTable(
apiVariables: readonly ApiVariable[],
config: ApiItemTransformationConfiguration,
options?: TableCreationOptions,
): Table | undefined {
if (apiVariables.length === 0) {
return undefined;
}
return createTableFromItems(apiVariables, {
columnOptions: [
{
title: { type: "text", value: "Variable" },
columnKind: "required",
createCellContent: (item) => createNameCell(item, config),
},
{
title: { type: "text", value: "Alerts" },
columnKind: "optional",
createCellContent: (item) => createAlertsCell(config.getAlertsForItem(item)),
},
{
title: { type: "text", value: "Modifiers" },
columnKind: "optional",
createCellContent: (item) => createModifiersCell(item, options?.modifiersToOmit),
},
{
title: { type: "text", value: "Type" },
columnKind: "required",
createCellContent: (item) => createTypeExcerptCell(item.variableTypeExcerpt, config),
},
{
title: { type: "text", value: "Description" },
columnKind: "required",
createCellContent: (item) => createDescriptionCell(item, config),
},
],
});
}
/**
* Creates a simple summary table for a list of packages.
* Displays each package's name and description
* ({@link https://tsdoc.org/pages/tags/packagedocumentation/ | @packageDocumentation}) comment.
*
* @param apiPackages - The package items to be displayed.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
export function createPackagesTable(
apiPackages: readonly ApiPackage[],
config: ApiItemTransformationConfiguration,
): Table | undefined {
if (apiPackages.length === 0) {
return undefined;
}
return createTableFromItems(apiPackages, {
columnOptions: [
{
title: { type: "text", value: "Package" },
columnKind: "required",
createCellContent: (item) => createNameCell(item, config),
},
{
title: { type: "text", value: "Alerts" },
columnKind: "optional",
createCellContent: (item) => createAlertsCell(config.getAlertsForItem(item)),
},
{
title: { type: "text", value: "Description" },
columnKind: "required",
createCellContent: (item) => createDescriptionCell(item, config),
},
],
});
}
/**
* Creates a table cell containing the description (summary) comment for the provided API item.
* If the item has an `@beta` release tag, the comment will be annotated as being beta content.
*
* @param apiItem - The API item whose comment will be rendered in the cell.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
export function createDescriptionCell(
apiItem: ApiItem,
config: ApiItemTransformationConfiguration,
): TableCell | undefined {
if (apiItem instanceof ApiDocumentedItem && apiItem.tsdocComment !== undefined) {
return createTableCellFromTsdocSection(
apiItem.tsdocComment.summarySection,
apiItem,
config,
);
}
return undefined;
}
/**
* Creates a table cell containing the name of the provided API item.
*
* @remarks This content will be generated as a link to the section content describing the API item.
*
* @param apiItem - The API item whose name will be displayed in the cell, and to whose content the generate link
* will point.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
export function createNameCell(
apiItem: ApiItem,
config: ApiItemTransformationConfiguration,
): TableCell {
const link = getLinkForApiItem(apiItem, config);
return {
type: "tableCell",
children: [link],
};
}
/**
* Creates a table cell containing a list of modifiers that apply.
*
* @param apiItem - The API item whose modifiers will be displayed in the cell.
* @param modifiersToOmit - List of modifiers to omit from the generated cell, even if they apply to the item.
*/
export function createModifiersCell(
apiItem: ApiItem,
modifiersToOmit?: ApiModifier[],
): TableCell | undefined {
const modifiers = getModifiers(apiItem, modifiersToOmit);
const contents: PhrasingContent[] = [];
let needsComma = false;
for (const modifier of modifiers) {
if (needsComma) {
contents.push({ type: "text", value: ", " });
}
contents.push({ type: "inlineCode", value: modifier });
needsComma = true;
}
return modifiers.length === 0
? undefined
: {
type: "tableCell",
children: contents,
};
}
/**
* Creates a table cell containing the provided alerts, displayed as comma-separated codespan nodes.
*
* @param apiItem - The alert values to display.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
export function createAlertsCell(alerts: string[]): TableCell | undefined {
const alertNodes: PhrasingContent[] = alerts.map((alert) => ({
type: "inlineCode",
value: alert,
}));
return alerts.length === 0
? undefined
: {
type: "tableCell",
children: injectSeparator(alertNodes, { type: "text", value: ", " }),
};
}
/**
* Creates a table cell containing the name of the provided parameter as plain text.
*
* @param apiParameter - The parameter whose name will be displayed in the cell.
*/
function createParameterTitleCell(apiParameter: Parameter): TableCell {
return createPlainTextTableCell(apiParameter.name);
}
/**
* Creates a table cell containing the description ({@link https://tsdoc.org/pages/tags/param/ | @param}) comment
* of the provided parameter.
* If the parameter has no documentation, an empty cell will be used.
*
* @param apiParameter - The parameter whose comment will be displayed in the cell.
* @param contextApiItem - The API item with which the parameter is associated.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
function createParameterSummaryCell(
apiParameter: Parameter,
contextApiItem: ApiItem,
config: ApiItemTransformationConfiguration,
): TableCell | undefined {
if (apiParameter.tsdocParamBlock === undefined) {
return undefined;
}
return createTableCellFromTsdocSection(
apiParameter.tsdocParamBlock.content,
contextApiItem,
config,
);
}
/**
* Creates a table cell containing the description ({@link https://tsdoc.org/pages/tags/typeparam/ | @typeParam}) comment
* of the provided parameter.
* If the parameter has no documentation, an empty cell will be used.
*
* @param apiTypeParameter - The type parameter whose comment will be displayed in the cell.
* @param contextApiItem - The API item with which the parameter is associated.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
function createTypeParameterSummaryCell(
apiTypeParameter: TypeParameter,
contextApiItem: ApiItem,
config: ApiItemTransformationConfiguration,
): TableCell | undefined {
if (apiTypeParameter.tsdocTypeParamBlock === undefined) {
return undefined;
}
return createTableCellFromTsdocSection(
apiTypeParameter.tsdocTypeParamBlock.content,
contextApiItem,
config,
);
}
/**
* Creates a table cell containing type information.
* @remarks This content will be generated as links to type signature documentation for other items local to the same
* API suite (model).
*
* @param typeExcerpt - An excerpt describing the type to be displayed in the cell.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
export function createTypeExcerptCell(
typeExcerpt: Excerpt,
config: ApiItemTransformationConfiguration,
): TableCell | undefined {
const excerptSpan = createExcerptSpanWithHyperlinks(typeExcerpt, config);
return excerptSpan.length === 0
? undefined
: {
type: "tableCell",
children: excerptSpan,
};
}
/**
* Gets the appropriate heading title for the provided item kind to be used in table entries.
*/
function getTableHeadingTitleForApiKind(itemKind: ApiItemKind): string {
switch (itemKind) {
case ApiItemKind.CallSignature: {
return "Call Signature";
}
case ApiItemKind.ConstructSignature: {
return "Constructor";
}
case ApiItemKind.EnumMember: {
return "Flag";
}
case ApiItemKind.IndexSignature: {
return "Index Signature";
}
case ApiItemKind.MethodSignature: {
return ApiItemKind.Method;
}
case ApiItemKind.PropertySignature: {
return ApiItemKind.Property;
}
default: {
return itemKind;
}
}
}
/**
* Transforms the contents of a TSDoc section node, and fine-tunes the output for use in a table cell.
*
* @remarks
* Notably, this optimizes away the generation of paragraph nodes around inner contents when there is only a
* single paragraph.
*/
export function createTableCellFromTsdocSection(
tsdocSection: DocSection,
contextApiItem: ApiItem,
config: ApiItemTransformationConfiguration,
): TableCell | undefined {
const transformed = transformTsdoc(tsdocSection, contextApiItem, config);
return createTableCellFromBlockContent(transformed);
}
/**
* Transforms the contents of a TSDoc section node, and fine-tunes the output for use in a table cell.
*
* @remarks
* Notably, this optimizes away the generation of paragraph nodes around inner contents when there is only a
* single paragraph.
*/
function createTableCellFromBlockContent(
blockContent: readonly BlockContent[],
): TableCell | undefined {
if (blockContent.length === 0) {
return undefined;
}
// If the transformed contents consist of a single paragraph (common case), inline that paragraph's contents
// directly in the cell.
if (blockContent.length === 1 && blockContent[0].type === "paragraph") {
return {
type: "tableCell",
children: blockContent[0].children,
};
}
// `mdast` does not allow block content in table cells, but we want to be able to include things like fenced code blocks, etc. in our table cells.
// To accommodate this, we convert the contents to HTML and put that inside the table cell.
const htmlTrees = blockContent.map((node) => toHast(node));
const htmlNodes: Html[] = htmlTrees.map((node) => ({
type: "html",
value: toHtml(node),
}));
return {
type: "tableCell",
children: htmlNodes,
};
}
function createPlainTextTableCell(text: string): TableCell {
return {
type: "tableCell",
children: [{ type: "text", value: text }],
};
}