Skip to content

Commit 6c923d3

Browse files
committed
fix: render constructors as \new ClassName(params)\ format (closes #2)
1 parent c35b948 commit 6c923d3

1 file changed

Lines changed: 99 additions & 2 deletions

File tree

plugins/theme/index.mjs

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,79 @@ export class DocKitThemeContext extends MarkdownThemeContext {
5050
...this.partials,
5151
...typePartials,
5252

53+
constructor: (model, options) => {
54+
return model.signatures?.map(signature => {
55+
const params = signature.parameters ?? [];
56+
const className = model.parent?.name ?? "Unknown";
57+
const allOptional = params.length > 0 &&
58+
params.every(p => p.flags?.isOptional);
59+
const paramStr = allOptional
60+
? `[${params.map(p => p.name).join(", ")}]`
61+
: params.map(p =>
62+
p.flags?.isOptional ? `[${p.name}]` : p.name
63+
).join(", ");
64+
const title = `\`new ${className}(${paramStr})\``;
65+
const paramsList = params.length
66+
? this.helpers.typedList(params)
67+
: "";
68+
return [
69+
`#### ${title}`,
70+
paramsList,
71+
].filter(Boolean).join("\n");
72+
}).join("\n\n") ?? "";
73+
},
74+
// Removes *** horizontal rules between members
75+
members: (model, options) => {
76+
const items = model.filter(
77+
(item) => !this.router.hasOwnDocument(item)
78+
);
79+
return items
80+
.map(item =>
81+
this.partials.memberContainer(item, {
82+
headingLevel: options.headingLevel,
83+
groupTitle: options.groupTitle,
84+
})
85+
)
86+
.filter(Boolean)
87+
.join("\n\n");
88+
},
89+
90+
// Removes ### Constructors / ### Methods / ### Properties headings
91+
groups: (model, options) => {
92+
return (model.groups ?? [])
93+
.flatMap(group => {
94+
// Skip properties — already shown in constructor params
95+
const isPropertiesGroup = group.children?.every(
96+
child => child.kind === ReflectionKind.Property
97+
);
98+
if (isPropertiesGroup) return [];
99+
100+
const children = group.children?.filter(
101+
child => child.isDeclaration()
102+
) ?? [];
103+
if (!children.length) return [];
104+
105+
return [
106+
this.partials.members(children, {
107+
headingLevel: options.headingLevel,
108+
groupTitle: group.title,
109+
})
110+
];
111+
})
112+
.filter(Boolean)
113+
.join("\n\n");
114+
},
115+
116+
body: (model, options) => {
117+
if (model.groups?.length) {
118+
return this.partials.groups(model, {
119+
headingLevel: options.headingLevel,
120+
kind: model.kind,
121+
});
122+
}
123+
return "";
124+
},
125+
53126
// Typed Lists
54127
parametersList: this.helpers.typedList,
55128
propertiesTable: this.helpers.typedList,
@@ -84,14 +157,38 @@ export class DocKitThemeContext extends MarkdownThemeContext {
84157

85158
// Titles
86159
memberTitle: (model) => {
160+
//DEBUG
161+
// console.log("KIND:", model.kind, "NAME:", model.name, "CONSTRUCTOR KIND:", ReflectionKind.Constructor);
162+
87163
const prefix = resolveMemberPrefix(model);
88164
const params = model.signatures?.[0]?.parameters ?? null;
89165
const name = params
90-
? // TODO: If params optional, wrap in `[]`
91-
`\`${model.name}(${params.map((p) => p.name).join(", ")})\``
166+
? `\`${model.name}(${params.map((p) => p.name).join(", ")})\``
92167
: `\`${model.name}\``;
93168
return prefix ? `${prefix}: ${name}` : name;
94169
},
170+
declarationTitle: (model) => {
171+
return this.helpers.typedListItem({
172+
name: model.name,
173+
type: model.type,
174+
comment: model.comment,
175+
});
176+
},
177+
memberContainer: (model, options) => {
178+
const md = [];
179+
if (!this.router.hasOwnDocument(model) &&
180+
![ReflectionKind.Constructor].includes(model.kind)) {
181+
md.push(
182+
"#".repeat(options.headingLevel) + " " +
183+
this.partials.memberTitle(model)
184+
);
185+
}
186+
md.push(this.partials.member(model, {
187+
headingLevel: options.headingLevel + 1, // ← methods get ####
188+
nested: options.nested,
189+
}));
190+
return md.filter(Boolean).join("\n\n");
191+
},
95192
};
96193
}
97194

0 commit comments

Comments
 (0)