@telegram-md/core 1.0.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @telegram-md/core
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [`3c677a5`](https://github.com/vlad-iakovlev/telegram-md/commit/3c677a50ce5071d3561165d3e196ed77ed7d0a8c) Thanks [@vlad-iakovlev](https://github.com/vlad-iakovlev)! - Initial monorepo release
8
+ - Latest `@telegram-md/core` release notes: https://github.com/vlad-iakovlev/telegram-md/releases/tag/v2.1.0
9
+ - Latest `@telegram-md/grammy-plugin` release notes: https://github.com/vlad-iakovlev/grammy-reply-with-markdown/releases/tag/v2.0.1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Vladislav Iakovlev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,132 @@
1
+ # @telegram-md/core
2
+
3
+ Telegram MarkdownV2 formatter
4
+
5
+ [![NPM](https://img.shields.io/npm/v/@telegram-md/core)](https://www.npmjs.org/package/@telegram-md/core)
6
+
7
+ `@telegram-md/core` is a powerful TypesScript library designed to simplify the formatting of text messages in Telegram MarkdownV2 format. It provides a range of methods for applying markdown formatting, such as bold, italic, and links, making it easier to create richly formatted messages programmatically.
8
+
9
+ ## Table of Contents
10
+
11
+ 1. [How to Install](#how-to-install)
12
+ 2. [Usage Examples](#usage-examples)
13
+ 3. [API Documentation](#api-documentation)
14
+
15
+ ## How to install
16
+
17
+ ```sh
18
+ npm install @telegram-md/core
19
+ ```
20
+
21
+ ## Usage Examples
22
+
23
+ ```ts
24
+ import { md } from '@telegram-md/core'
25
+
26
+ // Simple message formatting
27
+ const message = md`Hello, ${md.bold('World')}!`
28
+ api.sendMessage(chatId, md.build(message))
29
+
30
+ // Using different formatting styles
31
+ const complexMessage = md`
32
+ This is an ${md.italic('italic')}
33
+ and ${md.bold('bold')} text
34
+ with a ${md.link('link', 'http://example.com')}!
35
+ `
36
+ api.sendMessage(chatId, md.build(complexMessage))
37
+ ```
38
+
39
+ ## API Documentation
40
+
41
+ Every method escapes all unescaped input. Input is assumed to be escaped only when it's an instance of Markdown.
42
+
43
+ ### Markdown
44
+
45
+ Stores the result of executing md methods. Used to differentiate between normal strings and escaped strings.
46
+
47
+ ### md
48
+
49
+ Template tag which can be used to build markdown formatted messages.
50
+
51
+ ```ts
52
+ md`Hello, ${md.bold('World')}!` // => Markdown with value 'Hello, *World*\\!'
53
+ ```
54
+
55
+ ### md.build
56
+
57
+ Returns message text that cat be safely sent to telegram API.
58
+
59
+ ```ts
60
+ md.build(md`Hello, ${md.bold('World')}!`) // => 'Hello, *World*\\!'
61
+ md.build('Hello, World!') // => 'Hello, World\\!'
62
+ ```
63
+
64
+ ### md.bold
65
+
66
+ ```ts
67
+ md.bold('bold *text') // => Markdown with value '*bold \*text*'
68
+ ```
69
+
70
+ ### md.italic
71
+
72
+ ```ts
73
+ md.italic('italic *text') // => Markdown with value '_italic \*text_'
74
+ ```
75
+
76
+ ### md.underline
77
+
78
+ ```ts
79
+ md.underline('underline') // => Markdown with value '__underline__'
80
+ ```
81
+
82
+ ### md.strikethrough
83
+
84
+ ```ts
85
+ md.strikethrough('strikethrough') // => Markdown with value '~strikethrough~'
86
+ ```
87
+
88
+ ### md.spoiler
89
+
90
+ ```ts
91
+ md.spoiler('spoiler') // => Markdown with value '||spoiler||'
92
+ ```
93
+
94
+ ### md.link
95
+
96
+ ```ts
97
+ md.link('inline URL', 'http://www.example.com/') // => Markdown with value '[inline URL](http://www\\.example\\.com/)'
98
+ md.link('inline mention of a user', 'tg://user?id=123456789') // => Markdown with value '[inline mention of a user](tg://user?id\\=123456789)'
99
+ ```
100
+
101
+ ### md.inlineCode
102
+
103
+ ```ts
104
+ md.inlineCode('inline fixed-width code') // => Markdown with value '`inline fixed\\-width code`'
105
+ ```
106
+
107
+ ### md.blockquote
108
+
109
+ ```ts
110
+ md.blockquote('blockquote') // => Markdown with value '>blockquote'
111
+ ```
112
+
113
+ ### md.codeBlock
114
+
115
+ ````ts
116
+ md.code(
117
+ 'pre-formatted fixed-width code block',
118
+ ) // => Markdown with value
119
+ ```
120
+ pre\\-formatted fixed\\-width code block
121
+ ```
122
+ ````
123
+
124
+ ````ts
125
+ md.code(
126
+ 'pre-formatted fixed-width code block written in the Python',
127
+ 'python',
128
+ ) // => Markdown with value
129
+ ```python
130
+ pre\\-formatted fixed\\-width code block written in the Python
131
+ ```
132
+ ````
@@ -0,0 +1 @@
1
+ export declare const _escape: (text: string) => string;
@@ -0,0 +1 @@
1
+ export const _escape = (text) => text.replace(/([_*[\]()~`>#+\-=|{}.!])/g, '\\$1');
@@ -0,0 +1,2 @@
1
+ import { Markdown } from './markdown.js';
2
+ export declare const _toMarkdown: (text?: unknown, escaped?: boolean) => Markdown;
@@ -0,0 +1,7 @@
1
+ import { Markdown } from './markdown.js';
2
+ export const _toMarkdown = (text, escaped) => {
3
+ if (text instanceof Markdown) {
4
+ return text;
5
+ }
6
+ return new Markdown(text, escaped);
7
+ };
@@ -0,0 +1,7 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Make text with blockquote
4
+ * @example
5
+ * md.blockquote('text') // => Markdown with value '>text'
6
+ */
7
+ export declare const blockquote: (text: unknown) => Markdown;
@@ -0,0 +1,12 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Make text with blockquote
4
+ * @example
5
+ * md.blockquote('text') // => Markdown with value '>text'
6
+ */
7
+ export const blockquote = (text) => {
8
+ if (!text) {
9
+ return _toMarkdown();
10
+ }
11
+ return _toMarkdown(`>${_toMarkdown(text)}`, true);
12
+ };
package/dist/bold.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Make text bold
4
+ * @example
5
+ * md.bold('bold *text') // => Markdown with value '*bold \*text*'
6
+ */
7
+ export declare const bold: (text: unknown) => Markdown;
package/dist/bold.js ADDED
@@ -0,0 +1,12 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Make text bold
4
+ * @example
5
+ * md.bold('bold *text') // => Markdown with value '*bold \*text*'
6
+ */
7
+ export const bold = (text) => {
8
+ if (!text) {
9
+ return _toMarkdown();
10
+ }
11
+ return _toMarkdown(`*${_toMarkdown(text)}*`, true);
12
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Build message text for telegram API
3
+ * @example
4
+ * md.build(md`Hello, ${md.bold('World')}!`) // => 'Hello, *World*\\!'
5
+ * md.build('Hello, World!') // => 'Hello, World\\!'
6
+ */
7
+ export declare const build: (text: unknown) => string;
package/dist/build.js ADDED
@@ -0,0 +1,8 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Build message text for telegram API
4
+ * @example
5
+ * md.build(md`Hello, ${md.bold('World')}!`) // => 'Hello, *World*\\!'
6
+ * md.build('Hello, World!') // => 'Hello, World\\!'
7
+ */
8
+ export const build = (text) => _toMarkdown(text).value;
@@ -0,0 +1,11 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Create code block
4
+ * @example
5
+ * md.code('pre-formatted fixed-width code block')
6
+ * // => Markdown with value '```\npre\\-formatted fixed\\-width code block\n```'
7
+ *
8
+ * md.code('pre-formatted fixed-width code block written in the Python', 'python')
9
+ * // => Markdown with value '```python\npre\\-formatted fixed\\-width code block written in the Python\n```'
10
+ */
11
+ export declare const codeBlock: (code: unknown, language?: string) => Markdown;
@@ -0,0 +1,16 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Create code block
4
+ * @example
5
+ * md.code('pre-formatted fixed-width code block')
6
+ * // => Markdown with value '```\npre\\-formatted fixed\\-width code block\n```'
7
+ *
8
+ * md.code('pre-formatted fixed-width code block written in the Python', 'python')
9
+ * // => Markdown with value '```python\npre\\-formatted fixed\\-width code block written in the Python\n```'
10
+ */
11
+ export const codeBlock = (code, language = '') => {
12
+ if (!code) {
13
+ return _toMarkdown();
14
+ }
15
+ return _toMarkdown(`\`\`\`${_toMarkdown(language)}\n${_toMarkdown(code)}\n\`\`\``, true);
16
+ };
@@ -0,0 +1,21 @@
1
+ import { Markdown } from './markdown.js';
2
+ export { Markdown };
3
+ /**
4
+ * Creates Markdown from template
5
+ * @example
6
+ * md`Hello, ${md.bold('World')}!` // => Markdown with value 'Hello, *World*\\!'
7
+ */
8
+ export declare const md: {
9
+ (strings: TemplateStringsArray, ...values: unknown[]): Markdown;
10
+ blockquote: (text: unknown) => Markdown;
11
+ bold: (text: unknown) => Markdown;
12
+ build: (text: unknown) => string;
13
+ codeBlock: (code: unknown, language?: string) => Markdown;
14
+ inlineCode: (code: unknown) => Markdown;
15
+ italic: (text: unknown) => Markdown;
16
+ join: (texts: unknown[], separator?: unknown) => Markdown;
17
+ link: (name: unknown, url: unknown) => Markdown;
18
+ spoiler: (text: unknown) => Markdown;
19
+ strikethrough: (text: unknown) => Markdown;
20
+ underline: (text: unknown) => Markdown;
21
+ };
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ import { blockquote } from './blockquote.js';
3
+ import { bold } from './bold.js';
4
+ import { build } from './build.js';
5
+ import { codeBlock } from './codeBlock.js';
6
+ import { inlineCode } from './inlineCode.js';
7
+ import { italic } from './italic.js';
8
+ import { join } from './join.js';
9
+ import { link } from './link.js';
10
+ import { Markdown } from './markdown.js';
11
+ import { spoiler } from './spoiler.js';
12
+ import { strikethrough } from './strikethrough.js';
13
+ import { underline } from './underline.js';
14
+ // istanbul ignore next
15
+ export { Markdown };
16
+ /**
17
+ * Creates Markdown from template
18
+ * @example
19
+ * md`Hello, ${md.bold('World')}!` // => Markdown with value 'Hello, *World*\\!'
20
+ */
21
+ export const md = (strings, ...values) => {
22
+ let result = `${_toMarkdown(strings[0])}`;
23
+ for (let i = 1; i < strings.length; i += 1) {
24
+ result += `${_toMarkdown(values[i - 1])}${_toMarkdown(strings[i])}`;
25
+ }
26
+ return _toMarkdown(result, true);
27
+ };
28
+ md.blockquote = blockquote;
29
+ md.bold = bold;
30
+ md.build = build;
31
+ md.codeBlock = codeBlock;
32
+ md.inlineCode = inlineCode;
33
+ md.italic = italic;
34
+ md.join = join;
35
+ md.link = link;
36
+ md.spoiler = spoiler;
37
+ md.strikethrough = strikethrough;
38
+ md.underline = underline;
@@ -0,0 +1,7 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Create inline code
4
+ * @example
5
+ * md.inlineCode('inline fixed-width code') // => Markdown with value '`inline fixed\\-width code`'
6
+ */
7
+ export declare const inlineCode: (code: unknown) => Markdown;
@@ -0,0 +1,12 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Create inline code
4
+ * @example
5
+ * md.inlineCode('inline fixed-width code') // => Markdown with value '`inline fixed\\-width code`'
6
+ */
7
+ export const inlineCode = (code) => {
8
+ if (!code) {
9
+ return _toMarkdown();
10
+ }
11
+ return _toMarkdown(`\`${_toMarkdown(code)}\``, true);
12
+ };
@@ -0,0 +1,7 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Make text italic
4
+ * @example
5
+ * md.italic('italic *text') // => Markdown with value '_italic \*text_'
6
+ */
7
+ export declare const italic: (text: unknown) => Markdown;
package/dist/italic.js ADDED
@@ -0,0 +1,12 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Make text italic
4
+ * @example
5
+ * md.italic('italic *text') // => Markdown with value '_italic \*text_'
6
+ */
7
+ export const italic = (text) => {
8
+ if (!text) {
9
+ return _toMarkdown();
10
+ }
11
+ return _toMarkdown(`_${_toMarkdown(text)}_`, true);
12
+ };
package/dist/join.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Join items
4
+ * @example
5
+ * md.join(['foo_bar', 'bar_baz'], '\n') // => Markdown with value 'foo\\_bar\nbar\\_baz'
6
+ */
7
+ export declare const join: (texts: unknown[], separator?: unknown) => Markdown;
package/dist/join.js ADDED
@@ -0,0 +1,12 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Join items
4
+ * @example
5
+ * md.join(['foo_bar', 'bar_baz'], '\n') // => Markdown with value 'foo\\_bar\nbar\\_baz'
6
+ */
7
+ export const join = (texts, separator = '') => {
8
+ const value = texts
9
+ .map((text) => _toMarkdown(text))
10
+ .join(String(_toMarkdown(separator)));
11
+ return _toMarkdown(value, true);
12
+ };
package/dist/link.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Creates link
4
+ * @example
5
+ * md.link('inline URL', 'http://www.example.com/')
6
+ * // => Markdown with value '[inline URL](http://www\\.example\\.com/)'
7
+ *
8
+ * md.link('inline mention of a user', 'tg://user?id=123456789')
9
+ * // => Markdown with value '[inline mention of a user](tg://user?id\\=123456789)'
10
+ */
11
+ export declare const link: (name: unknown, url: unknown) => Markdown;
package/dist/link.js ADDED
@@ -0,0 +1,22 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Creates link
4
+ * @example
5
+ * md.link('inline URL', 'http://www.example.com/')
6
+ * // => Markdown with value '[inline URL](http://www\\.example\\.com/)'
7
+ *
8
+ * md.link('inline mention of a user', 'tg://user?id=123456789')
9
+ * // => Markdown with value '[inline mention of a user](tg://user?id\\=123456789)'
10
+ */
11
+ export const link = (name, url) => {
12
+ if (!name && !url) {
13
+ return _toMarkdown();
14
+ }
15
+ if (!name) {
16
+ return _toMarkdown(url);
17
+ }
18
+ if (!url) {
19
+ return _toMarkdown(name);
20
+ }
21
+ return _toMarkdown(`[${_toMarkdown(name)}](${_toMarkdown(url)})`, true);
22
+ };
@@ -0,0 +1,5 @@
1
+ export declare class Markdown {
2
+ value: string;
3
+ constructor(value?: unknown, escaped?: boolean);
4
+ toString(): string;
5
+ }
@@ -0,0 +1,10 @@
1
+ import { _escape } from './_escape.js';
2
+ export class Markdown {
3
+ value;
4
+ constructor(value = '', escaped = false) {
5
+ this.value = escaped ? String(value) : _escape(String(value));
6
+ }
7
+ toString() {
8
+ return this.value;
9
+ }
10
+ }
@@ -0,0 +1,7 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Creates spoiler
4
+ * @example
5
+ * md.spoiler('spoiler') // => Markdown with value '||spoiler||'
6
+ */
7
+ export declare const spoiler: (text: unknown) => Markdown;
@@ -0,0 +1,12 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Creates spoiler
4
+ * @example
5
+ * md.spoiler('spoiler') // => Markdown with value '||spoiler||'
6
+ */
7
+ export const spoiler = (text) => {
8
+ if (!text) {
9
+ return _toMarkdown();
10
+ }
11
+ return _toMarkdown(`||${_toMarkdown(text)}||`, true);
12
+ };
@@ -0,0 +1,7 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Strikethrough text
4
+ * @example
5
+ * md.strikethrough('strikethrough') // => Markdown with value '~strikethrough~'
6
+ */
7
+ export declare const strikethrough: (text: unknown) => Markdown;
@@ -0,0 +1,12 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Strikethrough text
4
+ * @example
5
+ * md.strikethrough('strikethrough') // => Markdown with value '~strikethrough~'
6
+ */
7
+ export const strikethrough = (text) => {
8
+ if (!text) {
9
+ return _toMarkdown();
10
+ }
11
+ return _toMarkdown(`~${_toMarkdown(text)}~`, true);
12
+ };
@@ -0,0 +1,7 @@
1
+ import { Markdown } from './markdown.js';
2
+ /**
3
+ * Underline text
4
+ * @example
5
+ * md.underline('underline') // => Markdown with value '__underline__'
6
+ */
7
+ export declare const underline: (text: unknown) => Markdown;
@@ -0,0 +1,12 @@
1
+ import { _toMarkdown } from './_toMarkdown.js';
2
+ /**
3
+ * Underline text
4
+ * @example
5
+ * md.underline('underline') // => Markdown with value '__underline__'
6
+ */
7
+ export const underline = (text) => {
8
+ if (!text) {
9
+ return _toMarkdown();
10
+ }
11
+ return _toMarkdown(`__${_toMarkdown(text)}__`, true);
12
+ };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@telegram-md/core",
3
+ "version": "1.0.0",
4
+ "description": "Telegram MarkdownV2 parser",
5
+ "keywords": [
6
+ "telegram",
7
+ "markdown",
8
+ "parser",
9
+ "md"
10
+ ],
11
+ "homepage": "https://github.com/vlad-iakovlev/telegram-md#readme",
12
+ "bugs": "https://github.com/vlad-iakovlev/telegram-md/issues",
13
+ "repository": "https://github.com/vlad-iakovlev/telegram-md.git",
14
+ "license": "MIT",
15
+ "author": "Vladislav Iakovlev",
16
+ "type": "module",
17
+ "exports": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "scripts": {
20
+ "build": "tsc",
21
+ "compile": "tsc --noEmit"
22
+ }
23
+ }