@gramio/format
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,6 +0,9 @@ | ||
import { FormattableString } from "./index"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FormattableMap = void 0; | ||
const index_1 = require("./index"); | ||
/** @codegenerated */ | ||
export const FormattableMap = { | ||
exports.FormattableMap = { | ||
sendMessage: (params) => { | ||
if (params.text instanceof FormattableString) { | ||
if (params.text instanceof index_1.FormattableString) { | ||
params.entities = params.text.entities; | ||
@@ -11,3 +14,3 @@ params.text = params.text.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -20,3 +23,3 @@ params.reply_parameters.quote.entities; | ||
copyMessage: (params) => { | ||
if (params.caption instanceof FormattableString) { | ||
if (params.caption instanceof index_1.FormattableString) { | ||
params.caption_entities = params.caption.entities; | ||
@@ -27,3 +30,3 @@ params.caption = params.caption.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -36,3 +39,3 @@ params.reply_parameters.quote.entities; | ||
sendPhoto: (params) => { | ||
if (params.caption instanceof FormattableString) { | ||
if (params.caption instanceof index_1.FormattableString) { | ||
params.caption_entities = params.caption.entities; | ||
@@ -43,3 +46,3 @@ params.caption = params.caption.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -52,3 +55,3 @@ params.reply_parameters.quote.entities; | ||
sendAudio: (params) => { | ||
if (params.caption instanceof FormattableString) { | ||
if (params.caption instanceof index_1.FormattableString) { | ||
params.caption_entities = params.caption.entities; | ||
@@ -59,3 +62,3 @@ params.caption = params.caption.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -68,3 +71,3 @@ params.reply_parameters.quote.entities; | ||
sendDocument: (params) => { | ||
if (params.caption instanceof FormattableString) { | ||
if (params.caption instanceof index_1.FormattableString) { | ||
params.caption_entities = params.caption.entities; | ||
@@ -75,3 +78,3 @@ params.caption = params.caption.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -84,3 +87,3 @@ params.reply_parameters.quote.entities; | ||
sendVideo: (params) => { | ||
if (params.caption instanceof FormattableString) { | ||
if (params.caption instanceof index_1.FormattableString) { | ||
params.caption_entities = params.caption.entities; | ||
@@ -91,3 +94,3 @@ params.caption = params.caption.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -100,3 +103,3 @@ params.reply_parameters.quote.entities; | ||
sendAnimation: (params) => { | ||
if (params.caption instanceof FormattableString) { | ||
if (params.caption instanceof index_1.FormattableString) { | ||
params.caption_entities = params.caption.entities; | ||
@@ -107,3 +110,3 @@ params.caption = params.caption.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -116,3 +119,3 @@ params.reply_parameters.quote.entities; | ||
sendVoice: (params) => { | ||
if (params.caption instanceof FormattableString) { | ||
if (params.caption instanceof index_1.FormattableString) { | ||
params.caption_entities = params.caption.entities; | ||
@@ -123,3 +126,3 @@ params.caption = params.caption.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -134,3 +137,3 @@ params.reply_parameters.quote.entities; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -145,3 +148,3 @@ params.reply_parameters.quote.entities; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -156,3 +159,3 @@ params.reply_parameters.quote.entities; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -167,3 +170,3 @@ params.reply_parameters.quote.entities; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -178,3 +181,3 @@ params.reply_parameters.quote.entities; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -187,3 +190,3 @@ params.reply_parameters.quote.entities; | ||
sendPoll: (params) => { | ||
if (params.explanation instanceof FormattableString) { | ||
if (params.explanation instanceof index_1.FormattableString) { | ||
params.explanation_entities = params.explanation.entities; | ||
@@ -194,3 +197,3 @@ params.explanation = params.explanation.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -205,3 +208,3 @@ params.reply_parameters.quote.entities; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -214,3 +217,3 @@ params.reply_parameters.quote.entities; | ||
editMessageText: (params) => { | ||
if (params.text instanceof FormattableString) { | ||
if (params.text instanceof index_1.FormattableString) { | ||
params.entities = params.text.entities; | ||
@@ -222,3 +225,3 @@ params.text = params.text.text; | ||
editMessageCaption: (params) => { | ||
if (params.caption instanceof FormattableString) { | ||
if (params.caption instanceof index_1.FormattableString) { | ||
params.caption_entities = params.caption.entities; | ||
@@ -232,3 +235,3 @@ params.caption = params.caption.text; | ||
"caption" in params.media && | ||
params.media.caption instanceof FormattableString) { | ||
params.media.caption instanceof index_1.FormattableString) { | ||
params.media.caption_entities = params.media.caption.entities; | ||
@@ -242,3 +245,3 @@ params.media.caption = params.media.caption.text; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -252,7 +255,7 @@ params.reply_parameters.quote.entities; | ||
if (params.results?.length) | ||
params.results.map((x) => "caption" in x && x.caption instanceof FormattableString | ||
params.results.map((x) => "caption" in x && x.caption instanceof index_1.FormattableString | ||
? { ...x, caption_entities: x.caption.entities } | ||
: x); | ||
if (params.results?.length) | ||
params.results.map((x) => "message_text" in x && x.message_text instanceof FormattableString | ||
params.results.map((x) => "message_text" in x && x.message_text instanceof index_1.FormattableString | ||
? { ...x, entities: x.message_text.entities } | ||
@@ -265,3 +268,3 @@ : x); | ||
"caption" in params.result && | ||
params.result.caption instanceof FormattableString) { | ||
params.result.caption instanceof index_1.FormattableString) { | ||
params.result.caption_entities = params.result.caption.entities; | ||
@@ -274,3 +277,3 @@ params.result.caption = params.result.caption.text; | ||
params.result.input_message_content.message_text instanceof | ||
FormattableString) { | ||
index_1.FormattableString) { | ||
params.result.input_message_content.entities = | ||
@@ -286,3 +289,3 @@ params.result.input_message_content.message_text.entities; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -297,3 +300,3 @@ params.reply_parameters.quote.entities; | ||
"quote" in params.reply_parameters && | ||
params.reply_parameters.quote instanceof FormattableString) { | ||
params.reply_parameters.quote instanceof index_1.FormattableString) { | ||
params.reply_parameters.quote_entities = | ||
@@ -300,0 +303,0 @@ params.reply_parameters.quote.entities; |
@@ -6,2 +6,3 @@ import { TelegramMessageEntity, TelegramUser } from "@gramio/types"; | ||
} | ||
/** Class-helper for work with formattable entities */ | ||
export declare class FormattableString implements Stringable { | ||
@@ -14,13 +15,103 @@ text: string; | ||
} | ||
/** Format text as **bold**. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* bold`test` | ||
* format`test ${bold(italic("GramIO"))}` | ||
* ``` | ||
*/ | ||
export declare const bold: (str: Stringable) => FormattableString; | ||
/** Format text as _italic_. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* italic`test` | ||
* format`test ${italic(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
export declare const italic: (str: Stringable) => FormattableString; | ||
/** Format text as underline. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* underline`test` | ||
* format`test ${underline(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
export declare const underline: (str: Stringable) => FormattableString; | ||
/** Format text as ~strikethrough~. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* strikethrough`test` | ||
* format`test ${strikethrough(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
export declare const strikethrough: (str: Stringable) => FormattableString; | ||
/** Format text as spoiler. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* spoiler`test` | ||
* format`test ${spoiler(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
export declare const spoiler: (str: Stringable) => FormattableString; | ||
/** Format text as blockquote. Cannot be nested. | ||
* @example | ||
* ```ts | ||
* blockquote`test` | ||
* format`test ${blockquote(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
export declare const blockquote: (str: Stringable) => FormattableString; | ||
/** Format text as `code`. Cannot be combined with any other format. | ||
* @example | ||
* ```ts | ||
* pre`test` | ||
* format`test ${pre(`console.log("GramIO")`, "js"}` | ||
* ``` | ||
* pre with language result is | ||
* ```js | ||
* console.log("GramIO") | ||
* ``` | ||
*/ | ||
export declare const code: (str: Stringable) => FormattableString; | ||
/** Format text as ```pre```. Cannot be combined with any other format. | ||
* @example | ||
* ```ts | ||
* code`test` | ||
* format`test ${code("GramIO")}` | ||
* ``` | ||
*/ | ||
export declare const pre: (str: Stringable, language?: string | undefined) => FormattableString; | ||
/** Format text as [link](https://github.com/gramiojs/gramio). Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* link("test", "https://...") | ||
* format`test ${bold(link("GramIO", "https://github.com/gramiojs/gramio"))}` | ||
* ``` | ||
*/ | ||
export declare const link: (str: Stringable, url: string) => FormattableString; | ||
/** Format text as mention. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* mention("friend", { id: 228, is_bot: false, first_name: "GramIO"}) | ||
* format`test ${mention("friend", { id: 228, is_bot: false, first_name: "GramIO"})}` | ||
* ``` | ||
*/ | ||
export declare const mention: (str: Stringable, user: TelegramUser) => FormattableString; | ||
/** Insert custom emoji by their id. | ||
* @example | ||
* ```ts | ||
* customEmoji("⚔️", "5222106016283378623") | ||
* format`test ${customEmoji("⚔️", "5222106016283378623")}` | ||
* ``` | ||
*/ | ||
export declare const customEmoji: (str: Stringable, custom_emoji_id: string) => FormattableString; | ||
/** Template literal that helps construct message entities for text formatting | ||
* @example | ||
* ```ts | ||
* bot.api.sendMessage({ | ||
* chat_id: 12321, | ||
* text: format`${bold`Hi!`} Can ${italic(`you`)} help ${spoiler`me`}? Can you give me a ${link("star", "https://github.com/gramiojs/gramio")}?` | ||
* }) | ||
* ``` | ||
*/ | ||
export declare function format(stringParts: TemplateStringsArray, ...strings: Stringable[]): FormattableString; |
@@ -1,3 +0,21 @@ | ||
export * from "./formats"; | ||
export class FormattableString { | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.format = exports.customEmoji = exports.mention = exports.link = exports.pre = exports.code = exports.blockquote = exports.spoiler = exports.strikethrough = exports.underline = exports.italic = exports.bold = exports.FormattableString = void 0; | ||
__exportStar(require("./formats"), exports); | ||
/** Class-helper for work with formattable entities */ | ||
class FormattableString { | ||
text; | ||
@@ -20,2 +38,3 @@ entities; | ||
} | ||
exports.FormattableString = FormattableString; | ||
function getFormattable(str) { | ||
@@ -42,17 +61,107 @@ if (str instanceof FormattableString) | ||
} | ||
export const bold = buildFormatter("bold"); | ||
export const italic = buildFormatter("italic"); | ||
export const underline = buildFormatter("underline"); | ||
export const strikethrough = buildFormatter("strikethrough"); | ||
export const spoiler = buildFormatter("spoiler"); | ||
export const blockquote = buildFormatter("blockquote"); | ||
export const code = buildFormatter("code"); | ||
export const pre = buildFormatter("pre", "language"); | ||
export const link = buildFormatter("text_link", "url"); | ||
export const mention = buildFormatter("text_mention", | ||
/** Format text as **bold**. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* bold`test` | ||
* format`test ${bold(italic("GramIO"))}` | ||
* ``` | ||
*/ | ||
exports.bold = buildFormatter("bold"); | ||
/** Format text as _italic_. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* italic`test` | ||
* format`test ${italic(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
exports.italic = buildFormatter("italic"); | ||
/** Format text as underline. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* underline`test` | ||
* format`test ${underline(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
exports.underline = buildFormatter("underline"); | ||
/** Format text as ~strikethrough~. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* strikethrough`test` | ||
* format`test ${strikethrough(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
exports.strikethrough = buildFormatter("strikethrough"); | ||
/** Format text as spoiler. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* spoiler`test` | ||
* format`test ${spoiler(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
exports.spoiler = buildFormatter("spoiler"); | ||
/** Format text as blockquote. Cannot be nested. | ||
* @example | ||
* ```ts | ||
* blockquote`test` | ||
* format`test ${blockquote(bold("GramIO"))}` | ||
* ``` | ||
*/ | ||
exports.blockquote = buildFormatter("blockquote"); | ||
/** Format text as `code`. Cannot be combined with any other format. | ||
* @example | ||
* ```ts | ||
* pre`test` | ||
* format`test ${pre(`console.log("GramIO")`, "js"}` | ||
* ``` | ||
* pre with language result is | ||
* ```js | ||
* console.log("GramIO") | ||
* ``` | ||
*/ | ||
exports.code = buildFormatter("code"); | ||
/** Format text as ```pre```. Cannot be combined with any other format. | ||
* @example | ||
* ```ts | ||
* code`test` | ||
* format`test ${code("GramIO")}` | ||
* ``` | ||
*/ | ||
exports.pre = buildFormatter("pre", "language"); | ||
/** Format text as [link](https://github.com/gramiojs/gramio). Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* link("test", "https://...") | ||
* format`test ${bold(link("GramIO", "https://github.com/gramiojs/gramio"))}` | ||
* ``` | ||
*/ | ||
exports.link = buildFormatter("text_link", "url"); | ||
/** Format text as mention. Cannot be combined with `code` and `pre`. | ||
* @example | ||
* ```ts | ||
* mention("friend", { id: 228, is_bot: false, first_name: "GramIO"}) | ||
* format`test ${mention("friend", { id: 228, is_bot: false, first_name: "GramIO"})}` | ||
* ``` | ||
*/ | ||
exports.mention = buildFormatter("text_mention", | ||
//@ts-expect-error wrong typings.... but it's works fine | ||
"user"); | ||
export const customEmoji = buildFormatter("custom_emoji", "custom_emoji_id"); | ||
/** Insert custom emoji by their id. | ||
* @example | ||
* ```ts | ||
* customEmoji("⚔️", "5222106016283378623") | ||
* format`test ${customEmoji("⚔️", "5222106016283378623")}` | ||
* ``` | ||
*/ | ||
exports.customEmoji = buildFormatter("custom_emoji", "custom_emoji_id"); | ||
// [INFO] Thanks https://github.com/grammyjs/parse-mode/blob/49ba35bac208536edfa6e8d4ea665ea0f7fff522/src/format.ts#L213 | ||
export function format(stringParts, ...strings) { | ||
/** Template literal that helps construct message entities for text formatting | ||
* @example | ||
* ```ts | ||
* bot.api.sendMessage({ | ||
* chat_id: 12321, | ||
* text: format`${bold`Hi!`} Can ${italic(`you`)} help ${spoiler`me`}? Can you give me a ${link("star", "https://github.com/gramiojs/gramio")}?` | ||
* }) | ||
* ``` | ||
*/ | ||
function format(stringParts, ...strings) { | ||
const entities = []; | ||
@@ -74,1 +183,2 @@ let text = ""; | ||
} | ||
exports.format = format; |
{ | ||
"name": "@gramio/format", | ||
"version": "0.0.3", | ||
"description": "WIP", | ||
"version": "0.0.4", | ||
"description": "Library for formatting text for Telegram Bot API", | ||
"homepage": "https://gramio.netlify.app/formatting.html", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/gramiojs/format" | ||
}, | ||
"keywords": [ | ||
@@ -6,0 +11,0 @@ "gramio", |
# @gramio/format | ||
WIP | ||
Library for formatting text for Telegram Bot API | ||
## Usage | ||
```ts | ||
bot.api.sendMessage({ | ||
chat_id: 12321, | ||
text: format`${bold`Hi!`} Can ${italic("you")} help ${spoiler`me`}? Can you give me a ${link("star", "https://github.com/gramiojs/gramio")}?`, | ||
}); | ||
``` | ||
![example](https://gramio.netlify.app/formatting/example.png) | ||
## See [Documentation](https://gramio.netlify.app/formatting.html) |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
23832
579
1
17
0