




















@@ -5,11 +5,11 @@ import {
55buildTelegramThreadParams,
66buildTypingThreadParams,
77describeReplyTarget,
8-expandTextLinks,
98getTelegramTextParts,
109hasBotMention,
1110isBinaryContent,
1211normalizeForwardedContext,
12+renderTelegramTextEntities,
1313resolveTelegramDirectPeerId,
1414resolveTelegramForumFlag,
1515resolveTelegramForumThreadId,
@@ -802,52 +802,55 @@ describe("hasBotMention", () => {
802802).toBe(false);
803803});
804804});
805-describe("expandTextLinks", () => {
806-it("returns text unchanged when no entities are provided", () => {
807-expect(expandTextLinks("Hello world")).toBe("Hello world");
808-expect(expandTextLinks("Hello world", null)).toBe("Hello world");
809-expect(expandTextLinks("Hello world", [])).toBe("Hello world");
810-});
811805812-it("returns text unchanged when there are no text_link entities", () => {
806+describe("renderTelegramTextEntities", () => {
807+it("renders Telegram formatting entities as markdown", () => {
808+const text = "bold italic code strike underline spoiler";
813809const entities = [
814-{ type: "mention", offset: 0, length: 5 },
815-{ type: "bold", offset: 6, length: 5 },
810+{ type: "bold", offset: 0, length: 4 },
811+{ type: "italic", offset: 5, length: 6 },
812+{ type: "code", offset: 12, length: 4 },
813+{ type: "strikethrough", offset: 17, length: 6 },
814+{ type: "underline", offset: 24, length: 9 },
815+{ type: "spoiler", offset: 34, length: 7 },
816816];
817-expect(expandTextLinks("@user hello", entities)).toBe("@user hello");
818-});
819817820-it("expands a single text_link entity", () => {
821-const text = "Check this link for details";
822-const entities = [{ type: "text_link", offset: 11, length: 4, url: "https://example.com" }];
823-expect(expandTextLinks(text, entities)).toBe(
824-"Check this [link](https://example.com) for details",
818+expect(renderTelegramTextEntities(text, entities)).toBe(
819+"**bold** _italic_ `code` ~~strike~~ __underline__ ||spoiler||",
825820);
826821});
827822828-it("expands multiple text_link entities", () => {
829-const text = "Visit Google or GitHub for more";
830-const entities = [
831-{ type: "text_link", offset: 6, length: 6, url: "https://google.com" },
832-{ type: "text_link", offset: 16, length: 6, url: "https://github.com" },
833-];
834-expect(expandTextLinks(text, entities)).toBe(
835-"Visit [Google](https://google.com) or [GitHub](https://github.com) for more",
836-);
823+it("renders pre entities with language fences", () => {
824+const text = "const value = 1;";
825+const entities = [{ type: "pre", offset: 0, length: text.length, language: "ts" }];
826+827+expect(renderTelegramTextEntities(text, entities)).toBe("```ts\nconst value = 1;\n```");
837828});
838829839-it("handles adjacent text_link entities", () => {
840-const text = "AB";
830+it("uses a pre fence that cannot close inside content", () => {
831+const text = "before\n```\ninside";
832+const entities = [{ type: "pre", offset: 0, length: text.length, language: "md" }];
833+834+expect(renderTelegramTextEntities(text, entities)).toBe("````md\nbefore\n```\ninside\n````");
835+});
836+837+it("renders links and formatting from original offsets", () => {
838+const text = "Read docs now";
841839const entities = [
842-{ type: "text_link", offset: 0, length: 1, url: "https://a.example" },
843-{ type: "text_link", offset: 1, length: 1, url: "https://b.example" },
840+{ type: "bold", offset: 5, length: 4 },
841+{ type: "text_link", offset: 5, length: 4, url: "https://docs.example" },
842+{ type: "italic", offset: 10, length: 3 },
844843];
845-expect(expandTextLinks(text, entities)).toBe("[A](https://a.example)[B](https://b.example)");
844+845+expect(renderTelegramTextEntities(text, entities)).toBe(
846+"Read **[docs](https://docs.example)** _now_",
847+);
846848});
847849848-it("preserves offsets from the original string", () => {
849-const text = " Hello world";
850-const entities = [{ type: "text_link", offset: 1, length: 5, url: "https://example.com" }];
851-expect(expandTextLinks(text, entities)).toBe(" [Hello](https://example.com) world");
850+it("uses UTF-16 Telegram offsets", () => {
851+const text = "Hi 😀 bold";
852+const entities = [{ type: "bold", offset: 6, length: 4 }];
853+854+expect(renderTelegramTextEntities(text, entities)).toBe("Hi 😀 **bold**");
852855});
853856});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。