Skip to content

Commit bcf3e04

Browse files
committed
Add excludeCategories option
Resolves #1407 Closes #1415
1 parent 653b281 commit bcf3e04

File tree

8 files changed

+102
-25
lines changed

8 files changed

+102
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
### Features
44

5-
- Added `stripYamlFrontmatter` config option, #2381.
5+
- Added `stripYamlFrontmatter` config option to remove YAML frontmatter from README.md, #2381.
6+
- Added `--excludeCategories` config option to remove reflections present in any excluded category, #1407.
67
- Navigation is now written to a JS file and built dynamically, which significantly decreases document generation time
78
with large projects and also provides large space benefits. Themes may now override `DefaultTheme.buildNavigation`
89
to customize the displayed navigation tree, #2287.

src/lib/converter/plugins/CategoryPlugin.ts

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ReflectionCategory } from "../../models";
88
import { Component, ConverterComponent } from "../components";
99
import { Converter } from "../converter";
1010
import type { Context } from "../context";
11-
import { Option, getSortFunction, removeIf } from "../../utils";
11+
import { Option, getSortFunction } from "../../utils";
1212

1313
/**
1414
* A handler that sorts and categorizes the found reflections in the resolving phase.
@@ -157,13 +157,13 @@ export class CategoryPlugin extends ConverterComponent {
157157
* relevance boost to be used when searching
158158
* @returns An array containing all children of the given reflection categorized
159159
*/
160-
getReflectionCategories(
160+
private getReflectionCategories(
161161
reflections: DeclarationReflection[],
162162
): ReflectionCategory[] {
163163
const categories = new Map<string, ReflectionCategory>();
164164

165165
for (const child of reflections) {
166-
const childCategories = this.getCategories(child);
166+
const childCategories = this.extractCategories(child);
167167
if (childCategories.size === 0) {
168168
childCategories.add(CategoryPlugin.defaultCategory);
169169
}
@@ -197,31 +197,18 @@ export class CategoryPlugin extends ConverterComponent {
197197
* @privateRemarks
198198
* If you change this, also update getGroups in GroupPlugin accordingly.
199199
*/
200-
getCategories(reflection: DeclarationReflection) {
201-
const categories = new Set<string>();
202-
function extractCategoryTags(comment: Comment | undefined) {
203-
if (!comment) return;
204-
removeIf(comment.blockTags, (tag) => {
205-
if (tag.tag === "@category") {
206-
categories.add(
207-
Comment.combineDisplayParts(tag.content).trim(),
208-
);
209-
210-
return true;
211-
}
212-
return false;
213-
});
214-
}
200+
private extractCategories(reflection: DeclarationReflection) {
201+
const categories = CategoryPlugin.getCategories(reflection);
215202

216-
extractCategoryTags(reflection.comment);
203+
reflection.comment?.removeTags("@category");
217204
for (const sig of reflection.getNonIndexSignatures()) {
218-
extractCategoryTags(sig.comment);
205+
sig.comment?.removeTags("@category");
219206
}
220207

221208
if (reflection.type?.type === "reflection") {
222-
extractCategoryTags(reflection.type.declaration.comment);
209+
reflection.type.declaration.comment?.removeTags("@category");
223210
for (const sig of reflection.type.declaration.getNonIndexSignatures()) {
224-
extractCategoryTags(sig.comment);
211+
sig.comment?.removeTags("@category");
225212
}
226213
}
227214

@@ -245,7 +232,7 @@ export class CategoryPlugin extends ConverterComponent {
245232
* @param b The right reflection to sort.
246233
* @returns The sorting weight.
247234
*/
248-
static sortCatCallback(
235+
private static sortCatCallback(
249236
a: ReflectionCategory,
250237
b: ReflectionCategory,
251238
): number {
@@ -268,4 +255,34 @@ export class CategoryPlugin extends ConverterComponent {
268255
}
269256
return aWeight - bWeight;
270257
}
258+
259+
static getCategories(reflection: DeclarationReflection) {
260+
const categories = new Set<string>();
261+
function discoverCategories(comment: Comment | undefined) {
262+
if (!comment) return;
263+
for (const tag of comment.blockTags) {
264+
if (tag.tag === "@category") {
265+
categories.add(
266+
Comment.combineDisplayParts(tag.content).trim(),
267+
);
268+
}
269+
}
270+
}
271+
272+
discoverCategories(reflection.comment);
273+
for (const sig of reflection.getNonIndexSignatures()) {
274+
discoverCategories(sig.comment);
275+
}
276+
277+
if (reflection.type?.type === "reflection") {
278+
discoverCategories(reflection.type.declaration.comment);
279+
for (const sig of reflection.type.declaration.getNonIndexSignatures()) {
280+
discoverCategories(sig.comment);
281+
}
282+
}
283+
284+
categories.delete("");
285+
286+
return categories;
287+
}
271288
}

src/lib/converter/plugins/CommentPlugin.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
unique,
2323
partition,
2424
} from "../../utils";
25+
import { CategoryPlugin } from "./CategoryPlugin";
2526

2627
/**
2728
* These tags are not useful to display in the generated documentation.
@@ -118,6 +119,12 @@ export class CommentPlugin extends ConverterComponent {
118119
@Option("excludeNotDocumented")
119120
accessor excludeNotDocumented!: boolean;
120121

122+
@Option("excludeCategories")
123+
accessor excludeCategories!: string[];
124+
125+
@Option("defaultCategory")
126+
accessor defaultCategory!: string;
127+
121128
private _excludeKinds: number | undefined;
122129
private get excludeNotDocumentedKinds(): number {
123130
this._excludeKinds ??= this.application.options
@@ -478,6 +485,10 @@ export class CommentPlugin extends ConverterComponent {
478485
return true;
479486
}
480487

488+
if (this.excludedByCategory(reflection)) {
489+
return true;
490+
}
491+
481492
if (!comment) {
482493
// We haven't moved comments from the parent for signatures without a direct
483494
// comment, so don't exclude those due to not being documented.
@@ -545,6 +556,26 @@ export class CommentPlugin extends ConverterComponent {
545556

546557
return isHidden;
547558
}
559+
560+
private excludedByCategory(reflection: Reflection): boolean {
561+
const excludeCategories = this.excludeCategories;
562+
563+
let target: DeclarationReflection | undefined;
564+
if (reflection instanceof DeclarationReflection) {
565+
target = reflection;
566+
} else if (reflection instanceof SignatureReflection) {
567+
target = reflection.parent;
568+
}
569+
570+
if (!target || !excludeCategories.length) return false;
571+
572+
const categories = CategoryPlugin.getCategories(target);
573+
if (categories.size === 0) {
574+
categories.add(this.defaultCategory);
575+
}
576+
577+
return excludeCategories.some((cat) => categories.has(cat));
578+
}
548579
}
549580

550581
function inTypeLiteral(refl: Reflection | undefined) {

src/lib/converter/plugins/GroupPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class GroupPlugin extends ConverterComponent {
9999
* Extracts the groups for a given reflection.
100100
*
101101
* @privateRemarks
102-
* If you change this, also update getCategories in CategoryPlugin accordingly.
102+
* If you change this, also update extractCategories in CategoryPlugin accordingly.
103103
*/
104104
getGroups(reflection: DeclarationReflection) {
105105
const groups = new Set<string>();

src/lib/utils/options/declaration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export interface TypeDocOptionMap {
105105
excludePrivate: boolean;
106106
excludeProtected: boolean;
107107
excludeReferences: boolean;
108+
excludeCategories: string[];
108109
name: string;
109110
includeVersion: boolean;
110111
disableSources: boolean;

src/lib/utils/options/sources/typedoc.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
142142
help: "Prevent symbols that are marked with @internal from being documented.",
143143
type: ParameterType.Boolean,
144144
});
145+
options.addDeclaration({
146+
name: "excludeCategories",
147+
help: "Exclude symbols within this category from the documentation.",
148+
type: ParameterType.Array,
149+
defaultValue: [],
150+
});
145151
options.addDeclaration({
146152
name: "excludePrivate",
147153
help: "Ignore private variables and methods.",

src/test/behavior.c2.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ describe("Behavior Tests", () => {
347347
logger.expectNoOtherMessages();
348348
});
349349

350+
it("Handles excludeCategories", () => {
351+
app.options.setValue("excludeCategories", ["A", "Default"]);
352+
app.options.setValue("defaultCategory", "Default");
353+
const project = convert("excludeCategories");
354+
equal(project.children?.map((c) => c.name), ["c"]);
355+
});
356+
350357
it("Handles excludeNotDocumentedKinds", () => {
351358
app.options.setValue("excludeNotDocumented", true);
352359
app.options.setValue("excludeNotDocumentedKinds", ["Property"]);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @category A
3+
*/
4+
export const a = 1;
5+
/**
6+
* @category A
7+
* @category B
8+
*/
9+
export const b = 1;
10+
/**
11+
* @category C
12+
*/
13+
export const c = 1;
14+
export const d = 1;

0 commit comments

Comments
 (0)