-
Notifications
You must be signed in to change notification settings - Fork 54
How to use a local image for post cover? #17
Comments
These should just work. |
Currently, local cover images are not supported by Gatsby-Starter-Casper. Images in the body of a Markdown file ARE supported because internally Gatsby is converting the Markdown back into HTML and it handles passing the images file. The cover data is in the frontmatter data, and Gatsby dynamically creates the GraphQL objects. There isn't an easy way to add custom object types to GraphQL in Gatsby. Both the cover images and also the author data could really utilize this functionality in Gatsby. As a workaround, you CAN hack support, by modifying these files, but I believe that ALL your covers must be local images. WorkaroundInstall gatsby-image with Update MainHeader.jsx with this code import React from "react";
import classNames from "classnames";
import "./MainHeader.css";
const hasImageSharpCover = cover => cover && cover.childImageSharp;
const hasCover = cover => cover && (hasImageSharpCover(cover) || typeof cover === "string");
const getCoverUrl = cover => {
if (hasImageSharpCover(cover)) {
return cover.childImageSharp.sizes.src;
}
return cover;
};
const getStyle = cover => {
if (hasCover(cover)) {
return { backgroundImage: `url("${getCoverUrl(cover)}")` };
}
return null;
};
class MainHeader extends React.Component {
render() {
const { children, cover } = this.props;
const classes = classNames("main-header", this.props.className, {
"no-cover": !cover
});
return (
<header className={classes} style={getStyle(cover)}>
{children}
</header>
);
}
}
export default MainHeader; Last of all, update the GraphQL pageQuery's that provide cover data to various JSX components that render the cover. The new cover GraphQL call pulls the needed ImageSharp data. You can tweak the maxWidth property. /* eslint no-undef: "off" */
export const pageQuery = graphql`
query BlogPostBySlug($slug: String!, $next: String, $prev: String) {
markdownRemark(fields: { slug: { eq: $slug } }) {
html
timeToRead
excerpt
frontmatter {
title
cover {
id
childImageSharp {
id
sizes(maxWidth: 2000) {
# Choose either the fragment including a small base64ed image, a traced placeholder SVG, or one without.
...GatsbyImageSharpSizes_noBase64
}
}
}
date
category
tags
author
}
fields {
slug
}
}
# prev post data
prev: markdownRemark(fields: { slug: { eq: $prev } }) {
excerpt(pruneLength: 112)
frontmatter {
title
cover {
id
childImageSharp {
id
sizes(maxWidth: 2000) {
# Choose either the fragment including a small base64ed image, a traced placeholder SVG, or one without.
...GatsbyImageSharpSizes_noBase64
}
}
}
date
}
fields {
slug
}
}
# next post data
next: markdownRemark(fields: { slug: { eq: $next } }) {
excerpt(pruneLength: 112)
frontmatter {
title
cover {
id
childImageSharp {
id
sizes(maxWidth: 2000) {
# Choose either the fragment including a small base64ed image, a traced placeholder SVG, or one without.
...GatsbyImageSharpSizes_noBase64
}
}
}
date
}
fields {
slug
}
}
# authors
authors: allAuthorsJson {
edges {
node {
id
name
image
url
bio
}
}
}
}
`; This solution is not checked in because it's so crude. If anyone has an alternative I would love to see a PR. |
@ZacharyChim not sure if it would be of any help, but you could to put the images in
|
* fix: re-adding Disqus support enable Disqus comments in posts by providing a ‘disqusShortname’ in the SiteConfig.js. Resolved haysclark#9 * chore(release): 1.0.4 * fix: adding basic validation for authors Adding build time error to warn users if ‘authors’ folder is missing and improving posts and authors content documentation. Resolves haysclark#10 * fix: fixing sample-post dates and folder names Posts now to have YEAR-MONTH-DAY formatting, allowing GraphQL to sort them correctly. Post folder names corrected to match the date in each .md file. * perf: removing dead GraphQL query * chore(release): 1.0.5 * fix: improving npm ‘clean’ script to wipe .cache Sometime Gatsby does not clear the .cache folder which can lead to confusion. Running ‘npm clean’, will now delete both public and .cache to aid in resolving this issue. * fix: fixing MainNav rendering Adding missing ‘overlay’ and ‘clearfix’ CSS classes to MainNav to resolve render issue when no Cover image is provided. Updating Big-Sample-Post to have no cover for validation Resolves haysclark#18 * chore(release): 1.0.6 * fix: adding yarn.lock to fix install issues Users installing the project with Yarn were missing a critical child dependancies for prismjs@1.9.0. Resolves haysclark#21 * chore(release): 1.0.7 * chore: updating README with more specific install steps * added import for prismjs css file to fix missing syntax highlighting * fix: Adding local image for post cover example Resolves haysclark#17 * chore: updating package-lock.json * fix: ignoring linting error - due to Prisma being added for Gatsby 2.0 support, we are ignoring this minor issue * chore(release): 1.0.8 * BREAKING CHANGE: installed latest gatsby version [2.0.20] * chore: updating yarn.lock * refactor: installing retaled package updates * refactor: missing 'prismjs' package needed for 'gatsby-transformer-remark' in v2 * refactor: change .babelrc to gatsby default * refactor: moved layouts to components updated relative imports * refactor: renamed index.jsx to layout.jsx and moved to components folder * refactor: wrapped template pages without layout component * refactor: changed 'children()' to 'children' in layout.jsx * refactor: pass location to Layout component in all templates * refactor: renamed id property of authors to uid | id seems to be a special proberty of nodes and messing with it breaks queries * refactor: deprecated gatsby-link | removed package and updated imports * refactor: explicit import of graphql tag * refactor: Rename boundActionCreators to actions * refactor: Rename pathContext to pageContext * refactor: Change modifyWebpackConfig to onCreateWebpackConfig * docs: Updating to reflect Gatsby 2.0 support * chore: linting JS and most component CSS errors - avoiding changes to layout.css for now * chore(release): 2.0.0
Hi, how can I use a local image in the post folder as the cover of the post? I tried to put the file name in the cover field, but it didn't show up.
I did some research and thinking this might relate to(should the cover image be treated by gatsby-plugin-sharp?): decaporg/decap-cms#325
The text was updated successfully, but these errors were encountered: