如何阻止 prettier 格式化 HTML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50261161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I stop prettier from formatting HTML files?
提问by Rachid O
So the problem is that prettier does not format html very well.
所以问题是 prettier 不能很好地格式化 html。
for instance if I have this angular template:
例如,如果我有这个角度模板:
<some-component
some-attribute
[ang-binding1]='someExpr'
[ang-binding2]='someExpr'
(someEvent)='someFunc($event)'>
</some-component>
prettier will format it to something like this:
更漂亮的将其格式化为这样的:
<some-component some-attribute [ang-binding1]='someExpr' [ang-binding2]='someExpr' (someEvent)='someFunc($event)'>
</some-component>
how do I disable prettier formating for html templates ?
如何禁用 html 模板的漂亮格式?
回答by clintoncrick
If you are using VS Code, you can prevent Prettier from running on HTML (or other specific languages) by adding the following to your settings:
如果您使用的是 VS Code,您可以通过将以下内容添加到您的设置中来阻止 Prettier 在 HTML(或其他特定语言)上运行:
"prettier.disableLanguages": ["html"]
"prettier.disableLanguages": ["html"]
You can find other VS Code specific options at the prettier/prettier-vscode
GitHub page.
您可以在prettier/prettier-vscode
GitHub 页面找到其他 VS Code 特定选项。
回答by RJ7
If you would like to retain vscodes html formatter for html
files, but leverage prettier for other files you can set the following in settings.json
.
如果您想为html
文件保留 vscodes html 格式化程序,但对其他文件使用更漂亮的文件,您可以在settings.json
.
"editor.formatOnSave": true,
"[html]": {
"editor.defaultFormatter": "vscode.html-language-features"
}
回答by Kishorevarma
If you are using VSCode, click File> Preferences> Settingsand add "html.format.enable": false,
如果您使用的是 VSCode,请单击文件>首选项>设置并添加"html.format.enable": false,
回答by JayChase
html.format.enablewill turn off the default VS Code formatter. To exclude all html files in a project from being formatted you can add a .prettierignorefile to the project root and ignore all html files.
html.format.enable将关闭默认的 VS Code 格式化程序。要从格式化中排除项目中的所有 html 文件,您可以将.prettierignore文件添加到项目根目录并忽略所有 html 文件。
*.html
回答by baitun
If you use prettier with pre-commit hook (for example, with husky), changing the editor settings will not help you.
You need to add file .prettierignore
with the following content:
如果您使用 prettier 和 pre-commit hook(例如,使用 husky),更改编辑器设置将无济于事。
您需要添加.prettierignore
具有以下内容的文件:
*.html
File format is similar to .gitignore. You can read more here: https://prettier.io/docs/en/ignore.html
文件格式类似于 .gitignore。您可以在此处阅读更多信息:https: //prettier.io/docs/en/ignore.html
回答by JerryGoyal
And in case you want to ignore a specific line to be formatted inside file you can do that via adding prettier-ignore
before the code.
如果您想忽略要在文件中格式化的特定行,您可以通过prettier-ignore
在代码前添加来实现。
<!-- prettier-ignore -->
<div class="x" >hello world</div >
Complete documentation: https://prettier.io/docs/en/ignore.html
回答by jahller
in addition to what was already written you can also disable formatting on save. then you would need to explicitly format the document via CMD/CTRL + P > Format document
除了已经编写的内容之外,您还可以在保存时禁用格式。那么您需要通过以下方式明确格式化文档CMD/CTRL + P > Format document
"[html]": {
"editor.formatOnSave": true
},