-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.ts
182 lines (169 loc) · 4.73 KB
/
toc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {
ApiItemKind,
ApiModel,
ApiPackage
} from '@microsoft/api-extractor-model'
import type {
MarkdownContent,
ReferenceResolver,
GenerateStyle
} from '../config'
import { ContentBuilder, createContentBuilder } from '../builder'
import {
buildClassContent,
buildEnumContent,
buildFunctionContent,
buildInterfaceContent,
buildTypeAliasContent,
buildVariableContent
} from './utils'
/**
* Process of API doc model
*
* @remarks
* Generate the markdown contents that have TOC.
* About API doc model, see the {@link https://api-extractor.com/pages/overview/demo_docs/ | doc model structure}, and {@link https://github.com/microsoft/rushstack/tree/master/apps/api-extractor-model | doc model API}.
* In about generate api docs, see the {@link https://github.com/kazupon/api-docs-gen/blob/master/api-docs-gen-api.md | api-docs-gen API References}
*
* @param model - a {@link https://rushstack.io/pages/api/api-extractor-model.apimodel/ | model}
* @param pkg - a {@link https://rushstack.io/pages/api/api-extractor-model.apipackage/ | package}
* @param style - generate style, See the {@link GenerateStyle}
* @param resolver - {@link ReferenceResolver | resolver} to resolve markdown content references
* @param customTags - TSDoc custom tags. This parameter is set to an array of custom tag names defined in `--tsdoc-config`.
*
* @returns markdown string content that have TOC
*
* @public
*/
export function process(
model: ApiModel,
pkg: ApiPackage,
style: GenerateStyle,
resolver: ReferenceResolver,
customTags?: string[]
): string | MarkdownContent[] {
function build(): string {
const builder = createContentBuilder()
builder.pushline(`# ${pkg.displayName} API References`)
builder.newline()
buildTOC(builder)
buildContents(builder)
return builder.content
}
function buildTOC(builder: ContentBuilder): void {
builder.pushline(`## Table Of Contents`)
builder.newline()
const builders = new Map<string, ContentBuilder>()
const items = pkg.members[0] ? pkg.members[0].members : []
for (const item of items) {
const { kind } = item
let tocBuilder = builders.get(kind)
if (!tocBuilder) {
tocBuilder = createContentBuilder()
tocBuilder.pushline(`- [${kind}](#${kind.toLowerCase()})`)
builders.set(kind, tocBuilder)
}
tocBuilder.pushline(
` - [${item.displayName}](#${item.displayName.toLowerCase()})`
)
}
for (const [, tocBuilder] of builders) {
builder.push(tocBuilder.content)
}
builder.newline()
}
function buildContents(builder: ContentBuilder): void {
const builders = new Map<string, ContentBuilder>()
const items = pkg.members[0] ? pkg.members[0].members : []
for (const item of items) {
const { kind } = item
let contentBuilder = builders.get(kind)
if (!contentBuilder) {
contentBuilder = createContentBuilder()
contentBuilder.pushline(`## ${kind}`)
contentBuilder.newline()
builders.set(kind, contentBuilder)
}
switch (kind) {
case ApiItemKind.Function:
buildFunctionContent(
style,
model,
pkg,
resolver,
contentBuilder,
item,
customTags || [],
3
)
break
case ApiItemKind.Enum:
buildEnumContent(
style,
model,
pkg,
resolver,
contentBuilder,
item,
customTags || [],
3
)
break
case ApiItemKind.Interface:
buildInterfaceContent(
style,
model,
pkg,
resolver,
contentBuilder,
item,
customTags || [],
3
)
break
case ApiItemKind.Class:
buildClassContent(
style,
model,
pkg,
resolver,
contentBuilder,
item,
customTags || [],
3
)
break
case ApiItemKind.TypeAlias:
buildTypeAliasContent(
style,
model,
pkg,
resolver,
contentBuilder,
item,
customTags || [],
3
)
break
case ApiItemKind.Variable:
buildVariableContent(
style,
model,
pkg,
resolver,
contentBuilder,
item,
customTags || [],
3
)
break
default:
break
}
}
for (const [, contentBuilder] of builders) {
builder.pushline(contentBuilder.content)
}
}
return build()
}