有 5 6 NINE(或 63*,取决于如何你算)在 YAML 中编写多行字符串的不同方法。
TL;DR
-
大多数时候使用>:内部换行符被去掉了,尽管最后你得到了一个:
key: >
Your long
string here.
-
如果您希望将这些换行符保留为 \n(例如,带有段落的嵌入式降价),请使用 |。
key: |
### Heading
* Bullet
* Points
-
如果您不想在末尾附加换行符,请改用 >- 或 |-。
-
如果您需要在单词中间分割行或想要将换行符按字面意思输入为\n,请使用"...":
key: "Antidisestab\
lishmentarianism.\n\nGet on it."
-
YAML 太疯狂了。
块标量样式(>、|)
这些允许\ 和" 等字符不转义,并在字符串末尾添加新行(\n)。
> Folded style 删除字符串中的单个换行符(但在末尾添加一个,并将双换行符转换为单个换行符):
Key: >
this is my very very very
long string
→this is my very very very long string\n
保留额外的前导空格并导致额外的换行符。请参阅下面的注释。
建议:使用这个。通常这就是你想要的。
| Literal style
将字符串中的每个换行符转换为文字换行符,并在末尾添加一个:
Key: |
this is my very very very
long string
→this is my very very very\nlong string\n
这是来自YAML Spec 1.2的官方定义
标量内容可以用块表示法编写,使用文字样式(用“|”表示),其中所有换行符都很重要。或者,它们可以用折叠样式(用“>”表示)编写,其中每个换行符都折叠到一个空格,除非它以空行或缩进更多的行结尾。
建议:使用它来插入 格式化文本(尤其是 Markdown)作为值。
带有块咀嚼指示器的块样式(>-、|-、>+、|+)
您可以通过添加 block chomping indicator 字符来控制字符串中最后一个新行以及任何尾随空行 (\n\n) 的处理:
-
>, |: "clip": 保持换行,去掉尾随的空行。
-
>-, |-: "strip": 去掉换行,去掉尾随的空行。
-
>+, |+: "keep": 保持换行,保持尾随空行。
“流”标量样式( 、"、')
这些具有有限的转义,并构造一个没有换行符的单行字符串。它们可以与键在同一行开始,也可以先使用额外的换行符,这些换行符被剥离。双倍换行符变成一个换行符。
plain style(不能转义,不能 #或: 组合,第一个字符不能是"、'或许多其他标点符号):
Key: this is my very very very
long string
建议:避免。可能看起来很方便,但您可能会因不小心使用禁止的标点符号并触发语法错误而自取其辱。
double-quoted style(\ 和" 必须用\ 转义,换行符可以用文字\n 序列插入,行可以不带空格并尾随@ 987654377@):
Key: "this is my very very \"very\" loooo\
ng string.\n\nLove, YAML."
→"this is my very very \"very\" loooong string.\n\nLove, YAML."
建议:在非常具体的情况下使用。这是您可以在不添加空格的情况下跨行打破非常长的标记(如 URL)的唯一方法。也许在中间添加换行符很有用。
single-quoted style(文字'必须加倍,没有特殊字符,可能对表达以双引号开头的字符串有用):
Key: 'this is my very very "very"
long string, isn''t it.'
→"this is my very very \"very\" long string, isn't it."
建议:避免。好处很少,主要是不便。
带有缩进指示符的块样式
如果以上内容对您来说还不够,您可以添加一个“block indentation indicator”(在您的块咀嚼指示器之后,如果您有的话):
- >8
My long string
starts over here
- |+1
This one
starts here
注意:折叠样式中的前导空格 (>)
如果您在折叠样式的非第一行的开头插入额外的空格,它们将被保留,并带有一个额外的换行符。 (流样式不会发生这种情况。)Section 6.5:
此外,折叠不适用于包含前导空格的文本行周围的换行符。请注意,这样一个缩进较多的行可能只包含这样的前导空格。
- >
my long
string
many spaces above
- my long
string
many spaces above
→["my long\n string\n \nmany spaces above\n","my long string\nmany spaces above"]
总结
在此表中,_ 表示space character。 \n 表示“换行符”(JavaScript 中的 \n),“其他功能”下除外。 “前导空格”在第一行之后应用(建立缩进)
|
> |
| |
|
" |
' |
>- |
>+ |
|- |
|+ |
| Spaces/newlines converted as: |
|
|
|
|
|
|
|
|
|
| Trailing space → |
_ |
_ |
|
|
|
_ |
_ |
_ |
_ |
| Leading space → |
\n_ |
\n_ |
|
|
|
\n_ |
\n_ |
\n_ |
\n_ |
| Single newline → |
_ |
\n |
_ |
_ |
_ |
_ |
_ |
\n |
\n |
| Double newline → |
\n |
\n\n |
\n |
\n |
\n |
\n |
\n |
\n\n |
\n\n |
| Final newline → |
\n |
\n |
|
|
|
|
\n |
|
\n |
| Final double newline → |
|
|
|
|
|
|
\n\n |
|
\n\n |
| How to create a literal: |
|
|
|
|
|
|
|
|
|
| Single quote |
' |
' |
' |
' |
'' |
' |
' |
' |
' |
| Double quote |
" |
" |
" |
\" |
" |
" |
" |
" |
" |
| Backslash |
\ |
\ |
\ |
\\ |
\ |
\ |
\ |
\ |
\ |
| Other features |
|
|
|
|
|
|
|
|
|
In-line newlines with \n
|
? |
? |
? |
✅ |
? |
? |
? |
? |
? |
Spaceless newlines with \
|
? |
? |
? |
✅ |
? |
? |
? |
? |
? |
# or : in value |
✅ |
✅ |
? |
✅ |
✅ |
✅ |
✅ |
✅ |
✅ |
Can start on same line as key |
? |
? |
✅ |
✅ |
✅ |
? |
? |
? |
? |
示例
注意“空格”前行的尾随空格。
- >
very "long"
'string' with
paragraph gap, \n and
spaces.
- |
very "long"
'string' with
paragraph gap, \n and
spaces.
- very "long"
'string' with
paragraph gap, \n and
spaces.
- "very \"long\"
'string' with
paragraph gap, \n and
s\
p\
a\
c\
e\
s."
- 'very "long"
''string'' with
paragraph gap, \n and
spaces.'
- >-
very "long"
'string' with
paragraph gap, \n and
spaces.
[
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.\n",
"very \"long\"\n'string' with\n\nparagraph gap, \\n and \nspaces.\n",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces."
]
*2 块样式,每个都有 2 个可能的块压缩指示符(或没有),以及 9 个可能的缩进指示符(或没有),1 个普通样式和 2 个引用样式:2 x (2 + 1 ) x (9 + 1) + 1 + 2 = 63
其中一些信息也已汇总here。