embed

package
v0.99.36-0...-9dd75c3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2026 License: Artistic-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package embed provides functionality to fetch and parse metadata from URLs for link previews.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPrivateIP         = errors.New("embed: connection to private IP refused")
	ErrNoAddresses       = errors.New("embed: no DNS addresses resolved")
	ErrTooManyRedirects  = errors.New("embed: too many redirects")
	ErrUnsupportedScheme = errors.New("embed: unsupported scheme")
)
View Source
var ErrHTTPStatus = errors.New("unexpected HTTP status")

ErrHTTPStatus is returned when the fetched URL responds with an error status code.

Functions

func RenderPhoto

func RenderPhoto(l *Link) string

RenderPhoto returns HTML for a photo embed.

func RenderRichCard

func RenderRichCard(l *Link) string

RenderRichCard returns HTML for a rich card embed.

func RenderVideo

func RenderVideo(l *Link, mimeType string) string

RenderVideo returns HTML for a video embed.

Types

type Client

type Client struct {
	HTTPClient *http.Client
	MaxEntries int
	TTL        time.Duration
	// contains filtered or unexported fields
}

Client fetches and caches URL metadata for link previews.

func NewClient

func NewClient() *Client

NewClient creates an embed Client with sensible defaults. The underlying HTTP client blocks requests to private/reserved IP ranges to prevent SSRF.

func (*Client) Fetch

func (c *Client) Fetch(ctx context.Context, rawURL, userAgent string) (*Link, error)

Fetch retrieves metadata for the given URL. Results are cached.

type Link struct {
	AuthorName   string `json:"author_name,omitempty"`
	AuthorURL    string `json:"author_url,omitempty"`
	CacheAge     int    `json:"cache_age,omitempty"`
	Description  string `json:"description,omitempty"`
	Height       int    `json:"height,omitempty"`
	HTML         string `json:"html,omitempty"`
	ProviderName string `json:"provider_name,omitempty"`
	ProviderURL  string `json:"provider_url,omitempty"`
	ThumbnailURL string `json:"thumbnail_url,omitempty"`
	Title        string `json:"title,omitempty"`
	Type         string `json:"type"`
	URL          string `json:"url"`
	Width        int    `json:"width,omitempty"`
}

Link holds metadata extracted from a URL, matching the LinkEmbedder JSON format.

func (*Link) ToMap

func (l *Link) ToMap() map[string]any

ToMap converts a Link to a map for the API response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL