postkit-excerpt
v1.0.0
Published
**Purpose:** Generates short summaries from post content. Used to create excerpts for post cards, previews, and search results.
Downloads
13
Readme
postkit-excerpt
Purpose: Generates short summaries from post content. Used to create excerpts for post cards, previews, and search results.
Exports
createExcerpt
- Input:
text: string,maxLength: number - Output:
string - Description: Returns a readable summary trimmed to the last complete word within
maxLengthcharacters. Appends"…"if truncated.
truncateByWords
- Input:
text: string,maxWords: number - Output:
string - Description: Shortens text to a maximum number of words. Appends
"…"if truncated.
normalizeWhitespace
- Input:
text: string - Output:
string - Description: Collapses extra spaces, tabs, and line breaks into single spaces and trims the result.
Example Usage
import { createExcerpt, truncateByWords, normalizeWhitespace } from 'postkit-excerpt'
createExcerpt('This is a long post body that keeps going.', 30)
// → "This is a long post body that…"
truncateByWords('The quick brown fox jumped over the lazy dog', 5)
// → "The quick brown fox jumped…"
normalizeWhitespace(' Hello world\n\nfoo ')
// → "Hello world foo"Edge Cases
- Empty string →
"" maxLengthormaxWordsof0→""- Text already within limit → returned as-is, no
"…"appended - Whitespace-only input →
"" maxLengthsmaller than the first word → returns that word + "…" even though the result exceedsmaxLength. Since truncation never cuts mid-word, this is the shortest possible output. The returned string may be longer thanmaxLengthin this case.
Design Notes
createExcerptcallsnormalizeWhitespaceinternally so callers don't need to clean input first.createExcerptandtruncateByWordsare separate because they solve different layout problems — character limits for fixed-width containers, word limits for more natural reading flow.- Truncation always breaks at word boundaries. Cutting mid-word looks like a bug.
- During implementation, a bug was found and fixed where
createExcerptwould incorrectly slice a word shorter thanmaxLengthbefore checking for spaces. Tests caught this.
- During implementation, a bug was found and fixed where
