generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
707d42a
commit 47bc539
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |