From ec806717db88d22e83679221dff83e2d85f98c49 Mon Sep 17 00:00:00 2001 From: Kirill Vasiltsov <38713361+jlkiri@users.noreply.github.com> Date: Fri, 10 Apr 2020 21:23:09 +0900 Subject: [PATCH] fix(gatsby-plugin-mdx): Truncate non-latin language excerpts correctly (#22638) * Truncate option for non-latin languages * Add mention of truncate to docs --- packages/gatsby-plugin-mdx/README.md | 16 ++++++++++++++++ .../gatsby-plugin-mdx/gatsby/source-nodes.js | 16 ++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-plugin-mdx/README.md b/packages/gatsby-plugin-mdx/README.md index e84135696b459..ebadfca7cec7e 100644 --- a/packages/gatsby-plugin-mdx/README.md +++ b/packages/gatsby-plugin-mdx/README.md @@ -562,6 +562,22 @@ export const pageQuery = graphql` ` ``` +## Troubleshooting + +### Excerpts for non-latin languages + +By default, `excerpt` uses `underscore.string/prune` which doesn't handle non-latin characters ([https://github.com/epeli/underscore.string/issues/418](https://github.com/epeli/underscore.string/issues/418)). + +If that is the case, you can set `truncate` option on `excerpt` field, like: + +```graphql +{ + markdownRemark { + excerpt(truncate: true) + } +} +``` + ## License MIT diff --git a/packages/gatsby-plugin-mdx/gatsby/source-nodes.js b/packages/gatsby-plugin-mdx/gatsby/source-nodes.js index e22e5dc0a0a6a..04063f32f2a34 100644 --- a/packages/gatsby-plugin-mdx/gatsby/source-nodes.js +++ b/packages/gatsby-plugin-mdx/gatsby/source-nodes.js @@ -1,4 +1,5 @@ const _ = require(`lodash`) +const { GraphQLBoolean } = require(`gatsby/graphql`) const remark = require(`remark`) const english = require(`retext-english`) const remark2retext = require(`remark-retext`) @@ -151,8 +152,12 @@ module.exports = ( type: `Int`, defaultValue: 140, }, + truncate: { + type: GraphQLBoolean, + defaultValue: false, + }, }, - async resolve(mdxNode, { pruneLength }) { + async resolve(mdxNode, { pruneLength, truncate }) { if (mdxNode.excerpt) { return Promise.resolve(mdxNode.excerpt) } @@ -166,7 +171,14 @@ module.exports = ( return }) - return prune(excerptNodes.join(` `), pruneLength, `…`) + if (!truncate) { + return prune(excerptNodes.join(` `), pruneLength, `…`) + } + + return _.truncate(excerptNodes.join(` `), { + length: pruneLength, + omission: `…`, + }) }, }, headings: {