Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement Phoenix.HTML.Safe protocol #5

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Be sure to use your own project's module name in place of `YourAppWeb`.

```html
<script type="importmap">
<%= raw PhoenixImportmap.importmap(YourAppWeb.Endpoint) %>
<%= PhoenixImportmap.importmap(YourAppWeb.Endpoint) %>
</script>
<script type="module">
import 'app';
Expand Down
7 changes: 4 additions & 3 deletions lib/phoenix_importmap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule PhoenixImportmap do

```html
<script type="importmap">
<%= raw PhoenixImportmap.importmap(YourAppWeb.Endpoint) %>
<%= PhoenixImportmap.importmap(YourAppWeb.Endpoint) %>
</script>
<script type="module">
import 'app';
Expand Down Expand Up @@ -87,14 +87,15 @@ defmodule PhoenixImportmap do
alias PhoenixImportmap.Importmap

@doc """
Returns a JSON-formatted importmap based on your application configuration.
Returns a `t:PhoenixImportmap.Importmap` struct that implements the `Phoenix.HTML.Safe` protocol, allwoing safe interpolation in your template.
hdahlheim marked this conversation as resolved.
Show resolved Hide resolved

The resulting JSON-formatted importmap is based on your application configuration.

Requires `YourAppWeb.Endpoint` to be passed in for path generation.
"""
def importmap(endpoint) do
application_importmap()
|> Importmap.prepare(endpoint)
|> Importmap.json()
end

@doc """
Expand Down
15 changes: 14 additions & 1 deletion lib/phoenix_importmap/importmap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ defmodule PhoenixImportmap.Importmap do

alias PhoenixImportmap.Asset

@derive Jason.Encoder
defstruct [:imports]

@type t() :: %__MODULE__{
imports: map()
}

@doc """
Copies importmap assets to `:copy_destination_path`.
"""
Expand Down Expand Up @@ -46,7 +53,7 @@ defmodule PhoenixImportmap.Importmap do
- Uses `YourAppWeb.Endpoint.static_path/1` to determine whether to use digest URLs.
"""
def prepare(importmap = %{}, endpoint) do
%{
%__MODULE__{
imports:
importmap
|> Enum.reduce(%{}, fn {specifier, path}, acc ->
Expand All @@ -58,4 +65,10 @@ defmodule PhoenixImportmap.Importmap do
end)
}
end

defimpl Phoenix.HTML.Safe do
def to_iodata(importmap) do
Jason.encode_to_iodata!(importmap)
end
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule PhoenixImportmap.MixProject do
[
{:file_system, "~> 1.0"},
{:jason, "~> 1.4.4"},
{:phoenix_html, "~> 4.1"},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
]
end
Expand Down
7 changes: 6 additions & 1 deletion test/phoenix_importmap_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ defmodule PhoenixImportmapTest do
end

test "importmap" do
assert PhoenixImportmap.importmap(MockEndpoint) ==
html_escaped_string =
PhoenixImportmap.importmap(MockEndpoint)
|> Phoenix.HTML.html_escape()
|> Phoenix.HTML.safe_to_string()

assert html_escaped_string ==
"{\"imports\":{\"remote\":\"https://cdn.es6/package.js?busted=t\",\"app\":\"/assets/app.js?busted=t\"}}"
end
end
Loading