How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Using GROQ to create an excerpt for a Sanity block

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

By Frikk Jarl


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

Frikk Jarl

Digital Agent | UI/UX, Marketing & Development

Visit Frikk Jarl's profile