Skip to content

Commit 6a73273

Browse files
devversionvivian-hu-zz
authored andcommitted
build(docs): show exported constants in docs (#13168)
* Currently constants which are exported, will not show up in the docs. This introduces support for displaying `constants` in the docs. * Also creates a Nunjucks plugin that supports highlighting specific code snippets. This is helpful when rendering a type in the docs (similar to angular/aio) * Marks most animation constants as `@docs-private` because those shouldn't be relevant to users.
1 parent afb0352 commit 6a73273

20 files changed

+147
-10
lines changed

src/cdk/overlay/overlay-module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class OverlayModule {}
3636
/**
3737
* @deprecated Use `OverlayModule` instead.
3838
* @breaking-change 7.0.0
39+
* @docs-private
3940
*/
4041
export const OVERLAY_PROVIDERS: Provider[] = [
4142
Overlay,

src/lib/datepicker/datepicker-animations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
AnimationTriggerMetadata,
1515
} from '@angular/animations';
1616

17-
/** Animations used by the Material datepicker. */
17+
/**
18+
* Animations used by the Material datepicker.
19+
* @docs-private
20+
*/
1821
export const matDatepickerAnimations: {
1922
readonly transformPanel: AnimationTriggerMetadata;
2023
readonly fadeInCalendar: AnimationTriggerMetadata;

src/lib/dialog/dialog-animations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const animationBody = [
2626
animate('75ms cubic-bezier(0.4, 0.0, 0.2, 1)', style({opacity: 0}))),
2727
];
2828

29-
/** Animations used by MatDialog. */
29+
/**
30+
* Animations used by MatDialog.
31+
* @docs-private
32+
*/
3033
export const matDialogAnimations: {
3134
readonly dialogContainer: AnimationTriggerMetadata;
3235
readonly slideDialog: AnimationTriggerMetadata;

src/lib/expansion/expansion-animations.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ export const EXPANSION_PANEL_ANIMATION_TIMING = '225ms cubic-bezier(0.4,0.0,0.2,
3434
* `collapsed` this acts a noop since no style values change.
3535
*
3636
* In the case where angular's animation state is out of sync with the expansion panel's state, the
37-
* expansion panel being `expanded` and angular animations being`void`, the animation from the
37+
* expansion panel being `expanded` and angular animations being `void`, the animation from the
3838
* `expanded`'s effective styles (though in a `void` animation state) to the collapsed state will
3939
* occur as expected.
4040
*
4141
* Angular Bug: https://github.com/angular/angular/issues/18847
42+
*
43+
* @docs-private
4244
*/
4345
export const matExpansionAnimations: {
4446
readonly indicatorRotate: AnimationTriggerMetadata;

src/lib/form-field/form-field-animations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
AnimationTriggerMetadata,
1515
} from '@angular/animations';
1616

17-
/** Animations used by the MatFormField. */
17+
/**
18+
* Animations used by the MatFormField.
19+
* @docs-private
20+
*/
1821
export const matFormFieldAnimations: {
1922
readonly transitionMessages: AnimationTriggerMetadata
2023
} = {

src/lib/menu/menu-animations.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import{
2121
* Animations used by the mat-menu component.
2222
* Animation duration and timing values are based on:
2323
* https://material.io/guidelines/components/menus.html#menus-usage
24+
* @docs-private
2425
*/
2526
export const matMenuAnimations: {
2627
readonly transformMenu: AnimationTriggerMetadata;
@@ -65,11 +66,13 @@ export const matMenuAnimations: {
6566
/**
6667
* @deprecated
6768
* @breaking-change 7.0.0
69+
* @docs-private
6870
*/
6971
export const fadeInItems = matMenuAnimations.fadeInItems;
7072

7173
/**
7274
* @deprecated
7375
* @breaking-change 7.0.0
76+
* @docs-private
7477
*/
7578
export const transformMenu = matMenuAnimations.transformMenu;

src/lib/select/select-animations.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
* const containing the metadata for one animation.
2121
*
2222
* The values below match the implementation of the AngularJS Material mat-select animation.
23+
* @docs-private
2324
*/
2425
export const matSelectAnimations: {
2526
readonly transformPanel: AnimationTriggerMetadata;
@@ -74,11 +75,13 @@ export const matSelectAnimations: {
7475
/**
7576
* @deprecated
7677
* @breaking-change 7.0.0
78+
* @docs-private
7779
*/
7880
export const transformPanel = matSelectAnimations.transformPanel;
7981

8082
/**
8183
* @deprecated
8284
* @breaking-change 7.0.0
85+
* @docs-private
8386
*/
8487
export const fadeInContent = matSelectAnimations.fadeInContent;

src/lib/sidenav/drawer-animations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
AnimationTriggerMetadata,
1515
} from '@angular/animations';
1616

17-
/** Animations used by the Material drawers. */
17+
/**
18+
* Animations used by the Material drawers.
19+
* @docs-private
20+
*/
1821
export const matDrawerAnimations: {
1922
readonly transformDrawer: AnimationTriggerMetadata;
2023
} = {

src/lib/snack-bar/snack-bar-animations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
AnimationTriggerMetadata,
1515
} from '@angular/animations';
1616

17-
/** Animations used by the Material snack bar. */
17+
/**
18+
* Animations used by the Material snack bar.
19+
* @docs-private
20+
*/
1821
export const matSnackBarAnimations: {
1922
readonly snackBarState: AnimationTriggerMetadata;
2023
} = {

src/lib/sort/sort-animations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {AnimationCurves, AnimationDurations} from '@angular/material/core';
1919
const SORT_ANIMATION_TRANSITION = AnimationDurations.ENTERING + ' ' +
2020
AnimationCurves.STANDARD_CURVE;
2121

22-
/** Animations used by MatSort. */
22+
/**
23+
* Animations used by MatSort.
24+
* @docs-private
25+
*/
2326
export const matSortAnimations: {
2427
readonly indicator: AnimationTriggerMetadata;
2528
readonly leftPointer: AnimationTriggerMetadata;

src/lib/stepper/stepper-animations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
AnimationTriggerMetadata,
1515
} from '@angular/animations';
1616

17-
/** Animations used by the Material steppers. */
17+
/**
18+
* Animations used by the Material steppers.
19+
* @docs-private
20+
*/
1821
export const matStepperAnimations: {
1922
readonly horizontalStepTransition: AnimationTriggerMetadata;
2023
readonly verticalStepTransition: AnimationTriggerMetadata;

src/lib/tabs/tabs-animations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
AnimationTriggerMetadata,
1515
} from '@angular/animations';
1616

17-
/** Animations used by the Material tabs. */
17+
/**
18+
* Animations used by the Material tabs.
19+
* @docs-private
20+
*/
1821
export const matTabsAnimations: {
1922
readonly translateTab: AnimationTriggerMetadata;
2023
} = {

src/lib/tooltip/tooltip-animations.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import {
1515
trigger,
1616
} from '@angular/animations';
1717

18-
/** Animations used by MatTooltip. */
18+
/**
19+
* Animations used by MatTooltip.
20+
* @docs-private
21+
*/
1922
export const matTooltipAnimations: {
2023
readonly tooltipState: AnimationTriggerMetadata;
2124
} = {

tools/dgeni/common/dgeni-definitions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {ApiDoc} from 'dgeni-packages/typescript/api-doc-types/ApiDoc';
22
import {ClassExportDoc} from 'dgeni-packages/typescript/api-doc-types/ClassExportDoc';
33
import {ClassLikeExportDoc} from 'dgeni-packages/typescript/api-doc-types/ClassLikeExportDoc';
4+
import {ConstExportDoc} from 'dgeni-packages/typescript/api-doc-types/ConstExportDoc';
45
import {PropertyMemberDoc} from 'dgeni-packages/typescript/api-doc-types/PropertyMemberDoc';
56
import {ParsedDecorator} from 'dgeni-packages/typescript/services/TsParser/getDecorators';
67
import {FunctionExportDoc} from 'dgeni-packages/typescript/api-doc-types/FunctionExportDoc';
@@ -51,3 +52,6 @@ export interface CategorizedMethodMemberDoc
5152
/** Extended Dgeni function export document that simplifies logic for the Dgeni template. */
5253
export interface CategorizedFunctionExportDoc
5354
extends NormalizedFunctionDoc, FunctionExportDoc, DeprecationDoc {}
55+
56+
/** Extended Dgeni const export document that simplifies logic for the Dgeni template. */
57+
export interface CategorizedConstExportDoc extends ConstExportDoc, DeprecationDoc {}

tools/dgeni/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Package} from 'dgeni';
22
import {Host} from 'dgeni-packages/typescript/services/ts-host/host';
3+
import {HighlightNunjucksExtension} from './nunjucks-tags/highlight';
34
import {patchLogService} from './patch-log-service';
45
import {DocsPrivateFilter} from './processors/docs-private-filter';
56
import {Categorizer} from './processors/categorizer';
@@ -163,4 +164,6 @@ apiDocsPackage.config((templateFinder: any, templateEngine: any) => {
163164
variableStart: '{$',
164165
variableEnd: '$}'
165166
};
167+
168+
templateEngine.tags.push(new HighlightNunjucksExtension());
166169
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const hljs = require('highlight.js');
2+
3+
/**
4+
* Nunjucks extension that supports rendering highlighted content. Content that is placed in
5+
* between a {% highlight %} and {% end_highlight %} block will be automatically highlighted.
6+
*
7+
* HighlightJS cannot detect the code language automatically. Therefore, developers need to
8+
* specify the language manually as first tag-block argument.
9+
*/
10+
export class HighlightNunjucksExtension {
11+
12+
/** Tags that will be parsed by this Nunjucks extension. */
13+
tags = ['highlight'];
14+
15+
/** Disable autoescaping for content that is rendered within this extension. */
16+
autoescape = false;
17+
18+
parse(parser: any, nodes: any) {
19+
const startToken = parser.nextToken();
20+
const args = parser.parseSignature(null, true);
21+
22+
// Jump to the end of the "{% highlight }" block.
23+
parser.advanceAfterBlockEnd(startToken.value);
24+
25+
// Parse text content until {% end_highlight }" has been reached.
26+
const textContent = parser.parseUntilBlocks('end_highlight');
27+
28+
// Jump to the end of the "{% highlight }" block.
29+
parser.advanceAfterBlockEnd();
30+
31+
return new nodes.CallExtension(this, 'render', args, [textContent]);
32+
}
33+
34+
render(_context: any, language: string, contentFn: () => string) {
35+
return hljs.highlight(language, contentFn()).value;
36+
}
37+
}

tools/dgeni/processors/categorizer.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import {
1313
CategorizedClassDoc,
1414
CategorizedClassLikeDoc,
15+
CategorizedConstExportDoc,
1516
CategorizedFunctionExportDoc,
1617
CategorizedMethodMemberDoc,
1718
CategorizedPropertyMemberDoc,
@@ -42,6 +43,10 @@ export class Categorizer implements Processor {
4243
docs
4344
.filter(doc => doc.docType === 'function')
4445
.forEach(doc => this.decorateFunctionExportDoc(doc));
46+
47+
docs
48+
.filter(doc => doc.docType === 'const')
49+
.forEach(doc => this.decorateConstExportDoc(doc));
4550
}
4651

4752
/**
@@ -117,6 +122,14 @@ export class Categorizer implements Processor {
117122
decorateDeprecatedDoc(functionDoc);
118123
}
119124

125+
/**
126+
* Method that will be called for each const export document. We decorate the const
127+
* documents with a property that states whether the constant is deprecated or not.
128+
*/
129+
private decorateConstExportDoc(doc: CategorizedConstExportDoc) {
130+
decorateDeprecatedDoc(doc);
131+
}
132+
120133
/**
121134
* Method that will be called for each property doc. Properties that are Angular inputs or
122135
* outputs will be marked. Aliases for the inputs or outputs will be stored as well.

tools/dgeni/processors/entry-point-grouper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {DocCollection, Document, Processor} from 'dgeni';
2+
import {ConstExportDoc} from 'dgeni-packages/typescript/api-doc-types/ConstExportDoc';
23
import {FunctionExportDoc} from 'dgeni-packages/typescript/api-doc-types/FunctionExportDoc';
34
import {InterfaceExportDoc} from 'dgeni-packages/typescript/api-doc-types/InterfaceExportDoc';
45
import {TypeAliasExportDoc} from 'dgeni-packages/typescript/api-doc-types/TypeAliasExportDoc';
@@ -50,6 +51,9 @@ export class EntryPointDoc {
5051
/** Functions that belong to the entry-point. */
5152
functions: FunctionExportDoc[] = [];
5253

54+
/** Constants that belong to the entry-point. */
55+
constants: ConstExportDoc[] = [];
56+
5357
/** NgModule that defines the current entry-point. */
5458
ngModule: CategorizedClassDoc | null = null;
5559

@@ -108,6 +112,8 @@ export class EntryPointGrouper implements Processor {
108112
entryPoint.typeAliases.push(doc);
109113
} else if (doc.docType === 'function') {
110114
entryPoint.functions.push(doc);
115+
} else if (doc.docType === 'const') {
116+
entryPoint.constants.push(doc);
111117
}
112118
});
113119

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% import "macros.html" as macros %}
2+
3+
<h4 id="{$ constant.name $}" class="docs-header-link docs-api-h4 docs-api-constant-name">
4+
<span header-link="{$ constant.name $}"></span>
5+
<code>{$ constant.name $}</code>
6+
</h4>
7+
8+
{%- if constant.isDeprecated -%}
9+
<div class="docs-api-constant-deprecated-marker" {$ macros.deprecationTitle(constant) $}>
10+
Deprecated
11+
</div>
12+
{%- endif -%}
13+
14+
{%- if constant.description -%}
15+
<p class="docs-api-constant-description">{$ constant.description | marked | safe $}</p>
16+
{%- endif -%}
17+
18+
<pre class="docs-markdown-pre">
19+
<code class="docs-markdown-code">
20+
{%- highlight "typescript" -%}
21+
const {$ constant.name | safe $}: {$ constant.type | safe $}
22+
{%- end_highlight -%}
23+
</code>
24+
</pre>

tools/dgeni/templates/entry-point.template.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
{% include 'type-alias.template.html' %}
2828
{% endmacro %}
2929

30+
{% macro constant(constant) -%}
31+
{% include 'constant.template.html' %}
32+
{% endmacro %}
33+
3034
<div class="docs-api">
3135
<h2>
3236
API reference for Angular {$ doc.packageDisplayName $} {$ doc.displayName $}
@@ -102,4 +106,14 @@ <h3 id="type_aliases" class="docs-header-link docs-api-h3">
102106
{$ typeAlias(item) $}
103107
{% endfor %}
104108
{%- endif -%}
109+
110+
{%- if doc.constants.length -%}
111+
<h3 id="constants" class="docs-header-link docs-api-h3">
112+
<span header-link="constants"></span>
113+
Constants
114+
</h3>
115+
{% for item in doc.constants %}
116+
{$ constant(item) $}
117+
{% endfor %}
118+
{%- endif -%}
105119
</div>

0 commit comments

Comments
 (0)