text-keyword-highlight Edge Function — Content
ContentHighlights 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.
Edge function text-keyword-highlight 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.. 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-keyword-highlight 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-keyword-highlight",
args: {
"text": "example_text",
"keywords": null,
"tag": "mark",
"className": "example_className",
"caseSensitive": false
}
}) text-keyword-highlight — Highlight Keywords in Text
Wraps keyword occurrences in text with configurable HTML tags. Handles case-insensitive matching, overlapping keywords (longest match wins), and safe HTML entity escaping.
API
POST /api/text-keyword-highlight
Request body
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
text |
string | ✅ | — | The plain text to highlight keywords in |
keywords |
string[] | ✅ | — | Keywords or phrases to highlight |
tag |
string | ❌ | "mark" |
HTML tag to wrap matches in |
className |
string | ❌ | — | Optional CSS class added to the wrapping tag |
caseSensitive |
boolean | ❌ | false |
Whether matching is case-sensitive |
Success response (200)
{
"success": true,
"data": {
"html": "The <mark>quick</mark> brown <mark>fox</mark> jumps.",
"matchCount": 2,
"matchedKeywords": ["quick", "fox"]
}
}
Error responses
| Code | HTTP | When |
|---|---|---|
INVALID_INPUT |
400 | Missing required field or wrong type |
INTERNAL_ERROR |
500 | Unexpected processing error |
Usage
cURL
curl -X POST "$FUNCTION_URL/api/text-keyword-highlight" \
-H "Content-Type: application/json" \
-d '{
"text": "The quick brown fox jumps over the lazy dog",
"keywords": ["quick", "fox", "lazy"],
"tag": "mark",
"className": "search-hit"
}'
TypeScript / JavaScript (HTTP)
const response = await fetch(`${FUNCTION_URL}/api/text-keyword-highlight`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: 'Cloudflare Workers are fast and scalable.',
keywords: ['Workers', 'fast'],
caseSensitive: false,
}),
});
const { data } = await response.json();
console.log(data.html);
// Cloudflare <mark>Workers</mark> are <mark>fast</mark> and scalable.
console.log(data.matchCount); // 2
Direct import (Node / Bun / Deno)
import { keywordHighlight } from '@aerostack/functions/text-keyword-highlight';
const result = keywordHighlight({
text: 'Build APIs fast with TypeScript and Hono.',
keywords: ['APIs', 'TypeScript'],
tag: 'span',
className: 'highlight',
});
console.log(result.html);
// Build <span class="highlight">APIs</span> fast with <span class="highlight">TypeScript</span> and Hono.
Use Cases
- Search result UIs: Highlight matching search terms in document excerpts and result snippets.
- Document review tools: Mark specified keywords in legal, medical, or compliance documents for review.
- Content moderation: Visually flag specific words or phrases in user-submitted content.
- AI output display: Highlight key entities or concepts identified by an LLM in the source text.
Notes
- The input text is HTML-escaped before highlighting, so the output is safe to inject into HTML.
- When multiple keywords could match the same position, the longest keyword wins (e.g.
"foo bar"takes priority over"foo"at the same position). matchedKeywordsreturns only keywords that were actually found — useful for showing "no results for X" messages.- Empty strings in the
keywordsarray are silently ignored.
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-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.
text-to-camel-case
by @navin
Converts a space-separated, snake_case, kebab-case, or PascalCase string to camelCase. Zero dependencies.
Frequently asked questions
What does the text-keyword-highlight function do? +
text-keyword-highlight 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-keyword-highlight function? +
Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/text-keyword-highlight ``` It will be live on Cloudflare Workers in seconds.
What runtime does text-keyword-highlight use? +
text-keyword-highlight runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.
Can I customise the text-keyword-highlight function? +
Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.