👀 See Sanity in action: Watch product demo now →

Using GROQ to create an excerpt for a Sanity block

By Frikk Jarl

This is a quick tip on how to truncate a Sanity block field to create an excerpt using GROQ.

Fetch an excerpt for a Sanity block

*[_type == "article"] {
   "excerpt": array::join(string::split((pt::text(body)), "")[0..255], "") + "..."
}

When querying the field using a Sanity Client and GROQ, there is no inbuilt excerpt function, or even a truncate function, so we need to do the steps ourselves.

All credits to Carl Topham

Please read his deeper explanation here:

https://carl-topham.com/articles/groq-excerpt-sanity-block

Contributor