Skip to content

Commit

Permalink
feat(category): add option to read more categories from doc
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric authored and Gerrit0 committed Jan 16, 2021
1 parent 1d03d4b commit 18d83f6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
67 changes: 36 additions & 31 deletions src/lib/converter/plugins/CategoryPlugin.ts
Expand Up @@ -2,6 +2,7 @@ import {
Reflection,
ContainerReflection,
DeclarationReflection,
CommentTag,
} from "../../models";
import { ReflectionCategory } from "../../models/ReflectionCategory";
import { Component, ConverterComponent } from "../components";
Expand Down Expand Up @@ -139,8 +140,8 @@ export class CategoryPlugin extends ConverterComponent {
const categories: ReflectionCategory[] = [];
let defaultCat: ReflectionCategory | undefined;
reflections.forEach((child) => {
const childCat = CategoryPlugin.getCategory(child);
if (childCat === "") {
const childCategories = CategoryPlugin.getCategories(child);
if (childCategories.length === 0) {
if (!defaultCat) {
defaultCat = categories.find(
(category) =>
Expand All @@ -156,14 +157,16 @@ export class CategoryPlugin extends ConverterComponent {
defaultCat.children.push(child);
return;
}
let category = categories.find((cat) => cat.title === childCat);
if (category) {
for (const childCat of childCategories) {
let category = categories.find((cat) => cat.title === childCat);
if (category) {
category.children.push(child);
return;
}
category = new ReflectionCategory(childCat);
category.children.push(child);
return;
categories.push(category);
}
category = new ReflectionCategory(childCat);
category.children.push(child);
categories.push(category);
});
return categories;
}
Expand All @@ -174,38 +177,40 @@ export class CategoryPlugin extends ConverterComponent {
* @param reflection The reflection.
* @returns The category the reflection belongs to
*/
static getCategory(reflection: Reflection): string {
function extractCategoryTag(comment: Comment) {
static getCategories(reflection: Reflection): string[] {
function extractCategoryTag(comment: Comment): string[] {
const categories: string[] = [];
const tags = comment.tags;
const tagIndex = tags.findIndex(
(tag) => tag.tagName === "category"
);
if (tagIndex >= 0) {
const tag = tags[tagIndex].text;
tags.splice(tagIndex, 1);
return tag.trim();
}
return "";
const commentTags: CommentTag[] = [];
tags.forEach((tag) => {
if (tag.tagName !== "category") {
commentTags.push(tag);
return;
}
categories.push(tag.text.trim());
});
comment.tags = commentTags;
return categories;
}

let category = "";
if (reflection.comment) {
category = extractCategoryTag(reflection.comment);
return extractCategoryTag(reflection.comment);
} else if (
reflection instanceof DeclarationReflection &&
reflection.signatures
) {
// If a reflection has signatures, use the first category tag amongst them
for (const sig of reflection.signatures) {
if (sig.comment) {
category = extractCategoryTag(sig.comment);
}
if (category != "") {
break;
}
}
return reflection.signatures.reduce(
(categories: string[], signature) => {
if (!signature.comment) {
return categories;
}
categories.push(...extractCategoryTag(signature.comment));
return categories;
},
[]
);
}
return category;
return [];
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/test/converter/class/class.ts
Expand Up @@ -35,11 +35,14 @@ export class TestClass {
/**
* protectedMethod short text.
* @category Test
* @category AnotherTest
*/
protected protectedMethod() {}

/**
* privateMethod short text.
*
* @category AnotherTest
*/
private privateMethod() {}

Expand Down
10 changes: 8 additions & 2 deletions src/test/converter/class/specs-with-lump-categories.json
Expand Up @@ -905,6 +905,13 @@
}
],
"categories": [
{
"title": "AnotherTest",
"children": [
32,
30
]
},
{
"title": "Other",
"children": [
Expand All @@ -913,7 +920,6 @@
26,
21,
34,
32,
22
]
},
Expand Down Expand Up @@ -3546,4 +3552,4 @@
]
}
]
}
}
10 changes: 8 additions & 2 deletions src/test/converter/class/specs.json
Expand Up @@ -903,11 +903,17 @@
22
],
"categories": [
{
"title": "AnotherTest",
"children": [
32,
30
]
},
{
"title": "Other",
"children": [
34,
32,
22
]
},
Expand Down Expand Up @@ -3542,4 +3548,4 @@
]
}
]
}
}

0 comments on commit 18d83f6

Please sign in to comment.