Skip to content

Commit

Permalink
fix: remark excerpt add halfChop option (#6992)
Browse files Browse the repository at this point in the history
* fix: remark excerpt add halfChop option

default prune won't work for non english text

* remark: renamed prune option, added docs

* transformer-remark readme wording changes

* Update README.md
  • Loading branch information
youngboy authored and pieh committed Aug 17, 2018
1 parent 584468e commit 4386750
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
16 changes: 16 additions & 0 deletions packages/gatsby-transformer-remark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ A sample GraphQL query to get MarkdownRemark nodes:
}
}
```

## 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)
}
}
```
16 changes: 13 additions & 3 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
GraphQLInt,
GraphQLEnumType,
GraphQLJSON,
GraphQLBoolean,
} = require(`gatsby/graphql`)
const Remark = require(`remark`)
const select = require(`unist-util-select`)
Expand Down Expand Up @@ -346,8 +347,12 @@ module.exports = (
type: GraphQLInt,
defaultValue: 140,
},
truncate: {
type: GraphQLBoolean,
defaultValue: false,
},
},
resolve(markdownNode, { pruneLength }) {
resolve(markdownNode, { pruneLength, truncate }) {
if (markdownNode.excerpt) {
return Promise.resolve(markdownNode.excerpt)
}
Expand All @@ -359,8 +364,13 @@ module.exports = (
}
return
})

return prune(excerptNodes.join(` `), pruneLength, `…`)
if (!truncate) {
return prune(excerptNodes.join(` `), pruneLength, `…`)
}
return _.truncate(excerptNodes.join(` `), {
length: pruneLength,
omission: `…`,
})
})
},
},
Expand Down

0 comments on commit 4386750

Please sign in to comment.