Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-mdx): Truncate non-latin language excerpts correctly #22638

Merged
merged 2 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/gatsby-plugin-mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,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
16 changes: 14 additions & 2 deletions packages/gatsby-plugin-mdx/gatsby/source-nodes.js
Original file line number Diff line number Diff line change
@@ -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`)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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: {
Expand Down