Skip to content

Commit eb18150

Browse files
committed
Fix #2272 category sort order
1 parent 5d38df1 commit eb18150

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
### Bug Fixes
4+
5+
- Category children are now sorted according to the `sort` option, #2272.
6+
37
## v0.24.6 (2023-04-24)
48

59
### Features

src/lib/converter/plugins/CategoryPlugin.ts

Lines changed: 22 additions & 26 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 { BindOption, removeIf } from "../../utils";
11+
import { BindOption, getSortFunction, removeIf } from "../../utils";
1212

1313
/**
1414
* A handler that sorts and categorizes the found reflections in the resolving phase.
@@ -17,6 +17,8 @@ import { BindOption, removeIf } from "../../utils";
1717
*/
1818
@Component({ name: "category" })
1919
export class CategoryPlugin extends ConverterComponent {
20+
sortFunction!: (reflections: DeclarationReflection[]) => void;
21+
2022
@BindOption("defaultCategory")
2123
defaultCategory!: string;
2224

@@ -55,6 +57,8 @@ export class CategoryPlugin extends ConverterComponent {
5557
* Triggered when the converter begins converting a project.
5658
*/
5759
private onBegin(_context: Context) {
60+
this.sortFunction = getSortFunction(this.application.options);
61+
5862
// Set up static properties
5963
if (this.defaultCategory) {
6064
CategoryPlugin.defaultCategory = this.defaultCategory;
@@ -156,40 +160,32 @@ export class CategoryPlugin extends ConverterComponent {
156160
getReflectionCategories(
157161
reflections: DeclarationReflection[]
158162
): ReflectionCategory[] {
159-
const categories: ReflectionCategory[] = [];
160-
let defaultCat: ReflectionCategory | undefined;
161-
reflections.forEach((child) => {
163+
const categories = new Map<string, ReflectionCategory>();
164+
165+
for (const child of reflections) {
162166
const childCategories = this.getCategories(child);
163167
if (childCategories.size === 0) {
164-
if (!defaultCat) {
165-
defaultCat = categories.find(
166-
(category) =>
167-
category.title === CategoryPlugin.defaultCategory
168-
);
169-
if (!defaultCat) {
170-
defaultCat = new ReflectionCategory(
171-
CategoryPlugin.defaultCategory
172-
);
173-
categories.push(defaultCat);
174-
}
175-
}
176-
177-
defaultCat.children.push(child);
178-
return;
168+
childCategories.add(CategoryPlugin.defaultCategory);
179169
}
170+
180171
for (const childCat of childCategories) {
181-
let category = categories.find((cat) => cat.title === childCat);
172+
const category = categories.get(childCat);
182173

183174
if (category) {
184175
category.children.push(child);
185-
continue;
176+
} else {
177+
const cat = new ReflectionCategory(childCat);
178+
cat.children.push(child);
179+
categories.set(childCat, cat);
186180
}
187-
category = new ReflectionCategory(childCat);
188-
category.children.push(child);
189-
categories.push(category);
190181
}
191-
});
192-
return categories;
182+
}
183+
184+
for (const cat of categories.values()) {
185+
this.sortFunction(cat.children);
186+
}
187+
188+
return Array.from(categories.values());
193189
}
194190

195191
/**

0 commit comments

Comments
 (0)