Skip to content

Commit 78ca4a6

Browse files
authored
feat: support custom tags (#31)
* feat: support custom tags * docs: update api docs * test: update
1 parent f024a0d commit 78ca4a6

21 files changed

+1677
-74
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
'@typescript-eslint/member-delimiter-style': 'off',
2727
'@typescript-eslint/no-use-before-define': 'off',
2828
'@typescript-eslint/explicit-module-boundary-types': 'off',
29-
'@typescript-eslint/ban-ts-comment': 'off'
29+
'@typescript-eslint/ban-ts-comment': 'off',
30+
'@typescript-eslint/no-non-null-assertion': 'off'
3031
}
3132
}

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ Usage
3333
$ api-docs-gen <package1.api.json> <package2.api.json> ...
3434
3535
Options
36-
--config, -c configuration file
36+
--config, -c configration file
3737
--output, -o output dierectory that is markdown contents
3838
--generate-style, -g document generating style, default 'prefix'
3939
'prefix': be able to separated with each package name
4040
'directory': be able to separated with each package directory
41+
--tsdoc-config, -t tsdoc configration file
4142
```
4243

4344

@@ -53,7 +54,7 @@ const input = [path.resolve(process.cwd(), './package1.api.json')]
5354
const output = path.resolve(process.cwd(), './docs')
5455

5556
// generate API docs with prefixed package name
56-
await generate(input, output, 'prefix', DefaultConfig)
57+
await generate(input, output, { style: 'prefix', config: DefaultConfig })
5758
```
5859

5960
About details, See the [API References](https://github.com/kazupon/api-docs-gen/blob/master/api-docs-gen-api.md)
@@ -76,6 +77,21 @@ $ yarn example:gen # genearte API docs with `api-docs-gen`
7677
$ yarn example:docs # run vuepress
7778
```
7879

80+
## :bookmark: TSDoc custom tags
81+
82+
`api-docs-gen` allows TSDoc custom tags to be processed using [tsdoc-config](https://github.com/microsoft/tsdoc/tree/master/tsdoc-config).
83+
84+
You can make it work from the `api-extractor` model by specifying tsdoc configration in the `--tsdoc-config` option as follows:
85+
86+
```bash
87+
$ api-docs-gen package1.api.json --tsdoc-config ./tsdoc.json
88+
```
89+
90+
If you want to output custom tags comment to markdown, you need to implement and configure the custom `MarkdownProcessor`.
91+
92+
For the `MarkdownProcessor`, see the [API References](https://github.com/kazupon/api-docs-gen/blob/master/api-docs-gen-api.md).
93+
94+
See how to configure it in the following configration.
7995

8096
## :wrench: Configration
8197
You can fully customize the generation of api docs using the config offered by `api-docs-gen`.

api-docs-gen-api.md

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [Config](#config)
77
- [ContentBuilder](#contentbuilder)
88
- [ContentBuilderOptions](#contentbuilderoptions)
9+
- [GenerateOptions](#generateoptions)
910
- [MarkdownContent](#markdowncontent)
1011
- [Function](#function)
1112
- [createContentBuilder](#createcontentbuilder)
@@ -199,6 +200,87 @@ indentLevel?: number;
199200
```
200201

201202

203+
### GenerateOptions
204+
205+
Generate Options for Generate API
206+
207+
**Signature:**
208+
```typescript
209+
export interface GenerateOptions
210+
```
211+
212+
213+
#### Methods
214+
215+
216+
#### Properties
217+
218+
##### config
219+
220+
configration
221+
222+
**Signature:**
223+
```typescript
224+
config: Config;
225+
```
226+
227+
#### Remarks
228+
229+
see the [Config](#config)
230+
231+
##### done
232+
233+
generate done callback
234+
235+
**Signature:**
236+
```typescript
237+
done?: (pkgname: string, filename: string) => void;
238+
```
239+
240+
#### Remarks
241+
242+
The callback that will be called when the generate process is finished.
243+
244+
##### errorOnTSDocConfig
245+
246+
TSDoc configration error callback
247+
248+
**Signature:**
249+
```typescript
250+
errorOnTSDocConfig?: (error: string) => void;
251+
```
252+
253+
#### Remarks
254+
255+
The callback occurs if you have an error in configration when `--tsdoc-config` is specified
256+
257+
##### style
258+
259+
generate style
260+
261+
**Signature:**
262+
```typescript
263+
style: GenerateStyle;
264+
```
265+
266+
#### Remarks
267+
268+
see the [GenerateStyle](#generatestyle)
269+
270+
##### tsdocConfigPath
271+
272+
TSDoc configration path
273+
274+
**Signature:**
275+
```typescript
276+
tsdocConfigPath?: string;
277+
```
278+
279+
#### Remarks
280+
281+
Optional, see the [here](https://github.com/microsoft/tsdoc/tree/master/tsdoc-config)
282+
283+
202284
### MarkdownContent
203285

204286
Markdown content
@@ -304,7 +386,7 @@ Generate API docs
304386

305387
**Signature:**
306388
```typescript
307-
export declare function generate(input: string[], output: string, style: GenerateStyle, config: Config, callback?: (pkgname: string, filename: string) => void): Promise<void>;
389+
export declare function generate(input: string[], output: string, options: GenerateOptions): Promise<void>;
308390
```
309391

310392
#### Parameters
@@ -313,17 +395,15 @@ export declare function generate(input: string[], output: string, style: Generat
313395
| --- | --- | --- |
314396
| input | string\[\] | input paths |
315397
| output | string | output api docs full path |
316-
| style | GenerateStyle | generate style, see the [GenerateStyle](#generatestyle) |
317-
| config | Config | configration, see the [Config](#config) |
318-
| callback | (pkgname: string, filename: string) =&gt; void | |
398+
| options | GenerateOptions | optiosn for generate, see the [GenerateOptions](#generateoptions) |
319399

320400
### getDocSectionContent
321401

322402
Get DocSection content
323403

324404
**Signature:**
325405
```typescript
326-
export declare function getDocSectionContent(model: ApiModel, pkg: ApiPackage, content: DocSection, contextItem: ApiItem, style: GenerateStyle, resolver: ReferenceResolver): string;
406+
export declare function getDocSectionContent(model: ApiModel, pkg: ApiPackage, content: DocSection, contextItem: ApiItem, style: GenerateStyle, resolver: ReferenceResolver, customTags: string[]): string;
327407
```
328408

329409
#### Parameters
@@ -336,6 +416,7 @@ export declare function getDocSectionContent(model: ApiModel, pkg: ApiPackage, c
336416
| contextItem | ApiItem | a context [item](https://rushstack.io/pages/api/api-extractor-model.apiitem/) |
337417
| style | GenerateStyle | generate style, See the [GenerateStyle](#generatestyle) |
338418
| resolver | ReferenceResolver | [resolver](#referenceresolver) to resolve markdown content references |
419+
| customTags | string\[\] | |
339420

340421
#### Returns
341422

@@ -366,7 +447,7 @@ Process of API doc model
366447

367448
**Signature:**
368449
```typescript
369-
export declare function process(model: ApiModel, pkg: ApiPackage, style: GenerateStyle, resolver: ReferenceResolver): string | MarkdownContent[];
450+
export declare function process(model: ApiModel, pkg: ApiPackage, style: GenerateStyle, resolver: ReferenceResolver, customTags?: string[]): string | MarkdownContent[];
370451
```
371452

372453
#### Parameters
@@ -377,6 +458,7 @@ export declare function process(model: ApiModel, pkg: ApiPackage, style: Generat
377458
| pkg | ApiPackage | a [package](https://rushstack.io/pages/api/api-extractor-model.apipackage/) |
378459
| style | GenerateStyle | generate style, See the [GenerateStyle](#generatestyle) |
379460
| resolver | ReferenceResolver | [resolver](#referenceresolver) to resolve markdown content references |
461+
| customTags | string\[\] | TSDoc custom tags. This parameter is set to an array of custom tag names defined in `--tsdoc-config`. |
380462

381463
#### Returns
382464

@@ -426,7 +508,7 @@ Process of API doc model
426508

427509
**Signature:**
428510
```typescript
429-
export declare function process(model: ApiModel, pkg: ApiPackage, style: GenerateStyle, resolver: ReferenceResolver): string | MarkdownContent[];
511+
export declare function process(model: ApiModel, pkg: ApiPackage, style: GenerateStyle, resolver: ReferenceResolver, customTags?: string[]): string | MarkdownContent[];
430512
```
431513

432514
#### Parameters
@@ -437,6 +519,7 @@ export declare function process(model: ApiModel, pkg: ApiPackage, style: Generat
437519
| pkg | ApiPackage | a [package](https://rushstack.io/pages/api/api-extractor-model.apipackage/) |
438520
| style | GenerateStyle | generate style, See the [GenerateStyle](#generatestyle) |
439521
| resolver | ReferenceResolver | [resolver](#referenceresolver) to resolve markdown content references |
522+
| customTags | string\[\] | TSDoc custom tags. This parameter is set to an array of custom tag names defined in `--tsdoc-config`. |
440523

441524
#### Returns
442525

@@ -520,7 +603,7 @@ Markdown docs processor
520603

521604
**Signature:**
522605
```typescript
523-
export declare type MarkdownProcessor = (model: ApiModel, pkg: ApiPackage, style: GenerateStyle, resolver: ReferenceResolver) => string | MarkdownContent[];
606+
export declare type MarkdownProcessor = (model: ApiModel, pkg: ApiPackage, style: GenerateStyle, resolver: ReferenceResolver, customTags?: string[]) => string | MarkdownContent[];
524607
```
525608

526609
### ReferenceResolver

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"dependencies": {
3030
"@microsoft/api-extractor-model": "^7.12.0",
3131
"@microsoft/tsdoc": "^0.12.21",
32+
"@microsoft/tsdoc-config": "^0.13.6",
3233
"chalk": "^4.1.0",
3334
"debug": "^4.3.1",
3435
"meow": "^8.0.0"

src/cli.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export const flags = {
2323
type: 'string',
2424
alias: 'g',
2525
default: GenerateStyle.Prefix
26+
},
27+
'tsdoc-config': {
28+
type: 'string',
29+
alias: 't'
2630
}
2731
} as const
2832

@@ -32,17 +36,19 @@ const cli = meow(
3236
$ api-docs-gen <package1.api.json> <package2.api.json> ...
3337
3438
Options
35-
--config, -c configuration file
39+
--config, -c configration file
3640
--output, -o output dierectory that is markdown contents
3741
--generate-style, -g document generating style, default 'prefix'
3842
'prefix': be able to separated with each package name
3943
'directory': be able to separated with each package directory
44+
--tsdoc-config, -t tsdoc configration file
4045
4146
Examples
4247
$ api-docs-gen package1.api.json
4348
$ api-docs-gen package1.api.json --output ./docs
4449
$ api-docs-gen package1.api.json --config docsgen.config.js
4550
$ api-docs-gen package1.api.json package2.api.json --generate-style directory
51+
$ api-docs-gen package1.api.json --tsdoc-config ./tsdoc.json
4652
`,
4753
{
4854
flags
@@ -91,18 +97,26 @@ const genStyle = [GenerateStyle.Prefix, GenerateStyle.Directory].includes(
9197
: GenerateStyle.Prefix
9298
debug('packageDocsStyle', genStyleFlag, genStyle)
9399

100+
// tsdoc configratio
101+
const tsdocConfig = cli.flags['tsdoc-config']
102+
94103
// run
95104
try {
96105
;(async () => {
97-
await generate(
98-
input,
99-
output,
100-
genStyle,
106+
await generate(input, output, {
107+
style: genStyle,
101108
config,
102-
(pkgname: string, filepath: string) => {
109+
tsdocConfigPath:
110+
tsdocConfig != null
111+
? path.resolve(process.cwd(), tsdocConfig)
112+
: undefined,
113+
errorOnTSDocConfig: (error: string): void => {
114+
console.log(chalk.yellow(`⚠️ Error on TSDoc configration: ${error}`))
115+
},
116+
done: (pkgname: string, filepath: string): void => {
103117
console.log(chalk.green(`📦 ${pkgname}: 📝 save ${filepath}`))
104118
}
105-
)
119+
})
106120
})()
107121
} catch (e) {
108122
console.error(chalk.red(`[api-docs-gen] ${e.message}`))

src/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export interface MarkdownContent {
6969
* @param pkg - a {@link https://rushstack.io/pages/api/api-extractor-model.apipackage/ | package}
7070
* @param style - generate style. see the {@link GenerateStyle}
7171
* @param resolver - the markdown reference {@link ReferenceResolver | resolver}. if you're specfified at {@link Config}, it's passed, else it's not specified passed internal refenrece resolver.
72+
* @param customTags - TSDoc custom tags. This parameter is set to an array of custom tag names defined in `--tsdoc-config`.
7273
*
7374
* @returns markdown content
7475
*
@@ -78,7 +79,8 @@ export type MarkdownProcessor = (
7879
model: ApiModel,
7980
pkg: ApiPackage,
8081
style: GenerateStyle,
81-
resolver: ReferenceResolver
82+
resolver: ReferenceResolver,
83+
customTags?: string[]
8284
) => string | MarkdownContent[]
8385

8486
/**

0 commit comments

Comments
 (0)