opengraph

package
v2.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package opengraph implements a way to represent data in the Open Graph protocol as typed Go structs, and render it as HTML meta tags. Typically, this is used to customise how a page looks when shared on Facebook or LinkedIn.

This is not a parser.

Note: currently only the Article and Website types are implemented. Generally, besides the use-case of sharing on Facebook or LinkedIn, you should avoid relying on the format for anything complicated. It has poor support for internationalisation, the protocol itself is archived, and the Open Web Foundation appears moribund.

Example (Article)
package main

import (
	"os"
	"time"

	"github.com/tawesoft/golib/v2/meta/opengraph"
	"github.com/tawesoft/golib/v2/must"
)

func main() {
	article := opengraph.Object{
		SiteName:    "My Site",
		Title:       "Top 10 reasons why I love my cat",
		Description: "My cat can even eat a whole watermelon.",
		Url:         "https://www.example.org/articles/my-cat",
		Locale:      "en-GB",
		Media: []opengraph.Media{
			{
				Type:   opengraph.MediaTypeImage,
				Url:    "https://www.example.org/media/cat-photos/cat1.jpg",
				Mime:   "image/jpeg",
				Width:  1024,
				Height: 768,
			},
			{
				Type:   opengraph.MediaTypeImage,
				Url:    "https://www.example.org/media/cat-photos/cat2.jpg",
				Mime:   "image/jpeg",
				Width:  1024,
				Height: 768,
			},
			{
				Type: opengraph.MediaTypeAudio,
				Url:  "https://www.example.org/media/cat-photos/purr.ogg",
				Mime: "audio/ogg",
			},
			{
				Type: opengraph.MediaTypeVideo,
				Url:  "https://www.example.org/media/cat-photos/hunting-toy.ogv",
				Mime: "video/ogg",
			},
		},
		Type: opengraph.ObjectTypeArticle,
		Article: opengraph.ObjectArticle{
			Published: time.Date(2022, 10, 31, 13, 17, 0, 0, must.Result(time.LoadLocation("Europe/London"))),
			Authors: []opengraph.Profile{
				{
					Url:       "https://www.example.org/authors/hopperg",
					FirstName: "Grace",
					LastName:  "Hopper",
				},
			},
			Section: "opinion",
			Tags:    []string{"cats", "pets", "cute"},
		},
	}

	must.Result(os.Stdout.WriteString(`<!doctype html>
<html lang="en-gb">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Open Graph example</title>
`))
	must.Check(opengraph.HTML(os.Stdout, article))
	must.Result(os.Stdout.WriteString(`</head><body>Test!</body></html>`))
}
Output:

Index

Examples

Constants

View Source
const (
	ObjectTypeArticle = "article"
	ObjectTypeWebsite = "website"
)
View Source
const (
	MediaTypeAudio = MediaType("audio")
	MediaTypeImage = MediaType("image")
	MediaTypeVideo = MediaType("video")
)

Variables

This section is empty.

Functions

func HTML

func HTML(wr io.Writer, object Object) error

HTML renders an Open Graph object as HTML.

If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer.

Types

type Media

type Media struct {
	Type MediaType
	Url  string // should always start with https://
	Mime string // e.g. image/jpeg, audio/mpeg, etc.

	// Alternate text (for accessibility reasons). May not work how you expect.
	// Leave blank if the parent object's title is a good description.
	Alt string

	// If Type field is MediaTypeImage or MediaTypeVideo
	Width  int // in pixels
	Height int // in pixels
}

type MediaType

type MediaType string

type Object

type Object struct {
	// If your object is part of a larger website, the name which should be
	// displayed for the overall site. e.g., "IMDb".
	SiteName string

	// The title of your object as it should appear within the graph, e.g.,
	// "The Rock".
	Title string

	// A one to two sentence description of your object.
	Description string

	// The canonical URL of your object that will be used as its permanent ID in
	// the graph, e.g., "https://www.imdb.com/title/tt0117500/".
	Url string

	// The locale these tags are marked up in. Of the format language_TERRITORY.
	// Default is en_US.
	Locale string

	// An array of media files that represent the content.
	Media []Media

	// The type of your object, e.g., "video.movie". Depending on the type you
	// specify, other properties may also be required. If blank, defaults to
	// "website".
	Type ObjectType

	// Discriminated by Type field
	Article ObjectArticle
}

type ObjectArticle

type ObjectArticle struct {
	Published time.Time // When the article was first published.
	Modified  time.Time // When the article was last changed.
	Expires   time.Time // When the article is out of date after.
	Authors   []Profile // Writers of the article. Also consider setting meta name="author".
	Section   string    // A high-level section name. E.g. Technology
	Tags      []string  // e.g. []string{"environment", "science"}...
}

type ObjectType

type ObjectType string

type Profile

type Profile struct {
	Url       string
	FirstName string
	LastName  string
}

Jump to

Keyboard shortcuts

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