telegram-md2html 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +320 -306
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +410 -0
- package/dist/index.umd.js +423 -0
- package/package.json +12 -17
package/README.md
CHANGED
|
@@ -1,306 +1,320 @@
|
|
|
1
|
-
# Telegram Markdown to HTML Converter
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/telegram-md2html)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
[](https://nodejs.org/)
|
|
9
|
+
[](https://www.npmjs.com/package/telegram-md2html)
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Complete Telegram Markdown Support**: All Telegram-specific formatting options
|
|
14
|
+
- **Smart Parsing**: Handles nested styles and complex formatting
|
|
15
|
+
- **HTML Safety**: Automatic escaping of HTML special characters
|
|
16
|
+
- **Code Block Support**: Inline code and multiline code blocks with language specification
|
|
17
|
+
- **Blockquote Support**: Regular and expandable blockquotes
|
|
18
|
+
- **Customizable**: Extensible with custom processors
|
|
19
|
+
- **TypeScript Ready**: Full TypeScript definitions included
|
|
20
|
+
- **Dual Module Support**: Works with both CommonJS and ES Modules
|
|
21
|
+
- **Browser Compatible**: Can be used in modern browsers
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install telegram-md2html
|
|
27
|
+
# or
|
|
28
|
+
yarn add telegram-md2html
|
|
29
|
+
# or
|
|
30
|
+
pnpm add telegram-md2html
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
// CommonJS
|
|
37
|
+
const { markdownToHtml } = require('telegram-md2html');
|
|
38
|
+
|
|
39
|
+
// ES Modules
|
|
40
|
+
import { markdownToHtml } from 'telegram-md2html';
|
|
41
|
+
|
|
42
|
+
// Convert Telegram markdown to HTML
|
|
43
|
+
const html = markdownToHtml('**Hello** *World*!');
|
|
44
|
+
console.log(html);
|
|
45
|
+
// Output: <b>Hello</b> <i>World</i>!
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage Examples
|
|
49
|
+
|
|
50
|
+
### Basic Conversion
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
import { markdownToHtml } from 'telegram-md2html';
|
|
54
|
+
|
|
55
|
+
const markdown = `
|
|
56
|
+
# Welcome to Telegram Bot
|
|
57
|
+
|
|
58
|
+
**Bold text** and *italic text*
|
|
59
|
+
__Underlined__ and ~~strikethrough~~
|
|
60
|
+
||Spoiler text||
|
|
61
|
+
|
|
62
|
+
\`inline code\`
|
|
63
|
+
|
|
64
|
+
[Visit Google](https://google.com)
|
|
65
|
+
|
|
66
|
+
> This is a quote
|
|
67
|
+
**> Expandable quote
|
|
68
|
+
|
|
69
|
+
\`\`\`javascript
|
|
70
|
+
console.log("Hello World");
|
|
71
|
+
\`\`\`
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
const html = markdownToHtml(markdown);
|
|
75
|
+
console.log(html);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Advanced Usage with Options
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
import { createConverter } from 'telegram-md2html';
|
|
82
|
+
|
|
83
|
+
// Create a converter with custom options
|
|
84
|
+
const converter = createConverter({
|
|
85
|
+
escapeHtml: true,
|
|
86
|
+
autoCloseCodeBlocks: true,
|
|
87
|
+
linkProcessor: (url, text) =>
|
|
88
|
+
`<a href="${url}" target="_blank" rel="noopener">${text}</a>`,
|
|
89
|
+
codeBlockProcessor: (code, language) =>
|
|
90
|
+
`<pre><code class="language-${language || 'text'}">${code}</code></pre>`
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const html = converter.convert('**[Important Link](https://example.com)**');
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Supported Markdown Syntax
|
|
97
|
+
|
|
98
|
+
| Markdown | HTML Output | Description |
|
|
99
|
+
|----------|-------------|-------------|
|
|
100
|
+
| `**text**` | `<b>text</b>` | Bold text |
|
|
101
|
+
| `*text*` or `_text_` | `<i>text</i>` | Italic text |
|
|
102
|
+
| `__text__` | `<u>text</u>` | Underlined text |
|
|
103
|
+
| `~~text~~` | `<s>text</s>` | Strikethrough text |
|
|
104
|
+
| `\|\|text\|\|` | `<span class="tg-spoiler">text</span>` | Spoiler text |
|
|
105
|
+
| `` `code` `` | `<code>code</code>` | Inline code |
|
|
106
|
+
| ```` ```language\ncode\n``` ```` | `<pre><code class="language-xxx">code</code></pre>` | Code block |
|
|
107
|
+
| `[text](url)` | `<a href="url">text</a>` | Link |
|
|
108
|
+
| `> text` | `<blockquote>text</blockquote>` | Blockquote |
|
|
109
|
+
| `**> text` | `<blockquote expandable>text</blockquote>` | Expandable blockquote |
|
|
110
|
+
|
|
111
|
+
## API Reference
|
|
112
|
+
|
|
113
|
+
### `markdownToHtml(text: string, options?: ConvertOptions): string`
|
|
114
|
+
|
|
115
|
+
Main conversion function that converts Telegram-style markdown to HTML.
|
|
116
|
+
|
|
117
|
+
**Parameters:**
|
|
118
|
+
- `text`: The markdown text to convert
|
|
119
|
+
- `options`: Optional conversion options
|
|
120
|
+
|
|
121
|
+
**Returns:** Telegram-compatible HTML string
|
|
122
|
+
|
|
123
|
+
### `createConverter(options?: ConvertOptions): MarkdownConverter`
|
|
124
|
+
|
|
125
|
+
Creates a converter instance with custom options for reuse.
|
|
126
|
+
|
|
127
|
+
### ConvertOptions Interface
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
interface ConvertOptions {
|
|
131
|
+
/** Whether to escape HTML special characters (default: true) */
|
|
132
|
+
escapeHtml?: boolean;
|
|
133
|
+
|
|
134
|
+
/** Whether to auto-close unclosed code blocks (default: true) */
|
|
135
|
+
autoCloseCodeBlocks?: boolean;
|
|
136
|
+
|
|
137
|
+
/** Custom link processor function */
|
|
138
|
+
linkProcessor?: (url: string, text: string) => string;
|
|
139
|
+
|
|
140
|
+
/** Custom code block processor function */
|
|
141
|
+
codeBlockProcessor?: (code: string, language?: string) => string;
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## TypeScript Support
|
|
146
|
+
|
|
147
|
+
The library includes full TypeScript definitions. Just import and use:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { markdownToHtml, ConvertOptions } from 'telegram-md2html';
|
|
151
|
+
|
|
152
|
+
const options: ConvertOptions = {
|
|
153
|
+
escapeHtml: false,
|
|
154
|
+
linkProcessor: (url: string, text: string): string => {
|
|
155
|
+
return `<a href="${url}" class="custom-link">${text}</a>`;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const html: string = markdownToHtml('**TypeScript** works!', options);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Browser Usage
|
|
163
|
+
|
|
164
|
+
The library can be used in modern browsers:
|
|
165
|
+
|
|
166
|
+
### Using ES Modules (Recommended)
|
|
167
|
+
|
|
168
|
+
```html
|
|
169
|
+
<script type="module">
|
|
170
|
+
import { markdownToHtml } from 'https://cdn.jsdelivr.net/npm/telegram-md2html/dist/index.mjs';
|
|
171
|
+
|
|
172
|
+
const html = markdownToHtml('**Hello** from browser!');
|
|
173
|
+
document.getElementById('output').innerHTML = html;
|
|
174
|
+
</script>
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Using from GitHub
|
|
178
|
+
|
|
179
|
+
```html
|
|
180
|
+
<script type="module">
|
|
181
|
+
import { markdownToHtml } from 'https://cdn.jsdelivr.net/gh/Soumyadeep765/telegram-md2html@main/dist/index.mjs';
|
|
182
|
+
|
|
183
|
+
const html = markdownToHtml('**Hello** World');
|
|
184
|
+
document.getElementById('output').innerHTML = html;
|
|
185
|
+
</script>
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Using a CDN
|
|
189
|
+
|
|
190
|
+
```html
|
|
191
|
+
<script src="https://cdn.jsdelivr.net/npm/telegram-md2html/dist/index.js"></script>
|
|
192
|
+
<script>
|
|
193
|
+
// Available as window.telegramMd2Html
|
|
194
|
+
const html = telegramMd2Html.markdownToHtml('**CDN** Example');
|
|
195
|
+
document.getElementById('output').innerHTML = html;
|
|
196
|
+
</script>
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Complex Nested Example
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
const result = markdownToHtml(`
|
|
203
|
+
**Welcome to our bot!**
|
|
204
|
+
|
|
205
|
+
Features:
|
|
206
|
+
• *Easy formatting*
|
|
207
|
+
• __Multiple styles__
|
|
208
|
+
• ~~Clean output~~
|
|
209
|
+
|
|
210
|
+
\`\`\`python
|
|
211
|
+
def greet():
|
|
212
|
+
print("Hello from Python!")
|
|
213
|
+
\`\`\`
|
|
214
|
+
|
|
215
|
+
> Remember: **Formatting** makes messages _better_
|
|
216
|
+
**> Click to expand details
|
|
217
|
+
`);
|
|
218
|
+
|
|
219
|
+
console.log(result);
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Error Handling
|
|
223
|
+
|
|
224
|
+
The library handles edge cases gracefully:
|
|
225
|
+
|
|
226
|
+
- Unclosed code blocks are automatically closed
|
|
227
|
+
- HTML characters are properly escaped by default
|
|
228
|
+
- Invalid markdown is treated as plain text
|
|
229
|
+
- Nested styles are processed correctly
|
|
230
|
+
|
|
231
|
+
## Performance
|
|
232
|
+
|
|
233
|
+
The library is optimized for performance:
|
|
234
|
+
- Efficient tokenization algorithm
|
|
235
|
+
- Minimal memory usage
|
|
236
|
+
- No external dependencies
|
|
237
|
+
- Fast parsing even for large documents
|
|
238
|
+
|
|
239
|
+
## Contributing
|
|
240
|
+
|
|
241
|
+
Contributions are welcome! Here's how you can help:
|
|
242
|
+
|
|
243
|
+
1. Fork the repository
|
|
244
|
+
2. Create a feature branch: `git checkout -b feature-name`
|
|
245
|
+
3. Make your changes
|
|
246
|
+
4. Add tests for new functionality
|
|
247
|
+
5. Run tests: `npm test`
|
|
248
|
+
6. Commit your changes: `git commit -am 'Add feature'`
|
|
249
|
+
7. Push to the branch: `git push origin feature-name`
|
|
250
|
+
8. Submit a pull request
|
|
251
|
+
|
|
252
|
+
### Development Setup
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
# Clone the repository
|
|
256
|
+
git clone https://github.com/Soumyadeep765/telegram-md2html.git
|
|
257
|
+
cd telegram-md2html
|
|
258
|
+
|
|
259
|
+
# Install dependencies
|
|
260
|
+
npm install
|
|
261
|
+
|
|
262
|
+
# Build the library
|
|
263
|
+
npm run build
|
|
264
|
+
|
|
265
|
+
# Run tests
|
|
266
|
+
npm test
|
|
267
|
+
|
|
268
|
+
# Watch mode for development
|
|
269
|
+
npm run dev
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## Testing
|
|
273
|
+
|
|
274
|
+
The library includes comprehensive tests:
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
# Run all tests
|
|
278
|
+
npm test
|
|
279
|
+
|
|
280
|
+
# Run tests with coverage
|
|
281
|
+
npm test -- --coverage
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## License
|
|
285
|
+
|
|
286
|
+
This project is licensed under the **MIT** License
|
|
287
|
+
|
|
288
|
+
## Support
|
|
289
|
+
|
|
290
|
+
If you find this library useful, please consider:
|
|
291
|
+
|
|
292
|
+
- Starring the repository on GitHub
|
|
293
|
+
- Sharing it with others
|
|
294
|
+
- Reporting issues
|
|
295
|
+
- Suggesting new features
|
|
296
|
+
|
|
297
|
+
## Changelog
|
|
298
|
+
|
|
299
|
+
### v1.0.1
|
|
300
|
+
- Initial release
|
|
301
|
+
- Complete Telegram markdown support
|
|
302
|
+
- TypeScript definitions
|
|
303
|
+
- Browser compatibility
|
|
304
|
+
- Custom processor support
|
|
305
|
+
|
|
306
|
+
## Author
|
|
307
|
+
|
|
308
|
+
**Soumyadeep Das**
|
|
309
|
+
- GitHub: [@Soumyadeep765](https://github.com/Soumyadeep765)
|
|
310
|
+
- Email: soumyadeepdas765@gmail.com
|
|
311
|
+
|
|
312
|
+
## Acknowledgments
|
|
313
|
+
|
|
314
|
+
- Telegram for their excellent Bot API
|
|
315
|
+
- All contributors and users of this library
|
|
316
|
+
- The open source community for inspiration and support
|
|
317
|
+
|
|
318
|
+
---
|
|
319
|
+
|
|
320
|
+
**Note**: This library is not affiliated with or endorsed by Telegram.
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED