gpt4 book ai didi

Gatsby v2 - 使用 Markdown 为失败的页面构建静态 HTML 时出错

转载 作者:行者123 更新时间:2023-12-02 02:08:46 25 4
gpt4 key购买 nike

GraphiQL filesystem

这是我第一次尝试使用 Gatsby v2。我有一个由 netlify cms 创建的 test.md 文件,该文件位于博客文件夹中,如您在上面的文件系统中看到的那样。

我还可以在浏览器中使用 GraphiQL 查询数据,如上所述。

我得到了一个博客文章列表(1),它是一个链接,但没有生成可供访问的页面。当我运行 gatsby build 时,出现错误。

错误为页面构建静态 HTML 失败

   6 | }) {
7 | const { markdownRemark } = data // data.markdownRemark holds our post data
> 8 | const { frontmatter, html } = markdownRemark
| ^
9 | return (
10 | <div className="blog-post-container">
11 | <div className="blog-post">

WebpackError:TypeError:无法读取 null 的属性“frontmatter”

我查看了所有 github 问题并尝试在 VSCode 中调试,但没有成功......

这是相关文件。

gatsby-config.js

module.exports = {
siteMetadata: {
title: `simco-cms`,
},
plugins: [
`gatsby-plugin-netlify-cms`,
`gatsby-transformer-remark`,
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/blog`,
name: "markdown-pages",
},
},
],
}

pages/index.js

import React from "react"

import { graphql } from "gatsby"
import PostLink from "../components/postLink"

const IndexPage = ({
data: {
allMarkdownRemark: { edges },
},
}) => {
const Posts = edges
.filter(edge => !!edge.node.frontmatter.date)
.map(edge => <PostLink key={edge.node.id} post={edge.node} />)

return <div>{Posts}</div>
}

export default IndexPage

export const pageQuery = graphql`
query {
allMarkdownRemark(sort: { order: DESC, fields: [frontmatter___date] }) {
edges {
node {
id
excerpt(pruneLength: 250)
frontmatter {
date(formatString: "MMMM DD, YYYY")
path
title
}
}
}
}
}
`

gatsby-node.js

const path = require("path")

exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions

const blogPostTemplate = path.resolve(`src/templates/blogTemplate.js`)

return graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
frontmatter {
path
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}

result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.frontmatter.path,
component: blogPostTemplate,
context: {}, // additional data can be passed via context
})
})
})
}

templates/blogTemplate.js

import React from "react"
import { graphql } from "gatsby"

export default function Template({
data, // this prop will be injected by the GraphQL query below.
}) {
const { markdownRemark } = data // data.markdownRemark holds our post data
const { frontmatter, html } = markdownRemark
return (
<div className="blog-post-container">
<div className="blog-post">
<h1>{frontmatter.title}</h1>
<h2>{frontmatter.date}</h2>
<div
className="blog-post-content"
dangerouslySetInnerHTML={{ __html: html }}
/>
</div>
</div>
)
}

export const pageQuery = graphql`
query($path: String!) {
markdownRemark(frontmatter: { path: { eq: $path } }) {
html
frontmatter {
date(formatString: "MMMM DD, YYYY")
path
title
}
}
}
`

最佳答案

解析为 markdown 的路径是相对于项目/存储库位置的根目录的。传递给模板的路径必须是绝对路径,以便查询能够解析 $path 处的数据。

博客/test.md

---
path: test
date: 2018-11-05T16:25:21.303Z
title: test
---
test

如果您确实希望此博文的路径为 /test,请更改路径:

---
path: /test
date: 2018-11-05T16:25:21.303Z
title: test
---
test

否则将其更改为路径:/blog/test

关于Gatsby v2 - 使用 Markdown 为失败的页面构建静态 HTML 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53176045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com