I have solved this now. I feel like it's a bit of a hack, but essentially...
- Get the string of the body as above:
pt::text(body)
- Split the string into an array of every character:
string::split(bodyString, "")
- truncate it to 255 characters:
[0..255]
- Join it back together:
array::join(truncated, "")
- Add an ellipsis to the end:
+ "..."
Joined together it can either be a set of queries piped together:
*[_type == "article" && draft != true ] | order(publishedOn desc)[0..5] {
"excerpt": (pt::text(body)),
} | {
"excerpt": string::split(excerpt, "")[0..255]
} | {
"excerpt": array::join(excerpt, "") + "..."
}
Or as one query:
*[_type == "article" && draft != true ] | order(publishedOn desc)[0..5] {
"excerpt": array::join(string::split((pt::text(body)), "")[0..255], "") + "..."
}