将ImageSharp作为字段添加到MarkdownRemark节点(不是frontmatter) [英] Add ImageSharp as a field to MarkdownRemark nodes (not frontmatter)
问题描述
我要尝试使用以下graphQL查询:
I have the following graphQL query I'm trying to get working:
{
allMarkdownRemark(
limit: 1000
) {
edges {
node {
id
parent {
id
}
fields{
slug
hero {
childImageSharp {
fixed {
src
}
}
}
}
frontmatter {
template
}
}
}
}
}
hero
字段当前使用以下代码返回图像的路径:
The hero
field currently returns a path to an image using the following code:
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
// Add slug to MarkdownRemark node
if (node.internal.type === 'MarkdownRemark') {
const value = createFilePath({ node, getNode, basePath: 'library' })
const { dir } = getNode(node.parent)
const getHero = (d) => {
let hero = `${__dirname}/src/images/no-hero.gif`
if (fs.existsSync(`${d}/hero.jpg`)) hero = `${d}/hero.jpg`
if (fs.existsSync(`${d}/hero.png`)) hero = `${d}/hero.png`
if (fs.existsSync(`${d}/hero.gif`)) hero = `${d}/hero.gif`
return hero
}
createNodeField({
node,
name: 'slug',
value,
})
createNodeField({
node,
name: 'hero',
value: getHero(dir),
})
}
}
我已经看到其他人对frontmatter
中的图像路径进行了类似的操作,但是我不想在很容易获得graphql来查看文件路径而不必指定它的情况下使用frontmatter .
I've seen other people do something similar with an image path in the frontmatter
but I don't want to have to use the frontmatter when it's easy enough to get graphql to see the file path without having to specify it.
但是,当我尝试上述操作时,出现以下错误:
However when I try the above I get the following error:
字段\"hero \"不能选择,因为类型\"String \"没有 子字段.
Field \"hero\" must not have a selection since type \"String\" has no subfields.
有没有办法让我childImageSharp
识别此字段?
Is there a way I can get childImageSharp
to recognize this field?
推荐答案
I'm back again to (hopefully) settle this issue once and for all (see our history here).
这一次,我们将英雄图像的ImageSharp
附加到MarkdownRemark
节点.您的方法正确,有1个警告:盖茨比似乎只能识别相对路径,即以点开头的路径.
This time, we'll attach the hero image's ImageSharp
to the MarkdownRemark
node. Your approach is correct, with 1 caveat: Gatsby seems to only recognize relative paths, i.e path starting with a dot.
您可以在代码中轻松解决此问题:
You can fix this easily in your code:
const getHero = (d) => {
let hero = `${__dirname}/src/images/no-hero.gif`
- if (fs.existsSync(`${d}/hero.jpg`)) hero = `${d}/hero.jpg`
- if (fs.existsSync(`${d}/hero.png`)) hero = `${d}/hero.png`
- if (fs.existsSync(`${d}/hero.gif`)) hero = `${d}/hero.gif`
+ if (fs.existsSync(`${d}/hero.jpg`)) hero = `./hero.jpg`
+ if (fs.existsSync(`${d}/hero.png`)) hero = `./hero.png`
+ if (fs.existsSync(`${d}/hero.gif`)) hero = `./hero.gif`
return hero
}
createNodeField({
node,
name: 'hero',
value: getHero(dir),
})
这应该可行,尽管我想提供另一种英雄搜索功能.我们可以使用fs.readdir
来获取dir
中的文件列表,然后找到名称为"hero"的文件:
This should work, though I want to provide an alternative hero search function. We can get a list of files in dir
with fs.readdir
, then find a file with the name 'hero':
exports.onCreateNode = async ({
node, actions,
}) => {
const { createNodeField } = actions
if (node.internal.type === 'MarkdownRemark') {
const { dir } = path.parse(node.fileAbsolutePath)
const heroImage = await new Promise((res, rej) => {
// get a list of files in `dir`
fs.readdir(dir, (err, files) => {
if (err) rej(err)
// if there's a file named `hero`, return it
res(files.find(file => file.includes('hero')))
})
})
// path.relative will return a (surprise!) a relative path from arg 1 to arg 2.
// you can use this to set up your default hero
const heroPath = heroImage
? `./${heroImage}`
: path.relative(dir, 'src/images/default-hero.jpg')
// create a node with relative path
createNodeField({
node,
name: 'hero',
value: `./${heroImage}`,
})
}
}
这样,只要存在,我们就不会在乎英雄图像的扩展名是什么.我使用String.prototype.includes
,但是为了安全起见,您可能想使用regex传递允许的扩展列表,例如/hero.(png|jpg|gif|svg)/
. (我认为您的解决方案更具可读性,但我更喜欢每个节点仅访问一次文件系统.)
This way we don't care what the hero image's extension is, as long as it exists. I use String.prototype.includes
, but you might want to use regex to pass in a list of allowed extensions, to be safe, like /hero.(png|jpg|gif|svg)/
. (I think your solution is more readable, but I prefer to access the file system only once per node.)
您还可以使用 path.relative
查找默认路径的相对路径英雄形象.
You can also use path.relative
to find the relative path to a default hero image.
现在,此graphql查询有效:
Now, this graphql query works:
但是,这种方法存在一个小问题:它破坏了graphql过滤器类型!当我尝试基于hero
进行查询和过滤时,出现此错误:
However, there's a minor problem with this approach: it breaks graphql filter type! When I try to query and filter based on hero
, I get this error:
也许盖茨比忘记了重新推断hero
的类型,所以它仍然不是File
,而是String
.如果需要过滤器工作,这很烦人.
Perhaps Gatsby forgot to re-infer the type of hero
, so instead of being a File
, it is still a String
. This is annoying if you need the filter to work.
这是一种解决方法:我们不会自己要求Gatsby链接文件,而是会自己处理.
Here's a workaround: Instead of asking Gatsby to link the file, we'll do it ourselves.
exports.onCreateNode = async ({
node, actions, getNode, getNodesByType,
}) => {
const { createNodeField } = actions
// Add slug to MarkdownRemark node
if (node.internal.type === 'MarkdownRemark') {
const { dir } = path.parse(node.fileAbsolutePath)
const heroImage = await new Promise((res, rej) => {
fs.readdir(dir, (err, files) => {
if (err) rej(err)
res(files.find(file => file.includes('hero')))
})
})
// substitute with a default image if there's no hero image
const heroPath = heroImage ? path.join(dir, heroImage) : path.resolve(__dirname, 'src/images/default-hero.jpg')
// get all file nodes
const fileNodes = getNodesByType('File')
// find the hero image's node
const heroNode = fileNodes.find(fileNode => fileNode.absolutePath === heroPath)
createNodeField({
node,
name: 'hero___NODE',
value: heroNode.id,
})
}
}
现在我们可以再次过滤hero
字段:
And now we can filter the hero
field again:
如果您不需要按英雄图像过滤内容,则最好让gatsby处理节点类型.
If you don't need to filter content by hero image though, letting gatsby handle node type is much preferable.
让我知道您是否在尝试此操作时遇到问题.
Let me know if you run into issues trying this.
这篇关于将ImageSharp作为字段添加到MarkdownRemark节点(不是frontmatter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!