This Friday: Hear from Sanity + Vercel experts on AI and better personalization in e-commerce

Create an excerpt for a block field

Truncate block content into a shorter value to use as an excerpt

By Carl Topham


GROQ query

*[_type == "article" && draft != true ] | order(publishedOn desc)[0..5] {
     "excerpt": array::join(string::split((pt::text(body)), "")[0..255], "") + "..."
}

This query takes a block field and creates an excerpt of the required length so that truncation doesn't need to be done on the client side.

Contributor

Carl Topham

Frontend JavaScript engineer

Visit Carl Topham's profile