You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决11ty文件经Netlify CMS编辑发布后Frontmatter双引号丢失问题

解决Netlify CMS编辑11ty文件时Frontmatter双引号丢失问题

方案1:全局配置YAML强制添加双引号

在Netlify CMS的admin/config.yml配置文件中,为目标集合添加YAML序列化选项,强制所有字符串字段使用双引号:

collections:
  - name: "posts" # 替换为你的集合名称
    label: "文章"
    folder: "_posts" # 替换为你的文件存储目录
    create: true
    slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
    # 配置Frontmatter格式及YAML选项
    frontmatter_format: yaml
    frontmatter_delimiter: "---"
    yaml_options:
      quotes: true # 开启强制引号
      quote_type: double # 指定使用双引号
    fields:
      - { label: "标题", name: "title", widget: "string" }
      - { label: "描述", name: "description", widget: "string" }
      - { label: "规范URL", name: "canonical", widget: "string" }

这个配置会让Netlify CMS在保存文件时,自动给所有字符串类型的Frontmatter字段加上双引号,无需手动输入,彻底避免无引号导致的YAML语法错误。

方案2:针对特定字段自定义序列化逻辑

如果只需要给部分字段添加双引号,可以为对应widget配置自定义的serialize和deserialize函数:

collections:
  - name: "posts"
    # 其他集合配置...
    fields:
      - label: "标题"
        name: "title"
        widget: "string"
        # 序列化:保存时给值套双引号
        serialize: function(value) {
          return `"${value}"`;
        }
        # 反序列化:编辑时移除首尾双引号,保持编辑框显示正常
        deserialize: function(value) {
          return value.replace(/^"|"$/g, '');
        }
      - { label: "描述", name: "description", widget: "string" }
      - { label: "规范URL", name: "canonical", widget: "string" }

这种方式更灵活,适合只需要为含特殊字符(如冒号、逗号)的字段添加引号的场景。

注意事项

  • 确保你的Netlify CMS版本在2.10.0以上,yaml_options是较新版本才支持的配置项。
  • 若使用自定义序列化函数,注意处理值中的转义字符(比如内容本身包含双引号时,需要转义为\")。

内容的提问来源于stack exchange,提问作者Hari Prasath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 06:08:26