mirror of
https://github.com/kremalicious/portfolio.git
synced 2025-02-01 20:39:45 +01:00
create truncated excerpt on node creation
This commit is contained in:
parent
0a17e42451
commit
d2a5f03435
@ -3,6 +3,18 @@ const remark = require('remark')
|
|||||||
const markdown = require('remark-parse')
|
const markdown = require('remark-parse')
|
||||||
const html = require('remark-html')
|
const html = require('remark-html')
|
||||||
|
|
||||||
|
function truncate(n, useWordBoundary) {
|
||||||
|
if (this.length <= n) {
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
const subString = this.substr(0, n - 1)
|
||||||
|
return (
|
||||||
|
(useWordBoundary
|
||||||
|
? subString.substr(0, subString.lastIndexOf(' '))
|
||||||
|
: subString) + '...'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
exports.onCreateNode = ({ node, actions }) => {
|
exports.onCreateNode = ({ node, actions }) => {
|
||||||
const { createNodeField } = actions
|
const { createNodeField } = actions
|
||||||
|
|
||||||
@ -29,6 +41,15 @@ exports.onCreateNode = ({ node, actions }) => {
|
|||||||
name: 'descriptionHtml',
|
name: 'descriptionHtml',
|
||||||
value: descriptionHtml
|
value: descriptionHtml
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Create excerpt from description
|
||||||
|
const excerpt = truncate.apply(description, [320, true])
|
||||||
|
|
||||||
|
createNodeField({
|
||||||
|
node,
|
||||||
|
name: 'excerpt',
|
||||||
|
value: excerpt
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,18 +3,6 @@ import Helmet from 'react-helmet'
|
|||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import { StaticQuery, graphql } from 'gatsby'
|
import { StaticQuery, graphql } from 'gatsby'
|
||||||
|
|
||||||
function truncate(n, useWordBoundary) {
|
|
||||||
if (this.length <= n) {
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
const subString = this.substr(0, n - 1)
|
|
||||||
return (
|
|
||||||
(useWordBoundary
|
|
||||||
? subString.substr(0, subString.lastIndexOf(' '))
|
|
||||||
: subString) + '...'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const query = graphql`
|
const query = graphql`
|
||||||
query {
|
query {
|
||||||
dataYaml {
|
dataYaml {
|
||||||
@ -22,7 +10,6 @@ const query = graphql`
|
|||||||
tagline
|
tagline
|
||||||
description
|
description
|
||||||
url
|
url
|
||||||
email
|
|
||||||
img {
|
img {
|
||||||
childImageSharp {
|
childImageSharp {
|
||||||
resize(width: 980) {
|
resize(width: 980) {
|
||||||
@ -31,11 +18,7 @@ const query = graphql`
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
social {
|
social {
|
||||||
Email
|
|
||||||
Blog
|
|
||||||
Twitter
|
Twitter
|
||||||
GitHub
|
|
||||||
Dribbble
|
|
||||||
}
|
}
|
||||||
gpg
|
gpg
|
||||||
addressbook
|
addressbook
|
||||||
@ -48,10 +31,6 @@ export default class SEO extends PureComponent {
|
|||||||
project: PropTypes.object
|
project: PropTypes.object
|
||||||
}
|
}
|
||||||
|
|
||||||
static defaultProps = {
|
|
||||||
project: {}
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { project } = this.props
|
const { project } = this.props
|
||||||
|
|
||||||
@ -61,24 +40,26 @@ export default class SEO extends PureComponent {
|
|||||||
render={data => {
|
render={data => {
|
||||||
const meta = data.dataYaml
|
const meta = data.dataYaml
|
||||||
|
|
||||||
const title = project.title || meta.title
|
const title = (project && project.title) || meta.title
|
||||||
const description = project.description
|
const description =
|
||||||
? truncate.apply(project.description, [320, true])
|
project && project.fields.excerpt
|
||||||
: truncate.apply(meta.description, [320, true])
|
? project.fields.excerpt
|
||||||
const image = project.img
|
: meta.description
|
||||||
? project.img.childImageSharp.twitterImage.src
|
const image =
|
||||||
: meta.img.childImageSharp.resize.src
|
project && project.img
|
||||||
const url = project.slug ? `${meta.url}${project.slug}` : meta.url
|
? project.img.childImageSharp.twitterImage.src
|
||||||
|
: meta.img.childImageSharp.resize.src
|
||||||
|
const url =
|
||||||
|
project && project.slug ? `${meta.url}${project.slug}` : meta.url
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Helmet
|
<Helmet
|
||||||
defaultTitle={`${meta.title.toLowerCase()} { ${meta.tagline.toLowerCase()} }`}
|
defaultTitle={`${meta.title.toLowerCase()} { ${meta.tagline.toLowerCase()} }`}
|
||||||
titleTemplate={`%s // ${meta.title.toLowerCase()} { ${meta.tagline.toLowerCase()} }`}
|
titleTemplate={`%s // ${meta.title.toLowerCase()} { ${meta.tagline.toLowerCase()} }`}
|
||||||
|
title={title}
|
||||||
>
|
>
|
||||||
<html lang="en" />
|
<html lang="en" />
|
||||||
|
|
||||||
<title>{title}</title>
|
|
||||||
|
|
||||||
{/* General tags */}
|
{/* General tags */}
|
||||||
<meta name="description" content={description} />
|
<meta name="description" content={description} />
|
||||||
<meta name="image" content={`${meta.url}${image}`} />
|
<meta name="image" content={`${meta.url}${image}`} />
|
||||||
|
@ -86,9 +86,9 @@ export const projectQuery = graphql`
|
|||||||
projectsYaml(slug: { eq: $slug }) {
|
projectsYaml(slug: { eq: $slug }) {
|
||||||
title
|
title
|
||||||
slug
|
slug
|
||||||
description
|
|
||||||
fields {
|
fields {
|
||||||
descriptionHtml
|
descriptionHtml
|
||||||
|
excerpt
|
||||||
}
|
}
|
||||||
links {
|
links {
|
||||||
title
|
title
|
||||||
|
Loading…
Reference in New Issue
Block a user