Cards
The struct unfurl() and unfurlHtml() return, and where each key comes from.
Every card has the same keys, whether it was read from a page, a provider's API or both. Addresses are absolute and http(s), or "". An unknown width or height is 0.
Keys
| Key | Where it comes from |
|---|---|
url | og:url, else the canonical link, else the address read |
canonical | <link rel="canonical"> |
type, siteName, locale | og:type, og:site_name, og:locale |
title | og:title, twitter:title, <title> |
description | og:description, twitter:description, <meta name="description"> |
image | images[ 1 ].url, or "" |
images | [ { url, secureUrl, type, width, height, alt } ] |
videos | [ { url, secureUrl, type, width, height } ] |
audios | [ { url, secureUrl, type } ] |
favicon | rel="icon", else an apple-touch-icon, else "" |
twitter | { card, site, creator } |
author | { name, url } from <meta name="author"> and article:author, or from a provider or oEmbed |
publishedAt | article:published_time, or a provider's date, as the source gives it |
oembedUrl | the page's <link rel="alternate" type="application/json+oembed"> |
embed | oEmbed's answer, or {} (below) |
details | a provider's own fields, or {}. See Providers. |
tags | every og:, twitter:, article:, book:, profile:, music:, video: and fb: tag, as arrays of values in page order |
publishedAt is a string, not a date: sites write it in more formats than are worth guessing at. Parse it if you need to sort by it.
Images
images is built from og:image and its structured properties (og:image:secure_url, og:image:type, og:image:width, og:image:height, og:image:alt), one entry per image in page order. A page with no og:image falls back to twitter:image, then to <link rel="image_src">.
Images are returned as addresses, never fetched. Showing one straight from the page's server tells that server who's reading your page; copy images to your own storage if that matters.
oEmbed
When a page names an oEmbed endpoint and oembed is on, that endpoint is read too. It fills in author and a missing image from its thumbnail, and embed gets:
{ type, html, width, height, providerName, providerUrl,
thumbnailUrl, thumbnailWidth, thumbnailHeight }
embed.html is the provider's own iframe. Rendering it sends your readers' browsers to that provider, so use it only if you're happy with that. A click-to-load player keeps it from happening when the page loads.
What unfurl() adds
| Key | |
|---|---|
requestedUrl | the address you passed |
fetchedUrl | where it ended up after redirects |
contentType | the answer's Content-Type |
source | page, image, or the provider's name |
When the address turns out to be an image, source is image and the image itself becomes the card's image, so a pasted picture still gets a preview.
Rendering a card
html<bx:if len( card.title )>
<a class="card" href="#encodeForHTMLAttribute( card.url )#" rel="noopener">
<bx:if len( card.image )>
<img src="#encodeForHTMLAttribute( card.image )#" alt="#encodeForHTMLAttribute( card.images[ 1 ].alt )#">
</bx:if>
<strong>#encodeForHTML( card.title )#</strong>
<p>#encodeForHTML( card.description )#</p>
<small>#encodeForHTML( len( card.siteName ) ? card.siteName : card.url )#</small>
</a>
</bx:if>
Every value comes from someone else's page, so encode all of it.