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: {