Skip to content

Commit

Permalink
Merge branch 'main' into ajiang/navigation-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed May 29, 2024
2 parents 8bd1aec + a2968d6 commit 358d8ee
Show file tree
Hide file tree
Showing 62 changed files with 702 additions and 269 deletions.
11 changes: 10 additions & 1 deletion fern/apis/fdr/definition/docs/v1/db/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,22 @@ types:

UnversionedTabbedNavigationConfig:
properties:
tabs: list<NavigationTab>
tabs: optional<list<NavigationTab>>
tabsV2: optional<list<NavigationTabV2>>

NavigationTab:
availability: deprecated
discriminated: false
union:
- NavigationTabGroup
- docsReadV1.NavigationTabLink

NavigationTabV2:
union:
group: NavigationTabGroup
link: docsReadV1.NavigationTabLink
changelog: docsReadV1.ChangelogSection

NavigationTabGroup:
properties:
title: string
Expand Down Expand Up @@ -156,6 +164,7 @@ types:
api: ApiSection
section: DocsSection
link: docsReadV1.LinkMetadata
changelog: docsReadV1.ChangelogSection

ApiSection:
properties:
Expand Down
29 changes: 21 additions & 8 deletions fern/apis/fdr/definition/docs/v1/read/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,26 @@ types:
tabs: list<NavigationTab>

NavigationTab:
discriminated: false
union:
group: NavigationTabGroup
link: NavigationTabLink
- NavigationTabGroupV1
- NavigationTabLinkV1
- ChangelogSectionV1

NavigationTabGroupV1:
extends: NavigationTabGroup
properties:
type: optional<literal<"group">>

NavigationTabLinkV1:
extends: NavigationTabLink
properties:
type: literal<"link">

ChangelogSectionV1:
extends: ChangelogSection
properties:
type: literal<"changelog">

NavigationTabGroup:
extends: NavigationNodeMetadata
Expand Down Expand Up @@ -289,6 +306,7 @@ types:
api: ApiSection
section: DocsSection
link: LinkMetadata
changelog: ChangelogSection

PageMetadata:
extends: NavigationNodeMetadata
Expand Down Expand Up @@ -398,17 +416,12 @@ types:
items: list<ApiNavigationConfigItemV1>

ChangelogSection:
extends: NavigationNodeMetadata
properties:
title: optional<string>
icon:
type: optional<string>
docs: Defaults to ActivityLog icon
hidden: optional<boolean>
description: optional<string>
pageId: optional<commons.PageId>
items: list<ChangelogItem>
urlSlug: string
fullSlug: optional<list<string>>

ChangelogItem:
properties:
Expand Down
23 changes: 22 additions & 1 deletion fern/apis/fdr/definition/docs/v1/write/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ types:
light: ThemeConfig

NavigationConfig:
availability: deprecated
discriminated: false
union:
- UnversionedNavigationConfig
Expand All @@ -156,14 +157,24 @@ types:

UnversionedTabbedNavigationConfig:
properties:
tabs: list<NavigationTab>
tabs:
type: optional<list<NavigationTab>>
availability: deprecated
tabsV2: optional<list<NavigationTabV2>>

NavigationTab:
availability: deprecated
discriminated: false
union:
- NavigationTabGroup
- NavigationTabLink

NavigationTabV2:
union:
group: NavigationTabGroup
link: NavigationTabLink
changelog: ChangelogSectionV2

NavigationTabGroup:
extends: NavigationNodeMetadata
properties:
Expand Down Expand Up @@ -242,6 +253,7 @@ types:
api: ApiSection
section: DocsSection
link: LinkMetadata
changelog: ChangelogSectionV2

PageMetadata:
extends: NavigationNodeMetadata
Expand Down Expand Up @@ -358,6 +370,7 @@ types:
items: list<ApiNavigationConfigItemV1>

ChangelogSection:
availability: deprecated
properties:
title:
type: optional<string>
Expand All @@ -376,6 +389,14 @@ types:
urlSlug: string
fullSlug: optional<list<string>>

ChangelogSectionV2:
extends: NavigationNodeMetadata
properties:
title: optional<string>
description: optional<string>
pageId: optional<string>
items: list<ChangelogItem>

ChangelogItem:
properties:
date: date
Expand Down
2 changes: 1 addition & 1 deletion packages/commons/fdr-utils/src/getAllUrlsFromDocsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function getAllUrlsFromDocsConfig(
const visitedSlugs: string[] = [];

visitSidebarNodeRaw(root, (node) => {
visitedSlugs.push(node.slug.join("/"));
visitedSlugs.push(urljoin(...node.slug));
});

return Array.from(new Set(visitedSlugs.map((slug) => urljoin(host, slug))));
Expand Down
52 changes: 39 additions & 13 deletions packages/commons/fdr-utils/src/getNavigationRoot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIV1Read, DocsV1Read, FdrAPI } from "@fern-api/fdr-sdk";
import { assertNever, visitDiscriminatedUnion } from "@fern-ui/core-utils";
import { resolveSidebarNodesRoot } from "./resolver";
import { SidebarNavigationRaw, SidebarNodeRaw, SidebarTab } from "./types";
import { visitSidebarNodeRaw } from "./visitSidebarNodeRaw";
Expand Down Expand Up @@ -31,6 +32,7 @@ const PRIORITY_LIST: Record<SidebarNodeRaw.VisitableNode["type"], number> = {
tabGroup: 3,
versionGroup: 2,
pageGroup: 1,
tabChangelog: 0,
page: 0,
};

Expand Down Expand Up @@ -88,18 +90,31 @@ export function getNavigationRoot(

const { items: tabItems, node: currentTab } = findSiblings<SidebarNodeRaw.Tab>(
parents,
(n): n is SidebarNodeRaw.TabGroup => n.type === "tabGroup" || n.type === "tabLink",
(n): n is SidebarNodeRaw.TabGroup => n.type === "tabGroup",
);

const tabs = tabItems
.filter((n) => n.type === "tabLink" || n.type === "tabGroup")
.map((tab): SidebarTab => {
if (tab.type === "tabLink") {
return tab;
}
const { items, ...tabGroup } = tab;
return tabGroup;
});
.filter((n) => n.type === "tabGroup" || n.type === "tabChangelog" || n.type === "tabLink")
.map((tab) =>
visitDiscriminatedUnion(tab, "type")._visit<SidebarTab>({
tabLink: (link) => link,
tabGroup: (group) => ({
type: "tabGroup",
title: group.title,
icon: group.icon,
index: group.index,
slug: group.slug,
}),
tabChangelog: (changelog) => ({
type: "tabChangelog",
title: changelog.title,
icon: changelog.icon,
index: changelog.index,
slug: changelog.slug,
}),
_other: (other) => assertNever(other as never),
}),
);

const { items: versionItems, node: currentVerion } = findSiblings<SidebarNodeRaw.VersionGroup>(
parents,
Expand Down Expand Up @@ -140,8 +155,13 @@ function resolveRedirect(node: SidebarNodeRaw.VisitableNode | undefined, from: s

if (isNavigationNode(node) || node.type === "section") {
const firstChild = node.items.filter(
(item): item is SidebarNodeRaw.TabGroup | SidebarNodeRaw.VersionGroup | SidebarNodeRaw =>
item.type !== "tabLink",
(
item,
): item is
| SidebarNodeRaw.TabGroup
| SidebarNodeRaw.TabChangelogPage
| SidebarNodeRaw.VersionGroup
| SidebarNodeRaw => item.type !== "tabLink",
)[0];
return resolveRedirect(firstChild, from);
}
Expand All @@ -162,9 +182,15 @@ function resolveRedirect(node: SidebarNodeRaw.VisitableNode | undefined, from: s
return { type: "redirect", redirect: "/" + node.slug.join("/") };
}

function findSiblings<T extends SidebarNodeRaw.ParentNode | SidebarNodeRaw.TabLink>(
function findSiblings<T extends SidebarNodeRaw.ParentNode | SidebarNodeRaw.TabLink | SidebarNodeRaw.TabChangelogPage>(
parents: readonly SidebarNodeRaw.ParentNode[],
match: (node: SidebarNodeRaw.VisitableNode | SidebarNodeRaw.TabLink | SidebarNodeRaw.Link) => boolean,
match: (
node:
| SidebarNodeRaw.VisitableNode
| SidebarNodeRaw.TabLink
| SidebarNodeRaw.Link
| SidebarNodeRaw.TabChangelogPage,
) => boolean,
): { items: readonly T[]; node: T | undefined } {
const navigationDepth = parents.findIndex(match);

Expand Down
66 changes: 53 additions & 13 deletions packages/commons/fdr-utils/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APIV1Read, DocsV1Read, FdrAPI } from "@fern-api/fdr-sdk";
import { isNonNullish, visitDiscriminatedUnion } from "@fern-ui/core-utils";
import { assertNever, isNonNullish, visitDiscriminatedUnion } from "@fern-ui/core-utils";
import { isUnversionedTabbedNavigationConfig, isVersionedNavigationConfig } from "./docs";
import {
FlattenedApiDefinitionPackage,
Expand Down Expand Up @@ -192,33 +192,55 @@ function resolveSidebarNodesVersionItems(
nav: DocsV1Read.UnversionedNavigationConfig,
apis: Record<FdrAPI.ApiId, APIV1Read.ApiDefinition>,
pages: Record<string, DocsV1Read.PageContent>,
parentSlugs: readonly string[],
fixedSlugs: readonly string[],
domain: string,
): SidebarNodeRaw.VersionGroup["items"] {
if (isUnversionedTabbedNavigationConfig(nav)) {
return nav.tabs.map((tab, index): SidebarNodeRaw.Tab => {
if (tab.type === "link") {
if (tab.type === "group" || (tab.type == null && tab.items != null)) {
const parentSlugs = [...fixedSlugs, ...tab.urlSlug.split("/")];
return {
type: "tabGroup",
title: tab.title,
icon: tab.icon,
slug: parentSlugs,
index,
items: resolveSidebarNodes(tab.items, apis, pages, parentSlugs, fixedSlugs, domain),
};
} else if (tab.type === "link") {
return {
type: "tabLink",
title: tab.title,
url: tab.url,
icon: tab.icon,
index,
};
} else if (tab.type === "changelog") {
return {
type: "tabChangelog",
id: tab.urlSlug,
title: tab.title ?? "Changelog",
description: tab.description,
pageId: tab.pageId,
slug:
tab.fullSlug != null
? [...fixedSlugs, ...tab.fullSlug]
: [...fixedSlugs, ...tab.urlSlug.split("/")],
items: tab.items.map((item) => ({
date: item.date,
pageId: item.pageId,
})),
icon: tab.icon,
hidden: tab.hidden ?? false,
index,
};
} else {
assertNever(tab as never);
}
const tabSlug = [...parentSlugs, ...tab.urlSlug.split("/")];
return {
type: "tabGroup",
title: tab.title,
icon: tab.icon,
slug: tabSlug,
index,
items: resolveSidebarNodes(tab.items, apis, pages, tabSlug, parentSlugs, domain),
};
});
}

return resolveSidebarNodes(nav.items, apis, pages, parentSlugs, parentSlugs, domain);
return resolveSidebarNodes(nav.items, apis, pages, fixedSlugs, fixedSlugs, domain);
}

export function resolveSidebarNodes(
Expand Down Expand Up @@ -363,6 +385,24 @@ export function resolveSidebarNodes(
link: (link) => {
pushPageGroup({ ...link, icon: link.icon });
},
changelog: (changelog) => ({
type: "page",
pageType: "changelog",
id: changelog.urlSlug,
title: changelog.title ?? "Changelog",
description: changelog.description,
pageId: changelog.pageId,
slug:
changelog.fullSlug != null
? [...fixedSlugs, ...changelog.fullSlug]
: [...parentSlugs, ...changelog.urlSlug.split("/")],
items: changelog.items.map((item) => ({
date: item.date,
pageId: item.pageId,
})),
icon: changelog.icon,
hidden: changelog.hidden ?? false,
}),
_other: () => Promise.resolve(),
});
}
Expand Down
Loading

0 comments on commit 358d8ee

Please sign in to comment.