Skip to content

Commit

Permalink
Use GatsbyImage in an article
Browse files Browse the repository at this point in the history
  • Loading branch information
iciclize committed Jan 7, 2024
1 parent 7bb1f8e commit 988d5fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .env.production
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
STRAPI_URL=http://localhost:1337
STRAPI_URL=https://nnyapi.iciclize.net
IMAGE_BASE_URL=https://nnyapi.iciclize.net
FEED_IFRAME_URL=https://memos.iciclize.net/u/1
28 changes: 26 additions & 2 deletions src/templates/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const PostTag = styled.li`
}
`;

const ArticleTemplate = hoge => {
const data = hoge.data;
const ArticleTemplate = props => {
const data = props.data;

const entry = data.strapiArticle;
const relatedPosts = data.relatedPosts;
Expand Down Expand Up @@ -115,6 +115,20 @@ const ArticleTemplate = hoge => {
? uri
: `${process.env.IMAGE_BASE_URL}${uri}`
}
components={{
img: ({node, ...imgProps}) => {
// console.log(imgProps);
// console.log(entry.content.medias);
const fileIndex = entry.content.medias.findIndex(file => file.url === imgProps.src);
// console.log("fileIndex=", fileIndex);
if (fileIndex >= 0) {
const medium = entry.content.medias[fileIndex];
return (<GatsbyImage image={medium.localFile.childImageSharp.gatsbyImageData} alt={medium.alternativeText} />);
}
// console.log(node);
return <img alt="this is a fallback text" {...imgProps} />;
}
}}
>
{entry.content.data.content}
</ReactMarkdown>
Expand Down Expand Up @@ -176,6 +190,16 @@ export const q = graphql`
excerpt(format: PLAIN, pruneLength: 100, truncate: true)
}
}
medias {
src
localFile {
childImageSharp {
gatsbyImageData
}
}
url
alternativeText
}
}
publish_date(formatString: "YYYY-MM-DD")
update_date(formatString: "YYYY-MM-DD")
Expand Down

0 comments on commit 988d5fe

Please sign in to comment.