@grammyjs/parse-mode
Advanced tools
Comparing version 1.5.0 to 1.6.0
import type { MessageEntity } from "./deps.node.js"; | ||
/** | ||
* Objects that implement this interface implement a `.toString()` | ||
* method that returns a `string` value representing the object. | ||
*/ | ||
export interface Stringable { | ||
/** | ||
* Returns the string representation of this object | ||
*/ | ||
toString(): string; | ||
} | ||
/** | ||
* Represents the formatted string after the parsing. | ||
*/ | ||
declare class FormattedString implements Stringable { | ||
/** | ||
* Plain text value for this `FormattedString` | ||
*/ | ||
text: string; | ||
/** | ||
* Format entities for this `FormattedString` | ||
*/ | ||
entities: MessageEntity[]; | ||
/** | ||
* Creates a new `FormattedString`. Useful for constructing a | ||
* `FormattedString` from user's formatted message | ||
* @param text Plain text value | ||
* @param entities Format entities | ||
* | ||
* ```ts | ||
* // Constructing a new `FormattedString` from user's message | ||
* const userMsg = new FormattedString(ctx.message.text, ctx.entities()); | ||
* ``` | ||
*/ | ||
constructor(text: string, entities: MessageEntity[]); | ||
/** | ||
* Returns the string representation of this object | ||
*/ | ||
toString(): string; | ||
} | ||
/** | ||
* Formats the `Stringable` as bold. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
declare const bold: (stringLike: Stringable) => FormattedString; | ||
/** | ||
* Formats the `Stringable` as inline code. Cannot be combined with any other formats. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
declare const code: (stringLike: Stringable) => FormattedString; | ||
/** | ||
* Formats the `Stringable` as italic. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
declare const italic: (stringLike: Stringable) => FormattedString; | ||
declare const link: (stringLike: Stringable, formatArgs_0: string) => FormattedString; | ||
declare const pre: (stringLike: Stringable, formatArgs_0: string) => FormattedString; | ||
/** | ||
* Formats the `Stringable` as a link. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
* @param url The URL to link to. | ||
*/ | ||
declare const link: (stringLike: Stringable, url: string) => FormattedString; | ||
/** | ||
* Formats the `Stringable` as a code block. Cannot be combined with any other formats. | ||
* @param stringLike The `Stringable` to format. | ||
* @param language The language of the code block. | ||
*/ | ||
declare const pre: (stringLike: Stringable, language: string) => FormattedString; | ||
/** | ||
* Formats the `Stringable` as a spoiler. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
declare const spoiler: (stringLike: Stringable) => FormattedString; | ||
/** | ||
* Formats the `Stringable` as a strikethrough. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
declare const strikethrough: (stringLike: Stringable) => FormattedString; | ||
/** | ||
* Formats the `Stringable` as a underline. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
declare const underline: (stringLike: Stringable) => FormattedString; | ||
/** | ||
* Formats the `Stringable` as an internal Telegram link to a user. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
* @param userId The user ID to link to. | ||
*/ | ||
declare const mentionUser: (stringLike: Stringable, userId: number) => FormattedString; | ||
/** | ||
* Formats the `Stringable`` as a Telegram link to a chat message. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
* @param chatId The chat ID to link to. | ||
* @param messageId The message ID to link to. | ||
*/ | ||
declare const linkMessage: (stringLike: Stringable, chatId: number, messageId: number) => Stringable; | ||
/** | ||
* This is the format tagged template function. It accepts a template literal | ||
* containing any mix of `Stringable` and `string` values, and constructs a | ||
* `FormattedString` that represents the combination of all the given values. | ||
* The constructed `FormattedString` also implements Stringable, and can be used | ||
* in further `fmt` tagged templates. | ||
* @param rawStringParts An array of `string` parts found in the tagged template | ||
* @param stringLikes An array of `Stringable`s found in the tagged template | ||
* | ||
* ```ts | ||
* // Using return values of fmt in fmt | ||
* const left = fmt`${bold('>>>')} >>>`; | ||
* const right = fmt`<<< ${bold('<<<')}`; | ||
* | ||
* const final = fmt`${left} ${ctx.msg.text} ${right}`; | ||
* await ctx.replyFmt(final); | ||
* ``` | ||
*/ | ||
declare const fmt: (rawStringParts: TemplateStringsArray | string[], ...stringLikes: Stringable[]) => FormattedString; | ||
export { bold, code, fmt, FormattedString, italic, link, linkMessage, mentionUser, pre, spoiler, strikethrough, underline, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.underline = exports.strikethrough = exports.spoiler = exports.pre = exports.mentionUser = exports.linkMessage = exports.link = exports.italic = exports.FormattedString = exports.fmt = exports.code = exports.bold = void 0; | ||
/** | ||
* Represents the formatted string after the parsing. | ||
*/ | ||
class FormattedString { | ||
/** | ||
* Creates a new `FormattedString`. Useful for constructing a | ||
* `FormattedString` from user's formatted message | ||
* @param text Plain text value | ||
* @param entities Format entities | ||
* | ||
* ```ts | ||
* // Constructing a new `FormattedString` from user's message | ||
* const userMsg = new FormattedString(ctx.message.text, ctx.entities()); | ||
* ``` | ||
*/ | ||
constructor(text, entities) { | ||
@@ -9,2 +23,5 @@ this.text = text; | ||
} | ||
/** | ||
* Returns the string representation of this object | ||
*/ | ||
toString() { | ||
@@ -33,20 +50,59 @@ return this.text; | ||
}; | ||
// Native entity functions | ||
// === Native entity functions | ||
/** | ||
* Formats the `Stringable` as bold. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
const bold = buildFormatter("bold"); | ||
exports.bold = bold; | ||
/** | ||
* Formats the `Stringable` as inline code. Cannot be combined with any other formats. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
const code = buildFormatter("code"); | ||
exports.code = code; | ||
/** | ||
* Formats the `Stringable` as italic. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
const italic = buildFormatter("italic"); | ||
exports.italic = italic; | ||
/** | ||
* Formats the `Stringable` as a link. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
* @param url The URL to link to. | ||
*/ | ||
const link = buildFormatter("text_link", "url"); | ||
exports.link = link; | ||
/** | ||
* Formats the `Stringable` as a code block. Cannot be combined with any other formats. | ||
* @param stringLike The `Stringable` to format. | ||
* @param language The language of the code block. | ||
*/ | ||
const pre = buildFormatter("pre", "language"); | ||
exports.pre = pre; | ||
/** | ||
* Formats the `Stringable` as a spoiler. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
const spoiler = buildFormatter("spoiler"); | ||
exports.spoiler = spoiler; | ||
/** | ||
* Formats the `Stringable` as a strikethrough. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
const strikethrough = buildFormatter("strikethrough"); | ||
exports.strikethrough = strikethrough; | ||
/** | ||
* Formats the `Stringable` as a underline. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
*/ | ||
const underline = buildFormatter("underline"); | ||
exports.underline = underline; | ||
// Utility functions | ||
/** | ||
* Formats the `Stringable` as an internal Telegram link to a user. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
* @param userId The user ID to link to. | ||
*/ | ||
const mentionUser = (stringLike, userId) => { | ||
@@ -56,2 +112,8 @@ return link(stringLike, `tg://user?id=${userId}`); | ||
exports.mentionUser = mentionUser; | ||
/** | ||
* Formats the `Stringable`` as a Telegram link to a chat message. Incompatible with `code` and `pre`. | ||
* @param stringLike The `Stringable` to format. | ||
* @param chatId The chat ID to link to. | ||
* @param messageId The message ID to link to. | ||
*/ | ||
const linkMessage = (stringLike, chatId, messageId) => { | ||
@@ -71,3 +133,21 @@ if (chatId > 0) { | ||
exports.linkMessage = linkMessage; | ||
// Root format function | ||
// === Format tagged template function | ||
/** | ||
* This is the format tagged template function. It accepts a template literal | ||
* containing any mix of `Stringable` and `string` values, and constructs a | ||
* `FormattedString` that represents the combination of all the given values. | ||
* The constructed `FormattedString` also implements Stringable, and can be used | ||
* in further `fmt` tagged templates. | ||
* @param rawStringParts An array of `string` parts found in the tagged template | ||
* @param stringLikes An array of `Stringable`s found in the tagged template | ||
* | ||
* ```ts | ||
* // Using return values of fmt in fmt | ||
* const left = fmt`${bold('>>>')} >>>`; | ||
* const right = fmt`<<< ${bold('<<<')}`; | ||
* | ||
* const final = fmt`${left} ${ctx.msg.text} ${right}`; | ||
* await ctx.replyFmt(final); | ||
* ``` | ||
*/ | ||
const fmt = (rawStringParts, ...stringLikes) => { | ||
@@ -74,0 +154,0 @@ let text = rawStringParts[0]; |
import type { Context, NextFunction } from "./deps.node.js"; | ||
import { type Stringable } from "./format.js"; | ||
declare type Tail<T extends Array<any>> = T extends [head: infer E1, ...tail: infer E2] ? E2 : []; | ||
declare type ParseModeFlavor<C extends Context> = C & { | ||
type Tail<T extends Array<any>> = T extends [head: infer E1, ...tail: infer E2] ? E2 : []; | ||
/** | ||
* Context flavor for `Context` that will be hydrated with | ||
* an additional set of reply methods from `hydrateReply` | ||
*/ | ||
type ParseModeFlavor<C extends Context> = C & { | ||
replyFmt: (stringLike: Stringable, ...args: Tail<Parameters<C["reply"]>>) => ReturnType<C["reply"]>; | ||
@@ -14,4 +18,9 @@ replyWithHTML: C["reply"]; | ||
*/ | ||
declare type ParseModeContext<C extends Context = Context> = ParseModeFlavor<C>; | ||
type ParseModeContext<C extends Context = Context> = ParseModeFlavor<C>; | ||
/** | ||
* Hydrates a context with an additional set of reply methods | ||
* @param ctx The context to hydrate | ||
* @param next The next middleware function | ||
*/ | ||
declare const middleware: <C extends Context>(ctx: ParseModeFlavor<C>, next: NextFunction) => Promise<void>; | ||
export { middleware as hydrateReply, type ParseModeFlavor, type ParseModeContext }; |
@@ -11,2 +11,7 @@ "use strict"; | ||
}; | ||
/** | ||
* Hydrates a context with an additional set of reply methods | ||
* @param ctx The context to hydrate | ||
* @param next The next middleware function | ||
*/ | ||
const middleware = async (ctx, next) => { | ||
@@ -13,0 +18,0 @@ ctx.replyFmt = (stringLike, ...args) => { |
import type { Transformer } from "./deps.node.js"; | ||
/** | ||
* Creates a new transformer for the given parse mode. | ||
* @param parseMode {string} The parse mode to use. If the parse mode is not in the well known parse modes map, it will be used as is. | ||
* @see https://core.telegram.org/bots/api#formatting-options for well known parse modes. | ||
* @returns {Transformer} The transformer. | ||
*/ | ||
declare const buildTransformer: (parseMode: string) => Transformer<import("grammy").RawApi>; | ||
export { buildTransformer as parseMode }; |
@@ -9,2 +9,8 @@ "use strict"; | ||
]); | ||
/** | ||
* Creates a new transformer for the given parse mode. | ||
* @param parseMode {string} The parse mode to use. If the parse mode is not in the well known parse modes map, it will be used as is. | ||
* @see https://core.telegram.org/bots/api#formatting-options for well known parse modes. | ||
* @returns {Transformer} The transformer. | ||
*/ | ||
const buildTransformer = (parseMode) => { | ||
@@ -11,0 +17,0 @@ const normalisedParseMode = wellKnownParseModesMap.get(parseMode.toLowerCase()) ?? parseMode; |
{ | ||
"name": "@grammyjs/parse-mode", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Plugin for hydrating ctx with familiar reply variants and for setting default parse_mode", | ||
@@ -24,8 +24,7 @@ "author": "KnightNiwrem", | ||
"devDependencies": { | ||
"@grammyjs/types": "^2.9.1", | ||
"@grammyjs/types": "^2.11.0", | ||
"@tsconfig/node16": "^1.0.2", | ||
"@types/node": "^16.6.1", | ||
"deno2node": "^1.4.0", | ||
"grammy": "^1.11.2", | ||
"typescript": "^4.5.5" | ||
"deno2node": "^1.7.1", | ||
"grammy": "^1.13.0" | ||
}, | ||
@@ -32,0 +31,0 @@ "files": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19796
5
398