Skip to content

Commit

Permalink
feat: add blockquote support for HTML and MD2 (#138)
Browse files Browse the repository at this point in the history
feat: add blockquote support for HTML and MD2
  • Loading branch information
adasauce authored Feb 7, 2024
1 parent 08d77d3 commit 09b08bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
39 changes: 25 additions & 14 deletions parse_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type ParseMode interface {
// Preformated text
Pre(v ...string) string

// Blockquote
Blockquote(v ...string) string

// Escape
Escape(v string) string
}
Expand All @@ -61,13 +64,14 @@ var (
name: "HTML",
separator: " ",

bold: parseModeTag{"<b>", "</b>"},
italic: parseModeTag{"<i>", "</i>"},
underline: parseModeTag{"<u>", "</u>"},
strike: parseModeTag{"<s>", "</s>"},
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{"<s>", "</s>"},
spoiler: parseModeTag{"<tg-spoiler>", "</tg-spoiler>"},
code: parseModeTag{"<code>", "</code>"},
pre: parseModeTag{"<pre>", "</pre>"},
blockquote: parseModeTag{"<blockquote>", "</blockquote>"},

linkTemplate: `<a href="{url}">{title}</a>`,

Expand Down Expand Up @@ -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})`,

Expand All @@ -121,6 +126,7 @@ type parseMode struct {
spoiler parseModeTag
code parseModeTag
pre parseModeTag
blockquote parseModeTag
linkTemplate string

escape func(string) string
Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions parse_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

Expand Down Expand Up @@ -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*]"))
Expand Down

0 comments on commit 09b08bb

Please sign in to comment.