npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

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

Readme

@darcas/wa-format

Buy me a coffee Dynamic JSON Badge

Bidirectional WhatsApp formatting library: toHTML converts WhatsApp-formatted text into HTML, while toWA converts 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.

Node.js TypeScript Tests

npm NPM Downloads

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-format

Set up the repository for local development:

git clone https://github.com/DarCas/wa-format.git
cd wa-format
npm install

Requirements:

  • Node.js >=20
  • Runtime dependencies: @modelcontextprotocol/server and zod, used only by the MCP server. toHTML and toWA have no dependencies of their own.
  • Development-only dependencies are typescript and @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 \n between 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.
  • toWA is 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 tests

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

  1. Fork the repository.
  2. Create a feature branch.
  3. Make your changes.
  4. Run npm run typecheck and npm test.
  5. Open a pull request.

Repository: https://github.com/DarCas/wa-format

Issues: https://github.com/DarCas/wa-format/issues

License

MIT