Use mix format for arbitrary HTML files

I really like the formatter, was one of the best additions to the Elixir code base ever, and with the support for .heex files, it became my defacto standard formatter. My question now as I tinkered with raw HTML, is there a way to formatted raw (valid) HTML? Running mix format test.html just does nothing. I guess this happens as the formatter does not know about heex files without the proper plugin Phoenix.LiveView.HTMLFormatter, but introducing the plugin also requires me to have a mix project :disappointed:

Any suggestions?

did you try renaming the filename to test.heex, running the formatter and then renaming it back to test.html ?

Formatters follow this contract mix format — Mix v1.16.1

I think you can write a custom formatter that mostly delegates to the Phoenix one (untested):

defmodule HTMLFormatter do
  @behaviour Mix.Tasks.Format

  @impl true
  def features(_options) do
    [sigils: [:H], extensions: [".heex", ".html"]]

  @impl true
  defdelegate format(source, options), to: Phoenix.LiveView.HTMLFormatter

edit: oh, if you’d rather not need to have a Mix project, then it might be pretty tricky.

1 Like

Maybe a single .exs file and Mix.install?