From ca1810392b0542d6ce9d2b9e585eab7fa8a6302a Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sun, 13 May 2018 01:05:09 +0200 Subject: [PATCH] truncate meta description --- src/components/atoms/SEO.jsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/components/atoms/SEO.jsx b/src/components/atoms/SEO.jsx index ef40295..eca8c50 100644 --- a/src/components/atoms/SEO.jsx +++ b/src/components/atoms/SEO.jsx @@ -2,11 +2,23 @@ import React from 'react' import Helmet from 'react-helmet' import PropTypes from 'prop-types' +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 SEO = ({ project, meta }) => { const title = project.title ? project.title : meta.title const description = project.description - ? project.description - : meta.description + ? truncate.apply(project.description, [320, true]) + : truncate.apply(meta.description, [320, true]) const image = project.img ? project.img.childImageSharp.twitterImage.src : meta.img.childImageSharp.resize.src