From 01510ea21c3582f21f7cf431f4157b725afba91e Mon Sep 17 00:00:00 2001 From: "josejisin@gmail.com" Date: Wed, 7 Aug 2024 11:55:58 +0100 Subject: [PATCH 1/3] feat(html): support blockquote tag --- telegram/message/html/parser.go | 24 +++++++++++++++--------- telegram/message/html/parser_test.go | 2 ++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/telegram/message/html/parser.go b/telegram/message/html/parser.go index 4a0652a159..ef10589bf5 100644 --- a/telegram/message/html/parser.go +++ b/telegram/message/html/parser.go @@ -36,15 +36,16 @@ func (p *htmlParser) fillAttrs() { } const ( - pre = "pre" - code = "code" - em = "em" - ins = "ins" - strike = "strike" - del = "del" - strong = "strong" - span = "span" - tgSpoiler = "tg-spoiler" + pre = "pre" + code = "code" + em = "em" + ins = "ins" + strike = "strike" + del = "del" + strong = "strong" + span = "span" + tgSpoiler = "tg-spoiler" + blockquote = "blockquote" ) func (p *htmlParser) tag(tn []byte) string { @@ -78,6 +79,8 @@ func (p *htmlParser) tag(tn []byte) string { return span case tgSpoiler: return tgSpoiler + case blockquote: + return blockquote default: return string(tn) } @@ -154,6 +157,9 @@ func (p *htmlParser) startTag() error { } case tgSpoiler: e.format = entity.Spoiler() + case blockquote: + _, collapsed := p.attr["expandable"] + e.format = entity.Blockquote(collapsed) } p.stack.push(e) diff --git a/telegram/message/html/parser_test.go b/telegram/message/html/parser_test.go index 208ec3bc11..967491ad47 100644 --- a/telegram/message/html/parser_test.go +++ b/telegram/message/html/parser_test.go @@ -107,6 +107,8 @@ func TestHTML(t *testing.T) { entities: getEntities(entity.Pre("python"))}, {html: "<", msg: "<", entities: getEntities(entity.Bold())}, {html: `spoiler`, msg: "spoiler", entities: getEntities(entity.Spoiler())}, + {html: "
quote
", msg: "quote", entities: getEntities(entity.Blockquote(true))}, + {html: "
quote
", msg: "quote", entities: getEntities(entity.Blockquote(false))}, } t.Run("Common", runTests(tests, false)) } From fa03a30326fc36017dec9b6842f6711837ec90b1 Mon Sep 17 00:00:00 2001 From: "josejisin@gmail.com" Date: Wed, 7 Aug 2024 12:10:30 +0100 Subject: [PATCH 2/3] feat(html): support tg-emoji tag --- telegram/message/html/parser.go | 8 ++++++++ telegram/message/html/parser_test.go | 1 + 2 files changed, 9 insertions(+) diff --git a/telegram/message/html/parser.go b/telegram/message/html/parser.go index ef10589bf5..4956f7ee89 100644 --- a/telegram/message/html/parser.go +++ b/telegram/message/html/parser.go @@ -2,6 +2,7 @@ package html import ( "io" + "strconv" "strings" "github.com/go-faster/errors" @@ -45,6 +46,7 @@ const ( strong = "strong" span = "span" tgSpoiler = "tg-spoiler" + tgEmoji = "tg-emoji" blockquote = "blockquote" ) @@ -79,6 +81,8 @@ func (p *htmlParser) tag(tn []byte) string { return span case tgSpoiler: return tgSpoiler + case tgEmoji: + return tgEmoji case blockquote: return blockquote default: @@ -157,6 +161,10 @@ func (p *htmlParser) startTag() error { } case tgSpoiler: e.format = entity.Spoiler() + case tgEmoji: + if id, err := strconv.ParseInt(p.attr["emoji-id"], 10, 64); err == nil { + e.format = entity.CustomEmoji(id) + } case blockquote: _, collapsed := p.attr["expandable"] e.format = entity.Blockquote(collapsed) diff --git a/telegram/message/html/parser_test.go b/telegram/message/html/parser_test.go index 967491ad47..3ac4808e19 100644 --- a/telegram/message/html/parser_test.go +++ b/telegram/message/html/parser_test.go @@ -107,6 +107,7 @@ func TestHTML(t *testing.T) { entities: getEntities(entity.Pre("python"))}, {html: "<", msg: "<", entities: getEntities(entity.Bold())}, {html: `spoiler`, msg: "spoiler", entities: getEntities(entity.Spoiler())}, + {html: "👍", msg: "👍", entities: getEntities(entity.CustomEmoji(5368324170671202286))}, {html: "
quote
", msg: "quote", entities: getEntities(entity.Blockquote(true))}, {html: "
quote
", msg: "quote", entities: getEntities(entity.Blockquote(false))}, } From ac6bc17e25a1d9af99788e6a851077b282d32beb Mon Sep 17 00:00:00 2001 From: "josejisin@gmail.com" Date: Wed, 7 Aug 2024 12:15:17 +0100 Subject: [PATCH 3/3] test(html): update bot api example --- telegram/message/html/html_example_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/telegram/message/html/html_example_test.go b/telegram/message/html/html_example_test.go index 4a83faddef..b9b51f02e1 100644 --- a/telegram/message/html/html_example_test.go +++ b/telegram/message/html/html_example_test.go @@ -27,12 +27,16 @@ func sendHTML(ctx context.Context) error { italic, italic underline, underline strikethrough, strikethrough, strikethrough -bold italic bold italic bold strikethrough underline italic bold bold +spoiler, spoiler +bold italic bold italic bold strikethrough italic bold strikethrough spoiler underline italic bold bold inline URL inline mention of a user +👍 inline fixed-width code
pre-formatted fixed-width code block
-
pre-formatted fixed-width code block written in the Python programming language
`)) +
pre-formatted fixed-width code block written in the Python programming language
+
Block quotation started\nBlock quotation continued\nThe last line of the block quotation
+
Expandable block quotation started\nExpandable block quotation continued\nExpandable block quotation continued\nHidden by default part of the block quotation started\nExpandable block quotation continued\nThe last line of the block quotation
`)) return err }) }