forked from webpack/webpack-doc-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
182 lines (167 loc) · 5.76 KB
/
Copy pathindex.mjs
File metadata and controls
182 lines (167 loc) · 5.76 KB
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 { MarkdownTheme, MarkdownThemeContext } from 'typedoc-plugin-markdown';
import helpers from './helpers/index.mjs';
import partials from './partials/index.mjs';
export class DocKitTheme extends MarkdownTheme {
getRenderContext(page) {
this.application.options.setValue('hidePageHeader', true);
this.application.options.setValue('hideBreadcrumbs', true);
this.application.options.setValue('propertiesFormat', 'table');
return new DocKitThemeContext(this, page, this.application.options);
}
}
export class DocKitThemeContext extends MarkdownThemeContext {
helpers = {
...this.helpers,
/** @param {import('typedoc').ParameterReflection} */
typedListItem: ({ label, name, type, comment }) => {
const namePart = label ? ` ${label}:` : name ? ` \`${name}\`` : "";
const typePart = type
? ` ${typeof type === "string" ? type : this.partials.someType(type)}`
: "";
const descPart = comment
? ` ${this.helpers.getCommentParts(comment.summary ?? comment.content)}`
: "";
return `*${namePart}${typePart}${descPart}`;
},
typedList: (entries) => entries.map(this.helpers.typedListItem).join("\n"),
};
partials = {
...this.partials,
...typePartials,
constructor: (model, options) => {
return model.signatures?.map(signature => {
const params = signature.parameters ?? [];
const className = model.parent?.name ?? "Unknown";
const allOptional = params.length > 0 &&
params.every(p => p.flags?.isOptional);
const paramStr = allOptional
? `[${params.map(p => p.name).join(", ")}]`
: params.map(p =>
p.flags?.isOptional ? `[${p.name}]` : p.name
).join(", ");
const title = `\`new ${className}(${paramStr})\``;
const paramsList = params.length
? this.helpers.typedList(params)
: "";
return [
`#### ${title}`,
paramsList,
].filter(Boolean).join("\n");
}).join("\n\n") ?? "";
},
// Removes *** horizontal rules between members
members: (model, options) => {
const items = model.filter(
(item) => !this.router.hasOwnDocument(item)
);
return items
.map(item =>
this.partials.memberContainer(item, {
headingLevel: options.headingLevel,
groupTitle: options.groupTitle,
})
)
.filter(Boolean)
.join("\n\n");
},
// Removes ### Constructors / ### Methods / ### Properties headings
groups: (model, options) => {
return (model.groups ?? [])
.flatMap(group => {
// Skip properties — already shown in constructor params
const isPropertiesGroup = group.children?.every(
child => child.kind === ReflectionKind.Property
);
if (isPropertiesGroup) return [];
const children = group.children?.filter(
child => child.isDeclaration()
) ?? [];
if (!children.length) return [];
return [
this.partials.members(children, {
headingLevel: options.headingLevel,
groupTitle: group.title,
})
];
})
.filter(Boolean)
.join("\n\n");
},
body: (model, options) => {
if (model.groups?.length) {
return this.partials.groups(model, {
headingLevel: options.headingLevel,
kind: model.kind,
});
}
return "";
},
// Typed Lists
parametersList: this.helpers.typedList,
propertiesTable: this.helpers.typedList,
signature: (model, options) => {
const comment = options.multipleSignatures
? model.comment
: model.comment || model.parent?.comment;
return [
model.typeParameters?.length &&
this.partials.typeParametersList(model.typeParameters, {
headingLevel: options.headingLevel,
}),
model.parameters?.length &&
this.partials.parametersList(model.parameters, {
headingLevel: options.headingLevel,
}),
this.helpers.typedListItem({
label: "Returns",
type: model.type ?? "void",
comment: model.comment?.getTag("@returns"),
}),
"",
comment &&
this.partials.comment(comment, {
headingLevel: options.headingLevel,
}),
]
.filter((x) => (typeof x === "string" ? x : Boolean(x)))
.join("\n");
},
// Titles
memberTitle: (model) => {
//DEBUG
// console.log("KIND:", model.kind, "NAME:", model.name, "CONSTRUCTOR KIND:", ReflectionKind.Constructor);
const prefix = resolveMemberPrefix(model);
const params = model.signatures?.[0]?.parameters ?? null;
const name = params
? `\`${model.name}(${params.map((p) => p.name).join(", ")})\``
: `\`${model.name}\``;
return prefix ? `${prefix}: ${name}` : name;
},
declarationTitle: (model) => {
return this.helpers.typedListItem({
name: model.name,
type: model.type,
comment: model.comment,
});
},
memberContainer: (model, options) => {
const md = [];
if (!this.router.hasOwnDocument(model) &&
![ReflectionKind.Constructor].includes(model.kind)) {
md.push(
"#".repeat(options.headingLevel) + " " +
this.partials.memberTitle(model)
);
}
md.push(this.partials.member(model, {
headingLevel: options.headingLevel + 1, // ← methods get ####
nested: options.nested,
}));
return md.filter(Boolean).join("\n\n");
},
};
}
/** @param {import('typedoc').Application} app */
export function load(app) {
app.renderer.defineTheme('doc-kit', DocKitTheme);
}