Skip to content

Commit 5c8daca

Browse files
committed
migrate code from processor to theme
1 parent 991b4ee commit 5c8daca

3 files changed

Lines changed: 83 additions & 86 deletions

File tree

plugins/processor.mjs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,6 @@ export function load(app) {
2727
.forEach((namespace) =>
2828
context.project.mergeReflections(namespace, namespace.parent),
2929
);
30-
31-
// Append examples to the body of members
32-
context.project
33-
.getReflectionsByKind(ReflectionKind.All)
34-
.forEach((reflection) => {
35-
if (reflection.comment) {
36-
const examples = reflection.comment.blockTags.filter(
37-
(tag) => tag.tag === "@example",
38-
);
39-
if (examples.length > 0) {
40-
if (reflection.comment.summary.length > 0) {
41-
reflection.comment.summary.push({ kind: "text", text: "\n\n" });
42-
}
43-
44-
examples.forEach((example, index) => {
45-
reflection.comment.summary.push(...example.content);
46-
if (index < examples.length - 1) {
47-
reflection.comment.summary.push({ kind: "text", text: "\n\n" });
48-
}
49-
});
50-
51-
reflection.comment.blockTags = reflection.comment.blockTags.filter(
52-
(tag) => tag.tag !== "@example",
53-
);
54-
}
55-
}
56-
});
5730
});
5831

5932
app.renderer.on(Renderer.EVENT_END, (context) => {

plugins/theme/helpers/index.mjs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default (ctx) => ({
2121
: "";
2222

2323
const descPart = comment
24-
? ` ${ctx.helpers.getCommentParts(comment.summary ?? comment.content)}`
24+
? ` ${ctx.helpers.getCommentParts(comment.summary ?? comment.content)}${ctx.helpers.renderExamples(comment)}`
2525
: "";
2626

2727
return `*${namePart}${typePart}${descPart}`;
@@ -64,4 +64,16 @@ export default (ctx) => ({
6464

6565
return null;
6666
},
67+
68+
renderExamples(comment) {
69+
if (!comment || !comment.blockTags) return "";
70+
const examples = comment.blockTags.filter((tag) => tag.tag === "@example");
71+
if (!examples.length) return "";
72+
return (
73+
"\n\n" +
74+
examples
75+
.map((tag) => ctx.helpers.getCommentParts(tag.content).trim())
76+
.join("\n\n")
77+
);
78+
},
6779
});

plugins/theme/partials/index.mjs

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -27,70 +27,82 @@ export const getMemberPrefix = (model) => {
2727
* @param {import('typedoc-plugin-markdown').MarkdownThemeContext} ctx
2828
* @returns {import('typedoc-plugin-markdown').MarkdownThemeContext['partials']}
2929
*/
30-
export default (ctx) => ({
31-
...ctx.partials,
32-
...typePartials,
30+
export default (ctx) => {
31+
const baseComment = ctx.partials.comment;
3332

34-
signature(model, options) {
35-
const comment = options.multipleSignatures
36-
? model.comment
37-
: model.comment || model.parent?.comment;
33+
return {
34+
...ctx.partials,
35+
...typePartials,
3836

39-
const stability = ctx.helpers.stabilityBlockquote(comment);
37+
comment(model, options) {
38+
return (
39+
baseComment.call(ctx, model, { ...options, showTags: false }) +
40+
ctx.helpers.renderExamples(model)
41+
);
42+
},
4043

41-
return [
42-
stability,
43-
stability && "",
44-
model.typeParameters?.length &&
45-
ctx.partials.typeParametersList(model.typeParameters, {
46-
headingLevel: options.headingLevel,
47-
}),
48-
model.parameters?.length &&
49-
ctx.partials.parametersList(model.parameters, {
50-
headingLevel: options.headingLevel,
51-
}),
52-
ctx.helpers.typedListItem({
53-
label: "Returns",
54-
type: model.type ?? "void",
55-
comment: model.comment?.getTag("@returns"),
56-
}),
57-
"",
58-
comment &&
59-
ctx.partials.comment(comment, {
60-
headingLevel: options.headingLevel,
61-
showTags: false,
44+
signature(model, options) {
45+
const comment = options.multipleSignatures
46+
? model.comment
47+
: model.comment || model.parent?.comment;
48+
49+
const stability = ctx.helpers.stabilityBlockquote(comment);
50+
51+
return [
52+
stability,
53+
stability && "",
54+
model.typeParameters?.length &&
55+
ctx.partials.typeParametersList(model.typeParameters, {
56+
headingLevel: options.headingLevel,
57+
}),
58+
model.parameters?.length &&
59+
ctx.partials.parametersList(model.parameters, {
60+
headingLevel: options.headingLevel,
61+
}),
62+
ctx.helpers.typedListItem({
63+
label: "Returns",
64+
type: model.type ?? "void",
65+
comment: model.comment?.getTag("@returns"),
6266
}),
63-
]
64-
.filter((x) => typeof x === "string" || Boolean(x))
65-
.join("\n");
66-
},
67+
"",
68+
comment &&
69+
baseComment.call(ctx, comment, {
70+
headingLevel: options.headingLevel,
71+
showTags: false,
72+
}),
73+
ctx.helpers.renderExamples(comment),
74+
]
75+
.filter((x) => typeof x === "string" || Boolean(x))
76+
.join("\n");
77+
},
6778

68-
memberTitle(model) {
69-
const prefix = getMemberPrefix(model);
70-
const params = model.signatures?.[0]?.parameters;
79+
memberTitle(model) {
80+
const prefix = getMemberPrefix(model);
81+
const params = model.signatures?.[0]?.parameters;
7182

72-
if (!params) {
73-
return `${prefix}\`${model.name}\``;
74-
}
83+
if (!params) {
84+
return `${prefix}\`${model.name}\``;
85+
}
7586

76-
const paramsString = params
77-
.map((param, index) => {
78-
const paramName = param.name;
79-
if (param.flags?.isOptional) {
80-
// For optional params, wrap comma + name in brackets (except for first param)
81-
return index === 0 ? `[${paramName}]` : `[, ${paramName}]`;
82-
} else {
83-
// For required params, add comma separator (except for first param)
84-
return index === 0 ? paramName : `, ${paramName}`;
85-
}
86-
})
87-
.join("");
87+
const paramsString = params
88+
.map((param, index) => {
89+
const paramName = param.name;
90+
if (param.flags?.isOptional) {
91+
// For optional params, wrap comma + name in brackets (except for first param)
92+
return index === 0 ? `[${paramName}]` : `[, ${paramName}]`;
93+
} else {
94+
// For required params, add comma separator (except for first param)
95+
return index === 0 ? paramName : `, ${paramName}`;
96+
}
97+
})
98+
.join("");
8899

89-
return `${prefix}\`${model.name}(${paramsString})\``;
90-
},
100+
return `${prefix}\`${model.name}(${paramsString})\``;
101+
},
91102

92-
parametersList: ctx.helpers.typedList,
93-
typedParametersList: ctx.helpers.typedList,
94-
typeDeclarationList: ctx.helpers.typedList,
95-
propertiesTable: ctx.helpers.typedList,
96-
});
103+
parametersList: ctx.helpers.typedList,
104+
typedParametersList: ctx.helpers.typedList,
105+
typeDeclarationList: ctx.helpers.typedList,
106+
propertiesTable: ctx.helpers.typedList,
107+
};
108+
};

0 commit comments

Comments
 (0)