I'd like to create an excerpt
from a (portable) text field. Is this something that is possible?
I know I can get the text value back using pt::text(body)
and I can get values such as length from that. Is there any way to cut the text after n
characters or words?
I have solved this now. I feel like it's a bit of a hack, but essentially...
pt::text(body)
string::split(bodyString, "")
[0..255]
array::join(truncated, "")
+ "..."
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], "") + "..."
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With