Skip to content

Commit ec80671

Browse files
authored
fix(gatsby-plugin-mdx): Truncate non-latin language excerpts correctly (#22638)
* Truncate option for non-latin languages * Add mention of truncate to docs
1 parent 84a37d2 commit ec80671

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

packages/gatsby-plugin-mdx/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,22 @@ export const pageQuery = graphql`
562562
`
563563
```
564564

565+
## Troubleshooting
566+
567+
### Excerpts for non-latin languages
568+
569+
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)).
570+
571+
If that is the case, you can set `truncate` option on `excerpt` field, like:
572+
573+
```graphql
574+
{
575+
markdownRemark {
576+
excerpt(truncate: true)
577+
}
578+
}
579+
```
580+
565581
## License
566582

567583
MIT

packages/gatsby-plugin-mdx/gatsby/source-nodes.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _ = require(`lodash`)
2+
const { GraphQLBoolean } = require(`gatsby/graphql`)
23
const remark = require(`remark`)
34
const english = require(`retext-english`)
45
const remark2retext = require(`remark-retext`)
@@ -151,8 +152,12 @@ module.exports = (
151152
type: `Int`,
152153
defaultValue: 140,
153154
},
155+
truncate: {
156+
type: GraphQLBoolean,
157+
defaultValue: false,
158+
},
154159
},
155-
async resolve(mdxNode, { pruneLength }) {
160+
async resolve(mdxNode, { pruneLength, truncate }) {
156161
if (mdxNode.excerpt) {
157162
return Promise.resolve(mdxNode.excerpt)
158163
}
@@ -166,7 +171,14 @@ module.exports = (
166171
return
167172
})
168173

169-
return prune(excerptNodes.join(` `), pruneLength, `…`)
174+
if (!truncate) {
175+
return prune(excerptNodes.join(` `), pruneLength, `…`)
176+
}
177+
178+
return _.truncate(excerptNodes.join(` `), {
179+
length: pruneLength,
180+
omission: `…`,
181+
})
170182
},
171183
},
172184
headings: {

0 commit comments

Comments
 (0)