Skip to content

Commit

Permalink
Fix page navigation sidebar with re-exports
Browse files Browse the repository at this point in the history
Closes #2625

Co-authored-by: Garrett Hopper <[email protected]>
  • Loading branch information
Gerrit0 and garrett-hopper committed Jul 10, 2024
1 parent 34024de commit dae2527
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

### Bug Fixes

- The page navigation sidebar no longer incorrectly includes re-exports if the same member is exported with multiple names #2625.
- Page navigation now ensures the current page is visible when the page is first loaded, #2626.
- Comments on re-exports are now rendered.

### Thanks!

- @garrett-hopper

## v0.26.3 (2024-06-28)

Expand Down
45 changes: 28 additions & 17 deletions src/lib/output/themes/default/DefaultTheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { type RenderTemplate, UrlMapping } from "../../models/UrlMapping";
import type { PageEvent } from "../../events";
import type { MarkedPlugin } from "../../plugins";
import { DefaultThemeRenderContext } from "./DefaultThemeRenderContext";
import { JSX } from "../../../utils";
import { filterMap, JSX } from "../../../utils";
import { classNames, getDisplayName, getHierarchyRoots, toStyleClass } from "../lib";
import { icons } from "./partials/icon";

Expand Down Expand Up @@ -319,20 +319,29 @@ export class DefaultTheme extends Theme {

function toNavigation(
element: ReflectionCategory | ReflectionGroup | DeclarationReflection | DocumentReflection,
): NavigationElement {
): NavigationElement | undefined {
const children = getNavigationElements(element);
if (element instanceof ReflectionCategory || element instanceof ReflectionGroup) {
if (!children?.length) {
return;
}

return {
text: element.title,
children: getNavigationElements(element),
children,
};
}

if (!element.hasOwnDocument) {
return;
}

return {
text: getDisplayName(element),
path: element.url,
kind: element.kind,
class: classNames({ deprecated: element.isDeprecated() }, theme.getReflectionClasses(element)),
children: getNavigationElements(element),
children: children?.length ? children : undefined,
};
}

Expand All @@ -345,14 +354,14 @@ export class DefaultTheme extends Theme {
| DocumentReflection,
): undefined | NavigationElement[] {
if (parent instanceof ReflectionCategory) {
return parent.children.map(toNavigation);
return filterMap(parent.children, toNavigation);
}

if (parent instanceof ReflectionGroup) {
if (shouldShowCategories(parent.owningReflection, opts) && parent.categories) {
return parent.categories.map(toNavigation);
return filterMap(parent.categories, toNavigation);
}
return parent.children.map(toNavigation);
return filterMap(parent.children, toNavigation);
}

if (leaves.includes(parent.getFullName())) {
Expand All @@ -364,28 +373,28 @@ export class DefaultTheme extends Theme {
}

if (parent.isDocument()) {
return parent.children?.map(toNavigation);
return filterMap(parent.children, toNavigation);
}

if (!parent.kindOf(ReflectionKind.SomeModule | ReflectionKind.Project)) {
// Tricky: Non-module children don't show up in the navigation pane,
// but any documents added by them should.
return parent.documents?.map(toNavigation);
return filterMap(parent.documents, toNavigation);
}

if (parent.categories && shouldShowCategories(parent, opts)) {
return parent.categories.map(toNavigation);
return filterMap(parent.categories, toNavigation);
}

if (parent.groups && shouldShowGroups(parent, opts)) {
return parent.groups.map(toNavigation);
return filterMap(parent.groups, toNavigation);
}

if (opts.includeFolders && parent.childrenIncludingDocuments?.some((child) => child.name.includes("/"))) {
return deriveModuleFolders(parent.childrenIncludingDocuments);
}

return parent.childrenIncludingDocuments?.map(toNavigation);
return filterMap(parent.childrenIncludingDocuments, toNavigation);
}

function deriveModuleFolders(children: Array<DeclarationReflection | DocumentReflection>) {
Expand Down Expand Up @@ -414,12 +423,14 @@ export class DefaultTheme extends Theme {

// Note: This might end up putting a module within another module if we document
// both foo/index.ts and foo/bar.ts.
for (const child of children) {
const parts = child.name.split("/");
const collection = resolveOrCreateParents(parts);
for (const child of children.filter((c) => c.hasOwnDocument)) {
const nav = toNavigation(child);
nav.text = parts[parts.length - 1];
collection.push(nav);
if (nav) {
const parts = child.name.split("/");
const collection = resolveOrCreateParents(parts);
nav.text = parts[parts.length - 1];
collection.push(nav);
}
}

// Now merge single-possible-paths together so we don't have folders in our navigation
Expand Down
11 changes: 10 additions & 1 deletion src/lib/output/themes/default/partials/member.reference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import type { DefaultThemeRenderContext } from "../DefaultThemeRenderContext";
import { JSX } from "../../../../utils";
import type { ReferenceReflection } from "../../../../models";

export const memberReference = ({ urlTo, i18n }: DefaultThemeRenderContext, props: ReferenceReflection) => {
export const memberReference = (
{ urlTo, i18n, commentSummary, commentTags }: DefaultThemeRenderContext,
props: ReferenceReflection,
) => {
const referenced = props.tryGetTargetReflectionDeep();

if (!referenced) {
return (
<>
{i18n.theme_re_exports()} {props.name}
{commentSummary(props)}
{commentTags(props)}
</>
);
}
Expand All @@ -17,13 +22,17 @@ export const memberReference = ({ urlTo, i18n }: DefaultThemeRenderContext, prop
return (
<>
{i18n.theme_re_exports()} <a href={urlTo(referenced)}>{referenced.name}</a>
{commentSummary(props)}
{commentTags(props)}
</>
);
}

return (
<>
{i18n.theme_renames_and_re_exports()} <a href={urlTo(referenced)}>{referenced.name}</a>
{commentSummary(props)}
{commentTags(props)}
</>
);
};
4 changes: 2 additions & 2 deletions src/lib/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ export function* zip<T extends Iterable<any>[]>(
}

export function filterMap<T, U>(
iter: Iterable<T>,
iter: Iterable<T> | undefined,
fn: (item: T) => U | undefined,
): U[] {
const result: U[] = [];

for (const item of iter) {
for (const item of iter || []) {
const newItem = fn(item);
if (newItem !== void 0) {
result.push(newItem);
Expand Down

0 comments on commit dae2527

Please sign in to comment.