Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Commit

Permalink
Create coverImage and custom excerpt fields
Browse files Browse the repository at this point in the history
  • Loading branch information
panr committed Mar 2, 2019
1 parent 7a5b2c1 commit 4f04335
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 334 deletions.
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
"babel-preset-gatsby",
],
"plugins": [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-nullish-coalescing-operator"
]
}
8 changes: 8 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ extends:
- airbnb
- prettier

plugins:
- react-hooks

rules:
# best practices
arrow-parens:
Expand Down Expand Up @@ -49,6 +52,9 @@ rules:
import/no-extraneous-dependencies: 0
import/prefer-default-export: 0
object-curly-newline: 0
operator-linebreak:
- 2
- after

react/jsx-filename-extension: 0
react/jsx-one-expression-per-line: 0
Expand All @@ -58,6 +64,8 @@ rules:
react/no-unescaped-entities: 0
react/destructuring-assignment: 0

react-hooks/rules-of-hooks: error

globals:
document: true
requestAnimationFrame: true
21 changes: 21 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,24 @@ exports.createPages = ({ actions, graphql }) => {
return sortedPages
})
}

exports.onCreateNode = async function({ node, getNodes }) {
const allNodes = getNodes()
const posts = allNodes.filter(n => n.internal.type === 'MarkdownRemark')
const atLeastOnePostHasCoverImage = posts.filter(
post => post.frontmatter.coverImage,
).length
const coverImagePlaceholder = atLeastOnePostHasCoverImage ?
null :
'../images/placeholder/image-placeholder.png'

if (node.internal.type === 'MarkdownRemark') {
node.frontmatter = {
...node.frontmatter,
coverImage: node.frontmatter.coverImage || coverImagePlaceholder,
excerpt: node.frontmatter.excerpt || '',
}
}

return node
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"react-helmet": "^5.2.0"
},
"devDependencies": {
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.2.0",
"@babel/plugin-proposal-optional-chaining": "^7.2.0",
"babel-eslint": "^10.0.1",
"babel-preset-gatsby": "^0.1.6",
"cssnano": "^4.1.8",
Expand All @@ -50,6 +52,7 @@
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.2",
"eslint-plugin-react": "^7.12.3",
"eslint-plugin-react-hooks": "^1.4.0",
"gatsby-remark-embed-video": "^1.7.0",
"postcss-import": "^12.0.1",
"postcss-loader": "^3.0.0",
Expand Down
5 changes: 4 additions & 1 deletion src/components/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const Post = ({
const nextPath = nextPost && nextPost.frontmatter.path
const nextLabel = nextPost && nextPost.frontmatter.title

const isPlaceholder =
coverImage?.childImageSharp.fluid.sizes.indexOf('1px') !== -1

return (
<div className={style.post}>
<div className={style.postContent}>
Expand All @@ -31,7 +34,7 @@ const Post = ({
<div className={style.meta}>
{date} {author && <>— Written by {author}</>}
</div>
{coverImage && (
{coverImage && !isPlaceholder && (
<Img
fluid={coverImage.childImageSharp.fluid}
className={style.coverImage}
Expand Down
11 changes: 0 additions & 11 deletions src/posts/.features-placeholder.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/styles/post.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

.meta {
font-size: 1rem;
margin-bottom: 20px;
margin-bottom: 30px;
}

.readMore {
Expand Down
5 changes: 1 addition & 4 deletions src/templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ Index.propTypes = {
export const postsQuery = graphql`
query($limit: Int!, $skip: Int!) {
allMarkdownRemark(
filter: {
fileAbsolutePath: { regex: "//posts//" }
frontmatter: { placeholder: { ne: true } }
}
filter: { fileAbsolutePath: { regex: "//posts//" } }
sort: { fields: [frontmatter___date], order: DESC }
limit: $limit
skip: $skip
Expand Down
Loading

0 comments on commit 4f04335

Please sign in to comment.