| 1 |
package passthrough |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/yuin/goldmark" |
| 9 |
"github.com/yuin/goldmark/ast" |
| 10 |
"github.com/yuin/goldmark/extension" |
| 11 |
"github.com/yuin/goldmark/text" |
| 12 |
|
| 13 |
qt "github.com/frankban/quicktest" |
| 14 |
) |
| 15 |
|
| 16 |
func buildTestParser() goldmark.Markdown { |
| 17 |
md := goldmark.New( |
| 18 |
goldmark.WithExtensions( |
| 19 |
extension.TaskList, |
| 20 |
extension.DefinitionList, |
| 21 |
New( |
| 22 |
Config{ |
| 23 |
InlineDelimiters: []Delimiters{ |
| 24 |
{ |
| 25 |
Open: "$", |
| 26 |
Close: "$", |
| 27 |
}, |
| 28 |
{ |
| 29 |
Open: "\\(", |
| 30 |
Close: "\\)", |
| 31 |
}, |
| 32 |
}, |
| 33 |
BlockDelimiters: []Delimiters{ |
| 34 |
{ |
| 35 |
Open: "$$", |
| 36 |
Close: "$$", |
| 37 |
}, |
| 38 |
{ |
| 39 |
Open: "\\[", |
| 40 |
Close: "\\]", |
| 41 |
}, |
| 42 |
}, |
| 43 |
}, |
| 44 |
)), |
| 45 |
) |
| 46 |
return md |
| 47 |
} |
| 48 |
|
| 49 |
func Parse(t *testing.T, input string) string { |
| 50 |
md := buildTestParser() |
| 51 |
var buf bytes.Buffer |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
if err := md.Convert([]byte(input), &buf); err != nil { |
| 57 |
t.Fatal(err) |
| 58 |
} |
| 59 |
return strings.TrimSpace(buf.String()) |
| 60 |
} |
| 61 |
|
| 62 |
func ParseWalk(t testing.TB, input string, cb func(n ast.Node, entering bool) bool) { |
| 63 |
t.Helper() |
| 64 |
md := buildTestParser() |
| 65 |
doc := md.Parser().Parse(text.NewReader([]byte(input))) |
| 66 |
err := ast.Walk( |
| 67 |
doc, |
| 68 |
func(n ast.Node, entering bool) (ast.WalkStatus, error) { |
| 69 |
if cb(n, entering) { |
| 70 |
return ast.WalkSkipChildren, nil |
| 71 |
} |
| 72 |
return ast.WalkContinue, nil |
| 73 |
}) |
| 74 |
if err != nil { |
| 75 |
t.Fatal(err) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func TestEmphasisOutsideOfMathmode(t *testing.T) { |
| 80 |
input := "Emph: _wow_" |
| 81 |
expected := "<p>Emph: <em>wow</em></p>" |
| 82 |
actual := Parse(t, input) |
| 83 |
|
| 84 |
c := qt.New(t) |
| 85 |
c.Assert(actual, qt.Equals, expected) |
| 86 |
} |
| 87 |
|
| 88 |
func TestInlineEquationWithEmphasisDelimiters(t *testing.T) { |
| 89 |
input := "An equation: $a^*=x-b^*$. Amazing" |
| 90 |
expected := "<p>An equation: $a^*=x-b^*$. Amazing</p>" |
| 91 |
actual := Parse(t, input) |
| 92 |
|
| 93 |
c := qt.New(t) |
| 94 |
c.Assert(actual, qt.Equals, expected) |
| 95 |
} |
| 96 |
|
| 97 |
func TestDump(t *testing.T) { |
| 98 |
input := "An equation: \\(a^*=x-b^*\\). Amazing" |
| 99 |
md := buildTestParser() |
| 100 |
root := md.Parser().Parse(text.NewReader([]byte(input))) |
| 101 |
root.Dump([]byte(input), 0) |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
func TestInlineEquationWithEmphasisDelimitersSplitAcrossLines(t *testing.T) { |
| 106 |
input := `An equation: $a^*= |
| 107 |
x-b^*$. Amazing` |
| 108 |
expected := `<p>An equation: $a^*= |
| 109 |
x-b^*$. Amazing</p>` |
| 110 |
actual := Parse(t, input) |
| 111 |
|
| 112 |
c := qt.New(t) |
| 113 |
c.Assert(actual, qt.Equals, expected) |
| 114 |
} |
| 115 |
|
| 116 |
func TestInlineEquationWithEmphasisDelimitersSplitAcrossLines2(t *testing.T) { |
| 117 |
input := `Inline $ |
| 118 |
a^*=x-b^* |
| 119 |
$ equation` |
| 120 |
expected := `<p>Inline $ |
| 121 |
a^*=x-b^* |
| 122 |
$ equation</p>` |
| 123 |
actual := Parse(t, input) |
| 124 |
|
| 125 |
c := qt.New(t) |
| 126 |
c.Assert(actual, qt.Equals, expected) |
| 127 |
} |
| 128 |
|
| 129 |
func TestInlineEquationWithEmphasisSplitAcrossParagraphs(t *testing.T) { |
| 130 |
input := `An equation: $a^ |
| 131 |
|
| 132 |
*=x-b^*$. Amazing` |
| 133 |
expected := `<p>An equation: $a^</p> |
| 134 |
<p><em>=x-b^</em>$. Amazing</p>` |
| 135 |
actual := Parse(t, input) |
| 136 |
|
| 137 |
c := qt.New(t) |
| 138 |
c.Assert(actual, qt.Equals, expected) |
| 139 |
} |
| 140 |
|
| 141 |
func TestBlockEquationWithEmphasisDelimiters(t *testing.T) { |
| 142 |
input := `An equation: |
| 143 |
|
| 144 |
$$ |
| 145 |
a^*=x-b^* |
| 146 |
$$ |
| 147 |
|
| 148 |
Amazing` |
| 149 |
expected := `<p>An equation:</p> |
| 150 |
$$ |
| 151 |
a^*=x-b^* |
| 152 |
$$ |
| 153 |
<p>Amazing</p>` |
| 154 |
|
| 155 |
actual := Parse(t, input) |
| 156 |
|
| 157 |
c := qt.New(t) |
| 158 |
c.Assert(actual, qt.Equals, expected) |
| 159 |
} |
| 160 |
|
| 161 |
func TestBlockEquationWithOpenAndCloseOnSameLines(t *testing.T) { |
| 162 |
input := `An equation: |
| 163 |
|
| 164 |
$$a^*=x-b^* |
| 165 |
=c$$ |
| 166 |
|
| 167 |
Amazing` |
| 168 |
expected := `<p>An equation:</p> |
| 169 |
$$a^*=x-b^* |
| 170 |
=c$$ |
| 171 |
<p>Amazing</p>` |
| 172 |
|
| 173 |
actual := Parse(t, input) |
| 174 |
|
| 175 |
c := qt.New(t) |
| 176 |
c.Assert(actual, qt.Equals, expected) |
| 177 |
} |
| 178 |
|
| 179 |
func TestUnterminatedDelimiters(t *testing.T) { |
| 180 |
input := `An equation: $a^*=x-b^* Amazing.` |
| 181 |
expected := `<p>An equation: $a^<em>=x-b^</em> Amazing.</p>` |
| 182 |
actual := Parse(t, input) |
| 183 |
c := qt.New(t) |
| 184 |
c.Assert(actual, qt.Equals, expected) |
| 185 |
} |
| 186 |
|
| 187 |
func TestEscapedSingleByteDelimiter(t *testing.T) { |
| 188 |
input := `I want \\$ *dollars*: $a^*=x-b^*$ Amazing.` |
| 189 |
expected := `<p>I want \$ <em>dollars</em>: $a^*=x-b^*$ Amazing.</p>` |
| 190 |
actual := Parse(t, input) |
| 191 |
c := qt.New(t) |
| 192 |
c.Assert(actual, qt.Equals, expected) |
| 193 |
} |
| 194 |
|
| 195 |
func TestFirstByteOfMultiByteDelimiterEndsText(t *testing.T) { |
| 196 |
input := `An equation: \` |
| 197 |
expected := `<p>An equation: \</p>` |
| 198 |
actual := Parse(t, input) |
| 199 |
c := qt.New(t) |
| 200 |
c.Assert(actual, qt.Equals, expected) |
| 201 |
} |
| 202 |
|
| 203 |
func TestExample1(t *testing.T) { |
| 204 |
input := `Inline $x = {-b \pm \sqrt{b^2-4ac} \over 2a}$ equation` |
| 205 |
|
| 206 |
expected := "<p>" + input + "</p>" |
| 207 |
actual := Parse(t, input) |
| 208 |
|
| 209 |
c := qt.New(t) |
| 210 |
c.Assert(actual, qt.Equals, expected) |
| 211 |
} |
| 212 |
|
| 213 |
func TestExample2(t *testing.T) { |
| 214 |
input := `Inline $ |
| 215 |
x = {-b \pm \sqrt{b^2-4ac} \over 2a} |
| 216 |
$ equation` |
| 217 |
|
| 218 |
expected := "<p>" + input + "</p>" |
| 219 |
actual := Parse(t, input) |
| 220 |
|
| 221 |
c := qt.New(t) |
| 222 |
c.Assert(actual, qt.Equals, expected) |
| 223 |
} |
| 224 |
|
| 225 |
func TestExample3(t *testing.T) { |
| 226 |
input := `Inline $x = {-b \pm \sqrt{b^2-4ac} \over 2a} |
| 227 |
$ equation` |
| 228 |
|
| 229 |
expected := "<p>" + input + "</p>" |
| 230 |
actual := Parse(t, input) |
| 231 |
|
| 232 |
c := qt.New(t) |
| 233 |
c.Assert(actual, qt.Equals, expected) |
| 234 |
} |
| 235 |
|
| 236 |
func TestExample4(t *testing.T) { |
| 237 |
input := `Inline $ |
| 238 |
x = {-b \pm \sqrt{b^2-4ac} \over 2a}$ equation` |
| 239 |
|
| 240 |
expected := "<p>" + input + "</p>" |
| 241 |
actual := Parse(t, input) |
| 242 |
|
| 243 |
c := qt.New(t) |
| 244 |
c.Assert(actual, qt.Equals, expected) |
| 245 |
} |
| 246 |
|
| 247 |
func TestExample5(t *testing.T) { |
| 248 |
input := `Block $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$ equation` |
| 249 |
expected := `<p>Block </p> |
| 250 |
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$ |
| 251 |
<p> equation</p>` |
| 252 |
actual := Parse(t, input) |
| 253 |
c := qt.New(t) |
| 254 |
c.Assert(actual, qt.Equals, expected) |
| 255 |
} |
| 256 |
|
| 257 |
func TestExample6(t *testing.T) { |
| 258 |
input := `Block $$ |
| 259 |
x = {-b \pm \sqrt{b^2-4ac} \over 2a} |
| 260 |
$$ equation` |
| 261 |
expected := `<p>Block </p> |
| 262 |
$$ |
| 263 |
x = {-b \pm \sqrt{b^2-4ac} \over 2a} |
| 264 |
$$ |
| 265 |
<p> equation</p>` |
| 266 |
actual := Parse(t, input) |
| 267 |
c := qt.New(t) |
| 268 |
c.Assert(actual, qt.Equals, expected) |
| 269 |
} |
| 270 |
|
| 271 |
func TestExample7(t *testing.T) { |
| 272 |
input := `Block $$x = {-b \pm \sqrt{b^2-4ac} \over 2a} |
| 273 |
$$ equation` |
| 274 |
expected := `<p>Block </p> |
| 275 |
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a} |
| 276 |
$$ |
| 277 |
<p> equation</p>` |
| 278 |
actual := Parse(t, input) |
| 279 |
|
| 280 |
c := qt.New(t) |
| 281 |
c.Assert(actual, qt.Equals, expected) |
| 282 |
} |
| 283 |
|
| 284 |
func TestExample8(t *testing.T) { |
| 285 |
input := `Block $$ |
| 286 |
x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$ equation` |
| 287 |
expected := `<p>Block </p> |
| 288 |
$$ |
| 289 |
x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$ |
| 290 |
<p> equation</p>` |
| 291 |
actual := Parse(t, input) |
| 292 |
|
| 293 |
c := qt.New(t) |
| 294 |
c.Assert(actual, qt.Equals, expected) |
| 295 |
} |
| 296 |
|
| 297 |
func TestExample9(t *testing.T) { |
| 298 |
input := `Inline $a^*=x-b^*$ equation` |
| 299 |
|
| 300 |
expected := "<p>" + input + "</p>" |
| 301 |
actual := Parse(t, input) |
| 302 |
|
| 303 |
c := qt.New(t) |
| 304 |
c.Assert(actual, qt.Equals, expected) |
| 305 |
} |
| 306 |
|
| 307 |
func TestExample10(t *testing.T) { |
| 308 |
input := `Inline $ |
| 309 |
a^*=x-b^* |
| 310 |
$ equation` |
| 311 |
|
| 312 |
expected := "<p>" + input + "</p>" |
| 313 |
actual := Parse(t, input) |
| 314 |
|
| 315 |
c := qt.New(t) |
| 316 |
c.Assert(actual, qt.Equals, expected) |
| 317 |
} |
| 318 |
|
| 319 |
func TestExample11(t *testing.T) { |
| 320 |
input := `Inline $a^*=x-b^* |
| 321 |
$ equation` |
| 322 |
|
| 323 |
expected := "<p>" + input + "</p>" |
| 324 |
actual := Parse(t, input) |
| 325 |
|
| 326 |
c := qt.New(t) |
| 327 |
c.Assert(actual, qt.Equals, expected) |
| 328 |
} |
| 329 |
|
| 330 |
func TestExample12(t *testing.T) { |
| 331 |
input := `Inline $ |
| 332 |
a^*=x-b^*$ equation` |
| 333 |
|
| 334 |
expected := "<p>" + input + "</p>" |
| 335 |
actual := Parse(t, input) |
| 336 |
|
| 337 |
c := qt.New(t) |
| 338 |
c.Assert(actual, qt.Equals, expected) |
| 339 |
} |
| 340 |
|
| 341 |
func TestExample13(t *testing.T) { |
| 342 |
input := `Block $$a^*=x-b^*$$ equation` |
| 343 |
expected := `<p>Block </p> |
| 344 |
$$a^*=x-b^*$$ |
| 345 |
<p> equation</p>` |
| 346 |
actual := Parse(t, input) |
| 347 |
c := qt.New(t) |
| 348 |
c.Assert(actual, qt.Equals, expected) |
| 349 |
} |
| 350 |
|
| 351 |
func TestExample14(t *testing.T) { |
| 352 |
input := `Block $$ |
| 353 |
a^*=x-b^* |
| 354 |
$$ equation` |
| 355 |
expected := `<p>Block </p> |
| 356 |
$$ |
| 357 |
a^*=x-b^* |
| 358 |
$$ |
| 359 |
<p> equation</p>` |
| 360 |
actual := Parse(t, input) |
| 361 |
c := qt.New(t) |
| 362 |
c.Assert(actual, qt.Equals, expected) |
| 363 |
} |
| 364 |
|
| 365 |
func TestExample15(t *testing.T) { |
| 366 |
input := `Block $$a^*=x-b^* |
| 367 |
$$ equation` |
| 368 |
expected := `<p>Block </p> |
| 369 |
$$a^*=x-b^* |
| 370 |
$$ |
| 371 |
<p> equation</p>` |
| 372 |
actual := Parse(t, input) |
| 373 |
c := qt.New(t) |
| 374 |
c.Assert(actual, qt.Equals, expected) |
| 375 |
} |
| 376 |
|
| 377 |
func TestExample16(t *testing.T) { |
| 378 |
input := `Block $$ |
| 379 |
a^*=x-b^*$$ equation` |
| 380 |
expected := `<p>Block </p> |
| 381 |
$$ |
| 382 |
a^*=x-b^*$$ |
| 383 |
<p> equation</p>` |
| 384 |
actual := Parse(t, input) |
| 385 |
c := qt.New(t) |
| 386 |
c.Assert(actual, qt.Equals, expected) |
| 387 |
} |
| 388 |
|
| 389 |
func TestExample17(t *testing.T) { |
| 390 |
input := `Inline \(a^*=x-b^*\) equation` |
| 391 |
|
| 392 |
expected := "<p>" + input + "</p>" |
| 393 |
actual := Parse(t, input) |
| 394 |
|
| 395 |
c := qt.New(t) |
| 396 |
c.Assert(actual, qt.Equals, expected) |
| 397 |
} |
| 398 |
|
| 399 |
func TestExample18(t *testing.T) { |
| 400 |
input := `Inline \( |
| 401 |
a^*=x-b^* |
| 402 |
\) equation` |
| 403 |
|
| 404 |
expected := "<p>" + input + "</p>" |
| 405 |
actual := Parse(t, input) |
| 406 |
|
| 407 |
c := qt.New(t) |
| 408 |
c.Assert(actual, qt.Equals, expected) |
| 409 |
} |
| 410 |
|
| 411 |
func TestExample19(t *testing.T) { |
| 412 |
input := `Inline \(a^*=x-b^* |
| 413 |
\) equation` |
| 414 |
|
| 415 |
expected := "<p>" + input + "</p>" |
| 416 |
actual := Parse(t, input) |
| 417 |
|
| 418 |
c := qt.New(t) |
| 419 |
c.Assert(actual, qt.Equals, expected) |
| 420 |
} |
| 421 |
|
| 422 |
func TestExample20(t *testing.T) { |
| 423 |
input := `Inline \( |
| 424 |
a^*=x-b^*\) equation` |
| 425 |
|
| 426 |
expected := "<p>" + input + "</p>" |
| 427 |
actual := Parse(t, input) |
| 428 |
|
| 429 |
c := qt.New(t) |
| 430 |
c.Assert(actual, qt.Equals, expected) |
| 431 |
} |
| 432 |
|
| 433 |
func TestExample21(t *testing.T) { |
| 434 |
input := `Block \[a^*=x-b^*\] equation` |
| 435 |
expected := `<p>Block </p> |
| 436 |
\[a^*=x-b^*\] |
| 437 |
<p> equation</p>` |
| 438 |
actual := Parse(t, input) |
| 439 |
c := qt.New(t) |
| 440 |
c.Assert(actual, qt.Equals, expected) |
| 441 |
} |
| 442 |
|
| 443 |
func TestExample22(t *testing.T) { |
| 444 |
input := `Block \[ |
| 445 |
a^*=x-b^* |
| 446 |
\] equation` |
| 447 |
expected := `<p>Block </p> |
| 448 |
\[ |
| 449 |
a^*=x-b^* |
| 450 |
\] |
| 451 |
<p> equation</p>` |
| 452 |
actual := Parse(t, input) |
| 453 |
c := qt.New(t) |
| 454 |
c.Assert(actual, qt.Equals, expected) |
| 455 |
} |
| 456 |
|
| 457 |
func TestExample23(t *testing.T) { |
| 458 |
input := `Block \[a^*=x-b^* |
| 459 |
\] equation` |
| 460 |
expected := `<p>Block </p> |
| 461 |
\[a^*=x-b^* |
| 462 |
\] |
| 463 |
<p> equation</p>` |
| 464 |
actual := Parse(t, input) |
| 465 |
c := qt.New(t) |
| 466 |
c.Assert(actual, qt.Equals, expected) |
| 467 |
} |
| 468 |
|
| 469 |
func TestExample24(t *testing.T) { |
| 470 |
input := `Block \[ |
| 471 |
a^*=x-b^*\] equation` |
| 472 |
expected := `<p>Block </p> |
| 473 |
\[ |
| 474 |
a^*=x-b^*\] |
| 475 |
<p> equation</p>` |
| 476 |
actual := Parse(t, input) |
| 477 |
|
| 478 |
c := qt.New(t) |
| 479 |
c.Assert(actual, qt.Equals, expected) |
| 480 |
} |
| 481 |
|
| 482 |
func TestExample25(t *testing.T) { |
| 483 |
input := `$$ |
| 484 |
\begin{array} {lcl} |
| 485 |
L(p,w_i) &=& \dfrac{1}{N}\Sigma_{i=1}^N(\underbrace{f_r(x_2 |
| 486 |
\rightarrow x_1 |
| 487 |
\rightarrow x_0)G(x_1 |
| 488 |
\longleftrightarrow x_2)f_r(x_3 |
| 489 |
\rightarrow x_2 |
| 490 |
\rightarrow x_1)}_{sample\, radiance\, evaluation\, in\, stage2} |
| 491 |
\\\\\\ &=& |
| 492 |
\prod_{i=3}^{k-1}(\underbrace{\dfrac{f_r(x_{i+1} |
| 493 |
\rightarrow x_i |
| 494 |
\rightarrow x_{i-1})G(x_i |
| 495 |
\longleftrightarrow x_{i-1})}{p_a(x_{i-1})}}_{stored\,in\,vertex\, during\,light\, path\, tracing\, in\, stage1})\dfrac{G(x_k |
| 496 |
\longleftrightarrow x_{k-1})L_e(x_k |
| 497 |
\rightarrow x_{k-1})}{p_a(x_{k-1})p_a(x_k)}) |
| 498 |
\end{array} |
| 499 |
$$` |
| 500 |
|
| 501 |
expected := input |
| 502 |
actual := Parse(t, input) |
| 503 |
|
| 504 |
c := qt.New(t) |
| 505 |
c.Assert(actual, qt.Equals, expected) |
| 506 |
} |
| 507 |
|
| 508 |
func TestExample26(t *testing.T) { |
| 509 |
input := `\[ |
| 510 |
\begin{array} {lcl} |
| 511 |
L(p,w_i) &=& \dfrac{1}{N}\Sigma_{i=1}^N(\underbrace{f_r(x_2 |
| 512 |
\rightarrow x_1 |
| 513 |
\rightarrow x_0)G(x_1 |
| 514 |
\longleftrightarrow x_2)f_r(x_3 |
| 515 |
\rightarrow x_2 |
| 516 |
\rightarrow x_1)}_{sample\, radiance\, evaluation\, in\, stage2} |
| 517 |
\\\\\\ &=& |
| 518 |
\prod_{i=3}^{k-1}(\underbrace{\dfrac{f_r(x_{i+1} |
| 519 |
\rightarrow x_i |
| 520 |
\rightarrow x_{i-1})G(x_i |
| 521 |
\longleftrightarrow x_{i-1})}{p_a(x_{i-1})}}_{stored\,in\,vertex\, during\,light\, path\, tracing\, in\, stage1})\dfrac{G(x_k |
| 522 |
\longleftrightarrow x_{k-1})L_e(x_k |
| 523 |
\rightarrow x_{k-1})}{p_a(x_{k-1})p_a(x_k)}) |
| 524 |
\end{array} |
| 525 |
\]` |
| 526 |
|
| 527 |
expected := input |
| 528 |
actual := Parse(t, input) |
| 529 |
|
| 530 |
c := qt.New(t) |
| 531 |
c.Assert(actual, qt.Equals, expected) |
| 532 |
} |
| 533 |
|
| 534 |
func TestRepeatedBlockNodesInOneParagraph(t *testing.T) { |
| 535 |
input := `Block $$x$$ equation $$y$$.` |
| 536 |
expected := `<p>Block </p> |
| 537 |
$$x$$ |
| 538 |
<p> equation </p> |
| 539 |
$$y$$ |
| 540 |
<p>.</p>` |
| 541 |
actual := Parse(t, input) |
| 542 |
c := qt.New(t) |
| 543 |
c.Assert(actual, qt.Equals, expected) |
| 544 |
} |
| 545 |
|
| 546 |
func TestExample27(t *testing.T) { |
| 547 |
input := `Block $$a^*=x-b^*$$ equation |
| 548 |
|
| 549 |
Inline $a^*=x-b^*$ equation` |
| 550 |
expected := `<p>Block </p> |
| 551 |
$$a^*=x-b^*$$ |
| 552 |
<p> equation</p> |
| 553 |
<p>Inline $a^*=x-b^*$ equation</p>` |
| 554 |
actual := Parse(t, input) |
| 555 |
|
| 556 |
c := qt.New(t) |
| 557 |
c.Assert(actual, qt.Equals, expected) |
| 558 |
} |
| 559 |
|
| 560 |
func TestExample28(t *testing.T) { |
| 561 |
input := `Inline $a^*=x-b^*$ equation |
| 562 |
|
| 563 |
Block $$a^*=x-b^*$$ equation` |
| 564 |
expected := `<p>Inline $a^*=x-b^*$ equation</p> |
| 565 |
<p>Block </p> |
| 566 |
$$a^*=x-b^*$$ |
| 567 |
<p> equation</p>` |
| 568 |
actual := Parse(t, input) |
| 569 |
|
| 570 |
c := qt.New(t) |
| 571 |
c.Assert(actual, qt.Equals, expected) |
| 572 |
} |
| 573 |
|
| 574 |
func TestIssue32(t *testing.T) { |
| 575 |
input := `line one |
| 576 |
$$ a^n + b^n = c^n $$ |
| 577 |
line two` |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
expected := `<p>line one |
| 582 |
</p> |
| 583 |
$$ a^n + b^n = c^n $$ |
| 584 |
<p> |
| 585 |
line two</p>` |
| 586 |
actual := Parse(t, input) |
| 587 |
|
| 588 |
c := qt.New(t) |
| 589 |
c.Assert(actual, qt.Equals, expected) |
| 590 |
} |
| 591 |
|
| 592 |
func TestNodeDelimiter(t *testing.T) { |
| 593 |
input := ` |
| 594 |
Block $$a^*=x-b^*$$ equation |
| 595 |
Inline $a^*=x-b^*$ equation |
| 596 |
|
| 597 |
` |
| 598 |
|
| 599 |
c := qt.New(t) |
| 600 |
|
| 601 |
ParseWalk(t, input, func(n ast.Node, entering bool) bool { |
| 602 |
if entering { |
| 603 |
switch nn := n.(type) { |
| 604 |
case *PassthroughBlock: |
| 605 |
c.Assert(nn.Delimiters.Open, qt.Equals, "$$") |
| 606 |
c.Assert(nn.Delimiters.Close, qt.Equals, "$$") |
| 607 |
case *PassthroughInline: |
| 608 |
c.Assert(nn.Delimiters.Open, qt.Equals, "$") |
| 609 |
c.Assert(nn.Delimiters.Close, qt.Equals, "$") |
| 610 |
} |
| 611 |
} |
| 612 |
return false |
| 613 |
}) |
| 614 |
} |
| 615 |
|
| 616 |
func TestBlockMathInTightUnorderedList(t *testing.T) { |
| 617 |
input := `- $$a^*=x-b^*$$ |
| 618 |
- item 2` |
| 619 |
expected := `<ul> |
| 620 |
<li> |
| 621 |
$$a^*=x-b^*$$ |
| 622 |
</li> |
| 623 |
<li>item 2</li> |
| 624 |
</ul>` |
| 625 |
actual := Parse(t, input) |
| 626 |
|
| 627 |
c := qt.New(t) |
| 628 |
c.Assert(actual, qt.Equals, expected) |
| 629 |
} |
| 630 |
|
| 631 |
func TestBlockMathWithTextInTightUnorderedList(t *testing.T) { |
| 632 |
input := `- before \[a^*=x-b^*\] after` |
| 633 |
expected := `<ul> |
| 634 |
<li>before |
| 635 |
\[a^*=x-b^*\] |
| 636 |
after</li> |
| 637 |
</ul>` |
| 638 |
actual := Parse(t, input) |
| 639 |
|
| 640 |
c := qt.New(t) |
| 641 |
c.Assert(actual, qt.Equals, expected) |
| 642 |
} |
| 643 |
|
| 644 |
func TestInlineMathInTightUnorderedList(t *testing.T) { |
| 645 |
input := `- $a^*=x-b^*$ |
| 646 |
- item 2` |
| 647 |
expected := `<ul> |
| 648 |
<li>$a^*=x-b^*$</li> |
| 649 |
<li>item 2</li> |
| 650 |
</ul>` |
| 651 |
actual := Parse(t, input) |
| 652 |
|
| 653 |
c := qt.New(t) |
| 654 |
c.Assert(actual, qt.Equals, expected) |
| 655 |
} |
| 656 |
|
| 657 |
func TestInlineMathWithTextInTightUnorderedList(t *testing.T) { |
| 658 |
input := `- before $a^*=x-b^*$ after` |
| 659 |
expected := `<ul> |
| 660 |
<li>before $a^*=x-b^*$ after</li> |
| 661 |
</ul>` |
| 662 |
actual := Parse(t, input) |
| 663 |
|
| 664 |
c := qt.New(t) |
| 665 |
c.Assert(actual, qt.Equals, expected) |
| 666 |
} |
| 667 |
|
| 668 |
func TestBlockMathInLooseUnorderedList(t *testing.T) { |
| 669 |
input := `- item 1 |
| 670 |
|
| 671 |
- $$a^*=x-b^*$$ |
| 672 |
|
| 673 |
- item 3` |
| 674 |
expected := `<ul> |
| 675 |
<li> |
| 676 |
<p>item 1</p> |
| 677 |
</li> |
| 678 |
<li> |
| 679 |
$$a^*=x-b^*$$ |
| 680 |
</li> |
| 681 |
<li> |
| 682 |
<p>item 3</p> |
| 683 |
</li> |
| 684 |
</ul>` |
| 685 |
actual := Parse(t, input) |
| 686 |
|
| 687 |
c := qt.New(t) |
| 688 |
c.Assert(actual, qt.Equals, expected) |
| 689 |
} |
| 690 |
|
| 691 |
func TestInlineMathInLooseUnorderedList(t *testing.T) { |
| 692 |
input := `- item 1 |
| 693 |
|
| 694 |
- $a^*=x-b^*$ |
| 695 |
|
| 696 |
- item 3` |
| 697 |
expected := `<ul> |
| 698 |
<li> |
| 699 |
<p>item 1</p> |
| 700 |
</li> |
| 701 |
<li> |
| 702 |
<p>$a^*=x-b^*$</p> |
| 703 |
</li> |
| 704 |
<li> |
| 705 |
<p>item 3</p> |
| 706 |
</li> |
| 707 |
</ul>` |
| 708 |
actual := Parse(t, input) |
| 709 |
|
| 710 |
c := qt.New(t) |
| 711 |
c.Assert(actual, qt.Equals, expected) |
| 712 |
} |
| 713 |
|
| 714 |
func TestBlockMathInTightOrderedList(t *testing.T) { |
| 715 |
input := `1. $$a^*=x-b^*$$ |
| 716 |
2. item 2` |
| 717 |
expected := `<ol> |
| 718 |
<li> |
| 719 |
$$a^*=x-b^*$$ |
| 720 |
</li> |
| 721 |
<li>item 2</li> |
| 722 |
</ol>` |
| 723 |
actual := Parse(t, input) |
| 724 |
|
| 725 |
c := qt.New(t) |
| 726 |
c.Assert(actual, qt.Equals, expected) |
| 727 |
} |
| 728 |
|
| 729 |
func TestBlockMathWithTextInTightOrderedList(t *testing.T) { |
| 730 |
input := `1. before \[a^*=x-b^*\] after` |
| 731 |
expected := `<ol> |
| 732 |
<li>before |
| 733 |
\[a^*=x-b^*\] |
| 734 |
after</li> |
| 735 |
</ol>` |
| 736 |
actual := Parse(t, input) |
| 737 |
|
| 738 |
c := qt.New(t) |
| 739 |
c.Assert(actual, qt.Equals, expected) |
| 740 |
} |
| 741 |
|
| 742 |
func TestInlineMathInTightOrderedList(t *testing.T) { |
| 743 |
input := `1. $a^*=x-b^*$ |
| 744 |
2. item 2` |
| 745 |
expected := `<ol> |
| 746 |
<li>$a^*=x-b^*$</li> |
| 747 |
<li>item 2</li> |
| 748 |
</ol>` |
| 749 |
actual := Parse(t, input) |
| 750 |
|
| 751 |
c := qt.New(t) |
| 752 |
c.Assert(actual, qt.Equals, expected) |
| 753 |
} |
| 754 |
|
| 755 |
func TestInlineMathWithTextInTightOrderedList(t *testing.T) { |
| 756 |
input := `1. before $a^*=x-b^*$ after` |
| 757 |
expected := `<ol> |
| 758 |
<li>before $a^*=x-b^*$ after</li> |
| 759 |
</ol>` |
| 760 |
actual := Parse(t, input) |
| 761 |
|
| 762 |
c := qt.New(t) |
| 763 |
c.Assert(actual, qt.Equals, expected) |
| 764 |
} |
| 765 |
|
| 766 |
func TestBlockMathInLooseOrderedList(t *testing.T) { |
| 767 |
input := `1. item 1 |
| 768 |
|
| 769 |
2. $$a^*=x-b^*$$ |
| 770 |
|
| 771 |
3. item 3` |
| 772 |
expected := `<ol> |
| 773 |
<li> |
| 774 |
<p>item 1</p> |
| 775 |
</li> |
| 776 |
<li> |
| 777 |
$$a^*=x-b^*$$ |
| 778 |
</li> |
| 779 |
<li> |
| 780 |
<p>item 3</p> |
| 781 |
</li> |
| 782 |
</ol>` |
| 783 |
actual := Parse(t, input) |
| 784 |
|
| 785 |
c := qt.New(t) |
| 786 |
c.Assert(actual, qt.Equals, expected) |
| 787 |
} |
| 788 |
|
| 789 |
func TestInlineMathInLooseOrderedList(t *testing.T) { |
| 790 |
input := `1. item 1 |
| 791 |
|
| 792 |
2. $a^*=x-b^*$ |
| 793 |
|
| 794 |
3. item 3` |
| 795 |
expected := `<ol> |
| 796 |
<li> |
| 797 |
<p>item 1</p> |
| 798 |
</li> |
| 799 |
<li> |
| 800 |
<p>$a^*=x-b^*$</p> |
| 801 |
</li> |
| 802 |
<li> |
| 803 |
<p>item 3</p> |
| 804 |
</li> |
| 805 |
</ol>` |
| 806 |
actual := Parse(t, input) |
| 807 |
|
| 808 |
c := qt.New(t) |
| 809 |
c.Assert(actual, qt.Equals, expected) |
| 810 |
} |
| 811 |
|
| 812 |
func TestBlockMathInNestedTightUnorderedList(t *testing.T) { |
| 813 |
input := `- outer 1 |
| 814 |
- $$a^*=x-b^*$$ |
| 815 |
- inner 2 |
| 816 |
- outer 2` |
| 817 |
expected := `<ul> |
| 818 |
<li>outer 1 |
| 819 |
<ul> |
| 820 |
<li> |
| 821 |
$$a^*=x-b^*$$ |
| 822 |
</li> |
| 823 |
<li>inner 2</li> |
| 824 |
</ul> |
| 825 |
</li> |
| 826 |
<li>outer 2</li> |
| 827 |
</ul>` |
| 828 |
actual := Parse(t, input) |
| 829 |
|
| 830 |
c := qt.New(t) |
| 831 |
c.Assert(actual, qt.Equals, expected) |
| 832 |
} |
| 833 |
|
| 834 |
func TestInlineMathInNestedTightUnorderedList(t *testing.T) { |
| 835 |
input := `- outer 1 |
| 836 |
- $a^*=x-b^*$ |
| 837 |
- inner 2 |
| 838 |
- outer 2` |
| 839 |
expected := `<ul> |
| 840 |
<li>outer 1 |
| 841 |
<ul> |
| 842 |
<li>$a^*=x-b^*$</li> |
| 843 |
<li>inner 2</li> |
| 844 |
</ul> |
| 845 |
</li> |
| 846 |
<li>outer 2</li> |
| 847 |
</ul>` |
| 848 |
actual := Parse(t, input) |
| 849 |
|
| 850 |
c := qt.New(t) |
| 851 |
c.Assert(actual, qt.Equals, expected) |
| 852 |
} |
| 853 |
|
| 854 |
func TestBlockMathInNestedLooseUnorderedList(t *testing.T) { |
| 855 |
input := `- outer 1 |
| 856 |
|
| 857 |
- $$a^*=x-b^*$$ |
| 858 |
|
| 859 |
- inner 2 |
| 860 |
|
| 861 |
- outer 2` |
| 862 |
expected := `<ul> |
| 863 |
<li> |
| 864 |
<p>outer 1</p> |
| 865 |
<ul> |
| 866 |
<li> |
| 867 |
$$a^*=x-b^*$$ |
| 868 |
</li> |
| 869 |
<li> |
| 870 |
<p>inner 2</p> |
| 871 |
</li> |
| 872 |
</ul> |
| 873 |
</li> |
| 874 |
<li> |
| 875 |
<p>outer 2</p> |
| 876 |
</li> |
| 877 |
</ul>` |
| 878 |
actual := Parse(t, input) |
| 879 |
|
| 880 |
c := qt.New(t) |
| 881 |
c.Assert(actual, qt.Equals, expected) |
| 882 |
} |
| 883 |
|
| 884 |
func TestInlineMathInNestedLooseUnorderedList(t *testing.T) { |
| 885 |
input := `- outer 1 |
| 886 |
|
| 887 |
- $a^*=x-b^*$ |
| 888 |
|
| 889 |
- inner 2 |
| 890 |
|
| 891 |
- outer 2` |
| 892 |
expected := `<ul> |
| 893 |
<li> |
| 894 |
<p>outer 1</p> |
| 895 |
<ul> |
| 896 |
<li> |
| 897 |
<p>$a^*=x-b^*$</p> |
| 898 |
</li> |
| 899 |
<li> |
| 900 |
<p>inner 2</p> |
| 901 |
</li> |
| 902 |
</ul> |
| 903 |
</li> |
| 904 |
<li> |
| 905 |
<p>outer 2</p> |
| 906 |
</li> |
| 907 |
</ul>` |
| 908 |
actual := Parse(t, input) |
| 909 |
|
| 910 |
c := qt.New(t) |
| 911 |
c.Assert(actual, qt.Equals, expected) |
| 912 |
} |
| 913 |
|
| 914 |
func TestBlockMathInNestedTightOrderedList(t *testing.T) { |
| 915 |
input := `1. outer 1 |
| 916 |
1. $$a^*=x-b^*$$ |
| 917 |
2. inner 2 |
| 918 |
2. outer 2` |
| 919 |
expected := `<ol> |
| 920 |
<li>outer 1 |
| 921 |
<ol> |
| 922 |
<li> |
| 923 |
$$a^*=x-b^*$$ |
| 924 |
</li> |
| 925 |
<li>inner 2</li> |
| 926 |
</ol> |
| 927 |
</li> |
| 928 |
<li>outer 2</li> |
| 929 |
</ol>` |
| 930 |
actual := Parse(t, input) |
| 931 |
|
| 932 |
c := qt.New(t) |
| 933 |
c.Assert(actual, qt.Equals, expected) |
| 934 |
} |
| 935 |
|
| 936 |
func TestInlineMathInNestedTightOrderedList(t *testing.T) { |
| 937 |
input := `1. outer 1 |
| 938 |
1. $a^*=x-b^*$ |
| 939 |
2. inner 2 |
| 940 |
2. outer 2` |
| 941 |
expected := `<ol> |
| 942 |
<li>outer 1 |
| 943 |
<ol> |
| 944 |
<li>$a^*=x-b^*$</li> |
| 945 |
<li>inner 2</li> |
| 946 |
</ol> |
| 947 |
</li> |
| 948 |
<li>outer 2</li> |
| 949 |
</ol>` |
| 950 |
actual := Parse(t, input) |
| 951 |
|
| 952 |
c := qt.New(t) |
| 953 |
c.Assert(actual, qt.Equals, expected) |
| 954 |
} |
| 955 |
|
| 956 |
func TestBlockMathInNestedLooseOrderedList(t *testing.T) { |
| 957 |
input := `1. outer 1 |
| 958 |
|
| 959 |
1. $$a^*=x-b^*$$ |
| 960 |
|
| 961 |
2. inner 2 |
| 962 |
|
| 963 |
2. outer 2` |
| 964 |
expected := `<ol> |
| 965 |
<li> |
| 966 |
<p>outer 1</p> |
| 967 |
<ol> |
| 968 |
<li> |
| 969 |
$$a^*=x-b^*$$ |
| 970 |
</li> |
| 971 |
<li> |
| 972 |
<p>inner 2</p> |
| 973 |
</li> |
| 974 |
</ol> |
| 975 |
</li> |
| 976 |
<li> |
| 977 |
<p>outer 2</p> |
| 978 |
</li> |
| 979 |
</ol>` |
| 980 |
actual := Parse(t, input) |
| 981 |
|
| 982 |
c := qt.New(t) |
| 983 |
c.Assert(actual, qt.Equals, expected) |
| 984 |
} |
| 985 |
|
| 986 |
func TestInlineMathInNestedLooseOrderedList(t *testing.T) { |
| 987 |
input := `1. outer 1 |
| 988 |
|
| 989 |
1. $a^*=x-b^*$ |
| 990 |
|
| 991 |
2. inner 2 |
| 992 |
|
| 993 |
2. outer 2` |
| 994 |
expected := `<ol> |
| 995 |
<li> |
| 996 |
<p>outer 1</p> |
| 997 |
<ol> |
| 998 |
<li> |
| 999 |
<p>$a^*=x-b^*$</p> |
| 1000 |
</li> |
| 1001 |
<li> |
| 1002 |
<p>inner 2</p> |
| 1003 |
</li> |
| 1004 |
</ol> |
| 1005 |
</li> |
| 1006 |
<li> |
| 1007 |
<p>outer 2</p> |
| 1008 |
</li> |
| 1009 |
</ol>` |
| 1010 |
actual := Parse(t, input) |
| 1011 |
|
| 1012 |
c := qt.New(t) |
| 1013 |
c.Assert(actual, qt.Equals, expected) |
| 1014 |
} |
| 1015 |
|
| 1016 |
func TestBlockMathInTaskList(t *testing.T) { |
| 1017 |
input := `- [ ] $$a^*=x-b^*$$ |
| 1018 |
- [x] item 2` |
| 1019 |
expected := "<ul>\n<li><input disabled=\"\" type=\"checkbox\"> \n$$a^*=x-b^*$$\n</li>\n<li><input checked=\"\" disabled=\"\" type=\"checkbox\"> item 2</li>\n</ul>" |
| 1020 |
actual := Parse(t, input) |
| 1021 |
|
| 1022 |
c := qt.New(t) |
| 1023 |
c.Assert(actual, qt.Equals, expected) |
| 1024 |
} |
| 1025 |
|
| 1026 |
func TestBlockMathWithTextInTaskList(t *testing.T) { |
| 1027 |
input := `- [ ] before \[a^*=x-b^*\] after` |
| 1028 |
expected := "<ul>\n<li><input disabled=\"\" type=\"checkbox\"> before\n\\[a^*=x-b^*\\]\nafter</li>\n</ul>" |
| 1029 |
actual := Parse(t, input) |
| 1030 |
|
| 1031 |
c := qt.New(t) |
| 1032 |
c.Assert(actual, qt.Equals, expected) |
| 1033 |
} |
| 1034 |
|
| 1035 |
func TestInlineMathInTaskList(t *testing.T) { |
| 1036 |
input := `- [ ] $a^*=x-b^*$ |
| 1037 |
- [x] item 2` |
| 1038 |
expected := "<ul>\n<li><input disabled=\"\" type=\"checkbox\"> $a^*=x-b^*$</li>\n<li><input checked=\"\" disabled=\"\" type=\"checkbox\"> item 2</li>\n</ul>" |
| 1039 |
actual := Parse(t, input) |
| 1040 |
|
| 1041 |
c := qt.New(t) |
| 1042 |
c.Assert(actual, qt.Equals, expected) |
| 1043 |
} |
| 1044 |
|
| 1045 |
func TestInlineMathWithTextInTaskList(t *testing.T) { |
| 1046 |
input := `- [x] before $a^*=x-b^*$ after` |
| 1047 |
expected := "<ul>\n<li><input checked=\"\" disabled=\"\" type=\"checkbox\"> before $a^*=x-b^*$ after</li>\n</ul>" |
| 1048 |
actual := Parse(t, input) |
| 1049 |
|
| 1050 |
c := qt.New(t) |
| 1051 |
c.Assert(actual, qt.Equals, expected) |
| 1052 |
} |
| 1053 |
|
| 1054 |
func TestBlockMathInDescriptionListDefinition(t *testing.T) { |
| 1055 |
input := `term |
| 1056 |
: $$a^*=x-b^*$$` |
| 1057 |
expected := `<dl> |
| 1058 |
<dt>term</dt> |
| 1059 |
<dd>$$a^*=x-b^*$$ |
| 1060 |
</dd> |
| 1061 |
</dl>` |
| 1062 |
actual := Parse(t, input) |
| 1063 |
|
| 1064 |
c := qt.New(t) |
| 1065 |
c.Assert(actual, qt.Equals, expected) |
| 1066 |
} |
| 1067 |
|
| 1068 |
func TestBlockMathWithTextInDescriptionListDefinition(t *testing.T) { |
| 1069 |
input := `term |
| 1070 |
: before \[a^*=x-b^*\] after` |
| 1071 |
expected := `<dl> |
| 1072 |
<dt>term</dt> |
| 1073 |
<dd>before |
| 1074 |
\[a^*=x-b^*\] |
| 1075 |
after</dd> |
| 1076 |
</dl>` |
| 1077 |
actual := Parse(t, input) |
| 1078 |
|
| 1079 |
c := qt.New(t) |
| 1080 |
c.Assert(actual, qt.Equals, expected) |
| 1081 |
} |
| 1082 |
|
| 1083 |
func TestInlineMathInDescriptionListDefinition(t *testing.T) { |
| 1084 |
input := `term |
| 1085 |
: $a^*=x-b^*$` |
| 1086 |
expected := `<dl> |
| 1087 |
<dt>term</dt> |
| 1088 |
<dd>$a^*=x-b^*$</dd> |
| 1089 |
</dl>` |
| 1090 |
actual := Parse(t, input) |
| 1091 |
|
| 1092 |
c := qt.New(t) |
| 1093 |
c.Assert(actual, qt.Equals, expected) |
| 1094 |
} |
| 1095 |
|
| 1096 |
func TestBlockMathInDescriptionListTerm(t *testing.T) { |
| 1097 |
|
| 1098 |
input := `$$a^*=x-b^*$$ |
| 1099 |
: definition` |
| 1100 |
expected := `<dl> |
| 1101 |
<dt>$$a^*=x-b^*$$</dt> |
| 1102 |
<dd>definition</dd> |
| 1103 |
</dl>` |
| 1104 |
actual := Parse(t, input) |
| 1105 |
|
| 1106 |
c := qt.New(t) |
| 1107 |
c.Assert(actual, qt.Equals, expected) |
| 1108 |
} |
| 1109 |
|
| 1110 |
func TestInlineMathInDescriptionListTerm(t *testing.T) { |
| 1111 |
input := `$a^*=x-b^*$ |
| 1112 |
: definition` |
| 1113 |
expected := `<dl> |
| 1114 |
<dt>$a^*=x-b^*$</dt> |
| 1115 |
<dd>definition</dd> |
| 1116 |
</dl>` |
| 1117 |
actual := Parse(t, input) |
| 1118 |
|
| 1119 |
c := qt.New(t) |
| 1120 |
c.Assert(actual, qt.Equals, expected) |
| 1121 |
} |
| 1122 |
|
| 1123 |
func BenchmarkWithAndWithoutPassthrough(b *testing.B) { |
| 1124 |
const input = ` |
| 1125 |
## Block |
| 1126 |
|
| 1127 |
$$ |
| 1128 |
a^*=x-b^* |
| 1129 |
$$ |
| 1130 |
|
| 1131 |
## Inline |
| 1132 |
|
| 1133 |
Inline $a^*=x-b^*$ equation.` |
| 1134 |
|
| 1135 |
b.Run("without passthrough", func(b *testing.B) { |
| 1136 |
md := goldmark.New() |
| 1137 |
b.ResetTimer() |
| 1138 |
for i := 0; i < b.N; i++ { |
| 1139 |
var buf bytes.Buffer |
| 1140 |
if err := md.Convert([]byte(input), &buf); err != nil { |
| 1141 |
b.Fatal(err) |
| 1142 |
} |
| 1143 |
} |
| 1144 |
}) |
| 1145 |
|
| 1146 |
b.Run("with passthrough", func(b *testing.B) { |
| 1147 |
md := buildTestParser() |
| 1148 |
b.ResetTimer() |
| 1149 |
for i := 0; i < b.N; i++ { |
| 1150 |
var buf bytes.Buffer |
| 1151 |
if err := md.Convert([]byte(input), &buf); err != nil { |
| 1152 |
b.Fatal(err) |
| 1153 |
} |
| 1154 |
} |
| 1155 |
}) |
| 1156 |
} |