我正在使用降价页面在 Gastby 上生成我的页面。我在 .md 文件中定义图像路径。
---
slug: "/blog/my-blog-post"
date: "2022-01-11"
title: "My title"
image: /images/blog/my-image-1.png
---
![image-alt](/images/blog/my-image-1.png)
# My content header
My content
{MarkdownRemark.frontmatter__slug}.js
import React from "react"
import { Helmet } from "react-helmet"
import BaseController from "../base-controller"
import { graphql } from "gatsby"
import { getSrc } from "gatsby-plugin-image"
const baseUrl = 'https://pppx.com';
export default function Template({
data, // this prop will be injected by the GraphQL query below.
}) {
const { markdownRemark } = data // data.markdownRemark holds your post data
const { frontmatter, html } = markdownRemark
const image = getSrc(frontmatter.image); // image returns as undefined.
console.log("image",image);
return (
<BaseController>
<Helmet>
<title>{frontmatter.title}</title>
<meta name="title" content={frontmatter.title} />
<!-- this line doesn'g work as expected --!>
<meta property="og:image" content={frontmatter.image} />
</Helmet>
<section>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12 col-lg-12" dangerouslySetInnerHTML={{ __html: html }}>
</div>
</div>
</div>
</section>
</BaseController>
)
}
export const pageQuery = graphql`
query og($id: String!) {
markdownRemark(id: { eq: $id }) {
html
frontmatter {
slug
title
image
}
}
}
`
在这里,我需要 og:image 作为由 gastby 生成的 url。但我不能那样做。顺便一提,
const image = getSrc(frontmatter.image); // image returns as undefined.
console.log("image",image);
返回未定义。所以 getSrc 对我没有帮助。
有没有办法让 og:image 工作?