|
| 1 | +import type { BlogAuthorIdentity } from '~/utils/blog-format' |
| 2 | +import { formatAuthors } from '~/utils/blog-format' |
| 3 | +import { |
| 4 | + getTanStackOrganizationJsonLd, |
| 5 | + TANSTACK_ORGANIZATION_ID, |
| 6 | +} from '~/utils/organization-structured-data' |
| 7 | +import { getAbsoluteOptimizedImageUrl } from '~/utils/optimizedImage' |
| 8 | +import { canonicalUrl, seo } from '~/utils/seo' |
| 9 | + |
| 10 | +export type BlogPostHeadData = { |
| 11 | + authorIdentities: Array<BlogAuthorIdentity> |
| 12 | + authors: Array<string> |
| 13 | + description: string |
| 14 | + isUnpublished: boolean |
| 15 | + published: string |
| 16 | + slug: string |
| 17 | + socialImage?: string |
| 18 | + title: string |
| 19 | + updated?: string |
| 20 | +} |
| 21 | + |
| 22 | +export function getBlogSocialImageUrl(headerImage: string | undefined) { |
| 23 | + if (!headerImage) { |
| 24 | + return undefined |
| 25 | + } |
| 26 | + |
| 27 | + if (headerImage.startsWith('http')) { |
| 28 | + return headerImage |
| 29 | + } |
| 30 | + |
| 31 | + return getAbsoluteOptimizedImageUrl(headerImage, { |
| 32 | + fit: 'cover', |
| 33 | + format: 'auto', |
| 34 | + height: 630, |
| 35 | + quality: 80, |
| 36 | + width: 1200, |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +export function getBlogPostingJsonLd(post: BlogPostHeadData) { |
| 41 | + const pageUrl = canonicalUrl(`/blog/${post.slug}`) |
| 42 | + |
| 43 | + return { |
| 44 | + '@context': 'https://schema.org', |
| 45 | + '@graph': [ |
| 46 | + getTanStackOrganizationJsonLd(), |
| 47 | + { |
| 48 | + '@type': 'BlogPosting' as const, |
| 49 | + '@id': `${pageUrl}#blog-post`, |
| 50 | + headline: post.title, |
| 51 | + description: post.description, |
| 52 | + url: pageUrl, |
| 53 | + mainEntityOfPage: { |
| 54 | + '@type': 'WebPage' as const, |
| 55 | + '@id': pageUrl, |
| 56 | + }, |
| 57 | + ...(post.socialImage ? { image: post.socialImage } : {}), |
| 58 | + datePublished: post.published, |
| 59 | + ...(post.updated ? { dateModified: post.updated } : {}), |
| 60 | + author: post.authorIdentities.map((author) => |
| 61 | + author.type === 'Organization' && author.name === 'TanStack' |
| 62 | + ? { '@id': TANSTACK_ORGANIZATION_ID } |
| 63 | + : { |
| 64 | + '@type': author.type, |
| 65 | + name: author.name, |
| 66 | + ...(author.url ? { url: author.url } : {}), |
| 67 | + }, |
| 68 | + ), |
| 69 | + publisher: { |
| 70 | + '@id': TANSTACK_ORGANIZATION_ID, |
| 71 | + }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export function getBlogPostHead(post: BlogPostHeadData | undefined) { |
| 78 | + if (!post) { |
| 79 | + return { meta: [], scripts: [] } |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + meta: [ |
| 84 | + ...seo({ |
| 85 | + title: `${post.title} | TanStack Blog`, |
| 86 | + description: post.description, |
| 87 | + image: post.socialImage, |
| 88 | + noindex: post.isUnpublished, |
| 89 | + ogType: 'article', |
| 90 | + articlePublishedTime: post.published, |
| 91 | + articleModifiedTime: post.updated, |
| 92 | + }), |
| 93 | + { |
| 94 | + name: 'author', |
| 95 | + content: `${ |
| 96 | + post.authors.length > 1 ? 'co-authored by ' : '' |
| 97 | + }${formatAuthors(post.authors)}`, |
| 98 | + }, |
| 99 | + ], |
| 100 | + scripts: post.isUnpublished |
| 101 | + ? [] |
| 102 | + : [ |
| 103 | + { |
| 104 | + type: 'application/ld+json', |
| 105 | + children: JSON.stringify(getBlogPostingJsonLd(post)), |
| 106 | + }, |
| 107 | + ], |
| 108 | + } |
| 109 | +} |
0 commit comments