Gatsby附带了一些插件可以帮助您:
gatsby-source-filesystem
会把文件数据放到你的盖茨比应用程序中,
gatsby-transformer-sharp
会问的
gatsby-plugin-sharp
您可以请求gatsby将一个文件链接到另一个节点,方法是在该节点中添加一个相对路径作为字段。你也可以手动链接。
设置
{
resolve: `gatsby-source-filesystem`,
options: {
path: absolute/path/to/a/directory,
name: `anything`,
},
},
它也可以找到根目录之外的目录。
path: path.resolve(__dirname, '../../images')
将现有路径添加到相对节点
假设你有一个
Json
节点,创建者
gatsby-transformer-remark
const fileAbsolutePath = /* find this node's absolute path */
const imageAboslutePath = /* do your get absolute path thing */
const imageRelativePath = path.relative(fileAbsolutePath, imageAboslutePath)
createNodeField({
node,
name: `image`,
value: imageRelativePath,
})
这将链接新字段
image
到文件节点,在其中可以找到
imageSharp
包含图像信息的节点。
然后你可以像
query {
markdownRemark {
fields { <-- fields created by `createNodeField` will live here
image { <-- point to a file node
childImageSharp { <-- point to imageSharp node of this file
fixed {
width <-- see more info in gatsby-plugin-sharp documentation
height
srcSet
src
}
}
}
}
}
}
如果要修改的节点没有文件,则必须手动链接它。
createNodeField
createNodeField({
node,
name: 'target___NODE', <--- `___NODE` will resolve to another node
value: targetNode.id,
})
other answer of mine
.