Skip to content

Commit 84a5def

Browse files
authored
feat(docs-utils,dgeni): pull examples out of content and store in doc (#3389)
1 parent 52f4973 commit 84a5def

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
lines changed

libs/docs-utils/src/doc/api.type.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { DaffDocExample } from './example.type';
12
import { DaffDoc } from './type';
23

34
/**
45
* An API doc that includes the type of the symbol.
56
*/
67
export interface DaffApiDoc extends DaffDoc {
78
docType: string;
9+
examples: Array<DaffDocExample>;
810
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* A usage example.
3+
*/
4+
export interface DaffDocExample {
5+
/**
6+
* The short caption describing the example.
7+
* This should be just plain text.
8+
*/
9+
caption: string;
10+
/**
11+
* The body of the example.
12+
* Can be HTML and should be rendered as such.
13+
*/
14+
body: string;
15+
}

libs/docs-utils/src/doc/public_api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './api.type';
2+
export * from './example.type';
23
export * from './guide.type';
34
export * from './type';

tools/dgeni/src/processors/markdown.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Marked } from 'marked';
1010
import { markedHighlight } from 'marked-highlight';
1111

1212
import {
13+
DaffDocExample,
1314
DaffDocKind,
1415
daffDocsGetLinkUrl,
1516
} from '@daffodil/docs-utils';
@@ -80,10 +81,16 @@ export class MarkdownCodeProcessor implements FilterableProcessor {
8081
$process(docs: Document[]) {
8182
const ret = docs.map((doc) => {
8283
if (this.docTypes.includes(doc.docType)) {
83-
doc[this.contentKey] = this.marked.parse(doc.content);
84+
doc[this.contentKey] = this.marked.parse(typeof doc.description === 'undefined' ? doc.content : doc.description);
8485
if (doc.kind === DaffDocKind.PACKAGE || doc.kind === DaffDocKind.COMPONENT) {
8586
doc.description = this.docDescription;
8687
}
88+
if (doc.examples) {
89+
doc.examples = (<Array<DaffDocExample>>doc.examples).map((example) => ({
90+
...example,
91+
body: this.marked.parse(example.body),
92+
}));
93+
}
8794
this.docDescription = null;
8895
};
8996
return doc;

tools/dgeni/src/transforms/daffodil-api-package/index.ts

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { Package } from 'dgeni';
1+
import {
2+
Document,
3+
Package,
4+
} from 'dgeni';
25
import linksPackage from 'dgeni-packages/links';
36
import typescriptPackage from 'dgeni-packages/typescript';
47

@@ -42,6 +45,11 @@ import {
4245
DESIGN_PATH,
4346
} from '../config';
4447
import { daffodilBasePackage } from '../daffodil-base-package';
48+
import {
49+
EXAMPLES_PROCESSOR_PROVIDER,
50+
ExamplesProcessor,
51+
} from './processors/examples';
52+
import { ConvertToJsonProcessor } from '../../processors/convertToJson';
4553

4654
const API_PACKAGE_NAME = 'daffodil-api';
4755

@@ -62,6 +70,7 @@ export const apiDocsBase = new Package('api-base', [
6270
.processor(...ADD_SUBPACKAGE_EXPORTS_PROCESSOR_PROVIDER)
6371
.processor(...MARKDOWN_CODE_PROCESSOR_PROVIDER)
6472
.processor(...COLLECT_LINKABLE_SYMBOLS_PROCESSOR_PROVIDER)
73+
.processor(...EXAMPLES_PROCESSOR_PROVIDER)
6574
.factory('API_DOC_TYPES_TO_RENDER', (EXPORT_DOC_TYPES) => EXPORT_DOC_TYPES.concat(['component', 'directive', 'pipe']))
6675
.config((readFilesProcessor, readTypeScriptModules, tsParser) => {
6776

@@ -76,8 +85,15 @@ export const apiDocsBase = new Package('api-base', [
7685
readTypeScriptModules.basePath = API_SOURCE_PATH;
7786
readTypeScriptModules.hidePrivateMembers = true;
7887
})
79-
.config((markdown: MarkdownCodeProcessor, EXPORT_DOC_TYPES, addKind: AddKindProcessor, breadcrumb: BreadcrumbProcessor) => {
88+
.config((
89+
markdown: MarkdownCodeProcessor,
90+
EXPORT_DOC_TYPES,
91+
addKind: AddKindProcessor,
92+
breadcrumb: BreadcrumbProcessor,
93+
examples: ExamplesProcessor,
94+
) => {
8095
markdown.docTypes.push(...EXPORT_DOC_TYPES);
96+
examples.docTypes.push(...EXPORT_DOC_TYPES);
8197
addKind.docTypes.push(...EXPORT_DOC_TYPES, 'package', 'module');
8298
breadcrumb.docTypes.push(...EXPORT_DOC_TYPES, 'package');
8399
markdown.contentKey = 'description';
@@ -96,10 +112,22 @@ export const apiDocsBase = new Package('api-base', [
96112
parseTagsProcessor.tagDefinitions = parseTagsProcessor.tagDefinitions.concat([
97113
{ name: 'docs-private' },
98114
{ name: 'inheritdoc' },
115+
{
116+
name: 'example',
117+
multi: true,
118+
transforms: (doc: Document, tag: any, value: any) => {
119+
const match = value.match(/^(.*)$/gm);
120+
tag.caption = match[0];
121+
tag.body = value.replace(match[0], '').trim();
122+
123+
return value;
124+
},
125+
},
99126
]);
100127
})
101-
.config((convertToJson, API_DOC_TYPES_TO_RENDER) => {
128+
.config((convertToJson: ConvertToJsonProcessor, API_DOC_TYPES_TO_RENDER) => {
102129
convertToJson.docTypes = convertToJson.docTypes.concat(API_DOC_TYPES_TO_RENDER);
130+
convertToJson.extraFields.push('examples');
103131
})
104132
.config((templateFinder) => {
105133
// Where to find the templates for the API doc rendering
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Document } from 'dgeni';
2+
3+
import { DaffDocExample } from '@daffodil/docs-utils';
4+
5+
import { MARKDOWN_CODE_PROCESSOR_NAME } from '../../../processors/markdown';
6+
import { FilterableProcessor } from '../../../utils/filterable-processor.type';
7+
8+
export const EXAMPLES_PROCESSOR_NAME = 'examples';
9+
10+
/**
11+
* Adds subpackage entry point docs to the containing package doc.
12+
*/
13+
export class ExamplesProcessor implements FilterableProcessor {
14+
readonly name = EXAMPLES_PROCESSOR_NAME;
15+
readonly $runAfter = ['docs-processed'];
16+
readonly $runBefore = ['rendering-docs', MARKDOWN_CODE_PROCESSOR_NAME];
17+
18+
docTypes = [];
19+
20+
$process(docs: Array<Document>): Array<Document> {
21+
return docs.map((doc) => {
22+
if (this.docTypes.includes(doc.docType)) {
23+
doc.examples = doc.tags.tagsByName.get('example')?.map((example): DaffDocExample => ({
24+
caption: example.caption,
25+
body: example.body,
26+
})) || [];
27+
}
28+
return doc;
29+
});
30+
}
31+
}
32+
33+
export const EXAMPLES_PROCESSOR_PROVIDER = <const>[
34+
EXAMPLES_PROCESSOR_NAME,
35+
() => new ExamplesProcessor(),
36+
];

0 commit comments

Comments
 (0)