|
| 1 | +import { Author, BlogPost, Category, Publisher } from "../types.ts"; |
| 2 | + |
| 3 | +const toAuthor = (author: Author) => { |
| 4 | + const type = author.type ?? "Person"; |
| 5 | + return { |
| 6 | + "@type": type, |
| 7 | + name: author.name, |
| 8 | + // jobTitle and worksFor are Person-only properties |
| 9 | + ...(type === "Person" && author.jobTitle |
| 10 | + ? { jobTitle: author.jobTitle } |
| 11 | + : {}), |
| 12 | + ...(type === "Person" && author.company |
| 13 | + ? { worksFor: { "@type": "Organization" as const, name: author.company } } |
| 14 | + : {}), |
| 15 | + }; |
| 16 | +}; |
| 17 | + |
| 18 | +export const toOrganization = (publisher: Publisher) => ({ |
| 19 | + "@type": "Organization" as const, |
| 20 | + name: publisher.name, |
| 21 | + ...(publisher.url ? { url: publisher.url } : {}), |
| 22 | + ...(publisher.logo |
| 23 | + ? { logo: { "@type": "ImageObject" as const, url: publisher.logo } } |
| 24 | + : {}), |
| 25 | +}); |
| 26 | + |
| 27 | +/** |
| 28 | + * Replaces the origin of a URL, keeping path, query and hash. Relative URLs |
| 29 | + * are resolved against the canonical base. |
| 30 | + */ |
| 31 | +export const withCanonicalBase = (url: string, canonicalBaseUrl?: string) => { |
| 32 | + if (!canonicalBaseUrl) { |
| 33 | + return url; |
| 34 | + } |
| 35 | + const { pathname, search, hash } = new URL(url, canonicalBaseUrl); |
| 36 | + return new URL(pathname + search + hash, canonicalBaseUrl).href; |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Maps a BlogPost to a schema.org BlogPosting node, following |
| 41 | + * https://developers.google.com/search/docs/appearance/structured-data/article |
| 42 | + * |
| 43 | + * The "@context" is intentionally omitted: the Seo component adds it when |
| 44 | + * serializing the top-level JSON-LD object. |
| 45 | + */ |
| 46 | +export const toBlogPosting = ( |
| 47 | + post: BlogPost, |
| 48 | + url?: string, |
| 49 | + publisher?: Publisher, |
| 50 | +) => { |
| 51 | + const image = post.seo?.image || post.image; |
| 52 | + const categories = post.categories |
| 53 | + ?.map((category) => category.name) |
| 54 | + .filter(Boolean); |
| 55 | + |
| 56 | + // AggregateRating requires ratingValue and ratingCount/reviewCount; |
| 57 | + // InteractionCounter requires userInteractionCount. Posts may carry empty |
| 58 | + // {"@type": ...} stubs, which are invalid in the Rich Results Test. |
| 59 | + const aggregateRating = post.aggregateRating?.ratingValue != null && |
| 60 | + (post.aggregateRating.ratingCount != null || |
| 61 | + post.aggregateRating.reviewCount != null) |
| 62 | + ? post.aggregateRating |
| 63 | + : undefined; |
| 64 | + const interactionStatistic = |
| 65 | + post.interactionStatistic?.userInteractionCount != null |
| 66 | + ? post.interactionStatistic |
| 67 | + : undefined; |
| 68 | + |
| 69 | + return { |
| 70 | + "@type": "BlogPosting" as const, |
| 71 | + headline: post.title, |
| 72 | + ...(post.excerpt ? { description: post.excerpt } : {}), |
| 73 | + ...(image ? { image: [image] } : {}), |
| 74 | + ...(post.date ? { datePublished: post.date } : {}), |
| 75 | + ...(post.dateModified ? { dateModified: post.dateModified } : {}), |
| 76 | + ...(post.authors?.length ? { author: post.authors.map(toAuthor) } : {}), |
| 77 | + ...(publisher?.name ? { publisher: toOrganization(publisher) } : {}), |
| 78 | + ...(categories?.length ? { articleSection: categories } : {}), |
| 79 | + ...(post.readTime && post.readTime > 0 && Number.isFinite(post.readTime) |
| 80 | + ? { timeRequired: `PT${post.readTime}M` } |
| 81 | + : {}), |
| 82 | + ...(url |
| 83 | + ? { url, mainEntityOfPage: { "@type": "WebPage" as const, "@id": url } } |
| 84 | + : {}), |
| 85 | + ...(aggregateRating ? { aggregateRating } : {}), |
| 86 | + ...(interactionStatistic ? { interactionStatistic } : {}), |
| 87 | + }; |
| 88 | +}; |
| 89 | + |
| 90 | +const humanize = (slug: string) => |
| 91 | + decodeURIComponent(slug) |
| 92 | + .replace(/[-_]+/g, " ") |
| 93 | + .replace(/^./, (char) => char.toUpperCase()); |
| 94 | + |
| 95 | +/** |
| 96 | + * Builds a schema.org BreadcrumbList from the pathname of the given absolute |
| 97 | + * URL. Intermediate segments resolve their names against the known categories |
| 98 | + * (falling back to a humanized slug) and link to absolute URLs; the last item |
| 99 | + * uses the real page name and omits "item", as allowed by Google. |
| 100 | + */ |
| 101 | +export const toBreadcrumbList = ( |
| 102 | + url: string, |
| 103 | + { currentName, categories }: { |
| 104 | + currentName?: string; |
| 105 | + categories?: Category[]; |
| 106 | + } = {}, |
| 107 | +) => { |
| 108 | + const { origin, pathname } = new URL(url); |
| 109 | + const segments = pathname.split("/").filter(Boolean); |
| 110 | + |
| 111 | + const nameOf = (segment: string) => |
| 112 | + categories?.find((category) => category.slug === segment)?.name ?? |
| 113 | + humanize(segment); |
| 114 | + |
| 115 | + const itemListElement = segments.map((segment, index) => { |
| 116 | + const isLast = index === segments.length - 1; |
| 117 | + return { |
| 118 | + "@type": "ListItem" as const, |
| 119 | + position: index + 1, |
| 120 | + name: isLast ? currentName || nameOf(segment) : nameOf(segment), |
| 121 | + ...(isLast |
| 122 | + ? {} |
| 123 | + : { item: `${origin}/${segments.slice(0, index + 1).join("/")}` }), |
| 124 | + }; |
| 125 | + }); |
| 126 | + |
| 127 | + return { "@type": "BreadcrumbList" as const, itemListElement }; |
| 128 | +}; |
0 commit comments