如何配置 prettier 来格式化 YAML 文件以使用零空格缩进来缩进列表项

问题描述 投票:0回答:3

当我运行

npx prettier --write pipeline.yml
时,它会格式化所有子行,包括破折号 (
-
),并从父行中缩进 2 个空格,如下所示:

resources:
  - name: concourse-examples
    type: git
    icon: github
    check_every: 30m
    source:
      uri: https://github.com/concourse/examples

jobs:
  - name: set-self
    public: true
    plan:
      - get: concourse-examples
        trigger: true
      - set_pipeline: self
        file: concourse-examples/pipelines/set-pipelines.yml

我希望所有子行(除了以破折号(

-
)开头的子行)都从其父行缩进 2 个空格,如下所示:

resources:
- name: concourse-examples
  type: git
  icon: github
  check_every: 30m
  source:
    uri: https://github.com/concourse/examples

jobs:
- name: set-self
  public: true
  plan:
  - get: concourse-examples
    trigger: true
  - set_pipeline: self
    file: concourse-examples/pipelines/set-pipelines.yml

我怎样才能实现这个目标?

yaml prettier
3个回答
10
投票

你不能。引用期权哲学

Prettier 不是一个厨房水槽代码格式化程序,它会尝试以您希望的任何方式打印您的代码。这是固执己见


5
投票

您最好的选择是通过创建一个

.prettierignore
文件并向其中添加以下内容,让 prettier 忽略所有 yml 文件...

*.yml

更多信息:https://prettier.io/docs/en/ignore.html


0
投票

您可以像这样使用

overrides

{
    "singleQuote": true,
    "overrides": [
        {
            "files": "*.yml",
            "options": {
                "singleQuote": false
            }
        }
    ]
}

P.S.:我知道这是一个旧的,但这是谷歌的第一次点击,而且答案确实不令人满意。

© www.soinside.com 2019 - 2024. All rights reserved.