Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ If you prefer package-style specifiers, add optional aliases for the surfaces yo
"imports": {
"@grammyjs/format/core": "https://denopkg.com/roziscoding/@grammyjs/format/src/core.ts",
"@grammyjs/format/components": "https://denopkg.com/roziscoding/@grammyjs/format/src/components.ts",
"@grammyjs/format/chaining": "https://denopkg.com/roziscoding/@grammyjs/format/src/chaining.ts",
},
"@grammyjs/format/chaining": "https://denopkg.com/roziscoding/@grammyjs/format/src/chaining.ts"
}
}
```

Expand All @@ -50,7 +50,7 @@ The examples below use these package-style specifiers; on Deno, either add the m
This library only builds the message object. grammY already exposes the Bot API's `ctx.replyWithRichMessage`, so composing and sending stay separate — compose with any of the [APIs below](#the-three-apis), then hand the result to grammY:

```tsx
import { Bot } from "grammy";
import { Bot } from "@grammyjs/grammy";
import { Bold, Paragraph, richMessage } from "@grammyjs/format/components";

const bot = new Bot(process.env.TELEGRAM_TOKEN!);
Expand All @@ -62,8 +62,7 @@ bot.command("start", (ctx) =>
Hello, <Bold>{ctx.from?.first_name ?? "there"}!</Bold>
</Paragraph>,
),
),
);
));

bot.start();
```
Expand Down Expand Up @@ -139,14 +138,12 @@ const message = new RichMessage({ skipEntityDetection: true })
.row((row) =>
row
.cell("Model", { header: true })
.cell("Score", { header: true, align: "right" }),
.cell("Score", { header: true, align: "right" })
)
.rows(results, (row, result) =>
row
.cell(result.model)
.cell(bold(result.score), { align: "right" }),
),
);
.cell(bold(result.score), { align: "right" })));
```

A `RichMessage` instance `implements InputRichMessage`, so it goes straight to grammY (or any Bot API client) and serializes to the canonical value via `toJSON()`. Every functional block builder has a matching method, including media (`photo`, `video`, …) and containers (`blockQuote`, `collage`, `details`, …). Use `.add(block)` to append any pre-built block, and read the `blocks` getter for a snapshot that later mutations don't change.
Expand All @@ -169,8 +166,8 @@ For Deno, add the JSX runtime import alias so the compiler can resolve `jsxImpor
```jsonc
{
"imports": {
"@grammyjs/format/jsx/jsx-runtime": "https://denopkg.com/roziscoding/@grammyjs/format/src/components/jsx-runtime.ts",
},
"@grammyjs/format/jsx/jsx-runtime": "https://denopkg.com/roziscoding/@grammyjs/format/src/components/jsx-runtime.ts"
}
}
```

Expand Down
8 changes: 5 additions & 3 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"imports": {
"@grammyjs/format/jsx/jsx-runtime": "./src/components/jsx-runtime.ts",
"@grammyjs/format/jsx/jsx-dev-runtime": "./src/components/jsx-runtime.ts",
"@grammyjs/grammy": "jsr:@grammyjs/grammy@2.0.0-beta.8",
"@std/expect": "jsr:@std/expect@^1",
"@std/testing": "jsr:@std/testing@^1",
"grammy": "npm:grammy@^1.45.1"
"@std/testing": "jsr:@std/testing@^1"
},
"tasks": {
"check": "deno check --allow-import src/core.ts src/components.ts src/chaining.ts test/ examples/",
Expand All @@ -36,5 +36,7 @@
"rules": {
"exclude": ["explicit-function-return-type"]
}
}
},
// TODO: Remove this (?)
"minimumDependencyAge": { "age": 0 }
}
2 changes: 1 addition & 1 deletion examples/bot.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot } from "grammy";
import { Bot } from "@grammyjs/grammy";
import {
Bold,
Divider,
Expand Down
2 changes: 1 addition & 1 deletion examples/core/slideshow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot } from "grammy";
import { Bot } from "@grammyjs/grammy";
import { divider, photo, richMessage, slideshow } from "../../src/core.ts";

const bot = new Bot(process.env.BOT_TOKEN!);
Expand Down
2 changes: 1 addition & 1 deletion src/chaining/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
cloneValue,
type RichMessageValue,
} from "../core/values.ts";
import type { InputRichMessage } from "grammy/types";
import type { InputRichMessage } from "@grammyjs/grammy/types";
import { TableBuilder, type TableConfigurator } from "./table.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/chaining/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
type TableCellValue,
type TableRowValue,
} from "../core/values.ts";
import type { InputRichBlockTable } from "grammy/types";
import type { InputRichBlockTable } from "@grammyjs/grammy/types";

/**
* Callback that populates a {@link TableRowBuilder}. Receives the row builder
Expand Down
2 changes: 1 addition & 1 deletion src/components/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
InputMediaVoiceNote,
InputRichBlock,
Location,
} from "grammy/types";
} from "@grammyjs/grammy/types";
import * as Blocks from "../core/blocks.ts";
import type { RichTextInput } from "../core/text.ts";
import type {
Expand Down
2 changes: 1 addition & 1 deletion src/components/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Child } from "./jsx-runtime.ts";
import * as Text from "../core/text.ts";
import type { ChildrenProps, NoChildrenProps } from "./shared.ts";
import type { RichTextValue } from "../core/values.ts";
import type * as grammY from "grammy/types";
import type * as grammY from "@grammyjs/grammy/types";

function richChildren(children: Child): Text.RichTextInput[] {
return children === undefined ? [] : [children as Text.RichTextInput];
Expand Down
4 changes: 2 additions & 2 deletions src/core/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
InputRichBlockListItem,
Location,
RichBlockTableCell,
} from "grammy/types";
} from "@grammyjs/grammy/types";
import {
brand,
type BlockValueOf,
Expand Down Expand Up @@ -115,7 +115,7 @@ export interface HeadingOptions {
export function heading(
options: HeadingOptions,
...children: readonly RichTextInput[]
): ReturnType<typeof block> {
): BrandedValue<InputRichBlock, "block"> {
return block({
type: "heading",
text: richText(children, "heading()"),
Expand Down
2 changes: 1 addition & 1 deletion src/core/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { InputRichMessage } from "grammy/types";
import type { InputRichMessage } from "@grammyjs/grammy/types";
import { brand, type RichMessageValue } from "../core/values.ts";
import { type BlockInput, blocks, splitOptions } from "./shared.ts";

Expand Down
13 changes: 3 additions & 10 deletions src/core/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RichBlockCaption, RichText } from "grammy/types";
import type { RichBlockCaption, RichText } from "@grammyjs/grammy/types";
import {
type BlockValue,
kindOf,
Expand Down Expand Up @@ -72,17 +72,10 @@ function describe(value: unknown): string {
export function richText(
values: readonly unknown[],
context: string,
): RichText {
): RichText | RichText[]{
const parts: RichText[] = [];
for (const item of flattenInputs(values)) {
if (item == null || typeof item === "boolean") continue;
if (typeof item === "string" || typeof item === "number") {
const text = String(item);
const last = parts.at(-1);
if (typeof last === "string") parts[parts.length - 1] = last + text;
else parts.push(text);
continue;
}
if (item == null || typeof item === "boolean" || typeof item === "string" || typeof item === "number") continue;
if (kindOf(item) !== "rich-text") {
throw new TypeError(
`${context} only accepts rich-text children, received ${
Expand Down
2 changes: 1 addition & 1 deletion src/core/text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type * as grammY from "grammy/types";
import type * as grammY from "@grammyjs/grammy/types";
import { brand, type RichTextValue } from "../core/values.ts";
import { assertNoChildren, richText, type RichTextInput } from "./shared.ts";

Expand Down
2 changes: 1 addition & 1 deletion src/core/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
InputRichMessage,
RichBlockTableCell,
RichText,
} from "grammy/types";
} from "@grammyjs/grammy/types";

const valueKindKey = "__telegramRichMessagesValueKind" as const;
type ValueKind =
Expand Down
2 changes: 1 addition & 1 deletion test/chaining.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
InputRichBlockParagraph,
InputRichMessage,
RichBlockTableCell,
} from "grammy/types";
} from "@grammyjs/grammy/types";

const results = [
{ model: "Aster-1", score: 98.4 },
Expand Down
2 changes: 1 addition & 1 deletion test/functions.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "@std/expect";
import { it as test } from "@std/testing/bdd";
import * as rm from "../src/core.ts";
import type { RichText } from "grammy/types";
import type { RichText } from "@grammyjs/grammy/types";
import {
bold,
italic,
Expand Down
2 changes: 1 addition & 1 deletion test/types.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
InputRichMessage,
MessageEntity,
RichBlockTableCell,
} from "grammy/types";
} from "@grammyjs/grammy/types";
import type { TableRowValue } from "../src/core.ts";

function expectType<T>(_value: T): void {}
Expand Down
Loading