New template formatter under test

See: GitHub - gohugoio/gotmplfmt: Work in progress. · GitHub

The most commonly used formatter for Go templates have been prettier-plugin-go-template, but that one is 1. No longer actively maintained (last commit was 3 years ago), 2. Was a little bit of a hazzle to install and 3. Had some quirks inherited from the Prettier HTML formatter that you either loved or hated …

So, I have spent parts of the weekend continuing the works from others and gotten to a state where I’m pretty happy with the output (I have tested this on all of the Hugo embedded templates + all the Hugo docs templates).

It’s still needs some work/more testing, but I appreciate any feedback if you …

  • Don’t agree with some of the choices I made (and have a convincing argument)
  • Or you find a bug.

Create an issue here: GitHub · Where software is built

For installation, see GitHub - gohugoio/gotmplfmt: Work in progress. · GitHub
For VS COde, see gotmplfmt/vscode/README.md at main · gohugoio/gotmplfmt · GitHub

(I will eventually get it into the VS code extension store)

I tried using gotmplfmt, but compared to prettier-plugin-go-template, I noticed a few unintended formatting changes. Here is an example.

from this

<time datetime="{{ .Date.Format " 2006-01-02T15:04:05JST“ }}”
            >{{ .Date.Format (
              .Site.Params.dateformat |
              default “2006/01/02”)
            }}</time
          >

to this.

<time datetime="{{ .Date.Format " 2006-01-02T15:04:05JST“ }}”
                    >{{ .Date.Format
    (.Site.Params.dateformat | default “2006/01/02”)
}}</time
                    >

If there isn’t enough time to implement this, I thought it might be a good idea to fork prettier-plugin-go-template and rebuild or update it as a separate npm package. Personally, I think developers should be free to choose between tab and space indentation.

By the way, since I have some expertise in this area, I think I could fork it and handle updates and issue responses.

If start out with something, in my eyes, messy like the above, you shouldn’t be surprised that the output from a new formatter would be different and slightly undefined.

I suggest you do some manual reformatting of this one.

Best of luck!