A smart converter for Telegram-style Markdown to Telegram-compatible HTML
npm install telegram-md2htmlbash
npm install telegram-md2html
or
yarn add telegram-md2html
or
pnpm add telegram-md2html
`
Quick Start
`javascript
// CommonJS
const { markdownToHtml } = require('telegram-md2html');
// ES Modules
import { markdownToHtml } from 'telegram-md2html';
// Convert Telegram markdown to HTML
const html = markdownToHtml('Hello World!');
console.log(html);
// Output: Hello World!
`
Usage Examples
$3
`javascript
import { markdownToHtml } from 'telegram-md2html';
const markdown =
inline code\
\\javascript
console.log("Hello World");
\\\
;
const html = markdownToHtml(markdown);
console.log(html);
`
$3
`javascript
import { createConverter } from 'telegram-md2html';
// Create a converter with custom options
const converter = createConverter({
escapeHtml: true,
autoCloseCodeBlocks: true,
linkProcessor: (url, text) =>
${text},
codeBlockProcessor: (code, language) =>
${code}
});
const html = converter.convert('Important Link');
`
Supported Markdown Syntax
| Markdown | HTML Output | Description |
|----------|-------------|-------------|
| text | text | Bold text |
| text or _text_ | text | Italic text |
| __text__ | text | Underlined text |
| ~~text~~ | | Strikethrough text |
| \|\|text\|\| | text | Spoiler text |
| code | code | Inline code |
| `` `language\ncode\n` `` | code | Code block |
| text | text | Link |
| > text | text
| Blockquote |
| **> text | text
| Expandable blockquote |
API Reference
$3
Main conversion function that converts Telegram-style markdown to HTML.
Parameters:
- text: The markdown text to convert
- options: Optional conversion options
Returns: Telegram-compatible HTML string
$3
Creates a converter instance with custom options for reuse.
$3
`typescript
interface ConvertOptions {
/* Whether to escape HTML special characters (default: true) /
escapeHtml?: boolean;
/* Whether to auto-close unclosed code blocks (default: true) /
autoCloseCodeBlocks?: boolean;
/* Custom link processor function /
linkProcessor?: (url: string, text: string) => string;
/* Custom code block processor function /
codeBlockProcessor?: (code: string, language?: string) => string;
}
`
TypeScript Support
The library includes full TypeScript definitions. Just import and use:
`typescript
import { markdownToHtml, ConvertOptions } from 'telegram-md2html';
const options: ConvertOptions = {
escapeHtml: false,
linkProcessor: (url: string, text: string): string => {
return ${text};
}
};
const html: string = markdownToHtml('TypeScript works!', options);
`
Browser Usage
The library can be used in modern browsers:
$3
`html
`
$3
`html
`
$3
`html
`
$3
`javascript
const result = markdownToHtml(
\\python
def greet():
print("Hello from Python!")
\\\
);
console.log(result);
`
Error Handling
The library handles edge cases gracefully:
- Unclosed code blocks are automatically closed
- HTML characters are properly escaped by default
- Invalid markdown is treated as plain text
- Nested styles are processed correctly
Performance
The library is optimized for performance:
- Efficient tokenization algorithm
- Minimal memory usage
- No external dependencies
- Fast parsing even for large documents
Contributing
Contributions are welcome! Here's how you can help:
1. Fork the repository
2. Create a feature branch: git checkout -b feature-name
3. Make your changes
4. Add tests for new functionality
5. Run tests: npm test
6. Commit your changes: git commit -am 'Add feature'
7. Push to the branch: git push origin feature-name
8. Submit a pull request
$3
`bash
Clone the repository
git clone https://github.com/Soumyadeep765/telegram-md2html.git
cd telegram-md2html
Install dependencies
npm install
Build the library
npm run build
Run tests
npm test
Watch mode for development
npm run dev
`
Testing
The library includes comprehensive tests:
`bash
Run all tests
npm test
Run tests with coverage
npm test -- --coverage
``