Skip to content

Commit f5f31b0

Browse files
committed
Expose Context.getNodeComment, #2498
1 parent d26f76f commit f5f31b0

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
- Added a new `--sitemapBaseUrl` option. When specified, TypeDoc will generate a `sitemap.xml` in your output folder that describes the site, #2480.
66
- Added support for the `@class` tag. When added to a comment on a variable or function, TypeDoc will convert the member as a class, #2479.
77
Note: This should only be used on symbols which actually represent a class, but are not declared as a class for some reason.
8-
9-
## Features
10-
118
- Added support for `@groupDescription` and `@categoryDescription` to provide a description of groups and categories, #2494.
9+
- Exposed `Context.getNodeComment` for plugin use, #2498.
1210

1311
## Bug Fixes
1412

src/lib/converter/comments/discovery.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ export function discoverFileComment(
133133
}
134134
}
135135

136+
export function discoverNodeComment(
137+
node: ts.Node,
138+
commentStyle: CommentStyle,
139+
): DiscoveredComment | undefined {
140+
const text = node.getSourceFile().text;
141+
const comments = collectCommentRanges(
142+
ts.getLeadingCommentRanges(text, node.pos),
143+
);
144+
comments.reverse();
145+
146+
const selectedDocComment = comments.find((ranges) =>
147+
permittedRange(text, ranges, commentStyle),
148+
);
149+
150+
if (selectedDocComment) {
151+
return {
152+
file: node.getSourceFile(),
153+
ranges: selectedDocComment,
154+
jsDoc: findJsDocForComment(node, selectedDocComment),
155+
};
156+
}
157+
}
158+
136159
export function discoverComment(
137160
symbol: ts.Symbol,
138161
kind: ReflectionKind,

src/lib/converter/comments/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
DiscoveredComment,
1111
discoverComment,
1212
discoverFileComment,
13+
discoverNodeComment,
1314
discoverSignatureComment,
1415
} from "./discovery";
1516
import { lexLineComments } from "./lineLexer";
@@ -171,6 +172,23 @@ export function getComment(
171172
return comment;
172173
}
173174

175+
export function getNodeComment(
176+
node: ts.Node,
177+
kind: ReflectionKind,
178+
config: CommentParserConfig,
179+
logger: Logger,
180+
commentStyle: CommentStyle,
181+
checker: ts.TypeChecker | undefined,
182+
) {
183+
return getCommentImpl(
184+
discoverNodeComment(node, commentStyle),
185+
config,
186+
logger,
187+
kind === ReflectionKind.Module,
188+
checker,
189+
);
190+
}
191+
174192
export function getFileComment(
175193
file: ts.SourceFile,
176194
config: CommentParserConfig,

src/lib/converter/context.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
getComment,
1919
getFileComment,
2020
getJsDocComment,
21+
getNodeComment,
2122
getSignatureComment,
2223
} from "./comments";
2324
import { getHumanName } from "../utils/tsutils";
@@ -270,6 +271,17 @@ export class Context {
270271
);
271272
}
272273

274+
getNodeComment(node: ts.Node, kind: ReflectionKind) {
275+
return getNodeComment(
276+
node,
277+
kind,
278+
this.converter.config,
279+
this.logger,
280+
this.converter.commentStyle,
281+
this.converter.useTsLinkResolution ? this.checker : undefined,
282+
);
283+
}
284+
273285
getFileComment(node: ts.SourceFile) {
274286
return getFileComment(
275287
node,

0 commit comments

Comments
 (0)