github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/utils/markdown_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package utils 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestStripMarkdown(t *testing.T) { 13 tests := []struct { 14 name string 15 args string 16 want string 17 }{ 18 { 19 name: "emoji: same", 20 args: "Hey :smile: :+1: :)", 21 want: "Hey :smile: :+1: :)", 22 }, 23 { 24 name: "at-mention: same", 25 args: "Hey @user and @test", 26 want: "Hey @user and @test", 27 }, 28 { 29 name: "channel-link: same", 30 args: "join ~channelname", 31 want: "join ~channelname", 32 }, 33 { 34 name: "codespan: single backtick", 35 args: "`single backtick`", 36 want: "single backtick", 37 }, 38 { 39 name: "codespan: double backtick", 40 args: "``double backtick``", 41 want: "double backtick", 42 }, 43 { 44 name: "codespan: triple backtick", 45 args: "```triple backtick```", 46 want: "triple backtick", 47 }, 48 { 49 name: "codespan: inline code", 50 args: "Inline `code` has ``double backtick`` and ```triple backtick``` around it.", 51 want: "Inline code has double backtick and triple backtick around it.", 52 }, 53 { 54 name: "code block: single line code block", 55 args: "Code block\n```\nline\n```", 56 want: "Code block line", 57 }, 58 { 59 name: "code block: multiline code block 2", 60 args: "Multiline\n```\nfunction(number) {\n return number + 1;\n}\n```", 61 want: "Multiline function(number) {\n return number + 1;\n}", 62 }, 63 { 64 name: "code block: language highlighting", 65 args: "```javascript\nvar s = \"JavaScript syntax highlighting\";\nalert(s);\n```", 66 want: "var s = \"JavaScript syntax highlighting\";\nalert(s);", 67 }, 68 { 69 name: "blockquote:", 70 args: "> Hey quote", 71 want: "Hey quote", 72 }, 73 { 74 name: "blockquote: multiline", 75 args: "> Hey quote.\n> Hello quote.", 76 want: "Hey quote.\nHello quote.", 77 }, 78 { 79 name: "heading: # H1 header", 80 args: "# H1 header", 81 want: "H1 header", 82 }, 83 { 84 name: "heading: heading with @user", 85 args: "# H1 @user", 86 want: "H1 @user", 87 }, 88 { 89 name: "heading: ## H2 header", 90 args: "## H2 header", 91 want: "H2 header", 92 }, 93 { 94 name: "heading: ### H3 header", 95 args: "### H3 header", 96 want: "H3 header", 97 }, 98 { 99 name: "heading: #### H4 header", 100 args: "#### H4 header", 101 want: "H4 header", 102 }, 103 { 104 name: "heading: ##### H5 header", 105 args: "##### H5 header", 106 want: "H5 header", 107 }, 108 { 109 name: "heading: ###### H6 header", 110 args: "###### H6 header", 111 want: "H6 header", 112 }, 113 { 114 name: "heading: multiline with header and paragraph", 115 args: "###### H6 header\nThis is next line.\nAnother line.", 116 want: "H6 header This is next line.\nAnother line.", 117 }, 118 { 119 name: "heading: multiline with header and list items", 120 args: "###### H6 header\n- list item 1\n- list item 2", 121 want: "H6 header list item 1 list item 2", 122 }, 123 { 124 name: "heading: multiline with header and links", 125 args: "###### H6 header\n[link 1](https://mattermost.com) - [link 2](https://mattermost.com)", 126 want: "H6 header link 1 - link 2", 127 }, 128 { 129 name: "list: 1. First ordered list item", 130 args: "1. First ordered list item", 131 want: "First ordered list item", 132 }, 133 { 134 name: "list: 2. Another item", 135 args: "1. 2. Another item", 136 want: "Another item", 137 }, 138 { 139 name: "list: * Unordered sub-list.", 140 args: "* Unordered sub-list.", 141 want: "Unordered sub-list.", 142 }, 143 { 144 name: "list: - Or minuses", 145 args: "- Or minuses", 146 want: "Or minuses", 147 }, 148 { 149 name: "list: + Or pluses", 150 args: "+ Or pluses", 151 want: "Or pluses", 152 }, 153 { 154 name: "list: multiline", 155 args: "1. First ordered list item\n2. Another item", 156 want: "First ordered list item Another item", 157 }, 158 { 159 name: "tablerow:)", 160 args: "Markdown | Less | Pretty\n" + 161 "--- | --- | ---\n" + 162 "*Still* | `renders` | **nicely**\n" + 163 "1 | 2 | 3\n", 164 want: "Markdown | Less | Pretty\n" + 165 "--- | --- | ---\n" + 166 "Still | renders | nicely\n" + 167 "1 | 2 | 3", 168 }, 169 { 170 name: "table:", 171 args: "| Tables | Are | Cool |\n" + 172 "| ------------- |:-------------:| -----:|\n" + 173 "| col 3 is | right-aligned | $1600 |\n" + 174 "| col 2 is | centered | $12 |\n" + 175 "| zebra stripes | are neat | $1 |\n", 176 want: "| Tables | Are | Cool |\n" + 177 "| ------------- |:-------------:| -----:|\n" + 178 "| col 3 is | right-aligned | $1600 |\n" + 179 "| col 2 is | centered | $12 |\n" + 180 "| zebra stripes | are neat | $1 |", 181 }, 182 { 183 name: "strong: Bold with **asterisks** or __underscores__.", 184 args: "Bold with **asterisks** or __underscores__.", 185 want: "Bold with asterisks or underscores.", 186 }, 187 { 188 name: "strong & em: Bold and italics with **asterisks and _underscores_**.", 189 args: "Bold and italics with **asterisks and _underscores_**.", 190 want: "Bold and italics with asterisks and underscores.", 191 }, 192 { 193 name: "em: Italics with *asterisks* or _underscores_.", 194 args: "Italics with *asterisks* or _underscores_.", 195 want: "Italics with asterisks or underscores.", 196 }, 197 { 198 name: "del: Strikethrough ~~strike this.~~", 199 args: "Strikethrough ~~strike this.~~", 200 want: "Strikethrough strike this.", 201 }, 202 { 203 name: "links: [inline-style link](http://localhost:8065)", 204 args: "[inline-style link](http://localhost:8065)", 205 want: "inline-style link", 206 }, 207 { 208 name: "image: ", 209 args: "", 210 want: "image link", 211 }, 212 { 213 name: "text: plain", 214 args: "This is plain text.", 215 want: "This is plain text.", 216 }, 217 { 218 name: "text: multiline", 219 args: "This is multiline text.\nHere is the next line.\n", 220 want: "This is multiline text.\nHere is the next line.", 221 }, 222 { 223 name: "text: multiline with blockquote", 224 args: "This is multiline text.\n> With quote", 225 want: "This is multiline text. With quote", 226 }, 227 { 228 name: "text: multiline with list items", 229 args: "This is multiline text.\n * List item ", 230 want: "This is multiline text. List item", 231 }, 232 { 233 name: "text: & entity", 234 args: "you & me", 235 want: "you & me", 236 }, 237 { 238 name: "text: < entity", 239 args: "1<2", 240 want: "1<2", 241 }, 242 { 243 name: "text: > entity", 244 args: "2>1", 245 want: "2>1", 246 }, 247 { 248 name: "text: ' entity", 249 args: "he's out", 250 want: "he's out", 251 }, 252 { 253 name: "text: " entity", 254 args: `That is "unique"`, 255 want: `That is "unique"`, 256 }, 257 { 258 name: "text: multiple entities", 259 args: "&<>'", 260 want: "&<>'", 261 }, 262 { 263 name: "text: multiple entities", 264 args: "'><&", 265 want: "'><&", 266 }, 267 { 268 name: "text: empty string", 269 args: "", 270 want: "", 271 }, 272 } 273 for _, tt := range tests { 274 t.Run(tt.name, func(t *testing.T) { 275 got, err := StripMarkdown(tt.args) 276 if err != nil { 277 t.Fatalf("error: %v", err) 278 } 279 assert.Equal(t, tt.want, got) 280 }) 281 } 282 }