【发布时间】:2018-12-11 12:52:20
【问题描述】:
我正在努力为 gatsby v2 网站添加图片来评论降价帖子。我的网站可以毫无怨言地编译,并且我的 markdown 文件中的所有文本内容都存在。但是,图像已损坏。我已经抛弃了我认为 的关键配置部分。我做错了什么?
这是我的文件结构:
src - *
| posts - *
| post_1 - *
| image.png
| index.md
这是我的插件 -- 我已将 gatsby-transformer-remark 设置为使用 gatsby-remark-images。
plugins: [
'gatsby-plugin-react-helmet',
'gatsby-plugin-offline',
{
resolve: `gatsby-source-filesystem`,
options: {
name: `src`,
path: `${__dirname}/src/`
}
},
{
resolve: 'gatsby-plugin-react-svg',
options: {
rule: {
include: /.svg/
}
}
},
`gatsby-plugin-sharp`,
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 590,
},
},
],
},
}
]
这就是我尝试在index.md 中调用图像的方式:

最后是gatsby-node.js:
const { createFilePath } = require(`gatsby-source-filesystem`)
const path = require('path')
exports.onCreateNode = ({ node, getNode, actions: {createNodeField} }) => {
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `posts` })
createNodeField({
node,
name: `slug`,
value: slug
})
}
}
exports.createPages = async ({ graphql, actions: {createPage} }) => {
const result = await graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`)
console.log(result)
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: path.resolve('./src/templates/blog-post.js'),
context: {
slug: node.fields.slug
}
})
})
}
【问题讨论】:
标签: gatsby