linkpreview

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: MIT Imports: 6 Imported by: 0

README

linkpreview

Small, opinionated Go library for generating link previews (title, description, image, favicon, site name) using goquery.

  • ✅ Open Graph + standard HTML fallbacks
  • ✅ Functional options (pick only what you need)
  • ✅ Sensible defaults (timeout, UA)
  • ✅ Tiny surface area

Install

go get github.com/Richy91/linkpreview

Use

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/Richy91/linkpreview"
)

func main() {
	lp := linkpreview.New(
		"https://github.com/PuerkitoBio/goquery",
		linkpreview.WithTitle(true),
		linkpreview.WithDescription(true),
		linkpreview.WithImage(true),
		linkpreview.WithFavicon(true),
		linkpreview.WithSiteName(true),
		linkpreview.WithTimeout(10*time.Second),
	)

	// GenerateLinkPreview returns JSON ([]byte) with the requested fields
	data, err := lp.GenerateLinkPreview()
	if err != nil {
		log.Fatalf("preview error: %v", err)
	}

	fmt.Printf("%s\n", data)
}

output

{
  "url": "https://github.com/PuerkitoBio/goquery",
  "title": "PuerkitoBio/goquery",
  "description": "A little like that jQuery you know...",
  "image": "https://opengraph.githubassets.com/...",
  "favicon": "https://github.githubassets.com/favicons/favicon.svg",
  "site_name": "GitHub"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LinkPreview

type LinkPreview struct {
	URL         string
	Title       bool
	Description bool
	Image       bool
	SiteName    bool
	Favicon     bool
	Timeout     time.Duration
	UserAgent   string
}

func New

func New(url string, opts ...Option) *LinkPreview

func (*LinkPreview) GenerateLinkPreview

func (l *LinkPreview) GenerateLinkPreview() ([]byte, error)

type Option

type Option func(*LinkPreview)

func WithDescription

func WithDescription(v bool) Option

func WithFavicon

func WithFavicon(v bool) Option

func WithImage

func WithImage(v bool) Option

func WithSiteName

func WithSiteName(v bool) Option

func WithTimeout

func WithTimeout(d time.Duration) Option

func WithTitle

func WithTitle(v bool) Option

func WithUserAgent

func WithUserAgent(ua string) Option

Jump to

Keyboard shortcuts

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