Skip to content

Instantly share code, notes, and snippets.

@addrummond
Last active March 17, 2021 09:10
Show Gist options
  • Save addrummond/49d508d2fefdb4548e1e41367c8214ef to your computer and use it in GitHub Desktop.
Save addrummond/49d508d2fefdb4548e1e41367c8214ef to your computer and use it in GitHub Desktop.
Example HTML lint plug code
defmodule HtmlLintPlug do
require Logger
def init(opts), do: opts
def call(conn, _opts) do
Plug.Conn.register_before_send(conn, &lint/1)
end
defmodule Error do
defexception message: "HTML linter returned errors"
end
defp lint(conn) do
body_string = to_string(conn.resp_body)
result = my_html_linting_function(body_string)
case result do
:ok ->
conn
{:error, error} ->
case Mix.env() do
:dev ->
Logger.warn("#{inspect(error)}")
conn
:test ->
raise Error, message: inspect(error)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment