- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 gatsby 网站上使用 Netlify CMS。我在我的帖子集合上使用了 netlify CMS 关系小部件来引用我的作者集合的标题字段,所以...
- {label: "Author", name: "author", widget: "relation", collection: "authors", searchFields: ["title", "firstName", "lastName"], valueField: "title"}
效果很好,但是当我在 GraphiQL 窗口中进行查询时,唯一可用的字段是作者,使用单个字符串,如何访问特定作者的所有 frontmatter?
最佳答案
事实证明,在 Netlify 中建立连接只是第一步。然后,您需要配置 gatsby-node.js 文件以在构建时建立这些关系。最终结果允许您对具有嵌套作者的帖子进行查询。这是整个设置。
首先是我在 static/admin 中的 netlifyCMS config.yml
backend:
name: git-gateway
branch: development
media_folder: "static/images/uploads" # Media files will be stored in the repo under static/images/uploads
public_folder: "/images/uploads"
collections:
- name: "blog" # Used in routes, e.g., /admin/collections/blog
label: "Blog" # Used in the UI
folder: "src/pages/blog/posts" # The path to the folder where the documents are stored
create: true # Allow users to create new documents in this collection
slug: "{{year}}-{{month}}-{{day}}-{{slug}}" # Filename template, e.g., YYYY-MM-DD-title.md
fields: # The fields for each document, usually in front matter
- {label: "Layout", name: "layout", widget: "hidden", default: "blog"}
- {label: "Author", name: "author", widget: "relation", collection: "authors", searchFields: ["title", "firstName", "lastName"], valueField: "title"}
- {label: "Title", name: "title", widget: "string"}
- {label: "Publish Date", name: "date", widget: "datetime"}
- {label: "Featured Image", name: "thumbnail", widget: "image"}
- {label: "Body", name: "body", widget: "markdown"}
- name: "authors"
label: "Authors"
folder: "src/authors"
create: true
slug: "{{slug}}"
fields:
- {label: "Layout", name: "layout", widget: "hidden", default: "author"}
- {label: "Full Name", name: "title", widget: "string"}
- {label: "First Name", name: "firstName", widget: "string"}
- {label: "Last Name", name: "lastName", widget: "string"}
- {label: "Position", name: "position", widget: "string"}
- {label: "Profile Picture", name: "profilePicture", widget: "image"}
- {label: "Email", name: "email", widget: "string"}
然后在我的 gatsby-node.js 文件的底部。
exports.sourceNodes = ({ boundActionCreators, getNodes, getNode }) => {
const { createNodeField } = boundActionCreators;
const postsOfAuthors = {};
// iterate thorugh all markdown nodes to link books to author
// and build author index
const markdownNodes = getNodes()
.filter(node => node.internal.type === "MarkdownRemark")
.forEach(node => {
if (node.frontmatter.author) {
const authorNode = getNodes().find(
node2 =>
node2.internal.type === "MarkdownRemark" &&
node2.frontmatter.title === node.frontmatter.author
);
if (authorNode) {
createNodeField({
node,
name: "author",
value: authorNode.id,
});
// if it's first time for this author init empty array for his posts
if (!(authorNode.id in postsOfAuthors)) {
postsOfAuthors[authorNode.id] = [];
}
// add book to this author
postsOfAuthors[authorNode.id].push(node.id);
}
}
});
Object.entries(postsOfAuthors).forEach(([authorNodeId, postIds]) => {
createNodeField({
node: getNode(authorNodeId),
name: "posts",
value: postIds,
});
});
};
最后,您需要在gastby-config.js 中添加映射。
mapping: {
"MarkdownRemark.fields.author": "MarkdownRemark",
"MarkdownRemark.fields.posts": "MarkdownRemark",
}
这使您能够进行像这样的查询...
query AllPosts {
allMarkdownRemark (
filter: { fileAbsolutePath: {regex : "\/posts/"} },
sort: {fields: [frontmatter___date], order: DESC}
){
edges {
node {
fields {
author {
fields {
slug
}
frontmatter {
title
firstName
lastName
profilePicture
}
}
slug
}
frontmatter {
title
date(formatString:"MMM DD 'YY")
thumbnail
layout
}
excerpt
}
}
}
}
关于gatsby - 如何在 Netlify CMS 和 Gatsby 之间建立一对多连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49456106/
我正在尝试将图像的路径作为 Prop 传递给组件 注意:组件和索引页都可以通过相同的路径访问图像。当我将路径作为 Prop 传递时它不起作用 在下面的代码中,我尝试使用 GatbsyImage 和 S
我在 gatsby 中的主要字体是在 index.scss 文件夹中定义的,就像这样在 body 标签中。 body { font-family: "Trebuchet MS", "Helveti
我一直在遵循以下步骤:https://github.com/strapi/gatsby-source-strapi 由于 image >> publicURL 也不起作用,我重新安装了最新版本的 ga
语境 Gatsby , headless CMS 奇迹,旁边是 gatsby-plugin-advanced-sitemap 使生成一个强大的 sitemap.xml文件轻而易举。 该插件支持通过 s
目前,我有两个使用 gatsby.js 开发的站点部署到 example.com 和 blog.example.com。我想创建一个 blog.example.com 的子目录到 example.co
我一直在学习本教程 https://www.gatsbyjs.org/packages/gatsby-image/#art-directing-multiple-images但普通人不可能写出 50
我正在使用 Gatsby 的 createResolver API 将 markdown 转换为 html。它在顶层数据上运行良好。但是,我无法让它在嵌套更深的数组上工作。 这是有效的: functi
当我使用 Gatsby (V2) 路由到新页面时,我正在尝试传递数据 我希望然后能够检索该页面上的数据。这可能吗?我已经研究了很多但没有运气所以我觉得我一定错过了什么......谢谢 最佳答案 如果你
默认情况下,我的 Gatsby 网址类似于 2018-09-06-hexagon 有什么办法可以让他们变成/blog/2018/09/06/hexagon ? 这是我的 gatsby-node.js
我有一个 json 文件和一个包含图像的文件夹 plugins/ └── gatsby-source-cars/ ├── images/ ├── cars.json └── g
在生产环境中运行 gatsby。 16. 在开发机器上工作。 来自服务器的错误: success write out redirect data - 0.002s success Build mani
当我想安装 gatsby starter 时,我在终端中遇到了这些错误。 任何人都知道如何解决它? [4/4] 🔨 Building fresh packages... [6/13] ⠁ shar
我正在创建一个名为“new-app”的项目。成功安装 gatsby 后,我尝试运行命令“gatsby new my-app”。我总是收到一个错误,其中指出: ERROR eating new site
我无法使用 gatsby cli 克隆 gatsby-starter-default.git,因为它使用的“git”url 被我们的防火墙规则阻止 我也尝试将以下内容添加到 git config 但仍
我正在创建一个名为“new-app”的项目。成功安装 gatsby 后,我尝试运行命令“gatsby new my-app”。我总是收到一个错误,其中指出: ERROR eating new site
我按照健全性文档创建了一个 internalLink 类型,并根据有关将 internalLinks 与 graphql api 一起使用的提示,我将其创建为单独的类型,如下所示: export de
我创建了由 Gatsby Cloud(个人试用计划)托管并使用 Contentful 的 Gatsby.js 网站。虽然我没有设置 Robot.txt,但生产站点自动在 HTTP header 处添加
最令人沮丧的部分是我之前有这个工作然后不知何故破坏了它,但我正在使用 gatsby-plugin-sharp 和 gatsby-plugin-image 将照片添加到我的主页并看到这个错误: Gats
我创建了由 Gatsby Cloud(个人试用计划)托管并使用 Contentful 的 Gatsby.js 网站。虽然我没有设置 Robot.txt,但生产站点自动在 HTTP header 处添加
最令人沮丧的部分是我之前有这个工作然后不知何故破坏了它,但我正在使用 gatsby-plugin-sharp 和 gatsby-plugin-image 将照片添加到我的主页并看到这个错误: Gats
我是一名优秀的程序员,十分优秀!