Convert Markdown formatting to WhatsApp message formatting
npm install md-to-whatsapp


Convert Markdown formatting to WhatsApp message formatting. Use it as a CLI tool or import it as a library.
``bashnpm
npm install md-to-whatsapp
For CLI-only usage:
`bash
npx md-to-whatsapp --help
`Formatting Conversion
| Element | Markdown | WhatsApp |
|---------|----------|----------|
| Bold |
text or __text__ | text |
| Italic | text or _text_ | _text_ |
| ~~Strikethrough~~ | ~~text~~ | ~text~ |
| Code | code | ``code` | `code` `` | ``code` |- item or * item | - item |1. item | 1. item |> quote | > quote |These Markdown elements are not natively supported by WhatsApp:
- Headers (#, ##, ###)
- Links text
- Images !alt
- Tables
- Horizontal rules (---)
The converter handles these gracefully based on the configured mode (see Options).
``bashPipe input
echo "bold and italic" | md-to-whatsappOutput: bold and _italic_
$3
`
--mode How to handle unsupported elements (default: warn)
strict - Throw error when unsupported elements found
strip - Remove unsupported elements
warn - Log warning and convert gracefully
ignore - Pass through unchanged--help, -h Show help message
`Library Usage
$3
`typescript
import { convert, convertToString } from 'md-to-whatsapp';// Simple conversion
const whatsappText = convertToString('bold and italic');
console.log(whatsappText); // "bold and _italic_"
// With result object
const result = convert('bold and italic');
console.log(result.text); // "bold and _italic_"
console.log(result.unsupportedElements); // []
`$3
`typescript
import { convert } from 'md-to-whatsapp';const markdown =
This is bold and a link.;
const result = convert(markdown, {
unsupportedMode: 'warn',
onUnsupported: (element) => {
console.warn(Unsupported: ${element.type} at line ${element.position?.start.line});
}
});
console.log(result.text);
// Welcome
//
// This is bold and a link (https://example.com).
console.log(result.unsupportedElements);
// [{ type: 'heading', position: {...} }, { type: 'link', position: {...} }]
`
| Mode | Behavior |
|------|----------|
| strict | Throws an error when unsupported elements are found |strip
| | Removes unsupported elements entirely |warn
| | Converts gracefully (headers → bold, links → text (url)) |ignore
| | Passes through as plain text |
Converts Markdown to WhatsApp format and returns detailed result.
Parameters:
- markdown: string - The Markdown text to convertoptions?: ConvertOptions
- - Optional configuration
Returns: ConvertResult
`typescript`
interface ConvertResult {
text: string; // The converted WhatsApp text
unsupportedElements: UnsupportedElement[]; // List of unsupported elements found
}
Converts Markdown to WhatsApp format and returns only the text.
Parameters:
- markdown: string - The Markdown text to convertoptions?: ConvertOptions
- - Optional configuration
Returns: string
`typescript
type UnsupportedMode = 'strict' | 'strip' | 'warn' | 'ignore';
interface ConvertOptions {
unsupportedMode?: UnsupportedMode; // Default: 'warn'
onUnsupported?: (element: UnsupportedElement) => void;
}
interface UnsupportedElement {
type: string;
value?: string;
position?: {
start: { line: number; column: number };
end: { line: number; column: number };
};
}
`
`typescript
import { readFileSync } from 'fs';
import { convertToString } from 'md-to-whatsapp';
const markdown = readFileSync('README.md', 'utf8');
const whatsappText = convertToString(markdown, {
unsupportedMode: 'warn'
});
console.log(whatsappText);
`
`typescript
import { convert } from 'md-to-whatsapp';
try {
convert('# Header not allowed', { unsupportedMode: 'strict' });
} catch (error) {
console.error('Document contains unsupported elements');
}
`
`typescript
import { convert } from 'md-to-whatsapp';
const unsupported: string[] = [];
const result = convert(markdown, {
onUnsupported: (el) => unsupported.push(el.type)
});
if (unsupported.length > 0) {
console.log(Warning: Found ${unsupported.length} unsupported elements);`
}
This package uses a native Rust implementation for high-performance markdown parsing via pulldown-cmark. The Rust code is exposed to Node.js through N-API bindings using napi-rs.
- macOS (x64, ARM64)
- Linux (x64, ARM64)
- Windows (x64)
`bashInstall dependencies
pnpm install
- Node.js 18+
- pnpm
- Rust toolchain (for building from source)
MIT