Skip to content

Commit

Permalink
Add OpenGraph gomponent (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswustenberg authored Oct 3, 2024
1 parent 707d42a commit 47bc539
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
19 changes: 18 additions & 1 deletion gallery.go
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
package template
package gallery

import (
. "github.com/maragudk/gomponents"
. "github.com/maragudk/gomponents/html"
)

// OpenGraph tags for a website.
// Title is always set, og:description, og:image, and og:url only if non-empty.
func OpenGraph(title, description, image, url string) Node {
return Group([]Node{
Meta(Attr("property", "og:type"), Content("website")),
Meta(Attr("property", "og:title"), Content(title)),
If(description != "", Meta(Attr("property", "og:description"), Content(description))),
If(image != "", Meta(Attr("property", "og:image"), Content(image))),
If(url != "", Meta(Attr("property", "og:url"), Content(url))),
})
}
28 changes: 28 additions & 0 deletions gallery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gallery_test

import (
"os"
"testing"

gallery "maragu.dev/gomponents-gallery"
"maragu.dev/gomponents-gallery/internal/assert"
)

func TestOpenGraph(t *testing.T) {
t.Run("can generate OpenGraph tags", func(t *testing.T) {
og := gallery.OpenGraph("Partyhats for everyone!", "Bring your own glitter.", "https://www.example.com/partyhat.jpg", "https://www.example.com/partyhats-for-everyone")
assert.Equal(t,
`<meta property="og:type" content="website"><meta property="og:title" content="Partyhats for everyone!"><meta property="og:description" content="Bring your own glitter."><meta property="og:image" content="https://www.example.com/partyhat.jpg"><meta property="og:url" content="https://www.example.com/partyhats-for-everyone">`,
og)
})
}

func ExampleOpenGraph() {
og := gallery.OpenGraph(
"Partyhats for everyone!",
"Bring your own glitter.",
"https://www.example.com/partyhat.jpg",
"https://www.example.com/partyhats-for-everyone")
_ = og.Render(os.Stdout)
// Output: <meta property="og:type" content="website"><meta property="og:title" content="Partyhats for everyone!"><meta property="og:description" content="Bring your own glitter."><meta property="og:image" content="https://www.example.com/partyhat.jpg"><meta property="og:url" content="https://www.example.com/partyhats-for-everyone">
}
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module template
module maragu.dev/gomponents-gallery

go 1.23
go 1.18

require github.com/maragudk/gomponents v0.21.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/maragudk/gomponents v0.21.0 h1:s0QbrirP8/rH1P4kqN48DN2zjvpk9wHkSqi4+xp99SQ=
github.com/maragudk/gomponents v0.21.0/go.mod h1:nHkNnZL6ODgMBeJhrZjkMHVvNdoYsfmpKB2/hjdQ0Hg=
29 changes: 29 additions & 0 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package assert provides testing helpers.
package assert

import (
"strings"
"testing"

g "github.com/maragudk/gomponents"
)

// Equal checks for equality between the given expected string and the rendered Node string.
func Equal(t *testing.T, expected string, actual g.Node) {
t.Helper()

var b strings.Builder
_ = actual.Render(&b)
if expected != b.String() {
t.Fatalf(`expected "%v" but got "%v"`, expected, b.String())
}
}

// Error checks for a non-nil error.
func Error(t *testing.T, err error) {
t.Helper()

if err == nil {
t.Fatal("error is nil")
}
}

0 comments on commit 47bc539

Please sign in to comment.