# Emphasis and strong emphasis
John Gruber’s original Markdown syntax description (opens new window) says:
Markdown treats asterisks (
*
) and underscores (_
) as indicators of emphasis. Text wrapped with one*
or_
will be wrapped with an HTML<em>
tag; double*
’s or_
’s will be wrapped with an HTML<strong>
tag.
This is enough for most users, but these rules leave much undecided, especially when it comes to nested emphasis. The original Markdown.pl
test suite makes it clear that triple ***
and ___
delimiters can be used for strong emphasis, and most implementations have also allowed the following patterns:
***strong emph***
***strong** in emph*
***emph* in strong**
**in strong *emph***
*in emph **strong***
The following patterns are less widely supported, but the intent is clear and they are useful (especially in contexts like bibliography entries):
*emph *with emph* in it*
**strong **with strong** in it**
Many implementations have also restricted intraword emphasis to the *
forms, to avoid unwanted emphasis in words containing internal underscores. (It is best practice to put these in code spans, but users often do not.)
internal emphasis: foo*bar*baz
no emphasis: foo_bar_baz
The rules given below capture all of these patterns, while allowing for efficient parsing strategies that do not backtrack.
First, some definitions. A delimiter run (opens new window) is either a sequence of one or more *
characters that is not preceded or followed by a non-backslash-escaped *
character, or a sequence of one or more _
characters that is not preceded or followed by a non-backslash-escaped _
character.
A left-flanking delimiter run (opens new window) is a delimiter run (opens new window) that is (1) not followed by Unicode whitespace (opens new window), and either (2a) not followed by a punctuation character (opens new window),or (2b) followed by a punctuation character and preceded by Unicode whitespace (opens new window) or a punctuation character (opens new window). For purposes of this definition, the beginning and the end of the line count as Unicode whitespace.
A right-flanking delimiter run (opens new window) is a delimiter run (opens new window) that is (1) not preceded by Unicode whitespace (opens new window), and either (2a) not preceded by a punctuation character,or (2b) preceded by a punctuation character and followed by Unicode whitespace (opens new window) or a punctuation character (opens new window). For purposes of this definition, the beginning and the end of the line count as Unicode whitespace.
Here are some examples of delimiter runs.
left-flanking but not right-flanking:
***abc _abc **"abc" _"abc"
right-flanking but not left-flanking:
abc*** abc_ "abc"** "abc"_
Both left and right-flanking:
abc***def "abc"_"def"
Neither left nor right-flanking:
abc *** def a _ b
(The idea of distinguishing left-flanking and right-flanking delimiter runs based on the character before and the character after comes from Roopesh Chander’s vfmd (opens new window). vfmd uses the terminology “emphasis indicator string” instead of “delimiter run,” and its rules for distinguishing left- and right-flanking runs are a bit more complex than the ones given here.)
The following rules define emphasis and strong emphasis:
- A single
*
character can open emphasis (opens new window) iff (if and only if) it is part of a left-flanking delimiter run (opens new window). - A single
_
character can open emphasis (opens new window) iff it is part of a left-flanking delimiter run (opens new window) and either (a) not part of a right-flanking delimiter run (opens new window) or (b) part of a right-flanking delimiter run (opens new window) preceded by punctuation. - A single
*
character can close emphasis (opens new window) iff it is part of a right-flanking delimiter run (opens new window). - A single
_
character can close emphasis (opens new window) iff it is part of a right-flanking delimiter run (opens new window) and either (a) not part of a left-flanking delimiter run (opens new window) or (b) part of a left-flanking delimiter run (opens new window) followed by punctuation. - A double
**
can open strong emphasis (opens new window) iff it is part of a left-flanking delimiter run (opens new window). - A double
__
can open strong emphasis (opens new window) iff it is part of a left-flanking delimiter run (opens new window) and either (a) not part of a right-flanking delimiter run (opens new window) or (b) part of a right-flanking delimiter run (opens new window) preceded by punctuation. - A double
**
can close strong emphasis (opens new window) iff it is part of a right-flanking delimiter run (opens new window). - A double
__
can close strong emphasis (opens new window) iff it is part of a right-flanking delimiter run (opens new window) and either (a) not part of a left-flanking delimiter run (opens new window) or (b) part of a left-flanking delimiter run (opens new window) followed by punctuation. - Emphasis begins with a delimiter that can open emphasis (opens new window) and ends with a delimiter that can close emphasis (opens new window), and that uses the same character (
_
or*
) as the opening delimiter. The opening and closing delimiters must belong to separate delimiter runs (opens new window). If one of the delimiters can both open and close emphasis, then the sum of the lengths of the delimiter runs containing the opening and closing delimiters must not be a multiple of 3 unless both lengths are multiples of 3. - Strong emphasis begins with a delimiter that can open strong emphasis (opens new window) and ends with a delimiter thatcan close strong emphasis (opens new window), and that uses the same character (
_
or*
) as the opening delimiter. The opening and closing delimiters must belong to separate delimiter runs (opens new window). If one of the delimiters can both open and close strong emphasis, then the sum of the lengths of the delimiter runs containing the opening and closing delimiters must not be a multiple of 3 unless both lengths are multiples of 3. - A literal
*
character cannot occur at the beginning or end of*
-delimited emphasis or**
-delimited strong emphasis, unless it is backslash-escaped. - A literal
_
character cannot occur at the beginning or end of_
-delimited emphasis or__
-delimited strong emphasis, unless it is backslash-escaped.
Where rules 1–12 above are compatible with multiple parsings, the following principles resolve ambiguity:
- The number of nestings should be minimized. Thus, for example, an interpretation
<strong>...</strong>
is always preferred to<em><em>...</em></em>
. - An interpretation
<em><strong>...</strong></em>
is always preferred to<strong><em>...</em></strong>
. - When two potential emphasis or strong emphasis spans overlap, so that the second begins before the first ends and ends after the first ends, the first takes precedence. Thus, for example,
*foo _bar* baz_
is parsed as<em>foo _bar</em> baz_
rather than*foo <em>bar* baz</em>
. - When there are two potential emphasis or strong emphasis spans with the same closing delimiter, the shorter one (the one that opens later) takes precedence. Thus, for example,
**foo **bar baz**
is parsed as**foo <strong>bar baz</strong>
rather than<strong>foo **bar baz</strong>
. - Inline code spans, links, images, and HTML tags group more tightly than emphasis. So, when there is a choice between an interpretation that contains one of these elements and one that does not, the former always wins. Thus, for example,
*[foo*](bar)
is parsed as*<a href="bar">foo*</a>
rather than as<em>[foo</em>](bar)
.
These rules can be illustrated through a series of examples.
Rule 1:
Example 360
Markdown | HTML | Demo |
---|---|---|
|
|
This is not emphasis, because the opening *
is followed by whitespace, and hence not part of a left-flanking delimiter run (opens new window):
Example 361
Markdown | HTML | Demo |
---|---|---|
|
|
This is not emphasis, because the opening *
is preceded by an alphanumeric and followed by punctuation, and hence not part of a left-flanking delimiter run (opens new window):
Example 362
Markdown | HTML | Demo |
---|---|---|
|
|
Unicode nonbreaking spaces count as whitespace, too:
Example 363
Markdown | HTML | Demo |
---|---|---|
|
|
Intraword emphasis with *
is permitted:
Example 364
Markdown | HTML | Demo |
---|---|---|
|
|
Example 365
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 2:
Example 366
Markdown | HTML | Demo |
---|---|---|
|
|
This is not emphasis, because the opening _
is followed by whitespace:
Example 367
Markdown | HTML | Demo |
---|---|---|
|
|
This is not emphasis, because the opening _
is preceded by an alphanumeric and followed by punctuation:
Example 368
Markdown | HTML | Demo |
---|---|---|
|
|
Emphasis with _
is not allowed inside words:
Example 369
Markdown | HTML | Demo |
---|---|---|
|
|
Example 370
Markdown | HTML | Demo |
---|---|---|
|
|
Example 371
Markdown | HTML | Demo |
---|---|---|
|
|
Here _
does not generate emphasis, because the first delimiter run is right-flanking and the second left-flanking:
Example 372
Markdown | HTML | Demo |
---|---|---|
|
|
This is emphasis, even though the opening delimiter is both left- and right-flanking, because it is preceded by punctuation:
Example 373
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 3:
This is not emphasis, because the closing delimiter does not match the opening delimiter:
Example 374
Markdown | HTML | Demo |
---|---|---|
|
|
This is not emphasis, because the closing *
is preceded by whitespace:
Example 375
Markdown | HTML | Demo |
---|---|---|
|
|
A newline also counts as whitespace:
Example 376
Markdown | HTML | Demo |
---|---|---|
|
|
This is not emphasis, because the second *
is preceded by punctuation and followed by an alphanumeric (hence it is not part of a right-flanking delimiter run (opens new window):
Example 377
Markdown | HTML | Demo |
---|---|---|
|
|
The point of this restriction is more easily appreciated with this example:
Example 378
Markdown | HTML | Demo |
---|---|---|
|
|
Intraword emphasis with *
is allowed:
Example 379
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 4:
This is not emphasis, because the closing _
is preceded by whitespace:
Example 380
Markdown | HTML | Demo |
---|---|---|
|
|
This is not emphasis, because the second _
is preceded by punctuation and followed by an alphanumeric:
Example 381
Markdown | HTML | Demo |
---|---|---|
|
|
This is emphasis within emphasis:
Example 382
Markdown | HTML | Demo |
---|---|---|
|
|
Intraword emphasis is disallowed for _
:
Example 383
Markdown | HTML | Demo |
---|---|---|
|
|
Example 384
Markdown | HTML | Demo |
---|---|---|
|
|
Example 385
Markdown | HTML | Demo |
---|---|---|
|
|
This is emphasis, even though the closing delimiter is both left- and right-flanking, because it is followed by punctuation:
Example 386
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 5:
Example 387
Markdown | HTML | Demo |
---|---|---|
|
|
This is not strong emphasis, because the opening delimiter is followed by whitespace:
Example 388
Markdown | HTML | Demo |
---|---|---|
|
|
This is not strong emphasis, because the opening **
is preceded by an alphanumeric and followed by punctuation, and hence not part of a left-flanking delimiter run (opens new window):
Example 389
Markdown | HTML | Demo |
---|---|---|
|
|
Intraword strong emphasis with **
is permitted:
Example 390
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 6:
Example 391
Markdown | HTML | Demo |
---|---|---|
|
|
This is not strong emphasis, because the opening delimiter is followed by whitespace:
Example 392
Markdown | HTML | Demo |
---|---|---|
|
|
A newline counts as whitespace:
Example 393
Markdown | HTML | Demo |
---|---|---|
|
|
This is not strong emphasis, because the opening __
is preceded by an alphanumeric and followed by punctuation:
Example 394
Markdown | HTML | Demo |
---|---|---|
|
|
Intraword strong emphasis is forbidden with __
:
Example 395
Markdown | HTML | Demo |
---|---|---|
|
|
Example 396
Markdown | HTML | Demo |
---|---|---|
|
|
Example 397
Markdown | HTML | Demo |
---|---|---|
|
|
Example 398
Markdown | HTML | Demo |
---|---|---|
|
|
This is strong emphasis, even though the opening delimiter is both left- and right-flanking, because it is preceded by punctuation:
Example 399
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 7:
This is not strong emphasis, because the closing delimiter is preceded by whitespace:
Example 400
Markdown | HTML | Demo |
---|---|---|
|
|
(Nor can it be interpreted as an emphasized *foo bar *
, because of Rule 11.)
This is not strong emphasis, because the second **
is preceded by punctuation and followed by an alphanumeric:
Example 401
Markdown | HTML | Demo |
---|---|---|
|
|
The point of this restriction is more easily appreciated with these examples:
Example 402
Markdown | HTML | Demo |
---|---|---|
|
|
Example 403
Markdown | HTML | Demo |
---|---|---|
|
|
Example 404
Markdown | HTML | Demo |
---|---|---|
|
|
Intraword emphasis:
Example 405
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 8:
This is not strong emphasis, because the closing delimiter is preceded by whitespace:
Example 406
Markdown | HTML | Demo |
---|---|---|
|
|
This is not strong emphasis, because the second __
is preceded by punctuation and followed by an alphanumeric:
Example 407
Markdown | HTML | Demo |
---|---|---|
|
|
The point of this restriction is more easily appreciated with this example:
Example 408
Markdown | HTML | Demo |
---|---|---|
|
|
Intraword strong emphasis is forbidden with __
:
Example 409
Markdown | HTML | Demo |
---|---|---|
|
|
Example 410
Markdown | HTML | Demo |
---|---|---|
|
|
Example 411
Markdown | HTML | Demo |
---|---|---|
|
|
This is strong emphasis, even though the closing delimiter is both left- and right-flanking, because it is followed by punctuation:
Example 412
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 9:
Any nonempty sequence of inline elements can be the contents of an emphasized span.
Example 413
Markdown | HTML | Demo |
---|---|---|
|
|
Example 414
Markdown | HTML | Demo |
---|---|---|
|
|
In particular, emphasis and strong emphasis can be nested inside emphasis:
Example 415
Markdown | HTML | Demo |
---|---|---|
|
|
Example 416
Markdown | HTML | Demo |
---|---|---|
|
|
Example 417
Markdown | HTML | Demo |
---|---|---|
|
|
Example 418
Markdown | HTML | Demo |
---|---|---|
|
|
Example 419
Markdown | HTML | Demo |
---|---|---|
|
|
Example 420
Markdown | HTML | Demo |
---|---|---|
|
|
Note that in the preceding case, the interpretation
<p><em>foo</em><em>bar<em></em>baz</em></p>
is precluded by the condition that a delimiter that can both open and close (like the *
after foo
) cannot form emphasis if the sum of the lengths of the delimiter runs containing the opening and closing delimiters is a multiple of 3 unless both lengths are multiples of 3.
For the same reason, we don’t get two consecutive emphasis sections in this example:
Example 421
Markdown | HTML | Demo |
---|---|---|
|
|
The same condition ensures that the following cases are all strong emphasis nested inside emphasis, even when the interior spaces are omitted:
Example 422
Markdown | HTML | Demo |
---|---|---|
|
|
Example 423
Markdown | HTML | Demo |
---|---|---|
|
|
Example 424
Markdown | HTML | Demo |
---|---|---|
|
|
When the lengths of the interior closing and opening delimiter runs are both multiples of 3, though, they can match to create emphasis:
Example 425
Markdown | HTML | Demo |
---|---|---|
|
|
Example 426
Markdown | HTML | Demo |
---|---|---|
|
|
Indefinite levels of nesting are possible:
Example 427
Markdown | HTML | Demo |
---|---|---|
|
|
Example 428
Markdown | HTML | Demo |
---|---|---|
|
|
There can be no empty emphasis or strong emphasis:
Example 429
Markdown | HTML | Demo |
---|---|---|
|
|
Example 430
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 10:
Any nonempty sequence of inline elements can be the contents of an strongly emphasized span.
Example 431
Markdown | HTML | Demo |
---|---|---|
|
|
Example 432
Markdown | HTML | Demo |
---|---|---|
|
|
In particular, emphasis and strong emphasis can be nested inside strong emphasis:
Example 433
Markdown | HTML | Demo |
---|---|---|
|
|
Example 434
Markdown | HTML | Demo |
---|---|---|
|
|
Example 435
Markdown | HTML | Demo |
---|---|---|
|
|
Example 436
Markdown | HTML | Demo |
---|---|---|
|
|
Example 437
Markdown | HTML | Demo |
---|---|---|
|
|
Example 438
Markdown | HTML | Demo |
---|---|---|
|
|
Example 439
Markdown | HTML | Demo |
---|---|---|
|
|
Example 440
Markdown | HTML | Demo |
---|---|---|
|
|
Indefinite levels of nesting are possible:
Example 441
Markdown | HTML | Demo |
---|---|---|
|
|
Example 442
Markdown | HTML | Demo |
---|---|---|
|
|
There can be no empty emphasis or strong emphasis:
Example 443
Markdown | HTML | Demo |
---|---|---|
|
|
Example 444
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 11:
Example 445
Markdown | HTML | Demo |
---|---|---|
|
|
Example 446
Markdown | HTML | Demo |
---|---|---|
|
|
Example 447
Markdown | HTML | Demo |
---|---|---|
|
|
Example 448
Markdown | HTML | Demo |
---|---|---|
|
|
Example 449
Markdown | HTML | Demo |
---|---|---|
|
|
Example 450
Markdown | HTML | Demo |
---|---|---|
|
|
Note that when delimiters do not match evenly, Rule 11 determines that the excess literal *
characters will appear outside of the emphasis, rather than inside it:
Example 451
Markdown | HTML | Demo |
---|---|---|
|
|
Example 452
Markdown | HTML | Demo |
---|---|---|
|
|
Example 453
Markdown | HTML | Demo |
---|---|---|
|
|
Example 454
Markdown | HTML | Demo |
---|---|---|
|
|
Example 455
Markdown | HTML | Demo |
---|---|---|
|
|
Example 456
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 12:
Example 457
Markdown | HTML | Demo |
---|---|---|
|
|
Example 458
Markdown | HTML | Demo |
---|---|---|
|
|
Example 459
Markdown | HTML | Demo |
---|---|---|
|
|
Example 460
Markdown | HTML | Demo |
---|---|---|
|
|
Example 461
Markdown | HTML | Demo |
---|---|---|
|
|
Example 462
Markdown | HTML | Demo |
---|---|---|
|
|
Example 463
Markdown | HTML | Demo |
---|---|---|
|
|
Note that when delimiters do not match evenly, Rule 12 determines that the excess literal _
characters will appear outside of the emphasis, rather than inside it:
Example 464
Markdown | HTML | Demo |
---|---|---|
|
|
Example 465
Markdown | HTML | Demo |
---|---|---|
|
|
Example 466
Markdown | HTML | Demo |
---|---|---|
|
|
Example 467
Markdown | HTML | Demo |
---|---|---|
|
|
Example 468
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 13 implies that if you want emphasis nested directly inside emphasis, you must use different delimiters:
Example 469
Markdown | HTML | Demo |
---|---|---|
|
|
Example 470
Markdown | HTML | Demo |
---|---|---|
|
|
Example 471
Markdown | HTML | Demo |
---|---|---|
|
|
Example 472
Markdown | HTML | Demo |
---|---|---|
|
|
However, strong emphasis within strong emphasis is possible without switching delimiters:
Example 473
Markdown | HTML | Demo |
---|---|---|
|
|
Example 474
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 13 can be applied to arbitrarily long sequences of delimiters:
Example 475
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 14:
Example 476
Markdown | HTML | Demo |
---|---|---|
|
|
Example 477
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 15:
Example 478
Markdown | HTML | Demo |
---|---|---|
|
|
Example 479
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 16:
Example 480
Markdown | HTML | Demo |
---|---|---|
|
|
Example 481
Markdown | HTML | Demo |
---|---|---|
|
|
Rule 17:
Example 482
Markdown | HTML | Demo |
---|---|---|
|
|
Example 483
Markdown | HTML | Demo |
---|---|---|
|
|
Example 484
Markdown | HTML | Demo |
---|---|---|
|
|
Example 485
Markdown | HTML | Demo |
---|---|---|
|
|
Example 486
Markdown | HTML | Demo |
---|---|---|
|
|
Example 487
Markdown | HTML | Demo |
---|---|---|
|
|
Example 488
Markdown | HTML | Demo |
---|---|---|
|
|
Example 489
Markdown | HTML | Demo |
---|---|---|
|
|
Example 490
Markdown | HTML | Demo |
---|---|---|
|
|