@codebit-programando-solucoes/codebit-md-to-whatsapp
v1.1.11
Published
Converts Markdown written by LLMs into WhatsApp-compatible text
Readme
@codebit-programando-solucoes/codebit-md-to-whatsapp
Converts the Markdown that an LLM writes into text that WhatsApp renders correctly.
import { convertToString, convert, UnsupportedMode } from '@codebit-programando-solucoes/codebit-md-to-whatsapp';
convertToString('**Total:** R$ 4,73 — veja [o produto](https://ex.com/p/1)');
// '*Total:* R$ 4,73 — veja o produto https://ex.com/p/1'
const { text, unsupportedElements } = convert('# Título\n\n---\n\ntexto', { unsupportedMode: UnsupportedMode.Warn });
// text: '*Título*\n\ntexto'
// unsupportedElements: [{ elementType: 'heading' }, { elementType: 'thematicBreak' }]Convert once, right before sending. The conversion is not idempotent: converting text that is already in WhatsApp format turns *bold* into _italic_.
What is converted
| Markdown | WhatsApp |
|------------------------------------------|--------------------------------------------------------------|
| **bold**, __bold__ | *bold* |
| *italic*, _italic_ | _italic_ |
| ***both***, ___both___ | *_both_* |
| ~~strike~~ | ~strike~ |
| `code` | ```code``` |
| fenced block (```, ~~~) | ```code``` (language tag dropped, content never altered) |
| - item, + item, * item | * item (indentation kept) |
| [text](url) | text url |
|  | alt: url |
| <https://x.com>, <[email protected]> | the address |
| # Heading | *Heading* (reported as unsupported) |
| ---, ***, ___ on their own line | removed (reported as unsupported) |
| real HTML (<div>…</div>, <br>) | removed with its content (reported as unsupported) |
| \*, \_, \~, \`, \[, \# … | the escaped character |
For the [text](url) and <https://x.com> rows, a plain space (visible on purpose, since WhatsApp keeps invisible characters inside the link) is added after the URL when a letter, a digit, or . , ; : ! ? ) directly follows it, so WhatsApp does not link that character. Images, spaces, line breaks, the end of the text and *, _, ~ get no separator.
Anything else passes through untouched, including > quotes and numbered lists.
The parser is deliberately conservative about what counts as markup, so data is not corrupted:
- Formatting never spans a line break.
- A
*,_or~~needs the usual CommonMark flanking (5 * 3 = 15andSELECT * FROM tstay as written), and a run between two letters or digits is literal (snake_case,2*3*4, the***TXID of a Pix code). - Bare
http(s)://URLs are kept unchanged, except that a space is inserted after the URL when it is directly followed by a letter, a digit, or. , ; : ! ? ), so WhatsApp does not include that character in the link. <is only HTML when it starts a real HTML tag;5 < 10,<3,List<String>and<abc123>are text.#,---and bullets are only recognized at the start of a line (Linguagem C# é boais untouched).- Line endings are normalized to
\n.
Unsupported modes
UnsupportedMode controls headings, thematic breaks and raw HTML, which WhatsApp cannot render. Links and images are never reported as unsupported.
| Mode | Heading / thematic break / HTML | Link | Image |
|--------------------|------------------------------------------------------------|------------|------------|
| Warn (default) | heading becomes bold, the others are dropped; all recorded | text url | alt: url |
| Ignore | as Warn, but raw HTML is kept verbatim | text url | alt: url |
| Strip | as Warn | text | alt |
| Strict | throws UnsupportedElementError | text url | alt: url |
Errors
TypeErrorwhen the input is not a string (for example an empty LLM completion that isnull) or the mode is unknown.UnsupportedElementErrorinStrictmode.
Development
pnpm --filter @codebit-programando-solucoes/codebit-md-to-whatsapp test
pnpm --filter @codebit-programando-solucoes/codebit-md-to-whatsapp buildpnpm pack/publish runs prepack (build + obfuscate). Do not publish with --ignore-scripts: the package would ship readable code.
