| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
export function buildJsonLd({ title, description, image, url, siteUrl, article, breadcrumbs }) { |
| 32 |
const desc = description.trim(); |
| 33 |
const schemas = []; |
| 34 |
|
| 35 |
if (!article) { |
| 36 |
schemas.push({ |
| 37 |
"@context": "https://schema.org", |
| 38 |
"@type": "WebSite", |
| 39 |
"name": "htmx", |
| 40 |
"url": siteUrl, |
| 41 |
"description": desc, |
| 42 |
}); |
| 43 |
} else { |
| 44 |
schemas.push({ |
| 45 |
"@context": "https://schema.org", |
| 46 |
"@type": "Article", |
| 47 |
"headline": title, |
| 48 |
"description": desc, |
| 49 |
"url": url.href, |
| 50 |
"image": new URL(image, url).href, |
| 51 |
...(article.publishedTime && { "datePublished": article.publishedTime }), |
| 52 |
...(article.modifiedTime && { "dateModified": article.modifiedTime }), |
| 53 |
...(article.authors?.length && { |
| 54 |
"author": article.authors.map(name => ({ "@type": "Person", name })) |
| 55 |
}), |
| 56 |
...(article.tags?.length && { "keywords": article.tags.join(", ") }), |
| 57 |
"publisher": { "@type": "Organization", "name": "htmx", "url": siteUrl }, |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
if (breadcrumbs?.length) { |
| 62 |
const allCrumbs = [{ label: 'Home', href: '/' }, ...breadcrumbs]; |
| 63 |
schemas.push({ |
| 64 |
"@context": "https://schema.org", |
| 65 |
"@type": "BreadcrumbList", |
| 66 |
"itemListElement": allCrumbs.map((crumb, i) => ({ |
| 67 |
"@type": "ListItem", |
| 68 |
"position": i + 1, |
| 69 |
"name": crumb.label.replace(/<[^>]*>/g, ''), |
| 70 |
...(crumb.href ? { "item": new URL(crumb.href, siteUrl).href } : {}), |
| 71 |
})), |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
return schemas.length === 1 ? schemas[0] : schemas; |
| 76 |
} |