@darcas/wa-format
v1.0.0
Published
Convert WhatsApp formatted text into HTML and back. Supports bold, italic, strikethrough, inline code, code blocks, block quotes and lists. Works in Node.js and the browser, and exposes an MCP server.
Maintainers
Readme
@darcas/wa-format
Bidirectional WhatsApp formatting library:
toHTMLconverts WhatsApp-formatted text into HTML, whiletoWAconverts supported HTML back into WhatsApp markup. The converters are dependency-free; the bundled MCP server uses the Model Context Protocol SDK. Works in Node.js and modern browsers, and ships ESM, CommonJS and TypeScript declarations.
import {toHTML, toWA} from "@darcas/wa-format"
toHTML("*Hello* _world_") // => "<b>Hello</b> <i>world</i>"
toWA("<b>Hello</b> <i>world</i>") // => "*Hello* _world_"Installation
Install the published package:
npm install @darcas/wa-formatSet up the repository for local development:
git clone https://github.com/DarCas/wa-format.git
cd wa-format
npm installRequirements:
- Node.js
>=20 - Runtime dependencies:
@modelcontextprotocol/serverandzod, used only by the MCP server.toHTMLandtoWAhave no dependencies of their own. - Development-only dependencies are
typescriptand@types/node
Usage
ESM
import {toHTML} from "@darcas/wa-format"
const html = toHTML("*Bold*, _italic_, ~strike~ and `code`.")CommonJS
const {toHTML} = require("@darcas/wa-format")
const html = toHTML("*Bold* and _italic_.")Browser
<script type="module">
import {toHTML} from "https://esm.sh/@darcas/wa-format"
document.querySelector("#message").innerHTML = toHTML("*Hello* _world_")
</script>MCP server
The package ships an MCP server, wa-format-mcp, that exposes the two
converters as tools: whatsapp_to_html and html_to_whatsapp. It uses the
stdio transport (Node.js 20 or later).
Register it in an MCP client (Claude Desktop, Codex, Cursor, …):
{
"mcpServers": {
"wa-format": {
"command": "npx",
"args": [
"-y",
"-p",
"@darcas/wa-format",
"wa-format-mcp"
]
}
}
}| Tool | Arguments | Returns |
|--------------------|--------------------------------------------------|-------------------------|
| whatsapp_to_html | text (required), style, lineBreaks, trim | HTML |
| html_to_whatsapp | html (required), bullet | WhatsApp-formatted text |
The arguments mirror the options of toHTML and toWA. Invalid arguments are
rejected by the tool schema.
Both tools are advertised as read-only and side-effect free (readOnlyHint: true,
destructiveHint: false, idempotentHint: true,
openWorldHint: false), so clients can auto-approve them.
Supported formatting
| Format | WhatsApp syntax | HTML output |
|---------------|-----------------------------------|---------------------------------|
| Bold | *text* | <b>text</b> |
| Italic | _text_ | <i>text</i> |
| Strikethrough | ~text~ | <s>text</s> |
| Inline code | `text` | <code>text</code> |
| Code block | ```text``` | <pre><code>text</code></pre> |
| Block quote | > text (line start) | <blockquote>text</blockquote> |
| Bulleted list | - text or * text (line start) | <ul><li>text</li></ul> |
| Numbered list | 1. text (line start) | <ol><li>text</li></ol> |
Inline styles can be nested and combined in any order:
toHTML("*_bold italic_*") // "<b><i>bold italic</i></b>"
toHTML("_*bold italic*_") // "<i><b>bold italic</b></i>"API
toHTML(input, options?)
Converts a WhatsApp-formatted string to HTML and returns it.
| Option | Type | Default | Description |
|--------------|--------------------------|--------------|-----------------------------------------------------------------------------|
| style | "semantic" \| "legacy" | "semantic" | semantic emits <b>/<i>/<s>; legacy emits <strong>/<em>/<s>. |
| lineBreaks | boolean | true | Render \n as <br>. When false, newlines are kept as \n. |
| trim | boolean | true | Trim leading/trailing whitespace from the input before parsing. |
Empty or whitespace-only input returns "". A non-string input throws a TypeError.
toWA(html, options?)
The inverse of toHTML: converts HTML into WhatsApp-formatted text.
| Option | Type | Default | Description |
|----------|--------------|---------|---------------------------------|
| bullet | "-" \| "*" | "-" | Marker used for bulleted lists. |
Recognized tags: <b>/<strong>, <i>/<em>, <s>/<strike>/<del>,
<code>, <pre>, <blockquote>, <ul>/<ol>/<li> (with start),
<a href> (kept as text (url)), <br>, and the line containers
<p>/<div>/<h1>–<h6>. Unknown tags are unwrapped and entities are decoded.
toWA("<b>Hello</b> <i>world</i>") // => "*Hello* _world_"
toWA("<blockquote>a<br>b</blockquote>") // => "> a\n> b"
toWA("<ul><li>one</li><li>two</li></ul>") // => "- one\n- two"parse(input)
Parses WhatsApp-formatted text into an AST (Document) instead of HTML. Input
line endings are normalized before parsing. Useful for custom rendering or
inspection. See src/ast.ts for the node types.
renderHtml(document, options)
Renders a Document produced by parse to HTML with resolved options.
import {parse, renderHtml, resolveOptions} from "@darcas/wa-format"
const doc = parse("*Hello*")
const html = renderHtml(doc, resolveOptions())parseHtml(html)
Parses an HTML fragment into a lightweight node tree (HtmlNode[]). Lower-level
building block used by toWA.
Additional exports include resolveOptions, DEFAULT_OPTIONS, FormatOptions,
ResolvedOptions, ToWAOptions and the AST node types.
Behavior notes
The implementation follows these rules:
- Markers must touch their content.
* bold *is not bold;*bold*is. A marker that sits inside a word (snake_case,2*3*4) is left literal. - No escaping character. WhatsApp has none, and neither does this library. To
show a marker literally, wrap the text in code (
`*not bold*`). - Code is opaque. Markers inside
`code`or```code```are never parsed, and code content is HTML-escaped. - Lists require a space after the marker:
- text,* text,1. text. Numbered lists accept up to two digits. Nested lists are not supported by WhatsApp and are not produced here. - Consecutive quote lines are grouped into a single
<blockquote>, with<br>between lines. - All text is HTML-escaped (
&,<,>,",'); the output is safe to inject into a container element. - Newlines: a
\nbetween text lines becomes<br>. A newline that joins a block element (list, quote, code block) to its neighbours is represented by the element itself. A blank line next to a block element is absorbed. toWAis best-effort and lossy. WhatsApp has no escape character, so a literal*/_/~/`in the text, marker runs adjacent to word characters (<b>a</b>b→*a*b), and code containing backticks cannot round-trip exactly. Whitespace at the edges of a style is moved outside its markers. The guaranteed property is HTML idempotence:toHTML(toWA(html)) === toHTML(html).
Development
Package entry points:
- ESM:
./dist/esm/index.js - CommonJS:
./dist/cjs/index.js - Types:
./dist/esm/index.d.ts - MCP binary:
./dist/esm/mcp/bin.js(wa-format-mcp)
Available scripts:
npm install # install dependencies and dev tooling
npm run build # emit dist/esm (ESM + .d.ts) and dist/cjs (CommonJS)
npm test # build, then run the node:test suite (80 tests)
npm run typecheck # type-check src and testsThe manifest also defines deploy, which builds and publishes the package
publicly. Tests run on the built output (dist/). The test runner is the Node.js
built-in node:test; no test framework is used. The MCP server is compiled into
dist/esm only (the CJS build excludes src/mcp).
Contributing
Contributions are welcome. Please open an issue to discuss significant changes before submitting a pull request.
- Fork the repository.
- Create a feature branch.
- Make your changes.
- Run
npm run typecheckandnpm test. - Open a pull request.
Repository: https://github.com/DarCas/wa-format
Issues: https://github.com/DarCas/wa-format/issues
