Getting Started
Install the module, unfurl a link, and handle the ways a fetch can fail.
Requirements
BoxLang 1.19+ (Java 21). The module ships jsoup 1.23.2 in libs/.
Install
bashinstall-bx-module bx-unfurl # into BoxLang HOME
install-bx-module bx-unfurl --local # into ./boxlang_modules
box install bx-unfurl # with CommandBox
What's in it
| Function | What it does |
|---|---|
unfurl( url, options ) | Fetches a link, or asks its site's API, and returns its card. Refuses addresses on your internal network. |
unfurlHtml( input, baseUrl ) | Reads the same card from HTML you already have, as a string or as bytes. baseUrl turns relative image and link addresses into absolute ones. |
jscard = unfurl( "https://example.com/article" )
card.title // og:title, twitter:title, or <title>
card.description // og:description, twitter:description, or <meta name="description">
card.image // the first image's address, or ""
card = unfurlHtml( html, "https://example.com/article" )
Links to GitHub, YouTube, Vimeo, npm, ForgeBox, Mastodon, Bluesky, Reddit and Wikipedia are read from those sites' APIs instead of their pages; see Providers.
The module was extracted from the link previews in DismalThreads, minus the parts that belong to an app: there's no database cache and no image copying. Cache cards and proxy images however your app already does.
Handling errors
unfurl() throws one of three types:
| Type | When |
|---|---|
Unfurl.Refused | The address isn't http(s), or its host resolves to an internal address. |
Unfurl.RateLimited | The site answered HTTP 429. extendedInfo holds Retry-After's seconds when it gave a number. |
Unfurl.FetchError | Anything else: a timeout, a 4xx or 5xx, too many redirects, a host that doesn't resolve. |
A link preview is decoration, so most apps show the plain link when any of them happens:
jstry {
card = unfurl( pastedUrl )
} catch ( "Unfurl.Refused" e ) {
card = {}
} catch ( "Unfurl.RateLimited" e ) {
// try again after e.extendedInfo seconds, if it's a number
card = {}
} catch ( "Unfurl.FetchError" e ) {
card = {}
}
A provider whose API doesn't answer (a rate limit, a private repository, a server that turns out not to be Mastodon) doesn't throw: the page is read instead.
Settings
In boxlang.json under modules.bxUnfurl.settings, or per call in unfurl( url, options ):
bxson"modules": {
"bxUnfurl": {
"settings": {
"userAgent": "MyApp/1.0 (+https://example.com)",
"githubToken": "${GITHUB_TOKEN}"
}
}
}
| Setting | Default | |
|---|---|---|
userAgent | bx-unfurl/<version> (+https://github.com/robertz/bx-unfurl) | Some sites only serve their tags to browser-like agents. |
timeoutSeconds | 8 | Connect and request timeout. |
maxBytes | 1048576 | Reads no more of the page than this. |
maxRedirects | 5 | |
allowPrivateAddresses | false | For development and tests only. See Security. |
providers | all nine | The providers to ask, by name. [] reads every address as a page. |
oembed | true | Reads a page's oEmbed endpoint when it names one. |
endpoints | the real APIs | Override one provider's API at a time. See Providers. |
githubToken | "" | GitHub allows 60 calls an hour without one and 5,000 with. |
Per call:
jscard = unfurl( pastedUrl, { timeoutSeconds: 3, providers: [] } )
Runnable example
examples/smoke.bxs in the repository reads a card from HTML, then unfurls a page, a redirect, an image, a page with an oEmbed endpoint and a GitHub repository, all from a server it starts on a free local port, and shows what gets refused. It needs no network:
bashboxlang examples/smoke.bxs
Why jsoup is bundled
bx-jsoup would be the natural dependency, but its htmlParse() takes only a string: there's no base address for relative links, and no bytes to detect a page's character set from. Bundling jsoup keeps both. scripts/fetch-libs.sh downloads it from Maven Central and checks its SHA-1.