Repository: mr-linch/go-tg
Pull request #138 (2024-02-05 19:13:00)

Add blockquote support for HTML and MD2
adds support for the blockquote formatting option as documented here: https://core.telegram.org/bots/api#formatting-options

I needed this for my own implementation, figured I would share it upstream.

Thanks for writing this :)


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/mr-linch/go-tg/pull/138?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`08d77d3`)](https://app.codecov.io/gh/mr-linch/go-tg/commit/08d77d3451c464b708cd59ef42d12b611418b323?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk) 94.01% compared to head [(`aedebfc`)](https://app.codecov.io/gh/mr-linch/go-tg/pull/138?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk) 94.02%.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #138   +/-   ##
=======================================
  Coverage   94.01%   94.02%           
=======================================
  Files          20       20           
  Lines        2056     2058    +2     
=======================================
+ Hits         1933     1935    +2     
  Misses         87       87           
  Partials       36       36           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/mr-linch/go-tg/pull/138?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sasha+Savchuk).


Patch:
diff --git a/parse_mode.go b/parse_mode.go
index c84bc0b..a621181 100644
--- a/parse_mode.go
+++ b/parse_mode.go
@@ -45,6 +45,9 @@ type ParseMode interface {
 	// Preformated text
 	Pre(v ...string) string
 
+	// Blockquote
+	Blockquote(v ...string) string
+
 	// Escape
 	Escape(v string) string
 }
@@ -61,13 +64,14 @@ var (
 		name:      "HTML",
 		separator: " ",
 
-		bold:      parseModeTag{"<b>", "</b>"},
-		italic:    parseModeTag{"<i>", "</i>"},
-		underline: parseModeTag{"<u>", "</u>"},
-		strike:    parseModeTag{"
", "
"},
-		spoiler:   parseModeTag{"<tg-spoiler>", "</tg-spoiler>"},
-		code:      parseModeTag{"<code>", "</code>"},
-		pre:       parseModeTag{"<pre>", "</pre>"},
+		bold:       parseModeTag{"<b>", "</b>"},
+		italic:     parseModeTag{"<i>", "</i>"},
+		underline:  parseModeTag{"<u>", "</u>"},
+		strike:     parseModeTag{"
", "
"},
+		spoiler:    parseModeTag{"<tg-spoiler>", "</tg-spoiler>"},
+		code:       parseModeTag{"<code>", "</code>"},
+		pre:        parseModeTag{"<pre>", "</pre>"},
+		blockquote: parseModeTag{"<blockquote>", "</blockquote>"},
 
 		linkTemplate: `<a href="{url}">{title}</a>`,
 
@@ -95,13 +99,14 @@ var (
 		name:      "MarkdownV2",
 		separator: " ",
 
-		bold:      parseModeTag{"*", "*"},
-		italic:    parseModeTag{"_", "_"},
-		underline: parseModeTag{"__", "__"},
-		strike:    parseModeTag{"~", "~"},
-		spoiler:   parseModeTag{"||", "||"},
-		code:      parseModeTag{"`", "`"},
-		pre:       parseModeTag{"```", "```"},
+		bold:       parseModeTag{"*", "*"},
+		italic:     parseModeTag{"_", "_"},
+		underline:  parseModeTag{"__", "__"},
+		strike:     parseModeTag{"~", "~"},
+		spoiler:    parseModeTag{"||", "||"},
+		code:       parseModeTag{"`", "`"},
+		pre:        parseModeTag{"```", "```"},
+		blockquote: parseModeTag{">", ""},
 
 		linkTemplate: `[{title}]({url})`,
 
@@ -121,6 +126,7 @@ type parseMode struct {
 	spoiler      parseModeTag
 	code         parseModeTag
 	pre          parseModeTag
+	blockquote   parseModeTag
 	linkTemplate string
 
 	escape func(string) string
@@ -199,6 +205,11 @@ func (pm parseMode) Pre(v ...string) string {
 	return pm.pre.wrap(strings.Join(v, pm.separator))
 }
 
+// Blockquote
+func (pm parseMode) Blockquote(v ...string) string {
+	return pm.blockquote.wrap(strings.Join(v, pm.separator))
+}
+
 func (pm parseMode) Escape(v string) string {
 	return pm.escape(v)
 }


Test patch:
diff --git a/parse_mode_test.go b/parse_mode_test.go
index 059673b..2027b75 100644
--- a/parse_mode_test.go
+++ b/parse_mode_test.go
@@ -23,6 +23,7 @@ func TestParseModeHTML(t *testing.T) {
 	assert.Equal(t, "<code>Hello World</code>", HTML.Code("Hello World"))
 	assert.Equal(t, "<pre>Hello World</pre>", HTML.Pre("Hello World"))
 	assert.Equal(t, "<b>Hello, World</b>", HTML.Sep(", ").Bold("Hello", "World"))
+	assert.Equal(t, "<blockquote>Hello, World</blockquote>", HTML.Sep(", ").Blockquote("Hello", "World"))
 	assert.Equal(t, "Me &amp; You", HTML.Escape("Me & You"))
 }
 
@@ -55,6 +56,7 @@ func TestParseModeMarkdownV2(t *testing.T) {
 	assert.Equal(t, "[Hello World](https://telegram.org)", MD2.Link("Hello World", "https://telegram.org"))
 	assert.Equal(t, "`Hello World`", MD2.Code("Hello World"))
 	assert.Equal(t, "```Hello World```", MD2.Pre("Hello World"))
+	assert.Equal(t, ">Hello World", MD2.Blockquote("Hello World"))
 	assert.Equal(t, "*Hello, World*", MD2.Sep(", ").Bold("Hello", "World"))
 
 	assert.Equal(t, "\\[\\*go\\_tg\\*\\]", MD2.Escape("[*go_tg*]"))