Self-healing markdown. Intelligently parses and styles incomplete Markdown blocks.
npm install remendSelf-healing markdown. Intelligently parses and styles incomplete Markdown blocks.

Remend is a lightweight utility that handles incomplete Markdown syntax during streaming. When AI models stream Markdown token-by-token, you often get partial formatting markers like unclosed bold or incomplete Remend powers the markdown termination logic in [Streamdown](https://streamdown.ai" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">links and can be used standalone in any streaming Markdown application.
- 🔄 Streaming-optimized - Handles incomplete Markdown gracefully
- 🎨 Smart completion - Auto-closes bold, italic, code, links, images, strikethrough, and math blocks
- ⚡ Performance-first - Optimized string operations, no regex allocations
- 🛡️ Context-aware - Respects code blocks, math blocks, and nested formatting
- 🎯 Edge case handling - List markers, word-internal characters, escaped sequences
- 📦 Zero dependencies - Pure TypeScript implementation
Remend intelligently completes the following incomplete Markdown patterns:
- Bold: text → text**
- Italic: text or _text → text* or _text_
- Bold + Italic: text → text*
- Inline code: ` code ` → code ~~text
- Strikethrough: → ~~text~~ → [text](streamdown:incomplete-link" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">text
- Links: ! → removed (can't display partial images" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">alt
- Images:
- Block math: $$formula → $$formula$$
`bash`
npm i remend
`typescript
import remend from "remend";
// During streaming
const partialMarkdown = "This is **bold text";
const completed = remend(partialMarkdown);
// Result: "This is bold text"
// With incomplete link
const partialLink = "Check out const completed = remend(partialLink" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">this link;
// Result: "Check out this link"
`
You can selectively disable specific completions by passing an options object. All options default to true:
`typescript
import remend from "remend";
// Disable link and KaTeX completion
const completed = remend(partialMarkdown, {
links: false,
katex: false,
});
`
Available options:
| Option | Description |
|--------|-------------|
| links | Complete incomplete links |images
| | Complete incomplete images |bold
| | Complete bold formatting (**) |italic
| | Complete italic formatting (* and _) |boldItalic
| | Complete bold-italic formatting (*) |inlineCode
| | Complete inline code formatting ( `) |strikethrough
| | Complete strikethrough formatting (~~) |katex
| | Complete block KaTeX math ($$) |setextHeadings
| | Handle incomplete setext headings |handlers
| | Custom handlers to extend remend |
You can extend remend with custom handlers to complete your own markers during streaming. This is useful for custom syntax like << blocks or other domain-specific patterns.
`typescript
import remend, { type RemendHandler } from "remend";
const jokeHandler: RemendHandler = {
name: "joke",
handle: (text) => {
// Complete <<
const match = text.match(/<<
if (match && !text.endsWith("<<
return ${text}<<;
}
return text;
},
priority: 80, // Runs after most built-ins (0-70)
};
const result = remend(content, { handlers: [jokeHandler] });
`
#### Handler Interface
`typescript`
interface RemendHandler {
name: string; // Unique identifier
handle: (text: string) => string; // Transform function
priority?: number; // Lower runs first (default: 100)
}
#### Built-in Priorities
Built-in handlers use priorities 0-70. Custom handlers default to 100 (run after built-ins):
| Handler | Priority |
|---------|----------|
| setextHeadings | 0 |links
| | 10 |boldItalic
| | 20 |bold
| | 30 |italic
| | 40-42 |inlineCode
| | 50 |strikethrough
| | 60 |katex
| | 70 |
| Custom (default) | 100 |
#### Exported Utilities
Remend exports utility functions for context detection in custom handlers:
`typescript
import {
isWithinCodeBlock,
isWithinMathBlock,
isWithinLinkOrImageUrl,
isWordChar,
} from "remend";
const handler: RemendHandler = {
name: "custom",
handle: (text) => {
// Skip if we're inside a code block
if (isWithinCodeBlock(text, text.length - 1)) {
return text;
}
// Your logic here
return text;
},
};
`
Remend is a preprocessor that must be run on the raw Markdown string before passing it into the unified/remark processing pipeline:
`typescript
import remend from "remend";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
const streamedMarkdown = "This is **incomplete bold";
// Run Remend first to complete incomplete syntax
const completedMarkdown = remend(streamedMarkdown);
// Then process with unified
const file = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeStringify)
.process(completedMarkdown);
console.log(String(file));
``
This is important because Remend operates on the raw string level, while remark/unified work with abstract syntax trees (ASTs). Running Remend after parsing would be ineffective.
Remend analyzes the input text and:
1. Detects incomplete formatting markers at the end of the text
2. Counts opening vs closing markers (considering escaped characters)
3. Intelligently adds closing markers when needed
4. Respects context like code blocks, math blocks, and list items
5. Handles edge cases like nested brackets and word-internal characters
The parser is designed to be defensive and only completes formatting when it's unambiguous that the block is incomplete.
Remend is built for high-performance streaming scenarios:
- Direct string iteration instead of regex splits
- ASCII fast-path for common characters
- Minimal memory allocations
- Early returns for common cases
For more info, see the documentation.