-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
6 changed files
with
160 additions
and
2 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
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
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,80 @@ | ||
defmodule PhxToolsWeb.SeoMetaTagComponent do | ||
@moduledoc false | ||
|
||
use PhxToolsWeb, :html | ||
|
||
alias PhxToolsWeb.Endpoint | ||
|
||
@type assigns :: map() | ||
@type rendered :: rendered() | ||
|
||
@default_description """ | ||
Phx.tools is a shell script for Linux and macOS that configures | ||
the development environment for you in a few easy steps. | ||
Once you finish running the script, you'll be able to start the database server, | ||
create a new Phoenix application, and launch the server... | ||
""" | ||
|
||
attr :attributes, :map | ||
|
||
@spec seo_meta_tags(assigns()) :: rendered() | ||
def seo_meta_tags(assigns) do | ||
assigns = | ||
assigns | ||
|> assign_new(:description, fn -> String.trim(@default_description) end) | ||
|> assign_new(:image_url, fn -> static_url(Endpoint, ~p"/images/phx_tools.png") end) | ||
|> assign_new(:url, fn | ||
%{attributes: %{url: url}} -> url | ||
_assigns -> url(~p"/") | ||
end) | ||
|
||
~H""" | ||
<.open_graph_meta_tags description={@description} image_url={@image_url} url={@url} /> | ||
<.other_meta_tags | ||
description={@description} | ||
keywords="elixir, erlang, homebrew, mise, phoenix, postgres, postgresql" | ||
/> | ||
<.twitter_meta_tags description={@description} image_url={@image_url} url={@url} /> | ||
""" | ||
end | ||
|
||
attr :description, :string, required: true | ||
attr :image_url, :string, required: true | ||
attr :url, :string, required: true | ||
|
||
defp open_graph_meta_tags(assigns) do | ||
~H""" | ||
<meta property="og:description" content={@description} /> | ||
<meta property="og:image" content={@image_url} /> | ||
<meta property="og:title" content="The Complete Development Environment for Elixir and Phoenix" /> | ||
<meta property="og:type" content="website" /> | ||
<meta property="og:url" content={@url} /> | ||
""" | ||
end | ||
|
||
attr :description, :string, required: true | ||
attr :keywords, :string, required: true | ||
|
||
defp other_meta_tags(assigns) do | ||
~H""" | ||
<meta property="description" content={@description} /> | ||
<meta name="keywords" content="elixir, erlang, homebrew, mise, phoenix, postgres, postgresql" /> | ||
""" | ||
end | ||
|
||
attr :description, :string, required: true | ||
attr :image_url, :string, required: true | ||
attr :url, :string, required: true | ||
|
||
defp twitter_meta_tags(assigns) do | ||
~H""" | ||
<meta name="twitter:card" content="summary_large_image" /> | ||
<meta name="twitter:description" content={@description} /> | ||
<meta name="twitter:image" content={@image_url} /> | ||
<meta name="twitter:site" content="@optimumBA" /> | ||
<meta name="twitter:title" content="The Complete Development Environment for Elixir and Phoenix" /> | ||
<meta name="twitter:type" content="website" /> | ||
<meta name="twitter:url" content={@url} /> | ||
""" | ||
end | ||
end |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions
60
test/phx_tools_web/components/seo_meta_tag_component_test.exs
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,60 @@ | ||
defmodule PhxToolsWeb.SeoMetaTagComponentTest do | ||
use PhxToolsWeb.ConnCase, async: true | ||
|
||
import Phoenix.LiveViewTest | ||
|
||
alias PhxToolsWeb.SeoMetaTagComponent | ||
|
||
describe "seo_meta_tags/1" do | ||
test "renders meta tags with default values" do | ||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta name=\"twitter:card\" content=\"summary_large_image\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta name=\"twitter:description\" content=\"Phx.tools is a shell script for Linux and macOS that configures\nthe development environment for you in a few easy steps.\nOnce you finish running the script, you'll be able to start the database server,\ncreate a new Phoenix application, and launch the server...\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta name=\"twitter:image\" content=\"http://localhost:4002/images/phx_tools.png\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta name=\"twitter:site\" content=\"@optimumBA\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta name=\"twitter:title\" content=\"The Complete Development Environment for Elixir and Phoenix\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta name=\"twitter:type\" content=\"website\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta name=\"twitter:url\" content=\"http://localhost:4002/\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta property=\"description\" content=\"Phx.tools is a shell script for Linux and macOS that configures\nthe development environment for you in a few easy steps.\nOnce you finish running the script, you'll be able to start the database server,\ncreate a new Phoenix application, and launch the server...\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta property=\"og:description\" content=\"Phx.tools is a shell script for Linux and macOS that configures\nthe development environment for you in a few easy steps.\nOnce you finish running the script, you'll be able to start the database server,\ncreate a new Phoenix application, and launch the server...\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta property=\"og:image\" content=\"http://localhost:4002/images/phx_tools.png\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta property=\"og:title\" content=\"The Complete Development Environment for Elixir and Phoenix\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta property=\"og:type\" content=\"website\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: nil) =~ | ||
"<meta property=\"og:url\" content=\"http://localhost:4002/\">" | ||
end | ||
|
||
test "renders meta tags with the values passed" do | ||
assigns = %{url: "http://sample.link"} | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: assigns) =~ | ||
"<meta name=\"twitter:url\" content=\"http://sample.link\">" | ||
|
||
assert render_component(&SeoMetaTagComponent.seo_meta_tags/1, attributes: assigns) =~ | ||
"<meta property=\"og:url\" content=\"http://sample.link\">" | ||
end | ||
end | ||
end |