|
| 1 | +import type { CollectionEntry } from 'astro:content' |
| 2 | + |
| 3 | +export interface BlogPostingSchemaOptions { |
| 4 | + title: string |
| 5 | + description: string |
| 6 | + datePublished: Date |
| 7 | + dateModified: Date |
| 8 | + authors: CollectionEntry<'authors'>[] |
| 9 | + url: string |
| 10 | + wordCount: number |
| 11 | + siteUrl: string |
| 12 | +} |
| 13 | + |
| 14 | +export function generateBlogPostingSchema( |
| 15 | + options: BlogPostingSchemaOptions, |
| 16 | +): object { |
| 17 | + return { |
| 18 | + '@context': 'https://schema.org', |
| 19 | + '@type': 'BlogPosting', |
| 20 | + headline: options.title, |
| 21 | + description: options.description, |
| 22 | + datePublished: options.datePublished.toISOString(), |
| 23 | + dateModified: options.dateModified.toISOString(), |
| 24 | + author: options.authors.map((author) => ({ |
| 25 | + '@type': 'Person', |
| 26 | + name: author.data.name, |
| 27 | + url: author.data.link, |
| 28 | + })), |
| 29 | + publisher: { |
| 30 | + '@type': 'Organization', |
| 31 | + name: 'Node.js Design Patterns', |
| 32 | + url: options.siteUrl, |
| 33 | + logo: { |
| 34 | + '@type': 'ImageObject', |
| 35 | + url: `${options.siteUrl}/images/og-image.jpg`, |
| 36 | + }, |
| 37 | + }, |
| 38 | + mainEntityOfPage: { '@type': 'WebPage', '@id': options.url }, |
| 39 | + wordCount: options.wordCount, |
| 40 | + inLanguage: 'en-US', |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export interface FAQItem { |
| 45 | + question: string |
| 46 | + answer: string |
| 47 | +} |
| 48 | + |
| 49 | +export function generateFAQPageSchema(items: FAQItem[]): object { |
| 50 | + return { |
| 51 | + '@context': 'https://schema.org', |
| 52 | + '@type': 'FAQPage', |
| 53 | + mainEntity: items.map((item) => ({ |
| 54 | + '@type': 'Question', |
| 55 | + name: item.question, |
| 56 | + acceptedAnswer: { '@type': 'Answer', text: item.answer }, |
| 57 | + })), |
| 58 | + } |
| 59 | +} |
0 commit comments