Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update parse mode plugin docs #520

Merged
merged 20 commits into from
Dec 30, 2022
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 123 additions & 16 deletions site/docs/plugins/parse-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,134 @@

This plugin provides a transformer for setting default `parse_mode`, and a middleware for hydrating `Context` with familiar `reply` variant methods - i.e. `replyWithHTML`, `replyWithMarkdown`, etc.

## Usage
## Usage (Using format)
KnightNiwrem marked this conversation as resolved.
Show resolved Hide resolved
rojvv marked this conversation as resolved.
Show resolved Hide resolved

<CodeGroup>
<CodeGroupItem title="TypeScript" active>

```ts
import { Bot } from "grammy";
import { Bot, Composer, Context } from "grammy";
rojvv marked this conversation as resolved.
Show resolved Hide resolved
import { bold, fmt, hydrateReply, italic } from "@grammyjs/parse-mode";
rojvv marked this conversation as resolved.
Show resolved Hide resolved

import type { ParseModeFlavor } from "@grammyjs/parse-mode";

const bot = new Bot<ParseModeFlavor<Context>>("");

// Install format reply variant to ctx
rojvv marked this conversation as resolved.
Show resolved Hide resolved
bot.use(hydrateReply);

bot.command("demo", async (ctx) => {
await ctx.replyFmt(fmt`${bold("bold!")}
${bold(italic("bitalic!"))}
${bold(fmt`bold ${link("blink", "example.com")} bold`)}`);
KnightNiwrem marked this conversation as resolved.
Show resolved Hide resolved

// fmt can also be called like any other function
rojvv marked this conversation as resolved.
Show resolved Hide resolved
await ctx.replyFmt(
fmt(
["", " and ", " and ", ""],
fmt`${bold("bold")}`,
fmt`${bold(italic("bitalic"))}`,
fmt`${italic("italic")}`,
),
);
});

bot.start();
```

</CodeGroupItem>
<CodeGroupItem title="JavaScript">

```js
const { Bot, Composer, Context } = require("grammy");
rojvv marked this conversation as resolved.
Show resolved Hide resolved
const { bold, fmt, hydrateReply, italic } = require("@grammyjs/parse-mode");
rojvv marked this conversation as resolved.
Show resolved Hide resolved

const bot = new Bot("");

// Install format reply variant to ctx
rojvv marked this conversation as resolved.
Show resolved Hide resolved
bot.use(hydrateReply);

bot.command("demo", async (ctx) => {
await ctx.replyFmt(fmt`${bold("bold!")}
${bold(italic("bitalic!"))}
${bold(fmt`bold ${link("blink", "example.com")} bold`)}`);

// fmt can also be called like any other function
rojvv marked this conversation as resolved.
Show resolved Hide resolved
await ctx.replyFmt(
fmt(
["", " and ", " and ", ""],
fmt`${bold("bold")}`,
fmt`${bold(italic("bitalic"))}`,
fmt`${italic("italic")}`,
),
);
});

bot.start();
```

</CodeGroupItem>
<CodeGroupItem title="Deno">

```ts
import { Bot, Composer, Context } from "https://deno.land/x/grammy/mod.ts";
rojvv marked this conversation as resolved.
Show resolved Hide resolved
import {
bold,
fmt,
hydrateReply,
italic,
rojvv marked this conversation as resolved.
Show resolved Hide resolved
} from "https://deno.land/x/grammy_parse_mode/mod.ts";

import type { ParseModeFlavor } from "https://deno.land/x/grammy_parse_mode/mod.ts";

const bot = new Bot<ParseModeFlavor<Context>>("");

// Install format reply variant to ctx
rojvv marked this conversation as resolved.
Show resolved Hide resolved
bot.use(hydrateReply);

bot.command("demo", async (ctx) => {
await ctx.replyFmt(fmt`${bold("bold!")}
${bold(italic("bitalic!"))}
${bold(fmt`bold ${link("blink", "example.com")} bold`)}`);

// fmt can also be called like any other function
rojvv marked this conversation as resolved.
Show resolved Hide resolved
await ctx.replyFmt(
fmt(
["", " and ", " and ", ""],
fmt`${bold("bold")}`,
fmt`${bold(italic("bitalic"))}`,
fmt`${italic("italic")}`,
),
);
});

bot.start();
```

</CodeGroupItem>
</CodeGroup>

## Usage (Using default parse mode and utility reply methods)
rojvv marked this conversation as resolved.
Show resolved Hide resolved

<CodeGroup>
<CodeGroupItem title="TypeScript" active>

```ts
import { Bot, Composer, Context } from "grammy";
import { hydrateReply, parseMode } from "@grammyjs/parse-mode";

import type { ParseModeContext } from "@grammyjs/parse-mode";
import type { ParseModeFlavor } from "@grammyjs/parse-mode";

const bot = new Bot<ParseModeContext>("");
const bot = new Bot<ParseModeFlavor<Context>>("");

// Use the plugin.
// Install familiar reply variants to ctx
rojvv marked this conversation as resolved.
Show resolved Hide resolved
bot.use(hydrateReply);

// Sets default parse_mode for ctx.reply
bot.api.config.use(parseMode("MarkdownV2"));

bot.command("demo", async (ctx) => {
await ctx.reply("*This* reply uses _MarkdownV2_ as the default `formatting`");
await ctx.reply("*This* is _the_ default `formatting`");
await ctx.replyWithHTML(
"<b>This</b> is <i>withHTML</i> <code>formatting</code>",
);
Expand All @@ -38,19 +145,19 @@ bot.start();
<CodeGroupItem title="JavaScript">

```js
const { Bot } = require("grammy");
const { Bot, Composer, Context } = require("grammy");
const { hydrateReply, parseMode } = require("@grammyjs/parse-mode");

const bot = new Bot("");

// Use the plugin.
// Install familiar reply variants to ctx
rojvv marked this conversation as resolved.
Show resolved Hide resolved
bot.use(hydrateReply);

// Set the default `parse_mode` of `ctx.reply`.
// Sets default parse_mode for ctx.reply
rojvv marked this conversation as resolved.
Show resolved Hide resolved
bot.api.config.use(parseMode("MarkdownV2"));

bot.command("demo", async (ctx) => {
await ctx.reply("*This* reply uses _MarkdownV2_ as the default `formatting`");
await ctx.reply("*This* is _the_ default `formatting`");
await ctx.replyWithHTML(
"<b>This</b> is <i>withHTML</i> <code>formatting</code>",
);
Expand All @@ -66,24 +173,24 @@ bot.start();
<CodeGroupItem title="Deno">

```ts
import { Bot } from "https://deno.land/x/grammy/mod.ts";
import { Bot, Composer, Context } from "https://deno.land/x/grammy/mod.ts";
import {
hydrateReply,
parseMode,
} from "https://deno.land/x/grammy_parse_mode/mod.ts";

import type { ParseModeContext } from "https://deno.land/x/grammy_parse_mode/mod.ts";
import type { ParseModeFlavor } from "https://deno.land/x/grammy_parse_mode/mod.ts";

const bot = new Bot<ParseModeContext>("");
const bot = new Bot<ParseModeFlavor<Context>>("");

// Use the plugin.
// Install familiar reply variants to ctx
rojvv marked this conversation as resolved.
Show resolved Hide resolved
bot.use(hydrateReply);

// Set the default `parse_mode` of `ctx.reply`.
// Sets default parse_mode for ctx.reply
rojvv marked this conversation as resolved.
Show resolved Hide resolved
bot.api.config.use(parseMode("MarkdownV2"));

bot.command("demo", async (ctx) => {
await ctx.reply("*This* reply uses _MarkdownV2_ as the default `formatting`");
await ctx.reply("*This* is _the_ default `formatting`");
await ctx.replyWithHTML(
"<b>This</b> is <i>withHTML</i> <code>formatting</code>",
);
Expand Down