fix: og image for glossary

This commit is contained in:
gosangam
2026-05-06 15:52:44 +05:30
parent e5fb370997
commit cbd9c55777
3 changed files with 90 additions and 8 deletions
+57 -7
View File
@@ -3,7 +3,7 @@ export const dynamic = "force-dynamic";
import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { getAllGlossaryEntries } from "@/lib/api";
import { buildOgMeta } from "@/lib/og";
import { buildOgMeta, extractQuoteFromHl } from "@/lib/og";
import { GlossaryClient } from "../glossary-client";
// ---------------------------------------------------------------------------
@@ -11,12 +11,20 @@ import { GlossaryClient } from "../glossary-client";
// ---------------------------------------------------------------------------
const DIACRITIC_TO_BASE: Record<string, string> = {
Ā: "A", Ä: "A",
Ī: "I", É: "I",
Ū: "U", Ü: "U",
Ṛ: "R", Ṝ: "R",
Ś: "S", Ç: "S", Ṣ: "S",
Ñ: "N", Ṇ: "N", Ṅ: "N",
Ā: "A",
Ä: "A",
Ī: "I",
É: "I",
Ū: "U",
Ü: "U",
Ṛ: "R",
Ṝ: "R",
Ś: "S",
Ç: "S",
Ṣ: "S",
Ñ: "N",
Ṇ: "N",
Ṅ: "N",
Ṭ: "T",
Ḍ: "D",
Ḥ: "H",
@@ -34,11 +42,53 @@ function getBaseLetter(term: string): string {
export async function generateMetadata({
params,
searchParams,
}: {
params: Promise<{ letter: string }>;
searchParams: Promise<Record<string, string | string[] | undefined>>;
}): Promise<Metadata> {
const { letter } = await params;
const sp = await searchParams;
const l = letter.toUpperCase();
// If a specific term is referenced via ?term= param, show it in the OG image
const termParam = typeof sp.term === "string" ? sp.term : null;
if (termParam) {
const allEntries = await getAllGlossaryEntries();
const entry = allEntries.find(
(e) => e.term.toLowerCase() === termParam.toLowerCase(),
);
if (entry) {
// Strip HTML tags from definition for OG quote
const plainDef = entry.definitionHtml
.replace(/<[^>]*>/g, "")
.replace(/&[^;]+;/g, " ")
.trim();
// Check for ?hl= highlight param — extract actual selected text from definition
const hlQuote = extractQuoteFromHl(sp, [entry.definitionHtml]);
return buildOgMeta({
title: entry.term,
detail: "Glossary — Prabhupāda Says",
quote:
hlQuote ||
(plainDef.length > 260 ? plainDef.slice(0, 257) + "..." : plainDef),
});
}
}
// Check for ?hl= without ?term= — try to extract quote from all entries for this letter
const hlQuote = extractQuoteFromHl(sp);
if (hlQuote) {
return buildOgMeta({
title: `Glossary — ${l}`,
detail: "Sanskrit & Vedic terms",
quote: hlQuote,
});
}
// Default: letter-level OG
return buildOgMeta({
title: `Glossary — ${l}`,
detail: `Sanskrit & Vedic terms starting with ${l}`,