Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: hide filesv2 from static props #2044

Merged
merged 14 commits into from
Jan 23, 2025
1 change: 1 addition & 0 deletions packages/fern-docs/bundle/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const isTrailingSlashEnabled =
process.env.TRAILING_SLASH === "1" ||
process.env.NEXT_PUBLIC_TRAILING_SLASH === "1";

// TODO: move this to a shared location (this is copied in @fern-docs/ui FernImage.tsx)
const DOCS_FILES_ALLOWLIST = [
{
protocol: "https",
Expand Down
51 changes: 48 additions & 3 deletions packages/fern-docs/bundle/src/server/withInitialProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import { addLeadingSlash, getRedirectForPath } from "@fern-docs/utils";
import { SidebarTab } from "@fern-platform/fdr-utils";
import { GetServerSidePropsResult, Redirect } from "next";
import { ComponentProps } from "react";
import { UnreachableCaseError } from "ts-essentials";
import urlJoin from "url-join";
import { LogoImageData } from "../../../ui/src/atoms";
import { DocsLoader } from "./DocsLoader";
import { getAuthState } from "./auth/getAuthState";
import { getReturnToQueryParam } from "./auth/return-to";
Expand Down Expand Up @@ -295,6 +297,29 @@ export async function withInitialProps({
pruneNavigationPredicate(tab, pruneOpts) || tab === found.currentTab
);

function resolveFileSrc(src: string): LogoImageData | undefined {
const fileId = FernNavigation.FileId(
src.startsWith("file:") ? src.slice(5) : src
);
const file = docs.definition.filesV2[fileId];
if (file == null) {
return undefined;
}

if (file.type === "image") {
return {
src: file.url,
width: file.width,
height: file.height,
blurDataURL: file.blurDataUrl,
};
} else if (file.type === "url") {
return { src: file.url };
} else {
throw new UnreachableCaseError(file);
}
}

const content = await withResolvedDocsContent({
domain: docs.baseUrl.domain,
found,
Expand All @@ -311,6 +336,7 @@ export async function withInitialProps({
tab: found?.currentTab?.title,
},
},
replaceSrc: resolveFileSrc,
});

if (content == null) {
Expand Down Expand Up @@ -353,6 +379,19 @@ export async function withInitialProps({
? undefined
: filteredTabs.indexOf(found.currentTab);

const lightLogoFileId =
docs.definition.config.colorsV3?.type === "light"
? docs.definition.config.colorsV3.logo
: docs.definition.config.colorsV3?.type === "darkAndLight"
? docs.definition.config.colorsV3.light.logo
: undefined;
const darkLogoFileId =
docs.definition.config.colorsV3?.type === "dark"
? docs.definition.config.colorsV3.logo
: docs.definition.config.colorsV3?.type === "darkAndLight"
? docs.definition.config.colorsV3.dark.logo
: undefined;

const props: ComponentProps<typeof DocsPage> = {
baseUrl: docs.baseUrl,
layout: docs.definition.config.layout,
Expand All @@ -361,9 +400,15 @@ export async function withInitialProps({
colors,
js: docs.definition.config.js,
navbarLinks,
logoHeight: docs.definition.config.logoHeight,
logoHref: logoHref != null ? FernNavigation.Url(logoHref) : undefined,
files: docs.definition.filesV2,
// logoHeight: docs.definition.config.logoHeight,
// logoHref: logoHref != null ? FernNavigation.Url(logoHref) : undefined,
logo: {
height: docs.definition.config.logoHeight,
href: logoHref != null ? FernNavigation.Url(logoHref) : undefined,
light:
lightLogoFileId != null ? resolveFileSrc(lightLogoFileId) : undefined,
dark: darkLogoFileId != null ? resolveFileSrc(darkLogoFileId) : undefined,
},
content,
announcement:
docs.definition.config.announcement != null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DocsV1Read } from "@fern-api/fdr-sdk";
import type * as FernNavigation from "@fern-api/fdr-sdk/navigation";
import * as FernNavigation from "@fern-api/fdr-sdk/navigation";
import { resolveDocsContent, type DocsContent } from "@fern-docs/ui";
import { serializeMdx } from "@fern-docs/ui/bundlers/mdx-bundler";
import { EdgeFlags } from "@fern-docs/utils";
import { LogoImageData } from "../../../ui/src/atoms/types";
import { AuthState } from "./auth/getAuthState";
import { withPrunedNavigation } from "./withPrunedNavigation";

Expand All @@ -13,6 +14,7 @@ interface WithResolvedDocsContentOpts {
definition: DocsV1Read.DocsDefinition;
edgeFlags: EdgeFlags;
scope?: Record<string, unknown>;
replaceSrc?: (src: string) => LogoImageData | undefined;
}

export async function withResolvedDocsContent({
Expand All @@ -22,6 +24,7 @@ export async function withResolvedDocsContent({
definition,
edgeFlags,
scope,
replaceSrc,
}: WithResolvedDocsContentOpts): Promise<DocsContent | undefined> {
const node = withPrunedNavigation(found.node, {
visibleNodeIds: [found.node.id],
Expand Down Expand Up @@ -67,6 +70,9 @@ export async function withResolvedDocsContent({
mdxOptions: {
files: definition.jsFiles,
scope,

// inject the file url and dimensions for images and other embeddable files
replaceSrc,
},
serializeMdx,
domain,
Expand Down
1 change: 1 addition & 0 deletions packages/fern-docs/local-preview-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"react": "^18",
"react-dom": "^18",
"styled-jsx": "^5.1.2",
"ts-essentials": "^10.0.1",
"url-join": "5.0.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import {
import { SidebarTab } from "@fern-platform/fdr-utils";
import type { GetServerSidePropsResult } from "next";
import { ComponentProps } from "react";
import { UnreachableCaseError } from "ts-essentials";
import urljoin from "url-join";
import { LogoImageData } from "../../../ui/src/atoms/types";

export async function getDocsPageProps(
docs: DocsV2Read.LoadDocsForUrlResponse,
Expand Down Expand Up @@ -84,6 +86,29 @@ export async function getDocsPageProps(
// TODO: get feature flags from the API
const edgeFlags: EdgeFlags = DEFAULT_EDGE_FLAGS;

function resolveFileSrc(src: string): LogoImageData | undefined {
const fileId = FernNavigation.FileId(
src.startsWith("file:") ? src.slice(5) : src
);
const file = docs.definition.filesV2[fileId];
if (file == null) {
return undefined;
}

if (file.type === "image") {
return {
src: file.url,
width: file.width,
height: file.height,
blurDataURL: file.blurDataUrl,
};
} else if (file.type === "url") {
return { src: file.url };
} else {
throw new UnreachableCaseError(file);
}
}

const content = await resolveDocsContent({
domain: docs.baseUrl.domain,
node: node.node,
Expand All @@ -109,6 +134,18 @@ export async function getDocsPageProps(
edgeFlags,
mdxOptions: {
files: docs.definition.jsFiles,
scope: {
env: "development",
props: {
authed: false,
user: undefined,
version: node.currentVersion?.versionId,
tab: node.currentTab?.title,
},
},

// inject the file url and dimensions for images and other embeddable files
replaceSrc: resolveFileSrc,
},
serializeMdx,
engine: "next-mdx-remote",
Expand Down Expand Up @@ -182,6 +219,19 @@ export async function getDocsPageProps(
}
});

const lightLogoFileId =
docs.definition.config.colorsV3?.type === "light"
? docs.definition.config.colorsV3.logo
: docs.definition.config.colorsV3?.type === "darkAndLight"
? docs.definition.config.colorsV3.light.logo
: undefined;
const darkLogoFileId =
docs.definition.config.colorsV3?.type === "dark"
? docs.definition.config.colorsV3.logo
: docs.definition.config.colorsV3?.type === "darkAndLight"
? docs.definition.config.colorsV3.dark.logo
: undefined;

const props: ComponentProps<typeof DocsPage> = {
baseUrl: docs.baseUrl,
layout: docs.definition.config.layout,
Expand All @@ -190,9 +240,13 @@ export async function getDocsPageProps(
colors,
js: docs.definition.config.js,
navbarLinks,
logoHeight: docs.definition.config.logoHeight,
logoHref: docs.definition.config.logoHref,
files: docs.definition.filesV2,
logo: {
height: docs.definition.config.logoHeight,
href: docs.definition.config.logoHref,
light:
lightLogoFileId != null ? resolveFileSrc(lightLogoFileId) : undefined,
dark: darkLogoFileId != null ? resolveFileSrc(darkLogoFileId) : undefined,
},
content,
announcement:
docs.definition.config.announcement != null
Expand Down
1 change: 1 addition & 0 deletions packages/fern-docs/mdx/src/mdx-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from "./is-mdx-element";
export * from "./is-mdx-expression";
export * from "./is-mdx-jsx-attr";
export * from "./markdown-to-string";
export * from "./mdx-jsx-attribute-to-string";
export * from "./unknown-to-estree-expression";
export * from "./unknown-to-mdx-jsx-attr";
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { MdxJsxAttribute } from "mdast-util-mdx-jsx";

export function mdxJsxAttributeToString(
attribute: MdxJsxAttribute
): string | undefined {
if (typeof attribute.value === "string") {
return attribute.value;
}

if (
typeof attribute.value === "object" &&
attribute.value?.type === "mdxJsxAttributeValueExpression"
) {
const expression = attribute.value.value;

// if expression is wrapped in "" or '', then return the value inside
if (expression.startsWith('"') && expression.endsWith('"')) {
return expression.slice(1, -1);
}

if (expression.startsWith("'") && expression.endsWith("'")) {
return expression.slice(1, -1);
}

// if the expression is wrapped in backticks, we cannot parse it, because it may contain a variable.
// for now, we'll just return undefined.
}

return undefined;
}
9 changes: 6 additions & 3 deletions packages/fern-docs/ui/src/atoms/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ export const EMPTY_DOCS_STATE: DocsProps = {
layout: undefined,
js: undefined,
navbarLinks: [],
logoHeight: undefined,
logoHref: undefined,
files: {},
logo: {
height: undefined,
href: undefined,
light: undefined,
dark: undefined,
},
content: {
type: "markdown-page",
slug: FernNavigation.Slug(""),
Expand Down
28 changes: 0 additions & 28 deletions packages/fern-docs/ui/src/atoms/files.ts

This file was deleted.

Loading
Loading