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

feat: support local embeds #2024

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/fern-docs/ui/src/mdx/components/html/embed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ComponentProps } from "react";

import { FC } from "react";

import { DocsV1Read, FdrAPI } from "@fern-api/fdr-sdk";
import { useAtomValue } from "jotai";
import { useMemo } from "react";
import { FILES_ATOM } from "../../../atoms/files";

export const Embed: FC<ComponentProps<"embed">> = ({ src, ...rest }) => {
const files = useAtomValue(FILES_ATOM);

const fernEmbedSrc = useMemo((): DocsV1Read.File_ | undefined => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably good to make this a utility function and share it across the codebase. i imagine we'll do this more.

if (src == null) {
return undefined;
}

// if src starts with `file:`, assume it's a referenced file; fallback to src if not found
if (src.startsWith("file:")) {
const fileId = FdrAPI.FileId(src.slice(5));
return files[fileId] ?? { type: "url", url: FdrAPI.Url(src) };
}

return { type: "url", url: FdrAPI.Url(src) };
}, [files, src]);

return (
<embed
src={fernEmbedSrc?.url ? fernEmbedSrc.url.toString() : undefined}
{...rest}
/>
);
};
1 change: 1 addition & 0 deletions packages/fern-docs/ui/src/mdx/components/html/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ export const A: FC<AnchorHTMLAttributes<HTMLAnchorElement>> = ({
);
};

export { Embed } from "./embed";
export { Image } from "./image";
4 changes: 3 additions & 1 deletion packages/fern-docs/ui/src/mdx/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { CodeGroup } from "./code/CodeGroup";
import { Column, ColumnGroup } from "./columns";
import { Feature } from "./feature";
import { Frame } from "./frame";
import { A, HeadingRenderer, Image, Li, Ol, Strong, Ul } from "./html";
import { A, Embed, HeadingRenderer, Image, Li, Ol, Strong, Ul } from "./html";
import { Table } from "./html-table";
import { If } from "./if";
import { IFrame } from "./iframe";
Expand Down Expand Up @@ -117,11 +117,13 @@ const HTML_COMPONENTS: MDXComponents = {
li: Li,
a: A,
img: Image,
embed: Embed,
strong: Strong,
};

const ALIASED_HTML_COMPONENTS = {
Image,
Embed,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add one for video since you're here?

IFrame,
H1: (props: ComponentProps<"h1">): ReactElement => HeadingRenderer(1, props),
H2: (props: ComponentProps<"h2">): ReactElement => HeadingRenderer(2, props),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export function rehypeFernComponents(): (tree: Root) => void {
} else if (node.name === "table") {
// DO NOT coerce <table> into <Table> (see: https://buildwithfern.slack.com/archives/C06QKJWD4VD/p1722602687550179)
// node.name = "Table";
} else if (node.name === "embed") {
node.name = "Embed";
}
}
});
Expand Down
Loading