Skip to content

Commit

Permalink
Fix #2272 category sort order
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed May 5, 2023
1 parent 5d38df1 commit eb18150
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

### Bug Fixes

- Category children are now sorted according to the `sort` option, #2272.

## v0.24.6 (2023-04-24)

### Features
Expand Down
48 changes: 22 additions & 26 deletions src/lib/converter/plugins/CategoryPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ReflectionCategory } from "../../models";
import { Component, ConverterComponent } from "../components";
import { Converter } from "../converter";
import type { Context } from "../context";
import { BindOption, removeIf } from "../../utils";
import { BindOption, getSortFunction, removeIf } from "../../utils";

/**
* A handler that sorts and categorizes the found reflections in the resolving phase.
Expand All @@ -17,6 +17,8 @@ import { BindOption, removeIf } from "../../utils";
*/
@Component({ name: "category" })
export class CategoryPlugin extends ConverterComponent {
sortFunction!: (reflections: DeclarationReflection[]) => void;

@BindOption("defaultCategory")
defaultCategory!: string;

Expand Down Expand Up @@ -55,6 +57,8 @@ export class CategoryPlugin extends ConverterComponent {
* Triggered when the converter begins converting a project.
*/
private onBegin(_context: Context) {
this.sortFunction = getSortFunction(this.application.options);

// Set up static properties
if (this.defaultCategory) {
CategoryPlugin.defaultCategory = this.defaultCategory;
Expand Down Expand Up @@ -156,40 +160,32 @@ export class CategoryPlugin extends ConverterComponent {
getReflectionCategories(
reflections: DeclarationReflection[]
): ReflectionCategory[] {
const categories: ReflectionCategory[] = [];
let defaultCat: ReflectionCategory | undefined;
reflections.forEach((child) => {
const categories = new Map<string, ReflectionCategory>();

for (const child of reflections) {
const childCategories = this.getCategories(child);
if (childCategories.size === 0) {
if (!defaultCat) {
defaultCat = categories.find(
(category) =>
category.title === CategoryPlugin.defaultCategory
);
if (!defaultCat) {
defaultCat = new ReflectionCategory(
CategoryPlugin.defaultCategory
);
categories.push(defaultCat);
}
}

defaultCat.children.push(child);
return;
childCategories.add(CategoryPlugin.defaultCategory);
}

for (const childCat of childCategories) {
let category = categories.find((cat) => cat.title === childCat);
const category = categories.get(childCat);

if (category) {
category.children.push(child);
continue;
} else {
const cat = new ReflectionCategory(childCat);
cat.children.push(child);
categories.set(childCat, cat);
}
category = new ReflectionCategory(childCat);
category.children.push(child);
categories.push(category);
}
});
return categories;
}

for (const cat of categories.values()) {
this.sortFunction(cat.children);
}

return Array.from(categories.values());
}

/**
Expand Down

0 comments on commit eb18150

Please sign in to comment.