Prettier
prettier-plugin-zamn formats zamn with Prettier 3: .zamn files, and <template lang="zamn"> in Vue components.
bun add -d prettier prettier-plugin-zamn// .prettierrc
{ "plugins": ["prettier-plugin-zamn"] }bunx prettier --write "src/**/*.{zamn,vue}"Options
| Option | Values | |
|---|---|---|
zamnMaxAttributesPerLine | 3 (default), or any number | up to that many attributes stay on the line of the tag; more go one per line |
0 | always one per line | |
-1 | as written: on one line, or one per line if the (...) spans lines | |
false | always on the line of the tag | |
zamnAttributeSeparator | "space" (default) | a(href="/" target="_blank") |
"comma" | a(href="/", target="_blank") |
singleQuote, printWidth, tabWidth and useTabs apply as well.
zamnMaxAttributesPerLine: false can only be set in a config file or through the API: on the command line, --zamn-max-attributes-per-line takes a number.
// prettier.config.ts
/// <reference types="prettier-plugin-zamn" />
import type { Config } from "prettier";
export default {
plugins: ["prettier-plugin-zamn"],
zamnMaxAttributesPerLine: 3,
zamnAttributeSeparator: "space",
} satisfies Config;What it does
- Puts one statement on each line, and the children of an element in a
{ }block: the{ends the line of the tag, and the}goes on a line of its own. A block written on one line (ul { li { a x } }) is printed over several. - Indents every block with
tabWidth/useTabs. Indentation means nothing to zamn, so this never changes the output, and it fixes misleading indentation. - Writes an element whose only child is a line of text as
tag text:p { | Hello }andp { |{ Hello } }becomep Hello.p: | Helloandp: |{ Hello }stay as written. - Keeps
| textlines and a bare|(a space) as they are. Text is trimmed at both ends, so the whitespace after a tag or a|becomes one space. Text statements that share a line stay on it. - Keeps a
|{ }written over several lines a block, with its lines indented one level deeper and the}on a line of its own. zamn strips the indentation the lines have in common, so the text doesn't change; deeper indentation stays. - Indents the content of
scriptandstyleone level deeper, with/scriptor/styleat the level of the tag, and changes nothing else in it. - Copies text, mustaches, escapes (
\{,\},\#{),= exprand comments as written. A block comment over several lines is kept exactly as it is, since a/*! */goes into the HTML. A comment written after a{or a}on the line of other statements goes on a line of its own. - Writes quoted values with the quotes
singleQuoteasks for, unless the other quote needs fewer escapes. Unquoted values (href=/docs,:class={ on: x },style={color: red}) stay as written. Attributes are printed with the separatorzamnAttributeSeparatorasks for. - Puts attributes one per line when there are more of them than
zamnMaxAttributesPerLine, or when the tag, ids, classes and attributes don't fit inprintWidth. Text after the tag doesn't count and is never wrapped. With-1andfalse, the print width is not looked at. An inline element#{...}always stays on its line, attributes and all, and a value that spans several lines puts every attribute on its own. - Keeps at most one blank line between statements, and writes an empty block as
{}. - Removes whitespace at the end of lines, which means nothing in zamn.
Before:
section.hero { h1 Welcome #{b back}
p Some text
b bold
input(type="text" required name="login" :class="{ error: failed }")
script
go()
/script
}After:
section.hero {
h1 Welcome #{b back}
p Some text
b bold
input(
type="text"
required
name="login"
:class="{ error: failed }"
)
script
go()
/script
}The b was never inside the p: formatting puts it where zamn reads it, and the warning goes away.
The output doesn't change
Formatting never changes the compiled HTML, with one exception: the continuation lines of an attribute value that spans several lines are re-indented with the attribute.
div(
:class="{
active: isActive
}"
@click="go"
)div(
:class="{
active: isActive
}"
@click="go"
)Errors
A template with errors is not formatted: Prettier reports the first error as a syntax error, with its position. Warnings don't stop it.
In VS Code
The Prettier extension loads prettier.config.ts once and keeps using that copy: Node caches imported modules, and the extension can't clear that cache. After editing the config, run "Developer: Restart Extension Host" (or reload the window). The Prettier output channel shows the options it actually used. The command line always reads the current file.
