Skip to content

Commit

Permalink
fix: filter out childless sections (#1602)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Oct 7, 2024
1 parent 674332e commit b01c770
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { withBasicTokenPublic } from "../withBasicTokenPublic";
import { NodeId, PageId, Slug, Url } from "@fern-api/fdr-sdk/navigation";
import { withBasicTokenPublic, withBasicTokenPublicCheck } from "../withBasicTokenPublic";

describe("withBasicTokenPublic", () => {
it("should deny the request if the allowlist is empty", () => {
Expand Down Expand Up @@ -26,4 +27,54 @@ describe("withBasicTokenPublic", () => {
it("shouuld respect denylist before allowlist", () => {
expect(withBasicTokenPublic({ allowlist: ["/public"], denylist: ["/public"] }, "/public")).toBe(false);
});

it("should never deny external links", () => {
expect(
withBasicTokenPublicCheck({ denylist: ["/(.*)"] })({
type: "link",
url: Url("https://example.com"),
title: "External url",
icon: undefined,
id: NodeId("1"),
}),
).toBe(true);
});

it("should prune childless non-leaf nodes", () => {
expect(
withBasicTokenPublicCheck({ allowlist: ["/public"] })({
type: "section",
title: "Public",
children: [],
id: NodeId("1"),
slug: Slug("public"),
collapsed: false,
canonicalSlug: undefined,
icon: undefined,
hidden: undefined,
overviewPageId: undefined,
noindex: undefined,
pointsTo: undefined,
}),
).toBe(false);
});

it("should not prune childless non-leaf nodes that have content", () => {
expect(
withBasicTokenPublicCheck({ allowlist: ["/public"] })({
type: "section",
title: "Public",
children: [],
id: NodeId("1"),
slug: Slug("public"),
collapsed: false,
canonicalSlug: undefined,
icon: undefined,
hidden: undefined,
overviewPageId: PageId("1.mdx"),
noindex: undefined,
pointsTo: undefined,
}),
).toBe(true);
});
});
21 changes: 16 additions & 5 deletions packages/ui/docs-bundle/src/server/withBasicTokenPublic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RootNode, isPage, utils } from "@fern-api/fdr-sdk/navigation";
import { getChildren, isLeaf, isPage, utils, type NavigationNode, type RootNode } from "@fern-api/fdr-sdk/navigation";
import { matchPath } from "@fern-ui/fern-docs-utils";
import { AuthEdgeConfigBasicTokenVerification } from "@fern-ui/ui/auth";
import type { AuthEdgeConfigBasicTokenVerification } from "@fern-ui/ui/auth";
import { captureMessage } from "@sentry/nextjs";

/**
Expand All @@ -26,14 +26,25 @@ export function withBasicTokenPublic(
return false;
}

export function pruneWithBasicTokenPublic(auth: AuthEdgeConfigBasicTokenVerification, node: RootNode): RootNode {
const result = utils.pruneNavigationTree(node, (node) => {
/**
* @internal visibleForTesting
*/
export function withBasicTokenPublicCheck(
auth: Pick<AuthEdgeConfigBasicTokenVerification, "allowlist" | "denylist">,
): (node: NavigationNode) => boolean {
return (node: NavigationNode) => {
if (isPage(node)) {
return withBasicTokenPublic(auth, `/${node.slug}`);
} else if (!isLeaf(node) && getChildren(node).length === 0) {
return false;
}

return true;
});
};
}

export function pruneWithBasicTokenPublic(auth: AuthEdgeConfigBasicTokenVerification, node: RootNode): RootNode {
const result = utils.pruneNavigationTree(node, withBasicTokenPublicCheck(auth));

// TODO: handle this more gracefully
if (result == null) {
Expand Down

0 comments on commit b01c770

Please sign in to comment.