我正在使用 Gatsbygatsby-plugin-netlify-cms
将 Netlify CMS 添加到网站,netlify-cms-backend-fs
以允许 Netlify CMS 使用文件系统而不是远程仓库在开发模式下工作,并gatsby-transformer-yaml
转换 yaml 文件以便可以通过 GraphQL 查询访问它。
以下是相关代码:
// src/cms/cms.js
import CMS from "netlify-cms-app";
/** Netlify CMS config */
const config = {
load_config_file: false,
media_folder: "static/img",
public_folder: "img",
collections: [
{
label: "Business Details",
name: "business",
files: [
{
label: "Contact Information",
name: "contactInfo",
file: "data/business.yaml",
fields: [
{ label: "Title", name: "title" },
{ label: "Business Name", name: "name" },
{ label: "Phone Number", name: "phone" },
{ label: "Email", name: "email" }
]
}
]
}
]
};
/** Change to file system backend for development */
if (process.env.NODE_ENV === "development") {
const FileSystemBackend = require("netlify-cms-backend-fs");
config.backend = {
name: "file-system",
api_root: "/api"
};
config.display_url = "http://localhost:8000";
CMS.registerBackend("file-system", FileSystemBackend);
} else {
config.backend = {
backend: {
name: "test-repo" // To be changed to repo when ready for deployment
}
};
}
CMS.init({ config });
我能够访问 Netlify CMS 仪表板、创建新条目并发布它。然而,当它发布时,它的内容data/business.yaml
是:
title: Business Details
name: My Business Name
phone: 555-5555
email: me@example.com
这应该被格式化为(注意连字符和缩进):
- title: Business Details
name: My Business Name
phone: 555-5555
email: me@example.com
结果是我无法使用以下方法查询此数据:
query MyQuery {
allBusinessYaml {
edges {
node {
email
name
phone
}
}
}
}
因为allBusinessYaml
不存在。
如果我手动将data/business.yaml
文件更改为带有连字符和缩进的正确格式,此错误将得到解决。
我尝试将小部件设置为object
or list
,并尝试显式设置format
and extension
,yaml
但这也不能解决问题。例如
const config = {
load_config_file: false,
media_folder: "static/img",
public_folder: "img",
collections: [
{
label: "Business Details",
name: "business",
files: [
{
label: "Contact Information",
name: "contactInfo",
file: "data/business.yaml",
format: "yaml",
extension: "yaml",
widget: "list", // NOTE: I've also tried using "object" here
fields: [
{ label: "Title", name: "title" },
{ label: "Business Name", name: "name" },
{ label: "Phone Number", name: "phone" },
{ label: "Email", name: "email" }
]
}
]
}
]
};
我在这里做错了什么?