Skip to content

Commit b35df71

Browse files
committed
fixing code for some edge cases
1 parent 5c8daca commit b35df71

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

plugins/theme/helpers/index.mjs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,39 @@ export default (ctx) => ({
6565
return null;
6666
},
6767

68-
renderExamples(comment) {
69-
if (!comment || !comment.blockTags) return "";
70-
const examples = comment.blockTags.filter((tag) => tag.tag === "@example");
68+
/**
69+
* Renders `@example` tags from a comment as Markdown.
70+
* Pass `headingLevel` to prepend a heading (block context);
71+
* omit it for inline use (e.g. inside a list item).
72+
* Always returns a string — never `null`.
73+
*
74+
* @param {import("typedoc").Comment | import("typedoc").CommentTag | null | undefined} comment
75+
* @param {number} [headingLevel]
76+
* @returns {string}
77+
*/
78+
renderExamples(comment, headingLevel) {
79+
const examples =
80+
comment?.blockTags?.filter((t) => t.tag === "@example") ?? [];
7181
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-
);
82+
83+
const bodies = examples
84+
.map((tag) => {
85+
const body = ctx.helpers.getCommentParts(tag.content).trim();
86+
if (!body) return null; // skip empty @example tags
87+
88+
if (headingLevel != null) {
89+
// Block context (signature / comment partial) → add heading
90+
const prefix = "#".repeat(headingLevel + 1);
91+
const suffix =
92+
examples.length > 1 ? ` ${examples.indexOf(tag) + 1}` : "";
93+
return `${prefix} Example${suffix}\n\n${body}`;
94+
}
95+
96+
// Inline context (typedListItem) → no heading, just body
97+
return body;
98+
})
99+
.filter(Boolean);
100+
101+
return bodies.length ? "\n\n" + bodies.join("\n\n") : "";
78102
},
79103
});

plugins/theme/partials/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default (ctx) => {
3737
comment(model, options) {
3838
return (
3939
baseComment.call(ctx, model, { ...options, showTags: false }) +
40-
ctx.helpers.renderExamples(model)
40+
ctx.helpers.renderExamples(model, options?.headingLevel)
4141
);
4242
},
4343

@@ -70,7 +70,7 @@ export default (ctx) => {
7070
headingLevel: options.headingLevel,
7171
showTags: false,
7272
}),
73-
ctx.helpers.renderExamples(comment),
73+
ctx.helpers.renderExamples(comment, options.headingLevel),
7474
]
7575
.filter((x) => typeof x === "string" || Boolean(x))
7676
.join("\n");

0 commit comments

Comments
 (0)