Skip to content

Commit

Permalink
docs: fix og files
Browse files Browse the repository at this point in the history
  • Loading branch information
damianricobelli committed Dec 1, 2024
1 parent e0f5de7 commit 72c3e32
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 112 deletions.
4 changes: 1 addition & 3 deletions apps/docs/app/og/[...slug]/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ export const GET = metadataImage.createAPI((page): ImageResponseType => {
);
});

export function generateStaticParams(): {
slug: string[];
}[] {
export function generateStaticParams() {
return metadataImage.generateParams();
}
110 changes: 1 addition & 109 deletions apps/docs/utils/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,7 @@
import { source } from "@/app/source";
import type { InferPageType, LoaderConfig, LoaderOutput } from "fumadocs-core/source";
import { notFound } from "next/navigation";
import type { NextRequest } from "next/server";
import { createMetadataImage } from "fumadocs-core/server";
import type { Metadata } from "next/types";

interface ImageMeta {
alt: string;
url: string;
width: number;
height: number;
}

interface RouteOptions {
params: {
slug: string[];
lang?: string;
};
}

export function createMetadataImage<S extends LoaderOutput<LoaderConfig>>(options: {
source: S;

/**
* the route of your OG image generator.
*
* @example '/docs-og'
* @defaultValue '/docs-og'
*/
imageRoute?: string;

/**
* The filename of generated OG Image
*
* @defaultValue 'image.png'
*/
filename?: string;
}): {
getImageMeta: (slugs: string[]) => ImageMeta;

/**
* Add image meta tags to metadata
*/
withImage: (slugs: string[], metadata?: Metadata) => Metadata;

/**
* Generate static params for OG Image Generator
*/
generateParams: () => {
slug: string[];
lang?: string;
}[];

/**
* create route handler for OG Image Generator
*/
createAPI: (
handler: (page: InferPageType<S>, request: NextRequest, options: RouteOptions) => Response | Promise<Response>,
) => (request: NextRequest, options: RouteOptions) => Response | Promise<Response>;
} {
const { filename = "image.png", imageRoute = "/docs-og" } = options;

function getImageMeta(slugs: string[]): ImageMeta {
return {
alt: "Banner",
url: `/${[...imageRoute.split("/"), ...slugs, filename].filter((v) => v.length > 0).join("/")}`,
width: 1200,
height: 630,
};
}

return {
getImageMeta,
withImage(slugs, data) {
const imageData = getImageMeta(slugs);

return {
...data,
openGraph: {
images: imageData,
...data?.openGraph,
},
twitter: {
images: imageData,
card: "summary_large_image",
...data?.twitter,
},
};
},
generateParams() {
return options.source.generateParams().map((params) => ({
...params,
slug: [...params.slug, filename],
}));
},
createAPI(handler) {
return (req, args) => {
const params = args.params;
const page = options.source.getPage(
params.slug.slice(0, -1), //remove filename
params.lang,
);
if (!page) {
notFound();
}

return handler(page as InferPageType<S>, req, args);
};
},
};
}

export function createMetadata(override: Metadata): Metadata {
return {
...override,
Expand Down

0 comments on commit 72c3e32

Please sign in to comment.