Skip to content
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
15 changes: 15 additions & 0 deletions app/app/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export function generateMetadata({ params }: { params: { slug: string } }): Meta
const post = getPost(params.slug)
if (!post) return {}
const url = `https://knowcap.ai/blog/${post.slug}`
const ogImageUrl = post.ogImage
? post.ogImage.startsWith('http')
? post.ogImage
: `https://knowcap.ai${post.ogImage}`
: null
const ogImages = ogImageUrl
? [{ url: ogImageUrl, width: 1200, height: 675, alt: post.title }]
: undefined
return {
title: `${post.title} — Knowcap Blog`,
description: post.description,
Expand All @@ -27,6 +35,13 @@ export function generateMetadata({ params }: { params: { slug: string } }): Meta
publishedTime: post.date,
authors: [post.author],
locale: post.dir === 'rtl' ? 'ar_AR' : 'en_US',
...(ogImages ? { images: ogImages } : {}),
},
twitter: {
card: ogImageUrl ? 'summary_large_image' : 'summary',
title: post.title,
description: post.description,
...(ogImageUrl ? { images: [ogImageUrl] } : {}),
},
}
}
Expand Down
20 changes: 20 additions & 0 deletions app/lib/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import matter from 'gray-matter'
*/

const BLOG_DIR = path.join(process.cwd(), 'content', 'blog')
const PUBLIC_DIR = path.join(process.cwd(), 'public')

export interface BlogPostMeta {
slug: string
Expand All @@ -25,6 +26,24 @@ export interface BlogPostMeta {
readMinutes: number
lang: string
dir: 'ltr' | 'rtl'
/** Absolute-from-root path to the post's social/OG image, or null. */
ogImage: string | null
}

/**
* Resolve a post's OG image. Explicit frontmatter (`og_image`/`ogImage`) wins;
* otherwise look for the per-post asset convention `public/blog/<slug>/og.{webp,png}`.
* Returns a root-relative path (e.g. `/blog/<slug>/og.webp`) or null when none exists.
*/
function resolveOgImage(slug: string, data: Record<string, any>): string | null {
const explicit = data.og_image ?? data.ogImage
if (explicit) return String(explicit)
for (const ext of ['webp', 'png']) {
if (fs.existsSync(path.join(PUBLIC_DIR, 'blog', slug, `og.${ext}`))) {
return `/blog/${slug}/og.${ext}`
}
}
return null
}

export interface Heading {
Expand Down Expand Up @@ -193,6 +212,7 @@ function toMeta(slug: string, data: Record<string, any>, body: string): BlogPost
readMinutes: Math.max(1, Math.round(words / 220)),
lang,
dir,
ogImage: resolveOgImage(slug, data),
}
}

Expand Down