Skip to content

Commit

Permalink
refactor(www): consolidate getting the excerpt from blog posts (#21386)
Browse files Browse the repository at this point in the history
  • Loading branch information
tesseralis authored Feb 19, 2020
1 parent 2c7212e commit 27aeb08
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 25 deletions.
5 changes: 2 additions & 3 deletions www/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,16 @@ module.exports = {
) {
edges {
node {
excerpt
html
frontmatter {
title
date
excerpt
author {
id
}
}
fields {
excerpt
slug
}
}
Expand All @@ -314,7 +313,7 @@ module.exports = {
allMdx.edges.map(({ node }) => {
return {
title: node.frontmatter.title,
description: node.frontmatter.excerpt || node.excerpt,
description: node.fields.excerpt,
url: site.siteMetadata.siteUrl + node.fields.slug,
guid: site.siteMetadata.siteUrl + node.fields.slug,
custom_elements: [{ "content:encoded": node.html }],
Expand Down
7 changes: 2 additions & 5 deletions www/src/components/blog-post-preview-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ const BlogPostPreviewItem = ({ post, className }) => (
<article css={{ position: `relative` }} className={className}>
<Link to={post.fields.slug} sx={{ "&&": { color: `card.color` } }}>
<h2 sx={{ color: `card.header`, mt: 0 }}>{post.frontmatter.title}</h2>
<p>
{post.frontmatter.excerpt ? post.frontmatter.excerpt : post.excerpt}
</p>
<p>{post.fields.excerpt}</p>
</Link>
<div
css={{
Expand Down Expand Up @@ -86,12 +84,11 @@ const BlogPostPreviewItem = ({ post, className }) => (

export const blogPostPreviewFragment = graphql`
fragment BlogPostPreview_item on Mdx {
excerpt
fields {
slug
excerpt
}
frontmatter {
excerpt
title
date
author {
Expand Down
6 changes: 1 addition & 5 deletions www/src/components/homepage/homepage-blog-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,8 @@ const HomepageBlogPost = ({
desktopViewport = false,
}) => {
const {
excerpt: automaticExcerpt,
fields: { slug },
fields: { slug, excerpt },
frontmatter: {
excerpt: handwrittenExcerpt,
author: {
id: authorName,
avatar: {
Expand All @@ -205,8 +203,6 @@ const HomepageBlogPost = ({
},
} = post

const excerpt = handwrittenExcerpt ? handwrittenExcerpt : automaticExcerpt

return (
<HomepageBlogPostRoot fullWidth={fullWidth}>
{desktopViewport && cover && (
Expand Down
16 changes: 4 additions & 12 deletions www/src/templates/template-blog-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,10 @@ class BlogPostTemplate extends React.Component {
rel="author"
href={`https://gatsbyjs.org${post.frontmatter.author.fields.slug}`}
/>
<meta
name="description"
content={
post.frontmatter.excerpt
? post.frontmatter.excerpt
: post.excerpt
}
/>
<meta name="description" content={post.fields.excerpt} />

<meta property="og:description" content={post.excerpt} />
<meta name="twitter:description" content={post.excerpt} />
<meta property="og:description" content={post.fields.excerpt} />
<meta name="twitter:description" content={post.fields.excerpt} />
<meta property="og:title" content={post.frontmatter.title} />
<meta property="og:url" content={href} />
{post.frontmatter.image && (
Expand Down Expand Up @@ -241,15 +234,14 @@ export const pageQuery = graphql`
query($slug: String!) {
mdx(fields: { slug: { eq: $slug } }) {
body
excerpt
timeToRead
fields {
slug
excerpt
publishedAt
}
frontmatter {
title
excerpt
date(formatString: "MMMM Do YYYY")
rawDate: date
canonicalLink
Expand Down
7 changes: 7 additions & 0 deletions www/src/utils/node/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ exports.onCreateNode = ({ node, actions, getNode }) => {
? publishedAt || url.parse(canonicalLink).hostname
: null,
})

// If an excerpt is defined, use it, otherwise default to autogenerated excerpt
createNodeField({
node,
name: `excerpt`,
value: node.frontmatter.excerpt || node.excerpt,
})
}
}

Expand Down

0 comments on commit 27aeb08

Please sign in to comment.