| 編輯器 | 快捷鍵 | 設定檔 | 備註 |
|---|---|---|---|
| VS Code | Shift+Alt+F (Win) / Shift+Option+F (Mac) | .vscode/settings.json + .prettierrc | 內建格式化,推薦 Prettier 擴充 |
| WebStorm/PhpStorm | Ctrl+Alt+L | Settings > Editor > Code Style > HTML | 強大內建,可匯出 .editorconfig |
| Sublime Text | 需安裝 HTML-CSS-JS Prettify | Package Settings | 需 Node 環境 |
| Vim/Neovim | gg=G (內建) 或 neoformat/conform.nvim | .vimrc/init.lua | 搭配 Prettier/LSP |
# 安裝
npm i -D prettier
# 格式化單檔
npx prettier --write index.html
# 格式化整個專案
npx prettier --write "**/*.html"
# 檢查不修改 (CI 用)
npx prettier --check "**/*.html"
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"singleQuote": false,
"bracketSameLine": false,
"htmlWhitespaceSensitivity": "css",
"endOfLine": "lf"
}
Prettier 不強制屬性排序,建議搭配 @html-eslint/parser + eslint-plugin-html 自訂規則,或使用 HTMLHint 規則 attr-order:
"attr-order": [
"id",
"class",
"name",
"data-*",
"src", "href",
"type", "value",
"placeholder",
"disabled", "readonly", "required",
"style",
"on*"
]
# GitHub Actions 範例
- name: Check HTML formatting
run: npx prettier --check "**/*.html"
# Husky + lint-staged (提交前自動格式化)
# package.json
"lint-staged": {
"*.html": ["prettier --write", "git add"]
}
prettier-plugin- 對應插件或 // prettier-ignore。