linkpreview

package module
v0.0.0-...-ed28db0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2024 License: MIT Imports: 6 Imported by: 1

README

Go Version GoDoc GoReport

Installation

go get github.com/tiendc/go-linkpreview

Usage

Simple one
    result, err := linkpreview.Parse(url)

    // Output:
    // {
    //   "title": <title>,
    //   "description": <description>,
    //   "og_meta": {<open graph metadata>},
    //   "twitter_meta": {<twitter metadata>},
    // }
Parse with configuration
    // Get everything
    result, err := linkpreview.Parse(url,
        linkpreview.ParseOGMeta(true),        // default: true
        linkpreview.ParseTwitterMeta(true),   // default: true
        linkpreview.ParseFavicons(true),      // default: false
        linkpreview.ReturnMetaTags(true),     // default: false
        linkpreview.ReturnLinkTags(true)      // default: false
    )

    // Output:
    // {
    //   "title": <title>,
    //   "description": <description>,
    //   "og_meta": {<open graph metadata>},
    //   "twitter_meta": {<twitter metadata>},
    //   "favicons": [],
    //   "meta_tags": [],
    //   "link_tags": [],
    // }

Contributing

  • You are welcome to make pull requests for new functions and bug fixes.

Authors

License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigOption

type ConfigOption func(*ParserConfig)

func ParseFavicons

func ParseFavicons(flag bool) ConfigOption

func ParseOGMeta

func ParseOGMeta(flag bool) ConfigOption

func ParseTwitterMeta

func ParseTwitterMeta(flag bool) ConfigOption

func ReturnLinkTags

func ReturnLinkTags(flag bool) ConfigOption

func ReturnMetaTags

func ReturnMetaTags(flag bool) ConfigOption

type Image

type Image struct {
	URL    string `json:"url"`
	Type   string `json:"type,omitempty"`
	Width  int    `json:"width,omitempty"`
	Height int    `json:"height,omitempty"`
	Alt    string `json:"alt,omitempty"`
}

type LinkTag

type LinkTag struct {
	Rel        string            `json:"rel,omitempty"`
	Href       string            `json:"href,omitempty"`
	Sizes      string            `json:"sizes,omitempty"`
	Type       string            `json:"type,omitempty"`
	OtherAttrs map[string]string `json:"other_attrs,omitempty"`
}

type MetaTag

type MetaTag struct {
	Name       string            `json:"name,omitempty"`
	Property   string            `json:"property,omitempty"`
	ItemProp   string            `json:"itemprop,omitempty"`
	Content    string            `json:"content,omitempty"`
	Value      string            `json:"value,omitempty"`
	OtherAttrs map[string]string `json:"other_attrs,omitempty"`
}

type OGImage

type OGImage struct {
	URL    string `json:"url"`
	Type   string `json:"type,omitempty"`
	Width  int    `json:"width,omitempty"`
	Height int    `json:"height,omitempty"`
	Alt    string `json:"alt,omitempty"`
}

type OGMeta

type OGMeta struct {
	URL         string     `json:"url,omitempty"`
	Title       string     `json:"title,omitempty"`
	Type        string     `json:"type,omitempty"`
	Description string     `json:"description,omitempty"`
	SiteName    string     `json:"site_name,omitempty"`
	Locale      string     `json:"locale,omitempty"`
	Images      []*OGImage `json:"images,omitempty"`
	Videos      []*OGVideo `json:"videos,omitempty"`
	Others      []*MetaTag `json:"others,omitempty"`
}

type OGVideo

type OGVideo struct {
	URL    string `json:"url"`
	Type   string `json:"type,omitempty"`
	Width  int    `json:"width,omitempty"`
	Height int    `json:"height,omitempty"`
}

type ParserConfig

type ParserConfig struct {
	ParseOGMeta      bool
	ParseTwitterMeta bool
	ParseFavicons    bool

	ReturnMetaTags bool
	ReturnLinkTags bool
}

type ParserContext

type ParserContext struct {
	Link   string
	Config ParserConfig

	MetaTags []*MetaTag
	LinkTags []*LinkTag

	Result ParserResult

	Doc *goquery.Document
}

func (*ParserContext) Parse

func (ctx *ParserContext) Parse() error

type ParserResult

type ParserResult struct {
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`

	OGMeta      *OGMeta      `json:"og_meta,omitempty"`
	TwitterMeta *TwitterMeta `json:"twitter_meta,omitempty"`
	Favicons    []*Image     `json:"favicons,omitempty"`

	MetaTags []*MetaTag `json:"meta_tags,omitempty"`
	LinkTags []*LinkTag `json:"link_tags,omitempty"`
}

func Parse

func Parse(link string, options ...ConfigOption) (ParserResult, error)

func ParseFromReader

func ParseFromReader(link string, data io.Reader, options ...ConfigOption) (ParserResult, error)

type TwitterMeta

type TwitterMeta struct {
	URL         string     `json:"url,omitempty"`
	Card        string     `json:"card,omitempty"`
	Site        string     `json:"site,omitempty"`
	SiteID      string     `json:"site_id,omitempty"`
	Creator     string     `json:"creator,omitempty"`
	CreatorID   string     `json:"creator_id,omitempty"`
	Title       string     `json:"title,omitempty"`
	Description string     `json:"description,omitempty"`
	Image       string     `json:"image,omitempty"`
	ImageAlt    string     `json:"image_alt,omitempty"`
	Others      []*MetaTag `json:"others,omitempty"`
}

Jump to

Keyboard shortcuts

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