This cost me some time googling and fiddling around with my config, so here is
a quick setup on how to get Go’s html/template
format working with neovim.
This template syntax is also used by Hugo (which I use for this page).
First, install a formatter which is able to format Go’s template syntax:
npm install --save-dev prettier prettier-plugin-go-template
To tell prettier to use this plugin for the current folder (and subfolders),
use the following .prettierrc
file:
{
"plugins": [
"prettier-plugin-go-template"
],
"overrides": [
{
"files": [
"*.html"
],
"options": {
"parser": "go-template"
}
}
]
}
Now all thats left is tell neovim to run prettier when formatting. This depends on the formatting setup you are using. Because I am using conform.nvim, I configured it to use prettier by default for HTML:
return {
"stevearc/conform.nvim",
event = { "BufReadPre", "BufNewFile" },
cmd = { "ConformInfo" },
keys = {
-- [...]
},
opts = {
formatters_by_ft = {
templ = { "templ" },
go = { "goimports", "gofmt" },
html = { "prettier" }
},
format_on_save = { timeout_ms = 500, lsp_format = "fallback" },
},
}
Thats it! Saving the file will now autoformat with the correct formatting for
Go’s text/html
templates.