Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions apps/api-documenter/src/documenters/MarkdownDocumenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export class MarkdownDocumenter {
case ApiItemKind.Function:
this._writeParameterTables(output, apiItem as ApiParameterListMixin);
this._writeThrowsSection(output, apiItem);
this._writeDefaultValueSection(output, apiItem);
break;
case ApiItemKind.Namespace:
this._writePackageOrNamespaceTables(output, apiItem as ApiNamespace);
Expand All @@ -285,10 +286,13 @@ export class MarkdownDocumenter {
break;
case ApiItemKind.Property:
case ApiItemKind.PropertySignature:
this._writeDefaultValueSection(output, apiItem);
break;
case ApiItemKind.TypeAlias:
this._writeDefaultValueSection(output, apiItem);
break;
case ApiItemKind.Variable:
this._writeDefaultValueSection(output, apiItem);
break;
default:
throw new Error('Unsupported API item kind: ' + apiItem.kind);
Expand Down Expand Up @@ -469,6 +473,30 @@ export class MarkdownDocumenter {
}
}

private _writeDefaultValueSection(output: DocSection, apiItem: ApiItem): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;

if (apiItem instanceof ApiDocumentedItem) {
const tsdocComment: DocComment | undefined = apiItem.tsdocComment;

if (tsdocComment) {
// Write the @defaultValue blocks
const defaultValueBlocks: DocBlock[] = tsdocComment.customBlocks.filter(
(x) => x.blockTag.tagNameWithUpperCase === StandardTags.defaultValue.tagNameWithUpperCase
);

if (defaultValueBlocks.length > 0) {
const heading: string = 'Default Value';
output.appendNode(new DocHeading({ configuration, title: heading }));

for (const defaultValueBlock of defaultValueBlocks) {
this._appendSection(output, defaultValueBlock.content);
}
}
}
}
}

/**
* GENERATE PAGE: MODEL
*/
Expand Down
12 changes: 12 additions & 0 deletions apps/api-documenter/src/documenters/YamlDocumenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,18 @@ export class YamlDocumenter {
yamlItem.example = [...(yamlItem.example || []), example];
}
}

// Write the @defaultValue block
const defaultValueBlocks: DocBlock[] = tsdocComment.customBlocks.filter(
(x) => x.blockTag.tagNameWithUpperCase === StandardTags.defaultValue.tagNameWithUpperCase
);

for (const defaultValueBlock of defaultValueBlocks) {
const defaultValueContent: string = this._renderMarkdown(defaultValueBlock.content, apiItem);
if (defaultValueContent) {
yamlItem.defaultValue = defaultValueContent.trim();
}
}
}

if (tsdocComment.deprecatedBlock) {
Expand Down
4 changes: 4 additions & 0 deletions apps/api-documenter/src/utils/ToSdpConvertHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ function convertCommonYamlModel(
result.example = [];
}

if (element.defaultValue) {
result.defaultValue = element.defaultValue;
}

result.isPreview = element.isPreview;
if (!result.isPreview) {
result.isPreview = false;
Expand Down
1 change: 1 addition & 0 deletions apps/api-documenter/src/yaml/ISDPYamlFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type CommonYamlModel = IBaseYamlModel & {
remarks?: string;
example?: string[];
customDeprecatedMessage?: string;
defaultValue?: string;
};

export type PackageYamlModel = CommonYamlModel & {
Expand Down
6 changes: 6 additions & 0 deletions apps/api-documenter/src/yaml/IYamlApiFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ export interface IYamlItem {
* NOTE: This is an extension and corresponds to `ItemViewModel.Metadata` in DocFX.
*/
package?: string;

/**
* The default value for the item.
* NOTE: This is an extension and corresponds to `ItemViewModel.Metadata` in DocFX.
*/
defaultValue?: string;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions apps/api-documenter/src/yaml/typescript.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@
},
"package": {
"type": "string"
},
"defaultValue": {
"type": "string"
}
},
"patternProperties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@
{
"kind": "Method",
"canonicalReference": "api-documenter-test!DocClass1#exampleFunction:member(2)",
"docComment": "/**\n * This is also an overloaded function.\n *\n * @param x - the number\n */\n",
"docComment": "/**\n * This is also an overloaded function.\n *\n * @param x - the number\n *\n * @defaultValue 123\n */\n",
"excerptTokens": [
{
"kind": "Content",
Expand Down Expand Up @@ -2119,7 +2119,7 @@
{
"kind": "PropertySignature",
"canonicalReference": "api-documenter-test!IDocInterface5#regularProperty:member",
"docComment": "/**\n * Property of type string that does something\n */\n",
"docComment": "/**\n * Property of type string that does something\n *\n * @defaultValue \"Hello World\"\n */\n",
"excerptTokens": [
{
"kind": "Content",
Expand Down
4 changes: 4 additions & 0 deletions build-tests/api-documenter-test/src/DocClass1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInter
/**
* This is also an overloaded function.
* @param x - the number
*
* @defaultValue 123
*/
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
exampleFunction(x: number): number;
Expand Down Expand Up @@ -316,6 +318,8 @@ export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInter
export interface IDocInterface5 {
/**
* Property of type string that does something
*
* @defaultValue "Hello World"
*/
regularProperty: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ methods:
summary: This is also an overloaded function.
remarks: ''
example: []
defaultValue: '123'
isPreview: false
isDeprecated: false
syntax:
Expand Down Expand Up @@ -885,6 +886,7 @@ properties:
summary: Property of type string that does something
remarks: ''
example: []
defaultValue: '\\"Hello World\\"'
isPreview: false
isDeprecated: false
syntax:
Expand Down Expand Up @@ -1800,6 +1802,10 @@ the number

number

## Default Value

123

",
"/api-documenter-test.docclass1.genericwithconstraintanddefault.md": "<!-- Do not edit this file. It is automatically generated by API Documenter. -->

Expand Down Expand Up @@ -3452,6 +3458,11 @@ Property of type string that does something
\`\`\`typescript
regularProperty: string;
\`\`\`

## Default Value

\\"Hello World\\"

",
"/api-documenter-test.idocinterface6.arrayproperty.md": "<!-- Do not edit this file. It is automatically generated by API Documenter. -->

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "Add support for @defaultValue in Markdown and Yaml documenters",
"type": "minor",
"packageName": "@microsoft/api-documenter"
}
],
"packageName": "@microsoft/api-documenter",
"email": "[email protected]"
}