]> git.giacomolli.uk - onechat/blobdiff - telegram_markdown_test.go
Escape tags in the Markdown of rich replies
[onechat] / telegram_markdown_test.go
index e2a253056e251b8877c6972c229d7b808f85061e..d80d57547de8fe24a86d07af40b4cf518d463daf 100644 (file)
@@ -239,3 +239,63 @@ func TestMarkdownEntities(t *testing.T) {
                })
        }
 }
+
+func TestEscapeTags(t *testing.T) {
+       testCases := []struct {
+               name     string
+               markdown string
+               escaped  string
+       }{
+               {
+                       name:     "tag in prose shows as text",
+                       markdown: "Use a <details> tag for the rest.",
+                       escaped:  `Use a \<details> tag for the rest.`,
+               },
+               {
+                       name:     "tag in inline code stays as written",
+                       markdown: "Use `<details>` and `` a<b ``, not <b>.",
+                       escaped:  "Use `<details>` and `` a<b ``, not \\<b>.",
+               },
+               {
+                       name:     "tag in a code block stays as written",
+                       markdown: "<p>\n\n```html\n<details>\n```\n\n<p>",
+                       escaped:  "\\<p>\n\n```html\n<details>\n```\n\n\\<p>",
+               },
+               {
+                       name:     "code block that does not close stays as written",
+                       markdown: "a <b>\n```\n<details>",
+                       escaped:  "a \\<b>\n```\n<details>",
+               },
+               {
+                       name:     "tag in a table cell shows as text",
+                       markdown: "| Tag | Use |\n|-----|-----|\n| <br> | `<br>` |",
+                       escaped:  "| Tag | Use |\n|-----|-----|\n| \\<br> | `<br>` |",
+               },
+               {
+                       name:     "autolink shows as text",
+                       markdown: "See <https://example.com>.",
+                       escaped:  `See \<https://example.com>.`,
+               },
+               {
+                       name:     "backtick run that opens no code stays as text",
+                       markdown: "a ``b` <c>",
+                       escaped:  "a ``b` \\<c>",
+               },
+               {
+                       name:     "escaped bracket stays as written",
+                       markdown: `a \<b> and \\<c>`,
+                       escaped:  `a \<b> and \\\<c>`,
+               },
+               {
+                       name:     "dollar, pipe, and equals signs stay as written",
+                       markdown: "$5 or $10, a || b, ==c==",
+                       escaped:  "$5 or $10, a || b, ==c==",
+               },
+       }
+
+       for _, tc := range testCases {
+               t.Run(tc.name, func(t *testing.T) {
+                       require.Equal(t, tc.escaped, telegramEscapeTags(tc.markdown))
+               })
+       }
+}