🎥 Make videos programmatically with React
Elements: Add Markdown pages and catalogs to library template
Igor Samokhovets88cdeb52758c
diff --git a/packages/template-element-library/README.md b/packages/template-element-library/README.mdindex 4fc1de24a3..8a89221b34 100644--- a/packages/template-element-library/README.md+++ b/packages/template-element-library/README.md@@ -68,6 +68,22 @@ npm run previewPublish `build/client` to a static host. All Element pages are prerendered from the registry. See the [React Router prerendering guide](https://reactrouter.com/how-to/pre-rendering) for static hosting and fallback routing. Use HTTPS and allow embedding in Remotion Studio, as described in the library integration guide.+## Markdown and agent discovery++Each page declares its Markdown URL with a `<link rel="alternate" type="text/markdown">` tag. Agents can follow `/index.md` from the homepage to find every Element without guessing URLs or running JavaScript.++Markdown uses the website's registry, installation payloads, and source files. New registry entries appear in the catalog and build automatically. Resource routes serve Markdown during development; builds write `.md` files to `build/client`. You don't need a production server or host integration.++Serve `.md` files directly, preferably with `Content-Type: text/markdown; charset=utf-8`. The routes set development headers; your host sets production headers.++### Optional content negotiation++Static files cannot inspect `Accept` headers. To serve Markdown at HTML URLs when a client prefers `text/markdown`, configure your host or server to rewrite each page URL to its Markdown file.++Keep HTML for browser requests. Respect quality values: an HTML preference or `text/markdown;q=0` must not select Markdown. Rewrite only existing pages on GET/HEAD, normalize trailing slashes, and leave assets and `.md` URLs unchanged. Set `Vary: Accept` on both responses and configure your CDN to cache them separately, or disable caching for these URLs.++Direct `.md` links work without negotiation.+## LicenseRemotion’s [license terms](https://www.remotion.dev/license) apply. Choose a license for the Elements you publish.diff --git a/packages/template-element-library/app/routes.ts b/packages/template-element-library/app/routes.tsindex ec7ede69c0..c58e75833b 100644--- a/packages/template-element-library/app/routes.ts+++ b/packages/template-element-library/app/routes.ts@@ -6,6 +6,9 @@ import {} from "@react-router/dev/routes";export default [+ route("index.md", "routes/library-markdown.ts"),+ route("captions.md", "routes/captions-markdown.ts"),+ route("elements/:slug.md", "routes/element-markdown.ts"),layout("routes/layout.tsx", [index("routes/library.tsx"),route("captions", "routes/captions.tsx"),diff --git a/packages/template-element-library/app/routes/captions-markdown.ts b/packages/template-element-library/app/routes/captions-markdown.tsnew file mode 100644index 0000000000..756a47f2a5--- /dev/null+++ b/packages/template-element-library/app/routes/captions-markdown.ts@@ -0,0 +1,6 @@+import { getCatalogMarkdown } from "../../lib/markdown";++export const loader = () =>+ new Response(getCatalogMarkdown("Captions"), {+ headers: { "Content-Type": "text/markdown; charset=utf-8" },+ });diff --git a/packages/template-element-library/app/routes/captions.tsx b/packages/template-element-library/app/routes/captions.tsxindex b432e4456a..dd074979b0 100644--- a/packages/template-element-library/app/routes/captions.tsx+++ b/packages/template-element-library/app/routes/captions.tsx@@ -7,6 +7,13 @@ import { ElementCard } from "../components/element-card";import { StudioActions } from "../components/studio-actions";export const meta: MetaFunction = () => [+ {+ tagName: "link",+ rel: "alternate",+ type: "text/markdown",+ href: "/captions.md",+ title: "Caption catalog as Markdown",+ },{ title: "Caption styles — Studio Supply" },{name: "description",diff --git a/packages/template-element-library/app/routes/element-markdown.ts b/packages/template-element-library/app/routes/element-markdown.tsnew file mode 100644index 0000000000..11dbcfc039--- /dev/null+++ b/packages/template-element-library/app/routes/element-markdown.ts@@ -0,0 +1,16 @@+import type { LoaderFunctionArgs } from "react-router";+import { catalog } from "../../lib/element-payload";+import { getElementMarkdown } from "../../lib/markdown";++export const loader = ({ params }: LoaderFunctionArgs) => {+ const entry = catalog.find(+ ({ definition }) => definition.slug === params.slug,+ );+ if (!entry) {+ throw new Response("Element not found", { status: 404 });+ }++ return new Response(getElementMarkdown(entry), {+ headers: { "Content-Type": "text/markdown; charset=utf-8" },+ });+};diff --git a/packages/template-element-library/app/routes/element.tsx b/packages/template-element-library/app/routes/element.tsxindex 46c1e697b7..834f5f98ec 100644--- a/packages/template-element-library/app/routes/element.tsx+++ b/packages/template-element-library/app/routes/element.tsx@@ -23,6 +23,17 @@ export const meta: MetaFunction = ({ params }) => {name: "description",content: entry?.definition.description ?? "Browse the Element library.",},+ ...(entry+ ? [+ {+ tagName: "link" as const,+ rel: "alternate",+ type: "text/markdown",+ href: `/elements/${entry.definition.slug}.md`,+ title: `${entry.definition.displayName} as Markdown`,+ },+ ]+ : []),];};diff --git a/packages/template-element-library/app/routes/library-markdown.ts b/packages/template-element-library/app/routes/library-markdown.tsnew file mode 100644index 0000000000..cd7ae24796--- /dev/null+++ b/packages/template-element-library/app/routes/library-markdown.ts@@ -0,0 +1,6 @@+import { getCatalogMarkdown } from "../../lib/markdown";++export const loader = () =>+ new Response(getCatalogMarkdown(null), {+ headers: { "Content-Type": "text/markdown; charset=utf-8" },+ });diff --git a/packages/template-element-library/app/routes/library.tsx b/packages/template-element-library/app/routes/library.tsxindex ca2b14bb9c..00cdeeb5fa 100644--- a/packages/template-element-library/app/routes/library.tsx+++ b/packages/template-element-library/app/routes/library.tsx@@ -4,6 +4,13 @@ import { catalog } from "../../lib/element-payload";import { ElementCard } from "../components/element-card";export const meta: MetaFunction = () => [+ {+ tagName: "link",+ rel: "alternate",+ type: "text/markdown",+ href: "/index.md",+ title: "Element catalog as Markdown",+ },{ title: "Studio Supply — Remotion Elements" },{name: "description",diff --git a/packages/template-element-library/lib/markdown.ts b/packages/template-element-library/lib/markdown.tsnew file mode 100644index 0000000000..ac3e3cf347--- /dev/null+++ b/packages/template-element-library/lib/markdown.ts@@ -0,0 +1,103 @@+import { catalog, type CatalogEntry } from "./element-payload";++const codeBlock = (language: string, content: string) => {+ const fence = "`".repeat(+ Math.max(+ 3,+ ...Array.from(content.matchAll(/`+/g), ([match]) => match.length + 1),+ ),+ );+ return `${fence}${language}\n${content}\n${fence}`;+};++export const getCatalogMarkdown = (category: string | null) => {+ const entries = catalog.filter(+ ({ definition }) => category === null || definition.category === category,+ );+ const categories = [+ ...new Set(entries.map(({ definition }) => definition.category)),+ ];++ return [+ `# ${category === null ? "Studio Supply - Remotion Elements" : `${category} - Studio Supply`}`,+ "Editable video building blocks for Remotion. Each Element document includes its source code and installation metadata.",+ "[All Elements](/index.md) | [Caption styles](/captions.md)",+ ...categories.map((name) =>+ [+ `## ${name}`,+ ...entries+ .filter(({ definition }) => definition.category === name)+ .map(+ ({ definition }) =>+ `- [${definition.displayName}](/elements/${definition.slug}.md) - ${definition.description}`,+ ),+ ].join("\n\n"),+ ),+ "",+ ].join("\n\n");+};++export const getElementMarkdown = ({+ definition,+ sourceCode,+ payload,+}: CatalogEntry) => {+ const remotionDependencies = definition.dependencies.filter(+ ({ version }) => version === null,+ );+ const otherDependencies = definition.dependencies.filter(+ ({ version }) => version !== null,+ );+ const installationCommands = [+ ...(remotionDependencies.length === 0+ ? []+ : [+ `npx remotion add ${remotionDependencies.map(({ name }) => name).join(" ")}`,+ ]),+ ...(otherDependencies.length === 0+ ? []+ : [+ `npm install --save-exact ${otherDependencies.map(({ name, version }) => `${name}@${version}`).join(" ")}`,+ ]),+ ];++ return [+ `# ${definition.displayName}`,+ definition.description,+ "[All Elements](/index.md) | [Caption styles](/captions.md)",+ "## Details",+ `- Category: ${definition.category}\n- Duration: ${definition.durationInFrames} frames (${definition.durationInFrames / definition.fps}s at ${definition.fps} fps)\n- Dimensions: ${definition.dimensions === null ? "Fills composition" : `${definition.dimensions.width} x ${definition.dimensions.height}`}`,+ "## Installation",+ `Save the source below as \`${definition.slug}.element.tsx\` in your Remotion project and import its exported component.`,+ installationCommands.length === 0+ ? "No additional package dependencies are required beyond React and Remotion."+ : codeBlock("sh", installationCommands.join("\n")),+ definition.installationMode === "wrapped"+ ? "Wrap the component in a Remotion Sequence and use the duration and dimensions below."+ : "The component owns its Sequence. Pass timing props to the component rather than adding an outer Sequence.",+ "Use the installation metadata below for default props, dependencies, and assets. Asset references in initialProps must be resolved to local staticFile() calls after saving the declared assets to your public directory. See the [Element Library integration guide](https://www.remotion.dev/docs/studio-protocol/component-library-integration.md) for asset handling and Studio installation.",+ "## Installation metadata",+ codeBlock(+ "json",+ JSON.stringify(+ {+ slug: definition.slug,+ displayName: definition.displayName,+ fps: definition.fps,+ durationInFrames: payload.durationInFrames,+ dimensions: payload.element.dimensions,+ installationMode: definition.installationMode,+ isCaptionStyle: definition.category === "Captions",+ initialProps: payload.element.initialProps,+ dependencies: payload.element.dependencies,+ assets: payload.element.assets,+ },+ null,+ 2,+ ),+ ),+ "## Source",+ codeBlock("tsx", sourceCode),+ "",+ ].join("\n\n");+};diff --git a/packages/template-element-library/react-router.config.ts b/packages/template-element-library/react-router.config.tsindex 66a88402fc..d8abf7b849 100644--- a/packages/template-element-library/react-router.config.ts+++ b/packages/template-element-library/react-router.config.ts@@ -5,7 +5,12 @@ export default {ssr: false,prerender: ["/",+ "/index.md","/captions",- ...elements.map(({ slug }) => `/elements/${slug}`),+ "/captions.md",+ ...elements.flatMap(({ slug }) => [+ `/elements/${slug}`,+ `/elements/${slug}.md`,+ ]),],} satisfies Config;