Skip to content

Commit 92f3f2a

Browse files
authored
[api-documenter] Add support for @DefaultValue in Markdown and Yaml documenters (#5733)
* feat(api-documenter): add support for @DefaultValue in Markdown and Yaml documenters * change: add rush change file for api-documenter
1 parent 8587eab commit 92f3f2a

10 files changed

Lines changed: 82 additions & 2 deletions

File tree

apps/api-documenter/src/documenters/MarkdownDocumenter.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ export class MarkdownDocumenter {
273273
case ApiItemKind.Function:
274274
this._writeParameterTables(output, apiItem as ApiParameterListMixin);
275275
this._writeThrowsSection(output, apiItem);
276+
this._writeDefaultValueSection(output, apiItem);
276277
break;
277278
case ApiItemKind.Namespace:
278279
this._writePackageOrNamespaceTables(output, apiItem as ApiNamespace);
@@ -285,10 +286,13 @@ export class MarkdownDocumenter {
285286
break;
286287
case ApiItemKind.Property:
287288
case ApiItemKind.PropertySignature:
289+
this._writeDefaultValueSection(output, apiItem);
288290
break;
289291
case ApiItemKind.TypeAlias:
292+
this._writeDefaultValueSection(output, apiItem);
290293
break;
291294
case ApiItemKind.Variable:
295+
this._writeDefaultValueSection(output, apiItem);
292296
break;
293297
default:
294298
throw new Error('Unsupported API item kind: ' + apiItem.kind);
@@ -469,6 +473,30 @@ export class MarkdownDocumenter {
469473
}
470474
}
471475

476+
private _writeDefaultValueSection(output: DocSection, apiItem: ApiItem): void {
477+
const configuration: TSDocConfiguration = this._tsdocConfiguration;
478+
479+
if (apiItem instanceof ApiDocumentedItem) {
480+
const tsdocComment: DocComment | undefined = apiItem.tsdocComment;
481+
482+
if (tsdocComment) {
483+
// Write the @defaultValue blocks
484+
const defaultValueBlocks: DocBlock[] = tsdocComment.customBlocks.filter(
485+
(x) => x.blockTag.tagNameWithUpperCase === StandardTags.defaultValue.tagNameWithUpperCase
486+
);
487+
488+
if (defaultValueBlocks.length > 0) {
489+
const heading: string = 'Default Value';
490+
output.appendNode(new DocHeading({ configuration, title: heading }));
491+
492+
for (const defaultValueBlock of defaultValueBlocks) {
493+
this._appendSection(output, defaultValueBlock.content);
494+
}
495+
}
496+
}
497+
}
498+
}
499+
472500
/**
473501
* GENERATE PAGE: MODEL
474502
*/

apps/api-documenter/src/documenters/YamlDocumenter.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,18 @@ export class YamlDocumenter {
427427
yamlItem.example = [...(yamlItem.example || []), example];
428428
}
429429
}
430+
431+
// Write the @defaultValue block
432+
const defaultValueBlocks: DocBlock[] = tsdocComment.customBlocks.filter(
433+
(x) => x.blockTag.tagNameWithUpperCase === StandardTags.defaultValue.tagNameWithUpperCase
434+
);
435+
436+
for (const defaultValueBlock of defaultValueBlocks) {
437+
const defaultValueContent: string = this._renderMarkdown(defaultValueBlock.content, apiItem);
438+
if (defaultValueContent) {
439+
yamlItem.defaultValue = defaultValueContent.trim();
440+
}
441+
}
430442
}
431443

432444
if (tsdocComment.deprecatedBlock) {

apps/api-documenter/src/utils/ToSdpConvertHelper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ function convertCommonYamlModel(
304304
result.example = [];
305305
}
306306

307+
if (element.defaultValue) {
308+
result.defaultValue = element.defaultValue;
309+
}
310+
307311
result.isPreview = element.isPreview;
308312
if (!result.isPreview) {
309313
result.isPreview = false;

apps/api-documenter/src/yaml/ISDPYamlFile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type CommonYamlModel = IBaseYamlModel & {
1616
remarks?: string;
1717
example?: string[];
1818
customDeprecatedMessage?: string;
19+
defaultValue?: string;
1920
};
2021

2122
export type PackageYamlModel = CommonYamlModel & {

apps/api-documenter/src/yaml/IYamlApiFile.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ export interface IYamlItem {
327327
* NOTE: This is an extension and corresponds to `ItemViewModel.Metadata` in DocFX.
328328
*/
329329
package?: string;
330+
331+
/**
332+
* The default value for the item.
333+
* NOTE: This is an extension and corresponds to `ItemViewModel.Metadata` in DocFX.
334+
*/
335+
defaultValue?: string;
330336
}
331337

332338
/**

apps/api-documenter/src/yaml/typescript.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@
262262
},
263263
"package": {
264264
"type": "string"
265+
},
266+
"defaultValue": {
267+
"type": "string"
265268
}
266269
},
267270
"patternProperties": {

build-tests/api-documenter-test/etc/api-documenter-test.api.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@
592592
{
593593
"kind": "Method",
594594
"canonicalReference": "api-documenter-test!DocClass1#exampleFunction:member(2)",
595-
"docComment": "/**\n * This is also an overloaded function.\n *\n * @param x - the number\n */\n",
595+
"docComment": "/**\n * This is also an overloaded function.\n *\n * @param x - the number\n *\n * @defaultValue 123\n */\n",
596596
"excerptTokens": [
597597
{
598598
"kind": "Content",
@@ -2119,7 +2119,7 @@
21192119
{
21202120
"kind": "PropertySignature",
21212121
"canonicalReference": "api-documenter-test!IDocInterface5#regularProperty:member",
2122-
"docComment": "/**\n * Property of type string that does something\n */\n",
2122+
"docComment": "/**\n * Property of type string that does something\n *\n * @defaultValue \"Hello World\"\n */\n",
21232123
"excerptTokens": [
21242124
{
21252125
"kind": "Content",

build-tests/api-documenter-test/src/DocClass1.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInter
219219
/**
220220
* This is also an overloaded function.
221221
* @param x - the number
222+
*
223+
* @defaultValue 123
222224
*/
223225
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
224226
exampleFunction(x: number): number;
@@ -316,6 +318,8 @@ export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInter
316318
export interface IDocInterface5 {
317319
/**
318320
* Property of type string that does something
321+
*
322+
* @defaultValue "Hello World"
319323
*/
320324
regularProperty: string;
321325
}

build-tests/api-documenter-test/src/test/__snapshots__/snapshot.test.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ methods:
349349
summary: This is also an overloaded function.
350350
remarks: ''
351351
example: []
352+
defaultValue: '123'
352353
isPreview: false
353354
isDeprecated: false
354355
syntax:
@@ -885,6 +886,7 @@ properties:
885886
summary: Property of type string that does something
886887
remarks: ''
887888
example: []
889+
defaultValue: '\\"Hello World\\"'
888890
isPreview: false
889891
isDeprecated: false
890892
syntax:
@@ -1800,6 +1802,10 @@ the number
18001802
18011803
number
18021804
1805+
## Default Value
1806+
1807+
123
1808+
18031809
",
18041810
"/api-documenter-test.docclass1.genericwithconstraintanddefault.md": "<!-- Do not edit this file. It is automatically generated by API Documenter. -->
18051811
@@ -3452,6 +3458,11 @@ Property of type string that does something
34523458
\`\`\`typescript
34533459
regularProperty: string;
34543460
\`\`\`
3461+
3462+
## Default Value
3463+
3464+
\\"Hello World\\"
3465+
34553466
",
34563467
"/api-documenter-test.idocinterface6.arrayproperty.md": "<!-- Do not edit this file. It is automatically generated by API Documenter. -->
34573468
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Add support for @defaultValue in Markdown and Yaml documenters",
5+
"type": "minor",
6+
"packageName": "@microsoft/api-documenter"
7+
}
8+
],
9+
"packageName": "@microsoft/api-documenter",
10+
"email": "40660121+colinaaa@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)