我们应该转义还是不转义 YAML 文件中 HTML 标签中的双引号?

Should we escape or not escape the double quotes living in an HTML tag inside a YAML file?

我对是否应该转义 YAML 文件中 HTML 标记内的双引号感到有点困惑。

playing_with_cards_html: Playing your best game with %{card_type} between now and <span class="best-card">%{worst_card}</span>.

它看起来是一个有效的 YAML,但如果我们为双引号添加转义字符:

playing_with_cards_html: Playing your best game with %{card_type} between now and <span class=\"best-card\">%{worst_card}</span>.

它还能正确呈现吗?

它可能无法正确呈现,除非加载 YAML 的程序对加载的字符串进行一些反斜杠转义。

playing_with_cards_html 的值为 plain scalar。 在这样的标量中,没有定义反斜杠转义:每个字符都按原样使用。因此 URL 的第二个版本实际上有两个额外的字符(都是 \),并且可能不会按您想要的方式呈现。

YAML 中的反斜杠转义仅在标量本身为 double quoted 时才有可能(即以双引号开头和结尾)。在这种情况下,您 必须 使用反斜杠转义中间标量中的任何双引号。