text-truncate-smart Edge Function — Content
ContentTruncates text at character, word, or sentence boundaries. Appends a configurable suffix and never cuts mid-word or mid-sentence when using smart modes.
Edge function text-truncate-smart Truncates text at character, word, or sentence boundaries. Appends a configurable suffix and never cuts mid-word or mid-sentence when using smart modes.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.
npx aerostack add navin/text-truncate-smart Use with AI Assistants
MCPConnect Claude, Cursor, or any MCP-compatible client — then call this function by slug
① Add MCP Server
Add this once — access all Aerostack functions from your AI tool.
{
"mcpServers": {
"aerostack": {
"url": "https://mcp.aerostack.dev",
"type": "http"
}
}
} ② Call this function
Ask your AI to use the call_function tool with this slug:
call_function({
slug: "text-truncate-smart",
args: {
"text": "example_text",
"maxLength": 42,
"mode": "word",
"suffix": "..."
}
}) text-truncate-smart — Smart Text Truncation
Truncates text at character, word, or sentence boundaries with a configurable suffix. Never cuts mid-word or mid-sentence when using smart modes.
API
POST /api/text-truncate-smart
Request body
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
text |
string | ✅ | — | The text to truncate |
maxLength |
number | ✅ | — | Maximum character length of the output (including suffix) |
mode |
"word" | "sentence" | "char" |
❌ | "word" |
Truncation strategy |
suffix |
string | ❌ | "..." |
String appended when truncation occurs |
Modes:
"char"— truncate at the exact character limit"word"— truncate at the last complete word beforemaxLength"sentence"— truncate at the last complete sentence (ending with.!?) beforemaxLength
Success response (200)
{
"success": true,
"data": {
"text": "The quick brown fox...",
"truncated": true,
"originalLength": 43
}
}
Error responses
| Code | HTTP | When |
|---|---|---|
INVALID_INPUT |
400 | Missing required field or invalid maxLength |
INTERNAL_ERROR |
500 | Unexpected processing error |
Usage
cURL
curl -X POST "$FUNCTION_URL/api/text-truncate-smart" \
-H "Content-Type: application/json" \
-d '{"text": "The quick brown fox jumps over the lazy dog", "maxLength": 20, "mode": "word"}'
TypeScript / JavaScript (HTTP)
const response = await fetch(`${FUNCTION_URL}/api/text-truncate-smart`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: 'First sentence. Second sentence. Third one.',
maxLength: 30,
mode: 'sentence',
}),
});
const { data } = await response.json();
console.log(data.text); // "First sentence..."
console.log(data.truncated); // true
Direct import (Node / Bun / Deno)
import { truncateSmart } from '@aerostack/functions/text-truncate-smart';
const result = truncateSmart({
text: 'The quick brown fox jumps over the lazy dog',
maxLength: 20,
mode: 'word',
suffix: '…',
});
console.log(result.text); // "The quick brown fox…"
console.log(result.truncated); // true
console.log(result.originalLength); // 43
Use Cases
- Card previews: Truncate blog post descriptions to fit card UI components without cutting mid-word.
- Search result snippets: Show a clean sentence-boundary excerpt from a long document in search results.
- Notification messages: Shorten push notification text to fit within platform character limits.
- Meta descriptions: Generate SEO meta description content that stays within the 160-character limit.
Notes
- The
suffixcounts towardmaxLength. A 3-character suffix withmaxLength: 20leaves 17 characters for text content. - If the text already fits within
maxLength, it is returned unchanged withtruncated: false. sentencemode falls back towordmode when no sentence boundary is found within the target length.- When
suffixis longer thanmaxLength, the text is truncated tomaxLengthcharacters with no suffix applied.
Metadata
Tags
Publisher
@navin verified
Build and publish your own functions
Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.
More Content Functions
Browse Content Functions →text-extract-emails
by @navin
Extracts all valid email addresses from free-form text using an RFC 5321 compliant pattern. Supports deduplication and preserves order of first appearance.
text-html-to-markdown
by @navin
Converts HTML to Markdown text. Handles headings, bold, italic, links, code, lists, blockquotes, and horizontal rules. Strips all other HTML tags while preserving text content.
text-markdown-to-html
by @navin
Converts Markdown text to HTML with optional sanitization. Supports all common Markdown syntax including headings, bold, italic, code blocks, lists, links, images, and blockquotes.
text-keyword-highlight
by @navin
Highlights keyword occurrences in text by wrapping them in configurable HTML tags. Handles case-insensitive matching, longest-match-first for overlapping keywords, and HTML entity escaping.
text-sanitize-html
by @navin
Sanitizes HTML by removing dangerous tags and attributes. Always strips script, style, iframe, form elements, and all event handlers. Keeps only whitelisted tags with whitelisted attributes.
text-slug-generate
by @navin
Generates a URL-safe slug from any string. Handles Unicode, accents, and special characters. Zero dependencies.
Frequently asked questions
What does the text-truncate-smart function do? +
text-truncate-smart is a serverless edge function for content automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.
How do I deploy the text-truncate-smart function? +
Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/text-truncate-smart ``` It will be live on Cloudflare Workers in seconds.
What runtime does text-truncate-smart use? +
text-truncate-smart runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.
Can I customise the text-truncate-smart function? +
Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.