Link Preview
The <LinkPreview>
component embeds the Open Graph media and metadata for a URL in your page.
Usage
The above code produces the following result:
Video metadata
If a URL’s tags include og:video
metadata, <LinkPreview>
will render a video player instead of an image.
The above code produces the following result:
Limitations
The available Open Graph metadata varies from site to site.
If a site doesn’t provide og:image
metadata, no image will be displayed, only the page title and description.
If no title is detected or the metadata collection step fails, <LinkPreview>
will display only the original link URL.
Styling the preview
By default the <LinkPreview>
card has some minimal layout styling, which should adapt to your site’s font settings etc.
You can customise it with CSS by targeting the .link-preview
class, for example:
The above styles would render <LinkPreview>
like this:
Custom properties API
The <LinkPreview>
component supports the following API for controlling its styles with CSS custom properties.
CSS class names API
.link-preview
: The root element of the<LinkPreview>
card..link-preview--has-video
: Class applied to the card when it includes video instead of an image..link-preview--no-media
: Class applied to the card when it includes no image or video..link-preview--no-metadata
: Class applied when metadata scraping failed, or no valid title was found. In this case the only contents of the card is the original URL link.
Standalone installation
If you only need the <LinkPreview>
component, you can install the package directly instead of the main astro-embed
package:
The <LinkPreview>
component can then be imported as: