Skip to content

Commit

Permalink
mdx for non manifest page metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
funkydunc committed Jan 20, 2025
1 parent d916336 commit 3621143
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 55 deletions.
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/en/collections-listing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Collections
path: "/collections"
---
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/en/exhibitions-listing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Exhibitions
path: "/exhibitions"
---
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/en/home.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Academic Heritage, History and Art
path: "/"
---
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/en/publications-listing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Publications
path: "/publications"
---
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/en/search.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Search
path: "/search"
---
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/nl/collections-listing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Collecties
path: "/collections"
---
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/nl/exhibitions-listing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Tentoonstellingen
path: "/exhibitions"
---
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/nl/home.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Academisch Erfgoed, Geschiedenis en Kunst
path: "/"
---
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/nl/publications-listing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Publicaties
path: "/publications"
---
4 changes: 4 additions & 0 deletions apps/static-site/content/pages/nl/search.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Zoek
path: "/search"
---
1 change: 1 addition & 0 deletions apps/static-site/contentlayer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Pages = defineDocumentType(() => ({
title: { type: "string", required: true },
path: { type: "string", required: true },
description: { type: "string", required: false },
image: { type: "string", required: false },
},
computedFields: {
lang: {
Expand Down
26 changes: 9 additions & 17 deletions apps/static-site/src/app/[locale]/about/page.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
import { allPages } from ".contentlayer/generated";
import { Slot } from "@/blocks/slot";
import { SlotContext } from "@/blocks/slot-context";
import { Page } from "@/components/Page";
import { Illustration } from "@/components/blocks/Illustration";
import { useMDXComponent } from "next-contentlayer/hooks";
import { Metadata } from "next";
import { getTranslations } from "next-intl/server";
import { getSiteName, getBasicMetadata, makeTitle } from "@/helpers/metadata";

function getAboutPage({ params }: { params: { locale: string } }) {
const aboutPages = allPages.filter((page) => page.path === "/about");
const aboutPage = aboutPages.find((page) => page.lang === params.locale) || aboutPages[0];
if (!aboutPage) throw new Error(`No about page found for locale ${params.locale}`);
return aboutPage;
}
import { getSiteName, getBasicMetadata, makeTitle, getMdx } from "@/helpers/metadata";

export async function generateMetadata({ params }: { params: { locale: string } }): Promise<Metadata> {
const t = await getTranslations();
const siteName = await getSiteName();
const aboutPage = getAboutPage({ params: params });
const aboutTitle = aboutPage.title || t("About");
const title = makeTitle([aboutTitle, siteName]);
const description = aboutPage.description || t("aboutDesc");
return getBasicMetadata(params.locale, siteName, title, description);
const page = getMdx({ params: { pageName: "About", path: "/about", locale: params.locale } });
const title = makeTitle([page.title || t("About"), siteName]);
const description = page.description || t("defaultDesc");
const image = page.image;
return getBasicMetadata(params.locale, siteName, title, description, image);
}

export default async function AboutPage({ params }: { params: { locale: string } }) {
const aboutPage = getAboutPage({ params: params });
const MDXContent = useMDXComponent(aboutPage.body.code);
const page = getMdx({ params: { pageName: "About", path: "/about", locale: params.locale } });
const MDXContent = useMDXComponent(page.body.code);
const CustomSlot = (inner: any) => {
return <Slot context={{ page: "about", locale: params.locale }} {...inner} />;
};

return (
<Page>
<h1 className="text-4xl font-medium">{aboutPage.title}</h1>
<h1 className="text-4xl font-medium">{page.title}</h1>
<div className="mb-32 mt-8 h-full lg:col-span-2">
<article className="prose md:prose-xl prose-lg max-w-full leading-snug md:leading-snug">
<SlotContext name="page" value={"about"}>
Expand Down
10 changes: 6 additions & 4 deletions apps/static-site/src/app/[locale]/collections/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { Page } from "@/components/Page";
import { Slot } from "@/blocks/slot";
import { getTranslations, unstable_setRequestLocale } from "next-intl/server";
import { CollectionListing } from "@/components/pages/CollectionListing";
import { getSiteName, getBasicMetadata, makeTitle } from "@/helpers/metadata";
import { getSiteName, getBasicMetadata, makeTitle, getMdx } from "@/helpers/metadata";
import { Metadata } from "next";

export async function generateMetadata({ params }: { params: { locale: string } }): Promise<Metadata> {
const t = await getTranslations();
const siteName = await getSiteName();
const title = makeTitle([t("Collections"), siteName]);
const description = t("collectionsDesc");
return getBasicMetadata(params.locale, siteName, title, description);
const page = getMdx({ params: { pageName: "Collections", path: "/collections", locale: params.locale } });
const title = makeTitle([page.title || t("Collections"), siteName]);
const description = page.description || t("defaultDesc");
const image = page.image;
return getBasicMetadata(params.locale, siteName, title, description, image);
}

export default async function Collections(props: { params: { locale: string } }) {
Expand Down
10 changes: 6 additions & 4 deletions apps/static-site/src/app/[locale]/exhibitions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { Slot } from "@/blocks/slot";
import exhibitions from "@repo/iiif/build/collections/exhibitions/collection.json";
import { Metadata } from "next";
import { getTranslations } from "next-intl/server";
import { getSiteName, getBasicMetadata, makeTitle } from "@/helpers/metadata";
import { getSiteName, getBasicMetadata, makeTitle, getMdx } from "@/helpers/metadata";

export async function generateMetadata({ params }: { params: { locale: string } }): Promise<Metadata> {
const t = await getTranslations();
const siteName = await getSiteName();
const title = makeTitle([t("Exhibitions"), siteName]);
const description = t("exhibitionsDesc");
return getBasicMetadata(params.locale, siteName, title, description);
const page = getMdx({ params: { pageName: "Exhibitions", path: "/exhibitions", locale: params.locale } });
const title = makeTitle([page.title || t("Exhibitions"), siteName]);
const description = page.description || t("defaultDesc");
const image = page.image;
return getBasicMetadata(params.locale, siteName, title, description, image);
}

export default function ExhibitionsPage({ params }: { params: { locale: string } }) {
Expand Down
10 changes: 6 additions & 4 deletions apps/static-site/src/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import { GlobalHeader } from "@/components/GlobalHeader";
import localFont from "next/font/local";
import { SlotContext } from "@/blocks/slot-context";
import { GlobalFooter } from "@/components/GlobalFooter";
import { getSiteName, getBasicMetadata } from "@/helpers/metadata";
import { getSiteName, getBasicMetadata, makeTitle, getMdx } from "@/helpers/metadata";
import { $ } from "bun";

export async function generateMetadata({ params }: { params: { locale: string } }): Promise<Metadata> {
const t = await getTranslations();
const siteName = await getSiteName();
const title = siteName;
const description = t("homeDesc");
return getBasicMetadata(params.locale, siteName, title, description);
const page = getMdx({ params: { pageName: "Home", path: "/", locale: params.locale } });
const title = makeTitle([page.title, siteName]);
const description = page.description || t("defaultDesc");
const image = page.image;
return getBasicMetadata(params.locale, siteName, title, description, image);
}

if (process.env.NODE_ENV !== "production") {
Expand Down
11 changes: 7 additions & 4 deletions apps/static-site/src/app/[locale]/publications/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { getTranslations, unstable_setRequestLocale } from "next-intl/server";
import { PublicationListPage } from "@/components/pages/PublicationListPage";
import { Page } from "@/components/Page";
import { Metadata } from "next";
import { getSiteName, getBasicMetadata, makeTitle } from "@/helpers/metadata";
import { getSiteName, getBasicMetadata, makeTitle, getMdx } from "@/helpers/metadata";

export async function generateMetadata({ params }: { params: { locale: string } }): Promise<Metadata> {
const t = await getTranslations();
const siteName = await getSiteName();
const title = makeTitle([t("Publications"), siteName]);
const description = t("publicationsDesc");
return getBasicMetadata(params.locale, siteName, title, description);
const page = getMdx({ params: { pageName: "Publications", path: "/publications", locale: params.locale } });
const title = makeTitle([page.title || t("Publications"), siteName]);
const description = page.description || t("defaultDesc");
const image = page.image;

return getBasicMetadata(params.locale, siteName, title, description, image);
}

export default async function PublicationsList({ params }: { params: { locale: string } }) {
Expand Down
14 changes: 8 additions & 6 deletions apps/static-site/src/app/[locale]/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ import { SearchPage } from "@/components/pages/SearchPage";
import { Page } from "@/components/Page";
import { getTranslations, unstable_setRequestLocale } from "next-intl/server";
import { Metadata } from "next";
import { getSiteName, getBasicMetadata, makeTitle } from "@/helpers/metadata";
import { getSiteName, getBasicMetadata, makeTitle, getMdx } from "@/helpers/metadata";

export async function generateMetadata({ params }: { params: { locale: string } }): Promise<Metadata> {
const t = await getTranslations();
const siteName = await getSiteName();
const title = makeTitle([t("Search"), siteName]);
const description = t("searchDesc");
return getBasicMetadata(params.locale, siteName, title, description);
const page = getMdx({ params: { pageName: "Search", path: "/search", locale: params.locale } });
const title = makeTitle([page.title || t("Search"), siteName]);
const description = page.description || t("defaultDesc");
const image = page.image;
return getBasicMetadata(params.locale, siteName, title, description, image);
}

export default async function Search({ params }: { params: { locale: string } }) {
unstable_setRequestLocale(params.locale);
const t = await getTranslations();
const page = getMdx({ params: { pageName: "Search", path: "/search", locale: params.locale } });
return (
<Page>
<SearchPage title={t("Search")} />
<SearchPage title={page.title} />
</Page>
);
}
20 changes: 18 additions & 2 deletions apps/static-site/src/helpers/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Metadata } from "next";
import { getTranslations } from "next-intl/server";
import { allPages } from ".contentlayer/generated";

export const siteURL = "https://heritage.tudelft.nl";
export const fallbackImage = "/logo/TUDelft_logo_rgb.png";
Expand All @@ -20,7 +21,15 @@ export function makeTitle(parts: (string | undefined | null)[]) {
return partsArray.join(" | ");
}

export function getBasicMetadata(locale: string, siteName: string, title: string, description: string): Metadata {
export const defaultImage = "/metadata/default.jpg";

export function getBasicMetadata(
locale: string,
siteName: string,
title: string,
description: string,
image: string | null | undefined
): Metadata {
return {
metadataBase: new URL(siteURL),
title: title,
Expand All @@ -30,7 +39,7 @@ export function getBasicMetadata(locale: string, siteName: string, title: string
description: description,
images: [
{
url: "/metadata/default.jpg",
url: image || defaultImage,
width: 800,
height: 800,
},
Expand All @@ -42,3 +51,10 @@ export function getBasicMetadata(locale: string, siteName: string, title: string
},
};
}

export function getMdx({ params }: { params: { pageName: string; path: string; locale: string } }) {
const pages = allPages.filter((page) => page.path === params.path);
const page = pages.find((p) => p.lang === params.locale) || pages[0];
if (!page) throw new Error(`No ${params.pageName} page found for locale ${params.locale}`);
return page;
}
9 changes: 2 additions & 7 deletions apps/static-site/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@
"Contact and accessibility": "Contact and accessibility",
"Article": "Article",
"Table of contents": "Table of contents",
"homeTitle": "Academic Heritage, History and Art",
"homeDesc": "Explore the history of Delft University of Technology and the Special Collections of TU Delft Library",
"aboutDesc": "Explore the history of Delft University of Technology and the Special Collections of TU Delft Library",
"searchDesc": "Explore the history of Delft University of Technology and the Special Collections of TU Delft Library",
"exhibitionsDesc": "Explore the history of Delft University of Technology and the Special Collections of TU Delft Library",
"collectionsDesc": "Explore the history of Delft University of Technology and the Special Collections of TU Delft Library",
"publicationsDesc": "Explore the history of Delft University of Technology and the Special Collections of TU Delft Library"
"defaultTitle": "Academic Heritage, History and Art",
"defaultDesc": "Explore the history of Delft University of Technology and the Special Collections of TU Delft Library"
}
9 changes: 2 additions & 7 deletions apps/static-site/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@
"Contact and accessibility": "Contact en bereikbaarheid",
"Article": "Artikel",
"Table of contents": "Inhoudsopgave",
"homeTitle": "Academisch Erfgoed, Geschiedenis en Kunst",
"homeDesc": "Verken de geschiedenis van de TU Delft en de bijzondere collecties van de TU Delft Library",
"aboutDesc": "Verken de geschiedenis van de TU Delft en de bijzondere collecties van de TU Delft Library",
"searchDesc": "Verken de geschiedenis van de TU Delft en de bijzondere collecties van de TU Delft Library",
"exhibitionsDesc": "Verken de geschiedenis van de TU Delft en de bijzondere collecties van de TU Delft Library",
"collectionsDesc": "Verken de geschiedenis van de TU Delft en de bijzondere collecties van de TU Delft Library",
"publicationsDesc": "Verken de geschiedenis van de TU Delft en de bijzondere collecties van de TU Delft Library"
"defaultTitle": "Academisch Erfgoed, Geschiedenis en Kunst",
"defaultDesc": "Verken de geschiedenis van de TU Delft en de bijzondere collecties van de TU Delft Library"
}

0 comments on commit 3621143

Please sign in to comment.