Aerostack
edit

text-keyword-highlight Edge Function — Content

Content

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.

navin @navin verified
Updated Mar 12, 2026
GitHub

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.

terminal — aerostack cli
$ npx aerostack add navin/text-keyword-highlight
smart_toy

Use with AI Assistants

MCP

Connect 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.

claude_desktop_config.json
{
  "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
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).
  • matchedKeywords returns only keywords that were actually found — useful for showing "no results for X" messages.
  • Empty strings in the keywords array are silently ignored.

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

text
deployed_code

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 →

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.