-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tree-aware version switcher (#1593)
- Loading branch information
1 parent
80af5e5
commit 6801d8a
Showing
10 changed files
with
234 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/fdr-sdk/src/navigation/utils/__test__/toUnversionedSlug.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Slug } from "../.."; | ||
import { toUnversionedSlug } from "../toUnversionedSlug"; | ||
|
||
describe("toUnversionedSlug", () => { | ||
it("should trim version slug from the beginning of the slug", () => { | ||
expect(toUnversionedSlug(Slug("docs/v1.0.0/foo/bar"), Slug("docs/v1.0.0"))).toBe("foo/bar"); | ||
}); | ||
|
||
it("should not trim version slug if it does not match", () => { | ||
expect(toUnversionedSlug(Slug("docs/v1.0.0/foo/bar"), Slug("docs/v2.0.0"))).toBe("docs/v1.0.0/foo/bar"); | ||
}); | ||
|
||
it("should not trim version slug if it does not match exactly", () => { | ||
expect(toUnversionedSlug(Slug("docs/v1.0.0/foo/bar"), Slug("docs/v1.0"))).toBe("docs/v1.0.0/foo/bar"); | ||
}); | ||
|
||
it("should return empty string if the slug is the same as the version slug", () => { | ||
expect(toUnversionedSlug(Slug("docs/v1.0.0"), Slug("docs/v1.0.0"))).toBe(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/fdr-sdk/src/navigation/utils/toUnversionedSlug.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Slug } from ".."; | ||
|
||
/** | ||
* This is the part of the slug after the version (or basepath) prefix. | ||
* | ||
* For example, if the original slug is "docs/v1.0.0/foo/bar", the unversionedSlug is "foo/bar". | ||
*/ | ||
export function toUnversionedSlug(slug: Slug, versionSlug: Slug): Slug { | ||
return Slug(slug.replace(new RegExp(`^${versionSlug}(/|$)`), "")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/ui/docs-bundle/src/server/__test__/withVersionSwitcherInfo.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { FernNavigation } from "@fern-api/fdr-sdk"; | ||
import { getNodesUnderCurrentVersionAscending } from "../withVersionSwitcherInfo"; | ||
|
||
describe("getNodesUnderCurrentVersionAscending", () => { | ||
it("should return nodes under the current version in ascending order", () => { | ||
const { version, nodes } = getNodesUnderCurrentVersionAscending< | ||
{ type: FernNavigation.NavigationNode["type"]; id: string }, | ||
{ type: FernNavigation.VersionNode["type"]; id: string } | ||
>({ type: "page", id: "a" }, [ | ||
{ type: "version", id: "d" }, | ||
{ type: "tab", id: "c" }, | ||
{ type: "section", id: "b" }, | ||
]); | ||
|
||
expect(version).toEqual({ type: "version", id: "d" }); | ||
expect(nodes.map((node) => node.id)).toEqual(["a", "b", "c"]); | ||
}); | ||
|
||
it("should return nothing if a version node is not found", () => { | ||
const { version, nodes } = getNodesUnderCurrentVersionAscending< | ||
{ type: FernNavigation.NavigationNode["type"]; id: string }, | ||
{ type: FernNavigation.VersionNode["type"]; id: string } | ||
>({ type: "page", id: "a" }, [ | ||
{ type: "tab", id: "c" }, | ||
{ type: "section", id: "b" }, | ||
]); | ||
|
||
expect(version).toBeUndefined(); | ||
expect(nodes.map((node) => node.id)).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
packages/ui/docs-bundle/src/server/withVersionSwitcherInfo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import * as FernNavigation from "@fern-api/fdr-sdk/navigation"; | ||
import { isNonNullish } from "@fern-ui/core-utils"; | ||
import { VersionSwitcherInfo } from "@fern-ui/fdr-utils"; | ||
|
||
interface WithVersionSwitcherInfoArgs { | ||
/** | ||
* The current node to mutate the version switcher info for. | ||
*/ | ||
node: FernNavigation.NavigationNodeWithMetadata; | ||
|
||
/** | ||
* The parents of the current node, in descending order. | ||
*/ | ||
parents: readonly FernNavigation.NavigationNode[]; | ||
|
||
/** | ||
* All available versions to be rendered in the version switcher. | ||
*/ | ||
versions: readonly FernNavigation.VersionNode[]; | ||
|
||
/** | ||
* A map of slugs to nodes for ALL nodes in the tree. | ||
* This is used to check if a node exists in a different version.s | ||
*/ | ||
slugMap: Map<string, FernNavigation.NavigationNodeWithMetadata>; | ||
} | ||
|
||
/** | ||
* In order to preserve the "context" of the current node when switching between versions, we need to check if | ||
* the current node exists in the other versions. If it doesn't, we can traverse up the tree to find the closest | ||
* page that exists on that version. | ||
* | ||
* For example, if the current node is `/docs/v1/foo/bar`, and the user switches to `/docs/v2`, we need to find | ||
* if `/docs/v2/foo/bar` exists. If it doesn't, we can try `/docs/v2/foo` and so on. | ||
* | ||
* ASSUMPTIONS: | ||
* This function relies on the slug and parent slugs to match between the versions. | ||
* If the slug is overridden in frontmatter, there is not currently a way to determine the correct slug to switch to. | ||
*/ | ||
export function withVersionSwitcherInfo({ | ||
node, | ||
parents, | ||
versions, | ||
slugMap, | ||
}: WithVersionSwitcherInfoArgs): VersionSwitcherInfo[] { | ||
const { version: currentVersion, nodes } = getNodesUnderCurrentVersionAscending(node, parents); | ||
|
||
const unversionedSlugs = | ||
currentVersion == null | ||
? [] | ||
: nodes | ||
.filter(FernNavigation.hasMetadata) | ||
.map((node) => node.slug) | ||
.map((slug) => FernNavigation.utils.toUnversionedSlug(slug, currentVersion.slug)); | ||
|
||
return versions | ||
.filter((version) => !version.hidden) | ||
.map((version, index) => { | ||
if (version.id === currentVersion?.id) { | ||
return { | ||
title: version.title, | ||
id: version.versionId, | ||
slug: version.slug, | ||
|
||
// the current version should always point to the current node | ||
pointsTo: node.slug, | ||
index, | ||
availability: version.availability, | ||
}; | ||
} | ||
|
||
const expectedSlugs = unversionedSlugs.map((slug) => FernNavigation.slugjoin(version.slug, slug)); | ||
|
||
const expectedSlug = expectedSlugs | ||
.map((slug) => { | ||
const node = slugMap.get(slug); | ||
|
||
// if the node doesn't exist in this version, return undefined | ||
if (node == null) { | ||
return undefined; | ||
} | ||
|
||
// if the node is a visitable page, return the slug | ||
else if (FernNavigation.isPage(node)) { | ||
return node.slug; | ||
} | ||
|
||
// if the node is a redirect, return the slug it points to (which can be undefined) | ||
else if (FernNavigation.hasRedirect(node)) { | ||
return node.pointsTo; | ||
} | ||
|
||
return undefined; | ||
}) | ||
// select the first non-nullish slug | ||
.filter(isNonNullish)[0]; | ||
|
||
// if the same page exists in this version, return the full slug of that page, otherwise default to version's landing page (pointsTo) | ||
const pointsTo = expectedSlug ?? version.pointsTo; | ||
|
||
return { | ||
title: version.title, | ||
id: version.versionId, | ||
slug: version.slug, | ||
pointsTo, | ||
index, | ||
availability: version.availability, | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* This function returns the current node + all parents under the current version, in ascending order | ||
* | ||
* ascending order = from the current node to its ancestors | ||
* | ||
* @internal visibleForTesting | ||
*/ | ||
export function getNodesUnderCurrentVersionAscending< | ||
NODE extends { type: FernNavigation.NavigationNode["type"] } = FernNavigation.NavigationNode, | ||
VERSION_NODE extends { type: FernNavigation.VersionNode["type"] } = FernNavigation.VersionNode, | ||
>( | ||
node: NODE, | ||
|
||
/** | ||
* The parents of the current node should be in descending order. | ||
*/ | ||
parents: readonly NODE[], | ||
): { | ||
version: VERSION_NODE | undefined; | ||
nodes: NODE[]; | ||
} { | ||
const currentVersionIndex = parents.findIndex((node) => node.type === "version"); | ||
|
||
// if the current node is not under a version, return an empty array | ||
if (currentVersionIndex < 0) { | ||
return { version: undefined, nodes: [] }; | ||
} | ||
|
||
const version = parents[currentVersionIndex]; | ||
if (version == null || version.type !== "version") { | ||
return { version: undefined, nodes: [] }; | ||
} | ||
|
||
parents = parents.slice(currentVersionIndex + 1); | ||
|
||
return { | ||
// this is safe because of the checks above | ||
version: version as unknown as VERSION_NODE, | ||
|
||
// reverse the array to get the nodes in ascending order | ||
nodes: [...parents, node].reverse(), | ||
}; | ||
} |