excerpt
Derives a short, readable excerpt from a longer text — for a card/header/preview where a full
paragraph doesn’t fit. Unlike truncate (a mechanical cut at exactly maxLength), this
prefers to cut at the end of a whole sentence (., !, or ?) when one fits within
maxLength, even if that leaves the result shorter than the limit. Only when no sentence fits
does it fall back to the last whole word before the limit — it never returns partial-word text.
Known limitation: the sentence-boundary check is a simple heuristic (punctuation followed by whitespace or the end of the text) — it doesn’t special-case abbreviations (“Mr.”) or decimal numbers (“3.14”), which can be misread as a sentence end.
Available since v3.1.2
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
text | string | The text to excerpt. Internal whitespace (line breaks, repeated spaces) is collapsed to single spaces before measuring length. |
maxLength | number | Maximum length of the result, including `ellipsis` when one is appended. |
ellipsis | string | Appended only when falling back to a word-boundary cut (a sentence-boundary cut never needs one). Defaults to `’…’`. |
Returns
Section titled “Returns”string — The original text unchanged if already within maxLength, otherwise a shortened version
Examples
Section titled “Examples”Cut at the end of a whole sentence
Section titled “Cut at the end of a whole sentence”Prefers a clean sentence break over a hard character cutoff, even if the result ends up shorter than maxLength.
Fall back to the last whole word
Section titled “Fall back to the last whole word”No sentence boundary fits, so it cuts at the last complete word and appends an ellipsis — never a partial word.
Already short enough — returned unchanged
Section titled “Already short enough — returned unchanged”Only whitespace is normalized; nothing is cut when the text already fits.
