免费 · 开发
代码格式化工具(Prettier).
在浏览器中用 Prettier 格式化 JavaScript、TypeScript、JSX、JSON、CSS、SCSS、Less、HTML、Vue、Markdown、YAML 和 GraphQL,并实时预览。
Formatting engine: Prettier 3 (MIT), running in your browser. Formatting changes layout only, never what the code does; comments are kept.
用 Prettier 格式化代码
- 把代码粘贴到
Your code,或点击Open a file。 - 保留
Detect automatically或选择语言:JavaScript、JSX、TypeScript、JSON、CSS、SCSS、Less、HTML、Vue、Markdown、YAML 或 GraphQL。 - 调整行宽和缩进;对于 JavaScript 和 TypeScript 还可设置分号、引号和尾随逗号。结果会随输入实时更新。
- 点击
Copy result、Download或Replace my code。
示例:const a={b:1,c:[1,2,3]};function f(x){return x*2} 会变成单行对象和使用两个空格缩进的函数块,每条语句都以分号结尾。
格式化会改变什么
Prettier 重写空格、换行、缩进、引号和逗号,使代码遵循统一的风格。它绝不会改变代码的行为,注释也会保留。如果代码有语法错误,则不会格式化,提示会给出行号和列号,例如 (1:10)。
如需针对单一语言的专门选项,请使用 JSON 格式化工具或 SQL 格式化工具;如需压缩生产环境的 JavaScript,请使用 JavaScript 压缩工具。
限制与支持格式
输入内容在您的设备上处理。
英语技术说明
Files up to 2 MB. Code with syntax errors is not formatted; the error shows the line and column. Formatting changes layout only, never behaviour; comments are kept.
Uses Prettier 3 standalone (MIT) with only the parsers the chosen language needs, loaded on demand in your browser. Same output rules as Prettier on the command line for the options shown.
const a={b:1,c:[1,2,3]};function f(x){return x*2} becomes const a = { b: 1, c: [1, 2, 3] }; followed by a function block indented with two spaces.
常见问题
我的代码会被上传吗?
不会。Prettier 在浏览器中运行,只加载所选语言需要的解析器。
结果与我电脑上的 Prettier 一样吗?
对于这里显示的选项是一样的:这就是 Prettier 3 本身,只是没有项目配置文件。
为什么我的代码没有被格式化?
解析器发现了语法错误。请修正提示中给出的行和列,或确认所选语言是否正确。