Debian Codegraph @

spec.txt

raw
1 ---
2 title: CommonMark Spec
3 author: John MacFarlane
4 version: 0.29
5 date: '2019-04-06'
6 license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
7 ...
8
9 # Introduction
10
11 ## What is Markdown?
12
13 Markdown is a plain text format for writing structured documents,
14 based on conventions for indicating formatting in email
15 and usenet posts. It was developed by John Gruber (with
16 help from Aaron Swartz) and released in 2004 in the form of a
17 [syntax description](http://daringfireball.net/projects/markdown/syntax)
18 and a Perl script (`Markdown.pl`) for converting Markdown to
19 HTML. In the next decade, dozens of implementations were
20 developed in many languages. Some extended the original
21 Markdown syntax with conventions for footnotes, tables, and
22 other document elements. Some allowed Markdown documents to be
23 rendered in formats other than HTML. Websites like Reddit,
24 StackOverflow, and GitHub had millions of people using Markdown.
25 And Markdown started to be used beyond the web, to author books,
26 articles, slide shows, letters, and lecture notes.
27
28 What distinguishes Markdown from many other lightweight markup
29 syntaxes, which are often easier to write, is its readability.
30 As Gruber writes:
31
32 > The overriding design goal for Markdown's formatting syntax is
33 > to make it as readable as possible. The idea is that a
34 > Markdown-formatted document should be publishable as-is, as
35 > plain text, without looking like it's been marked up with tags
36 > or formatting instructions.
37 > (<http://daringfireball.net/projects/markdown/>)
38
39 The point can be illustrated by comparing a sample of
40 [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
41 an equivalent sample of Markdown. Here is a sample of
42 AsciiDoc from the AsciiDoc manual:
43
44 ```
45 1. List item one.
46 +
47 List item one continued with a second paragraph followed by an
48 Indented block.
49 +
50 .................
51 $ ls *.sh
52 $ mv *.sh ~/tmp
53 .................
54 +
55 List item continued with a third paragraph.
56
57 2. List item two continued with an open block.
58 +
59 --
60 This paragraph is part of the preceding list item.
61
62 a. This list is nested and does not require explicit item
63 continuation.
64 +
65 This paragraph is part of the preceding list item.
66
67 b. List item b.
68
69 This paragraph belongs to item two of the outer list.
70 --
71 ```
72
73 And here is the equivalent in Markdown:
74 ```
75 1. List item one.
76
77 List item one continued with a second paragraph followed by an
78 Indented block.
79
80 $ ls *.sh
81 $ mv *.sh ~/tmp
82
83 List item continued with a third paragraph.
84
85 2. List item two continued with an open block.
86
87 This paragraph is part of the preceding list item.
88
89 1. This list is nested and does not require explicit item continuation.
90
91 This paragraph is part of the preceding list item.
92
93 2. List item b.
94
95 This paragraph belongs to item two of the outer list.
96 ```
97
98 The AsciiDoc version is, arguably, easier to write. You don't need
99 to worry about indentation. But the Markdown version is much easier
100 to read. The nesting of list items is apparent to the eye in the
101 source, not just in the processed document.
102
103 ## Why is a spec needed?
104
105 John Gruber's [canonical description of Markdown's
106 syntax](http://daringfireball.net/projects/markdown/syntax)
107 does not specify the syntax unambiguously. Here are some examples of
108 questions it does not answer:
109
110 1. How much indentation is needed for a sublist? The spec says that
111 continuation paragraphs need to be indented four spaces, but is
112 not fully explicit about sublists. It is natural to think that
113 they, too, must be indented four spaces, but `Markdown.pl` does
114 not require that. This is hardly a "corner case," and divergences
115 between implementations on this issue often lead to surprises for
116 users in real documents. (See [this comment by John
117 Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
118
119 2. Is a blank line needed before a block quote or heading?
120 Most implementations do not require the blank line. However,
121 this can lead to unexpected results in hard-wrapped text, and
122 also to ambiguities in parsing (note that some implementations
123 put the heading inside the blockquote, while others do not).
124 (John Gruber has also spoken [in favor of requiring the blank
125 lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
126
127 3. Is a blank line needed before an indented code block?
128 (`Markdown.pl` requires it, but this is not mentioned in the
129 documentation, and some implementations do not require it.)
130
131 ``` markdown
132 paragraph
133 code?
134 ```
135
136 4. What is the exact rule for determining when list items get
137 wrapped in `<p>` tags? Can a list be partially "loose" and partially
138 "tight"? What should we do with a list like this?
139
140 ``` markdown
141 1. one
142
143 2. two
144 3. three
145 ```
146
147 Or this?
148
149 ``` markdown
150 1. one
151 - a
152
153 - b
154 2. two
155 ```
156
157 (There are some relevant comments by John Gruber
158 [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
159
160 5. Can list markers be indented? Can ordered list markers be right-aligned?
161
162 ``` markdown
163 8. item 1
164 9. item 2
165 10. item 2a
166 ```
167
168 6. Is this one list with a thematic break in its second item,
169 or two lists separated by a thematic break?
170
171 ``` markdown
172 * a
173 * * * * *
174 * b
175 ```
176
177 7. When list markers change from numbers to bullets, do we have
178 two lists or one? (The Markdown syntax description suggests two,
179 but the perl scripts and many other implementations produce one.)
180
181 ``` markdown
182 1. fee
183 2. fie
184 - foe
185 - fum
186 ```
187
188 8. What are the precedence rules for the markers of inline structure?
189 For example, is the following a valid link, or does the code span
190 take precedence ?
191
192 ``` markdown
193 [a backtick (`)](/url) and [another backtick (`)](/url).
194 ```
195
196 9. What are the precedence rules for markers of emphasis and strong
197 emphasis? For example, how should the following be parsed?
198
199 ``` markdown
200 *foo *bar* baz*
201 ```
202
203 10. What are the precedence rules between block-level and inline-level
204 structure? For example, how should the following be parsed?
205
206 ``` markdown
207 - `a long code span can contain a hyphen like this
208 - and it can screw things up`
209 ```
210
211 11. Can list items include section headings? (`Markdown.pl` does not
212 allow this, but does allow blockquotes to include headings.)
213
214 ``` markdown
215 - # Heading
216 ```
217
218 12. Can list items be empty?
219
220 ``` markdown
221 * a
222 *
223 * b
224 ```
225
226 13. Can link references be defined inside block quotes or list items?
227
228 ``` markdown
229 > Blockquote [foo].
230 >
231 > [foo]: /url
232 ```
233
234 14. If there are multiple definitions for the same reference, which takes
235 precedence?
236
237 ``` markdown
238 [foo]: /url1
239 [foo]: /url2
240
241 [foo][]
242 ```
243
244 In the absence of a spec, early implementers consulted `Markdown.pl`
245 to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
246 gave manifestly bad results in many cases, so it was not a
247 satisfactory replacement for a spec.
248
249 Because there is no unambiguous spec, implementations have diverged
250 considerably. As a result, users are often surprised to find that
251 a document that renders one way on one system (say, a GitHub wiki)
252 renders differently on another (say, converting to docbook using
253 pandoc). To make matters worse, because nothing in Markdown counts
254 as a "syntax error," the divergence often isn't discovered right away.
255
256 ## About this document
257
258 This document attempts to specify Markdown syntax unambiguously.
259 It contains many examples with side-by-side Markdown and
260 HTML. These are intended to double as conformance tests. An
261 accompanying script `spec_tests.py` can be used to run the tests
262 against any Markdown program:
263
264 python test/spec_tests.py --spec spec.txt --program PROGRAM
265
266 Since this document describes how Markdown is to be parsed into
267 an abstract syntax tree, it would have made sense to use an abstract
268 representation of the syntax tree instead of HTML. But HTML is capable
269 of representing the structural distinctions we need to make, and the
270 choice of HTML for the tests makes it possible to run the tests against
271 an implementation without writing an abstract syntax tree renderer.
272
273 This document is generated from a text file, `spec.txt`, written
274 in Markdown with a small extension for the side-by-side tests.
275 The script `tools/makespec.py` can be used to convert `spec.txt` into
276 HTML or CommonMark (which can then be converted into other formats).
277
278 In the examples, the `→` character is used to represent tabs.
279
280 # Preliminaries
281
282 ## Characters and lines
283
284 Any sequence of [characters] is a valid CommonMark
285 document.
286
287 A [character](@) is a Unicode code point. Although some
288 code points (for example, combining accents) do not correspond to
289 characters in an intuitive sense, all code points count as characters
290 for purposes of this spec.
291
292 This spec does not specify an encoding; it thinks of lines as composed
293 of [characters] rather than bytes. A conforming parser may be limited
294 to a certain encoding.
295
296 A [line](@) is a sequence of zero or more [characters]
297 other than newline (`U+000A`) or carriage return (`U+000D`),
298 followed by a [line ending] or by the end of file.
299
300 A [line ending](@) is a newline (`U+000A`), a carriage return
301 (`U+000D`) not followed by a newline, or a carriage return and a
302 following newline.
303
304 A line containing no characters, or a line containing only spaces
305 (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
306
307 The following definitions of character classes will be used in this spec:
308
309 A [whitespace character](@) is a space
310 (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
311 form feed (`U+000C`), or carriage return (`U+000D`).
312
313 [Whitespace](@) is a sequence of one or more [whitespace
314 characters].
315
316 A [Unicode whitespace character](@) is
317 any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
318 carriage return (`U+000D`), newline (`U+000A`), or form feed
319 (`U+000C`).
320
321 [Unicode whitespace](@) is a sequence of one
322 or more [Unicode whitespace characters].
323
324 A [space](@) is `U+0020`.
325
326 A [non-whitespace character](@) is any character
327 that is not a [whitespace character].
328
329 An [ASCII punctuation character](@)
330 is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
331 `*`, `+`, `,`, `-`, `.`, `/` (U+0021–2F),
332 `:`, `;`, `<`, `=`, `>`, `?`, `@` (U+003A–0040),
333 `[`, `\`, `]`, `^`, `_`, `` ` `` (U+005B–0060),
334 `{`, `|`, `}`, or `~` (U+007B–007E).
335
336 A [punctuation character](@) is an [ASCII
337 punctuation character] or anything in
338 the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
339
340 ## Tabs
341
342 Tabs in lines are not expanded to [spaces]. However,
343 in contexts where whitespace helps to define block structure,
344 tabs behave as if they were replaced by spaces with a tab stop
345 of 4 characters.
346
347 Thus, for example, a tab can be used instead of four spaces
348 in an indented code block. (Note, however, that internal
349 tabs are passed through as literal tabs, not expanded to
350 spaces.)
351
352 ```````````````````````````````` example
353 →foo→baz→→bim
354 .
355 <pre><code>foo→baz→→bim
356 </code></pre>
357 ````````````````````````````````
358
359 ```````````````````````````````` example
360 →foo→baz→→bim
361 .
362 <pre><code>foo→baz→→bim
363 </code></pre>
364 ````````````````````````````````
365
366 ```````````````````````````````` example
367 a→a
368 ὐ→a
369 .
370 <pre><code>a→a
371 ὐ→a
372 </code></pre>
373 ````````````````````````````````
374
375 In the following example, a continuation paragraph of a list
376 item is indented with a tab; this has exactly the same effect
377 as indentation with four spaces would:
378
379 ```````````````````````````````` example
380 - foo
381
382 →bar
383 .
384 <ul>
385 <li>
386 <p>foo</p>
387 <p>bar</p>
388 </li>
389 </ul>
390 ````````````````````````````````
391
392 ```````````````````````````````` example
393 - foo
394
395 →→bar
396 .
397 <ul>
398 <li>
399 <p>foo</p>
400 <pre><code> bar
401 </code></pre>
402 </li>
403 </ul>
404 ````````````````````````````````
405
406 Normally the `>` that begins a block quote may be followed
407 optionally by a space, which is not considered part of the
408 content. In the following case `>` is followed by a tab,
409 which is treated as if it were expanded into three spaces.
410 Since one of these spaces is considered part of the
411 delimiter, `foo` is considered to be indented six spaces
412 inside the block quote context, so we get an indented
413 code block starting with two spaces.
414
415 ```````````````````````````````` example
416 >→→foo
417 .
418 <blockquote>
419 <pre><code> foo
420 </code></pre>
421 </blockquote>
422 ````````````````````````````````
423
424 ```````````````````````````````` example
425 -→→foo
426 .
427 <ul>
428 <li>
429 <pre><code> foo
430 </code></pre>
431 </li>
432 </ul>
433 ````````````````````````````````
434
435
436 ```````````````````````````````` example
437 foo
438 →bar
439 .
440 <pre><code>foo
441 bar
442 </code></pre>
443 ````````````````````````````````
444
445 ```````````````````````````````` example
446 - foo
447 - bar
448 → - baz
449 .
450 <ul>
451 <li>foo
452 <ul>
453 <li>bar
454 <ul>
455 <li>baz</li>
456 </ul>
457 </li>
458 </ul>
459 </li>
460 </ul>
461 ````````````````````````````````
462
463 ```````````````````````````````` example
464 #→Foo
465 .
466 <h1>Foo</h1>
467 ````````````````````````````````
468
469 ```````````````````````````````` example
470 *→*→*→
471 .
472 <hr />
473 ````````````````````````````````
474
475
476 ## Insecure characters
477
478 For security reasons, the Unicode character `U+0000` must be replaced
479 with the REPLACEMENT CHARACTER (`U+FFFD`).
480
481 # Blocks and inlines
482
483 We can think of a document as a sequence of
484 [blocks](@)---structural elements like paragraphs, block
485 quotations, lists, headings, rules, and code blocks. Some blocks (like
486 block quotes and list items) contain other blocks; others (like
487 headings and paragraphs) contain [inline](@) content---text,
488 links, emphasized text, images, code spans, and so on.
489
490 ## Precedence
491
492 Indicators of block structure always take precedence over indicators
493 of inline structure. So, for example, the following is a list with
494 two items, not a list with one item containing a code span:
495
496 ```````````````````````````````` example
497 - `one
498 - two`
499 .
500 <ul>
501 <li>`one</li>
502 <li>two`</li>
503 </ul>
504 ````````````````````````````````
505
506
507 This means that parsing can proceed in two steps: first, the block
508 structure of the document can be discerned; second, text lines inside
509 paragraphs, headings, and other block constructs can be parsed for inline
510 structure. The second step requires information about link reference
511 definitions that will be available only at the end of the first
512 step. Note that the first step requires processing lines in sequence,
513 but the second can be parallelized, since the inline parsing of
514 one block element does not affect the inline parsing of any other.
515
516 ## Container blocks and leaf blocks
517
518 We can divide blocks into two types:
519 [container blocks](@),
520 which can contain other blocks, and [leaf blocks](@),
521 which cannot.
522
523 # Leaf blocks
524
525 This section describes the different kinds of leaf block that make up a
526 Markdown document.
527
528 ## Thematic breaks
529
530 A line consisting of 0-3 spaces of indentation, followed by a sequence
531 of three or more matching `-`, `_`, or `*` characters, each followed
532 optionally by any number of spaces or tabs, forms a
533 [thematic break](@).
534
535 ```````````````````````````````` example
536 ***
537 ---
538 ___
539 .
540 <hr />
541 <hr />
542 <hr />
543 ````````````````````````````````
544
545
546 Wrong characters:
547
548 ```````````````````````````````` example
549 +++
550 .
551 <p>+++</p>
552 ````````````````````````````````
553
554
555 ```````````````````````````````` example
556 ===
557 .
558 <p>===</p>
559 ````````````````````````````````
560
561
562 Not enough characters:
563
564 ```````````````````````````````` example
565 --
566 **
567 __
568 .
569 <p>--
570 **
571 __</p>
572 ````````````````````````````````
573
574
575 One to three spaces indent are allowed:
576
577 ```````````````````````````````` example
578 ***
579 ***
580 ***
581 .
582 <hr />
583 <hr />
584 <hr />
585 ````````````````````````````````
586
587
588 Four spaces is too many:
589
590 ```````````````````````````````` example
591 ***
592 .
593 <pre><code>***
594 </code></pre>
595 ````````````````````````````````
596
597
598 ```````````````````````````````` example
599 Foo
600 ***
601 .
602 <p>Foo
603 ***</p>
604 ````````````````````````````````
605
606
607 More than three characters may be used:
608
609 ```````````````````````````````` example
610 _____________________________________
611 .
612 <hr />
613 ````````````````````````````````
614
615
616 Spaces are allowed between the characters:
617
618 ```````````````````````````````` example
619 - - -
620 .
621 <hr />
622 ````````````````````````````````
623
624
625 ```````````````````````````````` example
626 ** * ** * ** * **
627 .
628 <hr />
629 ````````````````````````````````
630
631
632 ```````````````````````````````` example
633 - - - -
634 .
635 <hr />
636 ````````````````````````````````
637
638
639 Spaces are allowed at the end:
640
641 ```````````````````````````````` example
642 - - - -
643 .
644 <hr />
645 ````````````````````````````````
646
647
648 However, no other characters may occur in the line:
649
650 ```````````````````````````````` example
651 _ _ _ _ a
652
653 a------
654
655 ---a---
656 .
657 <p>_ _ _ _ a</p>
658 <p>a------</p>
659 <p>---a---</p>
660 ````````````````````````````````
661
662
663 It is required that all of the [non-whitespace characters] be the same.
664 So, this is not a thematic break:
665
666 ```````````````````````````````` example
667 *-*
668 .
669 <p><em>-</em></p>
670 ````````````````````````````````
671
672
673 Thematic breaks do not need blank lines before or after:
674
675 ```````````````````````````````` example
676 - foo
677 ***
678 - bar
679 .
680 <ul>
681 <li>foo</li>
682 </ul>
683 <hr />
684 <ul>
685 <li>bar</li>
686 </ul>
687 ````````````````````````````````
688
689
690 Thematic breaks can interrupt a paragraph:
691
692 ```````````````````````````````` example
693 Foo
694 ***
695 bar
696 .
697 <p>Foo</p>
698 <hr />
699 <p>bar</p>
700 ````````````````````````````````
701
702
703 If a line of dashes that meets the above conditions for being a
704 thematic break could also be interpreted as the underline of a [setext
705 heading], the interpretation as a
706 [setext heading] takes precedence. Thus, for example,
707 this is a setext heading, not a paragraph followed by a thematic break:
708
709 ```````````````````````````````` example
710 Foo
711 ---
712 bar
713 .
714 <h2>Foo</h2>
715 <p>bar</p>
716 ````````````````````````````````
717
718
719 When both a thematic break and a list item are possible
720 interpretations of a line, the thematic break takes precedence:
721
722 ```````````````````````````````` example
723 * Foo
724 * * *
725 * Bar
726 .
727 <ul>
728 <li>Foo</li>
729 </ul>
730 <hr />
731 <ul>
732 <li>Bar</li>
733 </ul>
734 ````````````````````````````````
735
736
737 If you want a thematic break in a list item, use a different bullet:
738
739 ```````````````````````````````` example
740 - Foo
741 - * * *
742 .
743 <ul>
744 <li>Foo</li>
745 <li>
746 <hr />
747 </li>
748 </ul>
749 ````````````````````````````````
750
751
752 ## ATX headings
753
754 An [ATX heading](@)
755 consists of a string of characters, parsed as inline content, between an
756 opening sequence of 1--6 unescaped `#` characters and an optional
757 closing sequence of any number of unescaped `#` characters.
758 The opening sequence of `#` characters must be followed by a
759 [space] or by the end of line. The optional closing sequence of `#`s must be
760 preceded by a [space] and may be followed by spaces only. The opening
761 `#` character may be indented 0-3 spaces. The raw contents of the
762 heading are stripped of leading and trailing spaces before being parsed
763 as inline content. The heading level is equal to the number of `#`
764 characters in the opening sequence.
765
766 Simple headings:
767
768 ```````````````````````````````` example
769 # foo
770 ## foo
771 ### foo
772 #### foo
773 ##### foo
774 ###### foo
775 .
776 <h1>foo</h1>
777 <h2>foo</h2>
778 <h3>foo</h3>
779 <h4>foo</h4>
780 <h5>foo</h5>
781 <h6>foo</h6>
782 ````````````````````````````````
783
784
785 More than six `#` characters is not a heading:
786
787 ```````````````````````````````` example
788 ####### foo
789 .
790 <p>####### foo</p>
791 ````````````````````````````````
792
793
794 At least one space is required between the `#` characters and the
795 heading's contents, unless the heading is empty. Note that many
796 implementations currently do not require the space. However, the
797 space was required by the
798 [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
799 and it helps prevent things like the following from being parsed as
800 headings:
801
802 ```````````````````````````````` example
803 #5 bolt
804
805 #hashtag
806 .
807 <p>#5 bolt</p>
808 <p>#hashtag</p>
809 ````````````````````````````````
810
811
812 This is not a heading, because the first `#` is escaped:
813
814 ```````````````````````````````` example
815 \## foo
816 .
817 <p>## foo</p>
818 ````````````````````````````````
819
820
821 Contents are parsed as inlines:
822
823 ```````````````````````````````` example
824 # foo *bar* \*baz\*
825 .
826 <h1>foo <em>bar</em> *baz*</h1>
827 ````````````````````````````````
828
829
830 Leading and trailing [whitespace] is ignored in parsing inline content:
831
832 ```````````````````````````````` example
833 # foo
834 .
835 <h1>foo</h1>
836 ````````````````````````````````
837
838
839 One to three spaces indentation are allowed:
840
841 ```````````````````````````````` example
842 ### foo
843 ## foo
844 # foo
845 .
846 <h3>foo</h3>
847 <h2>foo</h2>
848 <h1>foo</h1>
849 ````````````````````````````````
850
851
852 Four spaces are too much:
853
854 ```````````````````````````````` example
855 # foo
856 .
857 <pre><code># foo
858 </code></pre>
859 ````````````````````````````````
860
861
862 ```````````````````````````````` example
863 foo
864 # bar
865 .
866 <p>foo
867 # bar</p>
868 ````````````````````````````````
869
870
871 A closing sequence of `#` characters is optional:
872
873 ```````````````````````````````` example
874 ## foo ##
875 ### bar ###
876 .
877 <h2>foo</h2>
878 <h3>bar</h3>
879 ````````````````````````````````
880
881
882 It need not be the same length as the opening sequence:
883
884 ```````````````````````````````` example
885 # foo ##################################
886 ##### foo ##
887 .
888 <h1>foo</h1>
889 <h5>foo</h5>
890 ````````````````````````````````
891
892
893 Spaces are allowed after the closing sequence:
894
895 ```````````````````````````````` example
896 ### foo ###
897 .
898 <h3>foo</h3>
899 ````````````````````````````````
900
901
902 A sequence of `#` characters with anything but [spaces] following it
903 is not a closing sequence, but counts as part of the contents of the
904 heading:
905
906 ```````````````````````````````` example
907 ### foo ### b
908 .
909 <h3>foo ### b</h3>
910 ````````````````````````````````
911
912
913 The closing sequence must be preceded by a space:
914
915 ```````````````````````````````` example
916 # foo#
917 .
918 <h1>foo#</h1>
919 ````````````````````````````````
920
921
922 Backslash-escaped `#` characters do not count as part
923 of the closing sequence:
924
925 ```````````````````````````````` example
926 ### foo \###
927 ## foo #\##
928 # foo \#
929 .
930 <h3>foo ###</h3>
931 <h2>foo ###</h2>
932 <h1>foo #</h1>
933 ````````````````````````````````
934
935
936 ATX headings need not be separated from surrounding content by blank
937 lines, and they can interrupt paragraphs:
938
939 ```````````````````````````````` example
940 ****
941 ## foo
942 ****
943 .
944 <hr />
945 <h2>foo</h2>
946 <hr />
947 ````````````````````````````````
948
949
950 ```````````````````````````````` example
951 Foo bar
952 # baz
953 Bar foo
954 .
955 <p>Foo bar</p>
956 <h1>baz</h1>
957 <p>Bar foo</p>
958 ````````````````````````````````
959
960
961 ATX headings can be empty:
962
963 ```````````````````````````````` example
964 ##
965 #
966 ### ###
967 .
968 <h2></h2>
969 <h1></h1>
970 <h3></h3>
971 ````````````````````````````````
972
973
974 ## Setext headings
975
976 A [setext heading](@) consists of one or more
977 lines of text, each containing at least one [non-whitespace
978 character], with no more than 3 spaces indentation, followed by
979 a [setext heading underline]. The lines of text must be such
980 that, were they not followed by the setext heading underline,
981 they would be interpreted as a paragraph: they cannot be
982 interpretable as a [code fence], [ATX heading][ATX headings],
983 [block quote][block quotes], [thematic break][thematic breaks],
984 [list item][list items], or [HTML block][HTML blocks].
985
986 A [setext heading underline](@) is a sequence of
987 `=` characters or a sequence of `-` characters, with no more than 3
988 spaces indentation and any number of trailing spaces. If a line
989 containing a single `-` can be interpreted as an
990 empty [list items], it should be interpreted this way
991 and not as a [setext heading underline].
992
993 The heading is a level 1 heading if `=` characters are used in
994 the [setext heading underline], and a level 2 heading if `-`
995 characters are used. The contents of the heading are the result
996 of parsing the preceding lines of text as CommonMark inline
997 content.
998
999 In general, a setext heading need not be preceded or followed by a
1000 blank line. However, it cannot interrupt a paragraph, so when a
1001 setext heading comes after a paragraph, a blank line is needed between
1002 them.
1003
1004 Simple examples:
1005
1006 ```````````````````````````````` example
1007 Foo *bar*
1008 =========
1009
1010 Foo *bar*
1011 ---------
1012 .
1013 <h1>Foo <em>bar</em></h1>
1014 <h2>Foo <em>bar</em></h2>
1015 ````````````````````````````````
1016
1017
1018 The content of the header may span more than one line:
1019
1020 ```````````````````````````````` example
1021 Foo *bar
1022 baz*
1023 ====
1024 .
1025 <h1>Foo <em>bar
1026 baz</em></h1>
1027 ````````````````````````````````
1028
1029 The contents are the result of parsing the headings's raw
1030 content as inlines. The heading's raw content is formed by
1031 concatenating the lines and removing initial and final
1032 [whitespace].
1033
1034 ```````````````````````````````` example
1035 Foo *bar
1036 baz*→
1037 ====
1038 .
1039 <h1>Foo <em>bar
1040 baz</em></h1>
1041 ````````````````````````````````
1042
1043
1044 The underlining can be any length:
1045
1046 ```````````````````````````````` example
1047 Foo
1048 -------------------------
1049
1050 Foo
1051 =
1052 .
1053 <h2>Foo</h2>
1054 <h1>Foo</h1>
1055 ````````````````````````````````
1056
1057
1058 The heading content can be indented up to three spaces, and need
1059 not line up with the underlining:
1060
1061 ```````````````````````````````` example
1062 Foo
1063 ---
1064
1065 Foo
1066 -----
1067
1068 Foo
1069 ===
1070 .
1071 <h2>Foo</h2>
1072 <h2>Foo</h2>
1073 <h1>Foo</h1>
1074 ````````````````````````````````
1075
1076
1077 Four spaces indent is too much:
1078
1079 ```````````````````````````````` example
1080 Foo
1081 ---
1082
1083 Foo
1084 ---
1085 .
1086 <pre><code>Foo
1087 ---
1088
1089 Foo
1090 </code></pre>
1091 <hr />
1092 ````````````````````````````````
1093
1094
1095 The setext heading underline can be indented up to three spaces, and
1096 may have trailing spaces:
1097
1098 ```````````````````````````````` example
1099 Foo
1100 ----
1101 .
1102 <h2>Foo</h2>
1103 ````````````````````````````````
1104
1105
1106 Four spaces is too much:
1107
1108 ```````````````````````````````` example
1109 Foo
1110 ---
1111 .
1112 <p>Foo
1113 ---</p>
1114 ````````````````````````````````
1115
1116
1117 The setext heading underline cannot contain internal spaces:
1118
1119 ```````````````````````````````` example
1120 Foo
1121 = =
1122
1123 Foo
1124 --- -
1125 .
1126 <p>Foo
1127 = =</p>
1128 <p>Foo</p>
1129 <hr />
1130 ````````````````````````````````
1131
1132
1133 Trailing spaces in the content line do not cause a line break:
1134
1135 ```````````````````````````````` example
1136 Foo
1137 -----
1138 .
1139 <h2>Foo</h2>
1140 ````````````````````````````````
1141
1142
1143 Nor does a backslash at the end:
1144
1145 ```````````````````````````````` example
1146 Foo\
1147 ----
1148 .
1149 <h2>Foo\</h2>
1150 ````````````````````````````````
1151
1152
1153 Since indicators of block structure take precedence over
1154 indicators of inline structure, the following are setext headings:
1155
1156 ```````````````````````````````` example
1157 `Foo
1158 ----
1159 `
1160
1161 <a title="a lot
1162 ---
1163 of dashes"/>
1164 .
1165 <h2>`Foo</h2>
1166 <p>`</p>
1167 <h2>&lt;a title=&quot;a lot</h2>
1168 <p>of dashes&quot;/&gt;</p>
1169 ````````````````````````````````
1170
1171
1172 The setext heading underline cannot be a [lazy continuation
1173 line] in a list item or block quote:
1174
1175 ```````````````````````````````` example
1176 > Foo
1177 ---
1178 .
1179 <blockquote>
1180 <p>Foo</p>
1181 </blockquote>
1182 <hr />
1183 ````````````````````````````````
1184
1185
1186 ```````````````````````````````` example
1187 > foo
1188 bar
1189 ===
1190 .
1191 <blockquote>
1192 <p>foo
1193 bar
1194 ===</p>
1195 </blockquote>
1196 ````````````````````````````````
1197
1198
1199 ```````````````````````````````` example
1200 - Foo
1201 ---
1202 .
1203 <ul>
1204 <li>Foo</li>
1205 </ul>
1206 <hr />
1207 ````````````````````````````````
1208
1209
1210 A blank line is needed between a paragraph and a following
1211 setext heading, since otherwise the paragraph becomes part
1212 of the heading's content:
1213
1214 ```````````````````````````````` example
1215 Foo
1216 Bar
1217 ---
1218 .
1219 <h2>Foo
1220 Bar</h2>
1221 ````````````````````````````````
1222
1223
1224 But in general a blank line is not required before or after
1225 setext headings:
1226
1227 ```````````````````````````````` example
1228 ---
1229 Foo
1230 ---
1231 Bar
1232 ---
1233 Baz
1234 .
1235 <hr />
1236 <h2>Foo</h2>
1237 <h2>Bar</h2>
1238 <p>Baz</p>
1239 ````````````````````````````````
1240
1241
1242 Setext headings cannot be empty:
1243
1244 ```````````````````````````````` example
1245
1246 ====
1247 .
1248 <p>====</p>
1249 ````````````````````````````````
1250
1251
1252 Setext heading text lines must not be interpretable as block
1253 constructs other than paragraphs. So, the line of dashes
1254 in these examples gets interpreted as a thematic break:
1255
1256 ```````````````````````````````` example
1257 ---
1258 ---
1259 .
1260 <hr />
1261 <hr />
1262 ````````````````````````````````
1263
1264
1265 ```````````````````````````````` example
1266 - foo
1267 -----
1268 .
1269 <ul>
1270 <li>foo</li>
1271 </ul>
1272 <hr />
1273 ````````````````````````````````
1274
1275
1276 ```````````````````````````````` example
1277 foo
1278 ---
1279 .
1280 <pre><code>foo
1281 </code></pre>
1282 <hr />
1283 ````````````````````````````````
1284
1285
1286 ```````````````````````````````` example
1287 > foo
1288 -----
1289 .
1290 <blockquote>
1291 <p>foo</p>
1292 </blockquote>
1293 <hr />
1294 ````````````````````````````````
1295
1296
1297 If you want a heading with `> foo` as its literal text, you can
1298 use backslash escapes:
1299
1300 ```````````````````````````````` example
1301 \> foo
1302 ------
1303 .
1304 <h2>&gt; foo</h2>
1305 ````````````````````````````````
1306
1307
1308 **Compatibility note:** Most existing Markdown implementations
1309 do not allow the text of setext headings to span multiple lines.
1310 But there is no consensus about how to interpret
1311
1312 ``` markdown
1313 Foo
1314 bar
1315 ---
1316 baz
1317 ```
1318
1319 One can find four different interpretations:
1320
1321 1. paragraph "Foo", heading "bar", paragraph "baz"
1322 2. paragraph "Foo bar", thematic break, paragraph "baz"
1323 3. paragraph "Foo bar --- baz"
1324 4. heading "Foo bar", paragraph "baz"
1325
1326 We find interpretation 4 most natural, and interpretation 4
1327 increases the expressive power of CommonMark, by allowing
1328 multiline headings. Authors who want interpretation 1 can
1329 put a blank line after the first paragraph:
1330
1331 ```````````````````````````````` example
1332 Foo
1333
1334 bar
1335 ---
1336 baz
1337 .
1338 <p>Foo</p>
1339 <h2>bar</h2>
1340 <p>baz</p>
1341 ````````````````````````````````
1342
1343
1344 Authors who want interpretation 2 can put blank lines around
1345 the thematic break,
1346
1347 ```````````````````````````````` example
1348 Foo
1349 bar
1350
1351 ---
1352
1353 baz
1354 .
1355 <p>Foo
1356 bar</p>
1357 <hr />
1358 <p>baz</p>
1359 ````````````````````````````````
1360
1361
1362 or use a thematic break that cannot count as a [setext heading
1363 underline], such as
1364
1365 ```````````````````````````````` example
1366 Foo
1367 bar
1368 * * *
1369 baz
1370 .
1371 <p>Foo
1372 bar</p>
1373 <hr />
1374 <p>baz</p>
1375 ````````````````````````````````
1376
1377
1378 Authors who want interpretation 3 can use backslash escapes:
1379
1380 ```````````````````````````````` example
1381 Foo
1382 bar
1383 \---
1384 baz
1385 .
1386 <p>Foo
1387 bar
1388 ---
1389 baz</p>
1390 ````````````````````````````````
1391
1392
1393 ## Indented code blocks
1394
1395 An [indented code block](@) is composed of one or more
1396 [indented chunks] separated by blank lines.
1397 An [indented chunk](@) is a sequence of non-blank lines,
1398 each indented four or more spaces. The contents of the code block are
1399 the literal contents of the lines, including trailing
1400 [line endings], minus four spaces of indentation.
1401 An indented code block has no [info string].
1402
1403 An indented code block cannot interrupt a paragraph, so there must be
1404 a blank line between a paragraph and a following indented code block.
1405 (A blank line is not needed, however, between a code block and a following
1406 paragraph.)
1407
1408 ```````````````````````````````` example
1409 a simple
1410 indented code block
1411 .
1412 <pre><code>a simple
1413 indented code block
1414 </code></pre>
1415 ````````````````````````````````
1416
1417
1418 If there is any ambiguity between an interpretation of indentation
1419 as a code block and as indicating that material belongs to a [list
1420 item][list items], the list item interpretation takes precedence:
1421
1422 ```````````````````````````````` example
1423 - foo
1424
1425 bar
1426 .
1427 <ul>
1428 <li>
1429 <p>foo</p>
1430 <p>bar</p>
1431 </li>
1432 </ul>
1433 ````````````````````````````````
1434
1435
1436 ```````````````````````````````` example
1437 1. foo
1438
1439 - bar
1440 .
1441 <ol>
1442 <li>
1443 <p>foo</p>
1444 <ul>
1445 <li>bar</li>
1446 </ul>
1447 </li>
1448 </ol>
1449 ````````````````````````````````
1450
1451
1452
1453 The contents of a code block are literal text, and do not get parsed
1454 as Markdown:
1455
1456 ```````````````````````````````` example
1457 <a/>
1458 *hi*
1459
1460 - one
1461 .
1462 <pre><code>&lt;a/&gt;
1463 *hi*
1464
1465 - one
1466 </code></pre>
1467 ````````````````````````````````
1468
1469
1470 Here we have three chunks separated by blank lines:
1471
1472 ```````````````````````````````` example
1473 chunk1
1474
1475 chunk2
1476
1477
1478
1479 chunk3
1480 .
1481 <pre><code>chunk1
1482
1483 chunk2
1484
1485
1486
1487 chunk3
1488 </code></pre>
1489 ````````````````````````````````
1490
1491
1492 Any initial spaces beyond four will be included in the content, even
1493 in interior blank lines:
1494
1495 ```````````````````````````````` example
1496 chunk1
1497
1498 chunk2
1499 .
1500 <pre><code>chunk1
1501
1502 chunk2
1503 </code></pre>
1504 ````````````````````````````````
1505
1506
1507 An indented code block cannot interrupt a paragraph. (This
1508 allows hanging indents and the like.)
1509
1510 ```````````````````````````````` example
1511 Foo
1512 bar
1513
1514 .
1515 <p>Foo
1516 bar</p>
1517 ````````````````````````````````
1518
1519
1520 However, any non-blank line with fewer than four leading spaces ends
1521 the code block immediately. So a paragraph may occur immediately
1522 after indented code:
1523
1524 ```````````````````````````````` example
1525 foo
1526 bar
1527 .
1528 <pre><code>foo
1529 </code></pre>
1530 <p>bar</p>
1531 ````````````````````````````````
1532
1533
1534 And indented code can occur immediately before and after other kinds of
1535 blocks:
1536
1537 ```````````````````````````````` example
1538 # Heading
1539 foo
1540 Heading
1541 ------
1542 foo
1543 ----
1544 .
1545 <h1>Heading</h1>
1546 <pre><code>foo
1547 </code></pre>
1548 <h2>Heading</h2>
1549 <pre><code>foo
1550 </code></pre>
1551 <hr />
1552 ````````````````````````````````
1553
1554
1555 The first line can be indented more than four spaces:
1556
1557 ```````````````````````````````` example
1558 foo
1559 bar
1560 .
1561 <pre><code> foo
1562 bar
1563 </code></pre>
1564 ````````````````````````````````
1565
1566
1567 Blank lines preceding or following an indented code block
1568 are not included in it:
1569
1570 ```````````````````````````````` example
1571
1572
1573 foo
1574
1575
1576 .
1577 <pre><code>foo
1578 </code></pre>
1579 ````````````````````````````````
1580
1581
1582 Trailing spaces are included in the code block's content:
1583
1584 ```````````````````````````````` example
1585 foo
1586 .
1587 <pre><code>foo
1588 </code></pre>
1589 ````````````````````````````````
1590
1591
1592
1593 ## Fenced code blocks
1594
1595 A [code fence](@) is a sequence
1596 of at least three consecutive backtick characters (`` ` ``) or
1597 tildes (`~`). (Tildes and backticks cannot be mixed.)
1598 A [fenced code block](@)
1599 begins with a code fence, indented no more than three spaces.
1600
1601 The line with the opening code fence may optionally contain some text
1602 following the code fence; this is trimmed of leading and trailing
1603 whitespace and called the [info string](@). If the [info string] comes
1604 after a backtick fence, it may not contain any backtick
1605 characters. (The reason for this restriction is that otherwise
1606 some inline code would be incorrectly interpreted as the
1607 beginning of a fenced code block.)
1608
1609 The content of the code block consists of all subsequent lines, until
1610 a closing [code fence] of the same type as the code block
1611 began with (backticks or tildes), and with at least as many backticks
1612 or tildes as the opening code fence. If the leading code fence is
1613 indented N spaces, then up to N spaces of indentation are removed from
1614 each line of the content (if present). (If a content line is not
1615 indented, it is preserved unchanged. If it is indented less than N
1616 spaces, all of the indentation is removed.)
1617
1618 The closing code fence may be indented up to three spaces, and may be
1619 followed only by spaces, which are ignored. If the end of the
1620 containing block (or document) is reached and no closing code fence
1621 has been found, the code block contains all of the lines after the
1622 opening code fence until the end of the containing block (or
1623 document). (An alternative spec would require backtracking in the
1624 event that a closing code fence is not found. But this makes parsing
1625 much less efficient, and there seems to be no real down side to the
1626 behavior described here.)
1627
1628 A fenced code block may interrupt a paragraph, and does not require
1629 a blank line either before or after.
1630
1631 The content of a code fence is treated as literal text, not parsed
1632 as inlines. The first word of the [info string] is typically used to
1633 specify the language of the code sample, and rendered in the `class`
1634 attribute of the `code` tag. However, this spec does not mandate any
1635 particular treatment of the [info string].
1636
1637 Here is a simple example with backticks:
1638
1639 ```````````````````````````````` example
1640 ```
1641 <
1642 >
1643 ```
1644 .
1645 <pre><code>&lt;
1646 &gt;
1647 </code></pre>
1648 ````````````````````````````````
1649
1650
1651 With tildes:
1652
1653 ```````````````````````````````` example
1654 ~~~
1655 <
1656 >
1657 ~~~
1658 .
1659 <pre><code>&lt;
1660 &gt;
1661 </code></pre>
1662 ````````````````````````````````
1663
1664 Fewer than three backticks is not enough:
1665
1666 ```````````````````````````````` example
1667 ``
1668 foo
1669 ``
1670 .
1671 <p><code>foo</code></p>
1672 ````````````````````````````````
1673
1674 The closing code fence must use the same character as the opening
1675 fence:
1676
1677 ```````````````````````````````` example
1678 ```
1679 aaa
1680 ~~~
1681 ```
1682 .
1683 <pre><code>aaa
1684 ~~~
1685 </code></pre>
1686 ````````````````````````````````
1687
1688
1689 ```````````````````````````````` example
1690 ~~~
1691 aaa
1692 ```
1693 ~~~
1694 .
1695 <pre><code>aaa
1696 ```
1697 </code></pre>
1698 ````````````````````````````````
1699
1700
1701 The closing code fence must be at least as long as the opening fence:
1702
1703 ```````````````````````````````` example
1704 ````
1705 aaa
1706 ```
1707 ``````
1708 .
1709 <pre><code>aaa
1710 ```
1711 </code></pre>
1712 ````````````````````````````````
1713
1714
1715 ```````````````````````````````` example
1716 ~~~~
1717 aaa
1718 ~~~
1719 ~~~~
1720 .
1721 <pre><code>aaa
1722 ~~~
1723 </code></pre>
1724 ````````````````````````````````
1725
1726
1727 Unclosed code blocks are closed by the end of the document
1728 (or the enclosing [block quote][block quotes] or [list item][list items]):
1729
1730 ```````````````````````````````` example
1731 ```
1732 .
1733 <pre><code></code></pre>
1734 ````````````````````````````````
1735
1736
1737 ```````````````````````````````` example
1738 `````
1739
1740 ```
1741 aaa
1742 .
1743 <pre><code>
1744 ```
1745 aaa
1746 </code></pre>
1747 ````````````````````````````````
1748
1749
1750 ```````````````````````````````` example
1751 > ```
1752 > aaa
1753
1754 bbb
1755 .
1756 <blockquote>
1757 <pre><code>aaa
1758 </code></pre>
1759 </blockquote>
1760 <p>bbb</p>
1761 ````````````````````````````````
1762
1763
1764 A code block can have all empty lines as its content:
1765
1766 ```````````````````````````````` example
1767 ```
1768
1769
1770 ```
1771 .
1772 <pre><code>
1773
1774 </code></pre>
1775 ````````````````````````````````
1776
1777
1778 A code block can be empty:
1779
1780 ```````````````````````````````` example
1781 ```
1782 ```
1783 .
1784 <pre><code></code></pre>
1785 ````````````````````````````````
1786
1787
1788 Fences can be indented. If the opening fence is indented,
1789 content lines will have equivalent opening indentation removed,
1790 if present:
1791
1792 ```````````````````````````````` example
1793 ```
1794 aaa
1795 aaa
1796 ```
1797 .
1798 <pre><code>aaa
1799 aaa
1800 </code></pre>
1801 ````````````````````````````````
1802
1803
1804 ```````````````````````````````` example
1805 ```
1806 aaa
1807 aaa
1808 aaa
1809 ```
1810 .
1811 <pre><code>aaa
1812 aaa
1813 aaa
1814 </code></pre>
1815 ````````````````````````````````
1816
1817
1818 ```````````````````````````````` example
1819 ```
1820 aaa
1821 aaa
1822 aaa
1823 ```
1824 .
1825 <pre><code>aaa
1826 aaa
1827 aaa
1828 </code></pre>
1829 ````````````````````````````````
1830
1831
1832 Four spaces indentation produces an indented code block:
1833
1834 ```````````````````````````````` example
1835 ```
1836 aaa
1837 ```
1838 .
1839 <pre><code>```
1840 aaa
1841 ```
1842 </code></pre>
1843 ````````````````````````````````
1844
1845
1846 Closing fences may be indented by 0-3 spaces, and their indentation
1847 need not match that of the opening fence:
1848
1849 ```````````````````````````````` example
1850 ```
1851 aaa
1852 ```
1853 .
1854 <pre><code>aaa
1855 </code></pre>
1856 ````````````````````````````````
1857
1858
1859 ```````````````````````````````` example
1860 ```
1861 aaa
1862 ```
1863 .
1864 <pre><code>aaa
1865 </code></pre>
1866 ````````````````````````````````
1867
1868
1869 This is not a closing fence, because it is indented 4 spaces:
1870
1871 ```````````````````````````````` example
1872 ```
1873 aaa
1874 ```
1875 .
1876 <pre><code>aaa
1877 ```
1878 </code></pre>
1879 ````````````````````````````````
1880
1881
1882
1883 Code fences (opening and closing) cannot contain internal spaces:
1884
1885 ```````````````````````````````` example
1886 ``` ```
1887 aaa
1888 .
1889 <p><code> </code>
1890 aaa</p>
1891 ````````````````````````````````
1892
1893
1894 ```````````````````````````````` example
1895 ~~~~~~
1896 aaa
1897 ~~~ ~~
1898 .
1899 <pre><code>aaa
1900 ~~~ ~~
1901 </code></pre>
1902 ````````````````````````````````
1903
1904
1905 Fenced code blocks can interrupt paragraphs, and can be followed
1906 directly by paragraphs, without a blank line between:
1907
1908 ```````````````````````````````` example
1909 foo
1910 ```
1911 bar
1912 ```
1913 baz
1914 .
1915 <p>foo</p>
1916 <pre><code>bar
1917 </code></pre>
1918 <p>baz</p>
1919 ````````````````````````````````
1920
1921
1922 Other blocks can also occur before and after fenced code blocks
1923 without an intervening blank line:
1924
1925 ```````````````````````````````` example
1926 foo
1927 ---
1928 ~~~
1929 bar
1930 ~~~
1931 # baz
1932 .
1933 <h2>foo</h2>
1934 <pre><code>bar
1935 </code></pre>
1936 <h1>baz</h1>
1937 ````````````````````````````````
1938
1939
1940 An [info string] can be provided after the opening code fence.
1941 Although this spec doesn't mandate any particular treatment of
1942 the info string, the first word is typically used to specify
1943 the language of the code block. In HTML output, the language is
1944 normally indicated by adding a class to the `code` element consisting
1945 of `language-` followed by the language name.
1946
1947 ```````````````````````````````` example
1948 ```ruby
1949 def foo(x)
1950 return 3
1951 end
1952 ```
1953 .
1954 <pre><code class="language-ruby">def foo(x)
1955 return 3
1956 end
1957 </code></pre>
1958 ````````````````````````````````
1959
1960
1961 ```````````````````````````````` example
1962 ~~~~ ruby startline=3 $%@#$
1963 def foo(x)
1964 return 3
1965 end
1966 ~~~~~~~
1967 .
1968 <pre><code class="language-ruby">def foo(x)
1969 return 3
1970 end
1971 </code></pre>
1972 ````````````````````````````````
1973
1974
1975 ```````````````````````````````` example
1976 ````;
1977 ````
1978 .
1979 <pre><code class="language-;"></code></pre>
1980 ````````````````````````````````
1981
1982
1983 [Info strings] for backtick code blocks cannot contain backticks:
1984
1985 ```````````````````````````````` example
1986 ``` aa ```
1987 foo
1988 .
1989 <p><code>aa</code>
1990 foo</p>
1991 ````````````````````````````````
1992
1993
1994 [Info strings] for tilde code blocks can contain backticks and tildes:
1995
1996 ```````````````````````````````` example
1997 ~~~ aa ``` ~~~
1998 foo
1999 ~~~
2000 .
2001 <pre><code class="language-aa">foo
2002 </code></pre>
2003 ````````````````````````````````
2004
2005
2006 Closing code fences cannot have [info strings]:
2007
2008 ```````````````````````````````` example
2009 ```
2010 ``` aaa
2011 ```
2012 .
2013 <pre><code>``` aaa
2014 </code></pre>
2015 ````````````````````````````````
2016
2017
2018
2019 ## HTML blocks
2020
2021 An [HTML block](@) is a group of lines that is treated
2022 as raw HTML (and will not be escaped in HTML output).
2023
2024 There are seven kinds of [HTML block], which can be defined by their
2025 start and end conditions. The block begins with a line that meets a
2026 [start condition](@) (after up to three spaces optional indentation).
2027 It ends with the first subsequent line that meets a matching [end
2028 condition](@), or the last line of the document, or the last line of
2029 the [container block](#container-blocks) containing the current HTML
2030 block, if no line is encountered that meets the [end condition]. If
2031 the first line meets both the [start condition] and the [end
2032 condition], the block will contain just that line.
2033
2034 1. **Start condition:** line begins with the string `<script`,
2035 `<pre`, or `<style` (case-insensitive), followed by whitespace,
2036 the string `>`, or the end of the line.\
2037 **End condition:** line contains an end tag
2038 `</script>`, `</pre>`, or `</style>` (case-insensitive; it
2039 need not match the start tag).
2040
2041 2. **Start condition:** line begins with the string `<!--`.\
2042 **End condition:** line contains the string `-->`.
2043
2044 3. **Start condition:** line begins with the string `<?`.\
2045 **End condition:** line contains the string `?>`.
2046
2047 4. **Start condition:** line begins with the string `<!`
2048 followed by an uppercase ASCII letter.\
2049 **End condition:** line contains the character `>`.
2050
2051 5. **Start condition:** line begins with the string
2052 `<![CDATA[`.\
2053 **End condition:** line contains the string `]]>`.
2054
2055 6. **Start condition:** line begins the string `<` or `</`
2056 followed by one of the strings (case-insensitive) `address`,
2057 `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
2058 `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
2059 `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
2060 `footer`, `form`, `frame`, `frameset`,
2061 `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
2062 `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
2063 `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
2064 `section`, `source`, `summary`, `table`, `tbody`, `td`,
2065 `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
2066 by [whitespace], the end of the line, the string `>`, or
2067 the string `/>`.\
2068 **End condition:** line is followed by a [blank line].
2069
2070 7. **Start condition:** line begins with a complete [open tag]
2071 (with any [tag name] other than `script`,
2072 `style`, or `pre`) or a complete [closing tag],
2073 followed only by [whitespace] or the end of the line.\
2074 **End condition:** line is followed by a [blank line].
2075
2076 HTML blocks continue until they are closed by their appropriate
2077 [end condition], or the last line of the document or other [container
2078 block](#container-blocks). This means any HTML **within an HTML
2079 block** that might otherwise be recognised as a start condition will
2080 be ignored by the parser and passed through as-is, without changing
2081 the parser's state.
2082
2083 For instance, `<pre>` within a HTML block started by `<table>` will not affect
2084 the parser state; as the HTML block was started in by start condition 6, it
2085 will end at any blank line. This can be surprising:
2086
2087 ```````````````````````````````` example
2088 <table><tr><td>
2089 <pre>
2090 **Hello**,
2091
2092 _world_.
2093 </pre>
2094 </td></tr></table>
2095 .
2096 <table><tr><td>
2097 <pre>
2098 **Hello**,
2099 <p><em>world</em>.
2100 </pre></p>
2101 </td></tr></table>
2102 ````````````````````````````````
2103
2104 In this case, the HTML block is terminated by the newline — the `**Hello**`
2105 text remains verbatim — and regular parsing resumes, with a paragraph,
2106 emphasised `world` and inline and block HTML following.
2107
2108 All types of [HTML blocks] except type 7 may interrupt
2109 a paragraph. Blocks of type 7 may not interrupt a paragraph.
2110 (This restriction is intended to prevent unwanted interpretation
2111 of long tags inside a wrapped paragraph as starting HTML blocks.)
2112
2113 Some simple examples follow. Here are some basic HTML blocks
2114 of type 6:
2115
2116 ```````````````````````````````` example
2117 <table>
2118 <tr>
2119 <td>
2120 hi
2121 </td>
2122 </tr>
2123 </table>
2124
2125 okay.
2126 .
2127 <table>
2128 <tr>
2129 <td>
2130 hi
2131 </td>
2132 </tr>
2133 </table>
2134 <p>okay.</p>
2135 ````````````````````````````````
2136
2137
2138 ```````````````````````````````` example
2139 <div>
2140 *hello*
2141 <foo><a>
2142 .
2143 <div>
2144 *hello*
2145 <foo><a>
2146 ````````````````````````````````
2147
2148
2149 A block can also start with a closing tag:
2150
2151 ```````````````````````````````` example
2152 </div>
2153 *foo*
2154 .
2155 </div>
2156 *foo*
2157 ````````````````````````````````
2158
2159
2160 Here we have two HTML blocks with a Markdown paragraph between them:
2161
2162 ```````````````````````````````` example
2163 <DIV CLASS="foo">
2164
2165 *Markdown*
2166
2167 </DIV>
2168 .
2169 <DIV CLASS="foo">
2170 <p><em>Markdown</em></p>
2171 </DIV>
2172 ````````````````````````````````
2173
2174
2175 The tag on the first line can be partial, as long
2176 as it is split where there would be whitespace:
2177
2178 ```````````````````````````````` example
2179 <div id="foo"
2180 class="bar">
2181 </div>
2182 .
2183 <div id="foo"
2184 class="bar">
2185 </div>
2186 ````````````````````````````````
2187
2188
2189 ```````````````````````````````` example
2190 <div id="foo" class="bar
2191 baz">
2192 </div>
2193 .
2194 <div id="foo" class="bar
2195 baz">
2196 </div>
2197 ````````````````````````````````
2198
2199
2200 An open tag need not be closed:
2201 ```````````````````````````````` example
2202 <div>
2203 *foo*
2204
2205 *bar*
2206 .
2207 <div>
2208 *foo*
2209 <p><em>bar</em></p>
2210 ````````````````````````````````
2211
2212
2213
2214 A partial tag need not even be completed (garbage
2215 in, garbage out):
2216
2217 ```````````````````````````````` example
2218 <div id="foo"
2219 *hi*
2220 .
2221 <div id="foo"
2222 *hi*
2223 ````````````````````````````````
2224
2225
2226 ```````````````````````````````` example
2227 <div class
2228 foo
2229 .
2230 <div class
2231 foo
2232 ````````````````````````````````
2233
2234
2235 The initial tag doesn't even need to be a valid
2236 tag, as long as it starts like one:
2237
2238 ```````````````````````````````` example
2239 <div *???-&&&-<---
2240 *foo*
2241 .
2242 <div *???-&&&-<---
2243 *foo*
2244 ````````````````````````````````
2245
2246
2247 In type 6 blocks, the initial tag need not be on a line by
2248 itself:
2249
2250 ```````````````````````````````` example
2251 <div><a href="bar">*foo*</a></div>
2252 .
2253 <div><a href="bar">*foo*</a></div>
2254 ````````````````````````````````
2255
2256
2257 ```````````````````````````````` example
2258 <table><tr><td>
2259 foo
2260 </td></tr></table>
2261 .
2262 <table><tr><td>
2263 foo
2264 </td></tr></table>
2265 ````````````````````````````````
2266
2267
2268 Everything until the next blank line or end of document
2269 gets included in the HTML block. So, in the following
2270 example, what looks like a Markdown code block
2271 is actually part of the HTML block, which continues until a blank
2272 line or the end of the document is reached:
2273
2274 ```````````````````````````````` example
2275 <div></div>
2276 ``` c
2277 int x = 33;
2278 ```
2279 .
2280 <div></div>
2281 ``` c
2282 int x = 33;
2283 ```
2284 ````````````````````````````````
2285
2286
2287 To start an [HTML block] with a tag that is *not* in the
2288 list of block-level tags in (6), you must put the tag by
2289 itself on the first line (and it must be complete):
2290
2291 ```````````````````````````````` example
2292 <a href="foo">
2293 *bar*
2294 </a>
2295 .
2296 <a href="foo">
2297 *bar*
2298 </a>
2299 ````````````````````````````````
2300
2301
2302 In type 7 blocks, the [tag name] can be anything:
2303
2304 ```````````````````````````````` example
2305 <Warning>
2306 *bar*
2307 </Warning>
2308 .
2309 <Warning>
2310 *bar*
2311 </Warning>
2312 ````````````````````````````````
2313
2314
2315 ```````````````````````````````` example
2316 <i class="foo">
2317 *bar*
2318 </i>
2319 .
2320 <i class="foo">
2321 *bar*
2322 </i>
2323 ````````````````````````````````
2324
2325
2326 ```````````````````````````````` example
2327 </ins>
2328 *bar*
2329 .
2330 </ins>
2331 *bar*
2332 ````````````````````````````````
2333
2334
2335 These rules are designed to allow us to work with tags that
2336 can function as either block-level or inline-level tags.
2337 The `<del>` tag is a nice example. We can surround content with
2338 `<del>` tags in three different ways. In this case, we get a raw
2339 HTML block, because the `<del>` tag is on a line by itself:
2340
2341 ```````````````````````````````` example
2342 <del>
2343 *foo*
2344 </del>
2345 .
2346 <del>
2347 *foo*
2348 </del>
2349 ````````````````````````````````
2350
2351
2352 In this case, we get a raw HTML block that just includes
2353 the `<del>` tag (because it ends with the following blank
2354 line). So the contents get interpreted as CommonMark:
2355
2356 ```````````````````````````````` example
2357 <del>
2358
2359 *foo*
2360
2361 </del>
2362 .
2363 <del>
2364 <p><em>foo</em></p>
2365 </del>
2366 ````````````````````````````````
2367
2368
2369 Finally, in this case, the `<del>` tags are interpreted
2370 as [raw HTML] *inside* the CommonMark paragraph. (Because
2371 the tag is not on a line by itself, we get inline HTML
2372 rather than an [HTML block].)
2373
2374 ```````````````````````````````` example
2375 <del>*foo*</del>
2376 .
2377 <p><del><em>foo</em></del></p>
2378 ````````````````````````````````
2379
2380
2381 HTML tags designed to contain literal content
2382 (`script`, `style`, `pre`), comments, processing instructions,
2383 and declarations are treated somewhat differently.
2384 Instead of ending at the first blank line, these blocks
2385 end at the first line containing a corresponding end tag.
2386 As a result, these blocks can contain blank lines:
2387
2388 A pre tag (type 1):
2389
2390 ```````````````````````````````` example
2391 <pre language="haskell"><code>
2392 import Text.HTML.TagSoup
2393
2394 main :: IO ()
2395 main = print $ parseTags tags
2396 </code></pre>
2397 okay
2398 .
2399 <pre language="haskell"><code>
2400 import Text.HTML.TagSoup
2401
2402 main :: IO ()
2403 main = print $ parseTags tags
2404 </code></pre>
2405 <p>okay</p>
2406 ````````````````````````````````
2407
2408
2409 A script tag (type 1):
2410
2411 ```````````````````````````````` example
2412 <script type="text/javascript">
2413 // JavaScript example
2414
2415 document.getElementById("demo").innerHTML = "Hello JavaScript!";
2416 </script>
2417 okay
2418 .
2419 <script type="text/javascript">
2420 // JavaScript example
2421
2422 document.getElementById("demo").innerHTML = "Hello JavaScript!";
2423 </script>
2424 <p>okay</p>
2425 ````````````````````````````````
2426
2427
2428 A style tag (type 1):
2429
2430 ```````````````````````````````` example
2431 <style
2432 type="text/css">
2433 h1 {color:red;}
2434
2435 p {color:blue;}
2436 </style>
2437 okay
2438 .
2439 <style
2440 type="text/css">
2441 h1 {color:red;}
2442
2443 p {color:blue;}
2444 </style>
2445 <p>okay</p>
2446 ````````````````````````````````
2447
2448
2449 If there is no matching end tag, the block will end at the
2450 end of the document (or the enclosing [block quote][block quotes]
2451 or [list item][list items]):
2452
2453 ```````````````````````````````` example
2454 <style
2455 type="text/css">
2456
2457 foo
2458 .
2459 <style
2460 type="text/css">
2461
2462 foo
2463 ````````````````````````````````
2464
2465
2466 ```````````````````````````````` example
2467 > <div>
2468 > foo
2469
2470 bar
2471 .
2472 <blockquote>
2473 <div>
2474 foo
2475 </blockquote>
2476 <p>bar</p>
2477 ````````````````````````````````
2478
2479
2480 ```````````````````````````````` example
2481 - <div>
2482 - foo
2483 .
2484 <ul>
2485 <li>
2486 <div>
2487 </li>
2488 <li>foo</li>
2489 </ul>
2490 ````````````````````````````````
2491
2492
2493 The end tag can occur on the same line as the start tag:
2494
2495 ```````````````````````````````` example
2496 <style>p{color:red;}</style>
2497 *foo*
2498 .
2499 <style>p{color:red;}</style>
2500 <p><em>foo</em></p>
2501 ````````````````````````````````
2502
2503
2504 ```````````````````````````````` example
2505 <!-- foo -->*bar*
2506 *baz*
2507 .
2508 <!-- foo -->*bar*
2509 <p><em>baz</em></p>
2510 ````````````````````````````````
2511
2512
2513 Note that anything on the last line after the
2514 end tag will be included in the [HTML block]:
2515
2516 ```````````````````````````````` example
2517 <script>
2518 foo
2519 </script>1. *bar*
2520 .
2521 <script>
2522 foo
2523 </script>1. *bar*
2524 ````````````````````````````````
2525
2526
2527 A comment (type 2):
2528
2529 ```````````````````````````````` example
2530 <!-- Foo
2531
2532 bar
2533 baz -->
2534 okay
2535 .
2536 <!-- Foo
2537
2538 bar
2539 baz -->
2540 <p>okay</p>
2541 ````````````````````````````````
2542
2543
2544
2545 A processing instruction (type 3):
2546
2547 ```````````````````````````````` example
2548 <?php
2549
2550 echo '>';
2551
2552 ?>
2553 okay
2554 .
2555 <?php
2556
2557 echo '>';
2558
2559 ?>
2560 <p>okay</p>
2561 ````````````````````````````````
2562
2563
2564 A declaration (type 4):
2565
2566 ```````````````````````````````` example
2567 <!DOCTYPE html>
2568 .
2569 <!DOCTYPE html>
2570 ````````````````````````````````
2571
2572
2573 CDATA (type 5):
2574
2575 ```````````````````````````````` example
2576 <![CDATA[
2577 function matchwo(a,b)
2578 {
2579 if (a < b && a < 0) then {
2580 return 1;
2581
2582 } else {
2583
2584 return 0;
2585 }
2586 }
2587 ]]>
2588 okay
2589 .
2590 <![CDATA[
2591 function matchwo(a,b)
2592 {
2593 if (a < b && a < 0) then {
2594 return 1;
2595
2596 } else {
2597
2598 return 0;
2599 }
2600 }
2601 ]]>
2602 <p>okay</p>
2603 ````````````````````````````````
2604
2605
2606 The opening tag can be indented 1-3 spaces, but not 4:
2607
2608 ```````````````````````````````` example
2609 <!-- foo -->
2610
2611 <!-- foo -->
2612 .
2613 <!-- foo -->
2614 <pre><code>&lt;!-- foo --&gt;
2615 </code></pre>
2616 ````````````````````````````````
2617
2618
2619 ```````````````````````````````` example
2620 <div>
2621
2622 <div>
2623 .
2624 <div>
2625 <pre><code>&lt;div&gt;
2626 </code></pre>
2627 ````````````````````````````````
2628
2629
2630 An HTML block of types 1--6 can interrupt a paragraph, and need not be
2631 preceded by a blank line.
2632
2633 ```````````````````````````````` example
2634 Foo
2635 <div>
2636 bar
2637 </div>
2638 .
2639 <p>Foo</p>
2640 <div>
2641 bar
2642 </div>
2643 ````````````````````````````````
2644
2645
2646 However, a following blank line is needed, except at the end of
2647 a document, and except for blocks of types 1--5, [above][HTML
2648 block]:
2649
2650 ```````````````````````````````` example
2651 <div>
2652 bar
2653 </div>
2654 *foo*
2655 .
2656 <div>
2657 bar
2658 </div>
2659 *foo*
2660 ````````````````````````````````
2661
2662
2663 HTML blocks of type 7 cannot interrupt a paragraph:
2664
2665 ```````````````````````````````` example
2666 Foo
2667 <a href="bar">
2668 baz
2669 .
2670 <p>Foo
2671 <a href="bar">
2672 baz</p>
2673 ````````````````````````````````
2674
2675
2676 This rule differs from John Gruber's original Markdown syntax
2677 specification, which says:
2678
2679 > The only restrictions are that block-level HTML elements —
2680 > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
2681 > surrounding content by blank lines, and the start and end tags of the
2682 > block should not be indented with tabs or spaces.
2683
2684 In some ways Gruber's rule is more restrictive than the one given
2685 here:
2686
2687 - It requires that an HTML block be preceded by a blank line.
2688 - It does not allow the start tag to be indented.
2689 - It requires a matching end tag, which it also does not allow to
2690 be indented.
2691
2692 Most Markdown implementations (including some of Gruber's own) do not
2693 respect all of these restrictions.
2694
2695 There is one respect, however, in which Gruber's rule is more liberal
2696 than the one given here, since it allows blank lines to occur inside
2697 an HTML block. There are two reasons for disallowing them here.
2698 First, it removes the need to parse balanced tags, which is
2699 expensive and can require backtracking from the end of the document
2700 if no matching end tag is found. Second, it provides a very simple
2701 and flexible way of including Markdown content inside HTML tags:
2702 simply separate the Markdown from the HTML using blank lines:
2703
2704 Compare:
2705
2706 ```````````````````````````````` example
2707 <div>
2708
2709 *Emphasized* text.
2710
2711 </div>
2712 .
2713 <div>
2714 <p><em>Emphasized</em> text.</p>
2715 </div>
2716 ````````````````````````````````
2717
2718
2719 ```````````````````````````````` example
2720 <div>
2721 *Emphasized* text.
2722 </div>
2723 .
2724 <div>
2725 *Emphasized* text.
2726 </div>
2727 ````````````````````````````````
2728
2729
2730 Some Markdown implementations have adopted a convention of
2731 interpreting content inside tags as text if the open tag has
2732 the attribute `markdown=1`. The rule given above seems a simpler and
2733 more elegant way of achieving the same expressive power, which is also
2734 much simpler to parse.
2735
2736 The main potential drawback is that one can no longer paste HTML
2737 blocks into Markdown documents with 100% reliability. However,
2738 *in most cases* this will work fine, because the blank lines in
2739 HTML are usually followed by HTML block tags. For example:
2740
2741 ```````````````````````````````` example
2742 <table>
2743
2744 <tr>
2745
2746 <td>
2747 Hi
2748 </td>
2749
2750 </tr>
2751
2752 </table>
2753 .
2754 <table>
2755 <tr>
2756 <td>
2757 Hi
2758 </td>
2759 </tr>
2760 </table>
2761 ````````````````````````````````
2762
2763
2764 There are problems, however, if the inner tags are indented
2765 *and* separated by spaces, as then they will be interpreted as
2766 an indented code block:
2767
2768 ```````````````````````````````` example
2769 <table>
2770
2771 <tr>
2772
2773 <td>
2774 Hi
2775 </td>
2776
2777 </tr>
2778
2779 </table>
2780 .
2781 <table>
2782 <tr>
2783 <pre><code>&lt;td&gt;
2784 Hi
2785 &lt;/td&gt;
2786 </code></pre>
2787 </tr>
2788 </table>
2789 ````````````````````````````````
2790
2791
2792 Fortunately, blank lines are usually not necessary and can be
2793 deleted. The exception is inside `<pre>` tags, but as described
2794 [above][HTML blocks], raw HTML blocks starting with `<pre>`
2795 *can* contain blank lines.
2796
2797 ## Link reference definitions
2798
2799 A [link reference definition](@)
2800 consists of a [link label], indented up to three spaces, followed
2801 by a colon (`:`), optional [whitespace] (including up to one
2802 [line ending]), a [link destination],
2803 optional [whitespace] (including up to one
2804 [line ending]), and an optional [link
2805 title], which if it is present must be separated
2806 from the [link destination] by [whitespace].
2807 No further [non-whitespace characters] may occur on the line.
2808
2809 A [link reference definition]
2810 does not correspond to a structural element of a document. Instead, it
2811 defines a label which can be used in [reference links]
2812 and reference-style [images] elsewhere in the document. [Link
2813 reference definitions] can come either before or after the links that use
2814 them.
2815
2816 ```````````````````````````````` example
2817 [foo]: /url "title"
2818
2819 [foo]
2820 .
2821 <p><a href="/url" title="title">foo</a></p>
2822 ````````````````````````````````
2823
2824
2825 ```````````````````````````````` example
2826 [foo]:
2827 /url
2828 'the title'
2829
2830 [foo]
2831 .
2832 <p><a href="/url" title="the title">foo</a></p>
2833 ````````````````````````````````
2834
2835
2836 ```````````````````````````````` example
2837 [Foo*bar\]]:my_(url) 'title (with parens)'
2838
2839 [Foo*bar\]]
2840 .
2841 <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
2842 ````````````````````````````````
2843
2844
2845 ```````````````````````````````` example
2846 [Foo bar]:
2847 <my url>
2848 'title'
2849
2850 [Foo bar]
2851 .
2852 <p><a href="my%20url" title="title">Foo bar</a></p>
2853 ````````````````````````````````
2854
2855
2856 The title may extend over multiple lines:
2857
2858 ```````````````````````````````` example
2859 [foo]: /url '
2860 title
2861 line1
2862 line2
2863 '
2864
2865 [foo]
2866 .
2867 <p><a href="/url" title="
2868 title
2869 line1
2870 line2
2871 ">foo</a></p>
2872 ````````````````````````````````
2873
2874
2875 However, it may not contain a [blank line]:
2876
2877 ```````````````````````````````` example
2878 [foo]: /url 'title
2879
2880 with blank line'
2881
2882 [foo]
2883 .
2884 <p>[foo]: /url 'title</p>
2885 <p>with blank line'</p>
2886 <p>[foo]</p>
2887 ````````````````````````````````
2888
2889
2890 The title may be omitted:
2891
2892 ```````````````````````````````` example
2893 [foo]:
2894 /url
2895
2896 [foo]
2897 .
2898 <p><a href="/url">foo</a></p>
2899 ````````````````````````````````
2900
2901
2902 The link destination may not be omitted:
2903
2904 ```````````````````````````````` example
2905 [foo]:
2906
2907 [foo]
2908 .
2909 <p>[foo]:</p>
2910 <p>[foo]</p>
2911 ````````````````````````````````
2912
2913 However, an empty link destination may be specified using
2914 angle brackets:
2915
2916 ```````````````````````````````` example
2917 [foo]: <>
2918
2919 [foo]
2920 .
2921 <p><a href="">foo</a></p>
2922 ````````````````````````````````
2923
2924 The title must be separated from the link destination by
2925 whitespace:
2926
2927 ```````````````````````````````` example
2928 [foo]: <bar>(baz)
2929
2930 [foo]
2931 .
2932 <p>[foo]: <bar>(baz)</p>
2933 <p>[foo]</p>
2934 ````````````````````````````````
2935
2936
2937 Both title and destination can contain backslash escapes
2938 and literal backslashes:
2939
2940 ```````````````````````````````` example
2941 [foo]: /url\bar\*baz "foo\"bar\baz"
2942
2943 [foo]
2944 .
2945 <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
2946 ````````````````````````````````
2947
2948
2949 A link can come before its corresponding definition:
2950
2951 ```````````````````````````````` example
2952 [foo]
2953
2954 [foo]: url
2955 .
2956 <p><a href="url">foo</a></p>
2957 ````````````````````````````````
2958
2959
2960 If there are several matching definitions, the first one takes
2961 precedence:
2962
2963 ```````````````````````````````` example
2964 [foo]
2965
2966 [foo]: first
2967 [foo]: second
2968 .
2969 <p><a href="first">foo</a></p>
2970 ````````````````````````````````
2971
2972
2973 As noted in the section on [Links], matching of labels is
2974 case-insensitive (see [matches]).
2975
2976 ```````````````````````````````` example
2977 [FOO]: /url
2978
2979 [Foo]
2980 .
2981 <p><a href="/url">Foo</a></p>
2982 ````````````````````````````````
2983
2984
2985 ```````````````````````````````` example
2986 [ΑΓΩ]: /φου
2987
2988 [αγω]
2989 .
2990 <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
2991 ````````````````````````````````
2992
2993
2994 Here is a link reference definition with no corresponding link.
2995 It contributes nothing to the document.
2996
2997 ```````````````````````````````` example
2998 [foo]: /url
2999 .
3000 ````````````````````````````````
3001
3002
3003 Here is another one:
3004
3005 ```````````````````````````````` example
3006 [
3007 foo
3008 ]: /url
3009 bar
3010 .
3011 <p>bar</p>
3012 ````````````````````````````````
3013
3014
3015 This is not a link reference definition, because there are
3016 [non-whitespace characters] after the title:
3017
3018 ```````````````````````````````` example
3019 [foo]: /url "title" ok
3020 .
3021 <p>[foo]: /url &quot;title&quot; ok</p>
3022 ````````````````````````````````
3023
3024
3025 This is a link reference definition, but it has no title:
3026
3027 ```````````````````````````````` example
3028 [foo]: /url
3029 "title" ok
3030 .
3031 <p>&quot;title&quot; ok</p>
3032 ````````````````````````````````
3033
3034
3035 This is not a link reference definition, because it is indented
3036 four spaces:
3037
3038 ```````````````````````````````` example
3039 [foo]: /url "title"
3040
3041 [foo]
3042 .
3043 <pre><code>[foo]: /url &quot;title&quot;
3044 </code></pre>
3045 <p>[foo]</p>
3046 ````````````````````````````````
3047
3048
3049 This is not a link reference definition, because it occurs inside
3050 a code block:
3051
3052 ```````````````````````````````` example
3053 ```
3054 [foo]: /url
3055 ```
3056
3057 [foo]
3058 .
3059 <pre><code>[foo]: /url
3060 </code></pre>
3061 <p>[foo]</p>
3062 ````````````````````````````````
3063
3064
3065 A [link reference definition] cannot interrupt a paragraph.
3066
3067 ```````````````````````````````` example
3068 Foo
3069 [bar]: /baz
3070
3071 [bar]
3072 .
3073 <p>Foo
3074 [bar]: /baz</p>
3075 <p>[bar]</p>
3076 ````````````````````````````````
3077
3078
3079 However, it can directly follow other block elements, such as headings
3080 and thematic breaks, and it need not be followed by a blank line.
3081
3082 ```````````````````````````````` example
3083 # [Foo]
3084 [foo]: /url
3085 > bar
3086 .
3087 <h1><a href="/url">Foo</a></h1>
3088 <blockquote>
3089 <p>bar</p>
3090 </blockquote>
3091 ````````````````````````````````
3092
3093 ```````````````````````````````` example
3094 [foo]: /url
3095 bar
3096 ===
3097 [foo]
3098 .
3099 <h1>bar</h1>
3100 <p><a href="/url">foo</a></p>
3101 ````````````````````````````````
3102
3103 ```````````````````````````````` example
3104 [foo]: /url
3105 ===
3106 [foo]
3107 .
3108 <p>===
3109 <a href="/url">foo</a></p>
3110 ````````````````````````````````
3111
3112
3113 Several [link reference definitions]
3114 can occur one after another, without intervening blank lines.
3115
3116 ```````````````````````````````` example
3117 [foo]: /foo-url "foo"
3118 [bar]: /bar-url
3119 "bar"
3120 [baz]: /baz-url
3121
3122 [foo],
3123 [bar],
3124 [baz]
3125 .
3126 <p><a href="/foo-url" title="foo">foo</a>,
3127 <a href="/bar-url" title="bar">bar</a>,
3128 <a href="/baz-url">baz</a></p>
3129 ````````````````````````````````
3130
3131
3132 [Link reference definitions] can occur
3133 inside block containers, like lists and block quotations. They
3134 affect the entire document, not just the container in which they
3135 are defined:
3136
3137 ```````````````````````````````` example
3138 [foo]
3139
3140 > [foo]: /url
3141 .
3142 <p><a href="/url">foo</a></p>
3143 <blockquote>
3144 </blockquote>
3145 ````````````````````````````````
3146
3147
3148 Whether something is a [link reference definition] is
3149 independent of whether the link reference it defines is
3150 used in the document. Thus, for example, the following
3151 document contains just a link reference definition, and
3152 no visible content:
3153
3154 ```````````````````````````````` example
3155 [foo]: /url
3156 .
3157 ````````````````````````````````
3158
3159
3160 ## Paragraphs
3161
3162 A sequence of non-blank lines that cannot be interpreted as other
3163 kinds of blocks forms a [paragraph](@).
3164 The contents of the paragraph are the result of parsing the
3165 paragraph's raw content as inlines. The paragraph's raw content
3166 is formed by concatenating the lines and removing initial and final
3167 [whitespace].
3168
3169 A simple example with two paragraphs:
3170
3171 ```````````````````````````````` example
3172 aaa
3173
3174 bbb
3175 .
3176 <p>aaa</p>
3177 <p>bbb</p>
3178 ````````````````````````````````
3179
3180
3181 Paragraphs can contain multiple lines, but no blank lines:
3182
3183 ```````````````````````````````` example
3184 aaa
3185 bbb
3186
3187 ccc
3188 ddd
3189 .
3190 <p>aaa
3191 bbb</p>
3192 <p>ccc
3193 ddd</p>
3194 ````````````````````````````````
3195
3196
3197 Multiple blank lines between paragraph have no effect:
3198
3199 ```````````````````````````````` example
3200 aaa
3201
3202
3203 bbb
3204 .
3205 <p>aaa</p>
3206 <p>bbb</p>
3207 ````````````````````````````````
3208
3209
3210 Leading spaces are skipped:
3211
3212 ```````````````````````````````` example
3213 aaa
3214 bbb
3215 .
3216 <p>aaa
3217 bbb</p>
3218 ````````````````````````````````
3219
3220
3221 Lines after the first may be indented any amount, since indented
3222 code blocks cannot interrupt paragraphs.
3223
3224 ```````````````````````````````` example
3225 aaa
3226 bbb
3227 ccc
3228 .
3229 <p>aaa
3230 bbb
3231 ccc</p>
3232 ````````````````````````````````
3233
3234
3235 However, the first line may be indented at most three spaces,
3236 or an indented code block will be triggered:
3237
3238 ```````````````````````````````` example
3239 aaa
3240 bbb
3241 .
3242 <p>aaa
3243 bbb</p>
3244 ````````````````````````````````
3245
3246
3247 ```````````````````````````````` example
3248 aaa
3249 bbb
3250 .
3251 <pre><code>aaa
3252 </code></pre>
3253 <p>bbb</p>
3254 ````````````````````````````````
3255
3256
3257 Final spaces are stripped before inline parsing, so a paragraph
3258 that ends with two or more spaces will not end with a [hard line
3259 break]:
3260
3261 ```````````````````````````````` example
3262 aaa
3263 bbb
3264 .
3265 <p>aaa<br />
3266 bbb</p>
3267 ````````````````````````````````
3268
3269
3270 ## Blank lines
3271
3272 [Blank lines] between block-level elements are ignored,
3273 except for the role they play in determining whether a [list]
3274 is [tight] or [loose].
3275
3276 Blank lines at the beginning and end of the document are also ignored.
3277
3278 ```````````````````````````````` example
3279
3280
3281 aaa
3282
3283
3284 # aaa
3285
3286
3287 .
3288 <p>aaa</p>
3289 <h1>aaa</h1>
3290 ````````````````````````````````
3291
3292
3293
3294 # Container blocks
3295
3296 A [container block](#container-blocks) is a block that has other
3297 blocks as its contents. There are two basic kinds of container blocks:
3298 [block quotes] and [list items].
3299 [Lists] are meta-containers for [list items].
3300
3301 We define the syntax for container blocks recursively. The general
3302 form of the definition is:
3303
3304 > If X is a sequence of blocks, then the result of
3305 > transforming X in such-and-such a way is a container of type Y
3306 > with these blocks as its content.
3307
3308 So, we explain what counts as a block quote or list item by explaining
3309 how these can be *generated* from their contents. This should suffice
3310 to define the syntax, although it does not give a recipe for *parsing*
3311 these constructions. (A recipe is provided below in the section entitled
3312 [A parsing strategy](#appendix-a-parsing-strategy).)
3313
3314 ## Block quotes
3315
3316 A [block quote marker](@)
3317 consists of 0-3 spaces of initial indent, plus (a) the character `>` together
3318 with a following space, or (b) a single character `>` not followed by a space.
3319
3320 The following rules define [block quotes]:
3321
3322 1. **Basic case.** If a string of lines *Ls* constitute a sequence
3323 of blocks *Bs*, then the result of prepending a [block quote
3324 marker] to the beginning of each line in *Ls*
3325 is a [block quote](#block-quotes) containing *Bs*.
3326
3327 2. **Laziness.** If a string of lines *Ls* constitute a [block
3328 quote](#block-quotes) with contents *Bs*, then the result of deleting
3329 the initial [block quote marker] from one or
3330 more lines in which the next [non-whitespace character] after the [block
3331 quote marker] is [paragraph continuation
3332 text] is a block quote with *Bs* as its content.
3333 [Paragraph continuation text](@) is text
3334 that will be parsed as part of the content of a paragraph, but does
3335 not occur at the beginning of the paragraph.
3336
3337 3. **Consecutiveness.** A document cannot contain two [block
3338 quotes] in a row unless there is a [blank line] between them.
3339
3340 Nothing else counts as a [block quote](#block-quotes).
3341
3342 Here is a simple example:
3343
3344 ```````````````````````````````` example
3345 > # Foo
3346 > bar
3347 > baz
3348 .
3349 <blockquote>
3350 <h1>Foo</h1>
3351 <p>bar
3352 baz</p>
3353 </blockquote>
3354 ````````````````````````````````
3355
3356
3357 The spaces after the `>` characters can be omitted:
3358
3359 ```````````````````````````````` example
3360 ># Foo
3361 >bar
3362 > baz
3363 .
3364 <blockquote>
3365 <h1>Foo</h1>
3366 <p>bar
3367 baz</p>
3368 </blockquote>
3369 ````````````````````````````````
3370
3371
3372 The `>` characters can be indented 1-3 spaces:
3373
3374 ```````````````````````````````` example
3375 > # Foo
3376 > bar
3377 > baz
3378 .
3379 <blockquote>
3380 <h1>Foo</h1>
3381 <p>bar
3382 baz</p>
3383 </blockquote>
3384 ````````````````````````````````
3385
3386
3387 Four spaces gives us a code block:
3388
3389 ```````````````````````````````` example
3390 > # Foo
3391 > bar
3392 > baz
3393 .
3394 <pre><code>&gt; # Foo
3395 &gt; bar
3396 &gt; baz
3397 </code></pre>
3398 ````````````````````````````````
3399
3400
3401 The Laziness clause allows us to omit the `>` before
3402 [paragraph continuation text]:
3403
3404 ```````````````````````````````` example
3405 > # Foo
3406 > bar
3407 baz
3408 .
3409 <blockquote>
3410 <h1>Foo</h1>
3411 <p>bar
3412 baz</p>
3413 </blockquote>
3414 ````````````````````````````````
3415
3416
3417 A block quote can contain some lazy and some non-lazy
3418 continuation lines:
3419
3420 ```````````````````````````````` example
3421 > bar
3422 baz
3423 > foo
3424 .
3425 <blockquote>
3426 <p>bar
3427 baz
3428 foo</p>
3429 </blockquote>
3430 ````````````````````````````````
3431
3432
3433 Laziness only applies to lines that would have been continuations of
3434 paragraphs had they been prepended with [block quote markers].
3435 For example, the `> ` cannot be omitted in the second line of
3436
3437 ``` markdown
3438 > foo
3439 > ---
3440 ```
3441
3442 without changing the meaning:
3443
3444 ```````````````````````````````` example
3445 > foo
3446 ---
3447 .
3448 <blockquote>
3449 <p>foo</p>
3450 </blockquote>
3451 <hr />
3452 ````````````````````````````````
3453
3454
3455 Similarly, if we omit the `> ` in the second line of
3456
3457 ``` markdown
3458 > - foo
3459 > - bar
3460 ```
3461
3462 then the block quote ends after the first line:
3463
3464 ```````````````````````````````` example
3465 > - foo
3466 - bar
3467 .
3468 <blockquote>
3469 <ul>
3470 <li>foo</li>
3471 </ul>
3472 </blockquote>
3473 <ul>
3474 <li>bar</li>
3475 </ul>
3476 ````````````````````````````````
3477
3478
3479 For the same reason, we can't omit the `> ` in front of
3480 subsequent lines of an indented or fenced code block:
3481
3482 ```````````````````````````````` example
3483 > foo
3484 bar
3485 .
3486 <blockquote>
3487 <pre><code>foo
3488 </code></pre>
3489 </blockquote>
3490 <pre><code>bar
3491 </code></pre>
3492 ````````````````````````````````
3493
3494
3495 ```````````````````````````````` example
3496 > ```
3497 foo
3498 ```
3499 .
3500 <blockquote>
3501 <pre><code></code></pre>
3502 </blockquote>
3503 <p>foo</p>
3504 <pre><code></code></pre>
3505 ````````````````````````````````
3506
3507
3508 Note that in the following case, we have a [lazy
3509 continuation line]:
3510
3511 ```````````````````````````````` example
3512 > foo
3513 - bar
3514 .
3515 <blockquote>
3516 <p>foo
3517 - bar</p>
3518 </blockquote>
3519 ````````````````````````````````
3520
3521
3522 To see why, note that in
3523
3524 ```markdown
3525 > foo
3526 > - bar
3527 ```
3528
3529 the `- bar` is indented too far to start a list, and can't
3530 be an indented code block because indented code blocks cannot
3531 interrupt paragraphs, so it is [paragraph continuation text].
3532
3533 A block quote can be empty:
3534
3535 ```````````````````````````````` example
3536 >
3537 .
3538 <blockquote>
3539 </blockquote>
3540 ````````````````````````````````
3541
3542
3543 ```````````````````````````````` example
3544 >
3545 >
3546 >
3547 .
3548 <blockquote>
3549 </blockquote>
3550 ````````````````````````````````
3551
3552
3553 A block quote can have initial or final blank lines:
3554
3555 ```````````````````````````````` example
3556 >
3557 > foo
3558 >
3559 .
3560 <blockquote>
3561 <p>foo</p>
3562 </blockquote>
3563 ````````````````````````````````
3564
3565
3566 A blank line always separates block quotes:
3567
3568 ```````````````````````````````` example
3569 > foo
3570
3571 > bar
3572 .
3573 <blockquote>
3574 <p>foo</p>
3575 </blockquote>
3576 <blockquote>
3577 <p>bar</p>
3578 </blockquote>
3579 ````````````````````````````````
3580
3581
3582 (Most current Markdown implementations, including John Gruber's
3583 original `Markdown.pl`, will parse this example as a single block quote
3584 with two paragraphs. But it seems better to allow the author to decide
3585 whether two block quotes or one are wanted.)
3586
3587 Consecutiveness means that if we put these block quotes together,
3588 we get a single block quote:
3589
3590 ```````````````````````````````` example
3591 > foo
3592 > bar
3593 .
3594 <blockquote>
3595 <p>foo
3596 bar</p>
3597 </blockquote>
3598 ````````````````````````````````
3599
3600
3601 To get a block quote with two paragraphs, use:
3602
3603 ```````````````````````````````` example
3604 > foo
3605 >
3606 > bar
3607 .
3608 <blockquote>
3609 <p>foo</p>
3610 <p>bar</p>
3611 </blockquote>
3612 ````````````````````````````````
3613
3614
3615 Block quotes can interrupt paragraphs:
3616
3617 ```````````````````````````````` example
3618 foo
3619 > bar
3620 .
3621 <p>foo</p>
3622 <blockquote>
3623 <p>bar</p>
3624 </blockquote>
3625 ````````````````````````````````
3626
3627
3628 In general, blank lines are not needed before or after block
3629 quotes:
3630
3631 ```````````````````````````````` example
3632 > aaa
3633 ***
3634 > bbb
3635 .
3636 <blockquote>
3637 <p>aaa</p>
3638 </blockquote>
3639 <hr />
3640 <blockquote>
3641 <p>bbb</p>
3642 </blockquote>
3643 ````````````````````````````````
3644
3645
3646 However, because of laziness, a blank line is needed between
3647 a block quote and a following paragraph:
3648
3649 ```````````````````````````````` example
3650 > bar
3651 baz
3652 .
3653 <blockquote>
3654 <p>bar
3655 baz</p>
3656 </blockquote>
3657 ````````````````````````````````
3658
3659
3660 ```````````````````````````````` example
3661 > bar
3662
3663 baz
3664 .
3665 <blockquote>
3666 <p>bar</p>
3667 </blockquote>
3668 <p>baz</p>
3669 ````````````````````````````````
3670
3671
3672 ```````````````````````````````` example
3673 > bar
3674 >
3675 baz
3676 .
3677 <blockquote>
3678 <p>bar</p>
3679 </blockquote>
3680 <p>baz</p>
3681 ````````````````````````````````
3682
3683
3684 It is a consequence of the Laziness rule that any number
3685 of initial `>`s may be omitted on a continuation line of a
3686 nested block quote:
3687
3688 ```````````````````````````````` example
3689 > > > foo
3690 bar
3691 .
3692 <blockquote>
3693 <blockquote>
3694 <blockquote>
3695 <p>foo
3696 bar</p>
3697 </blockquote>
3698 </blockquote>
3699 </blockquote>
3700 ````````````````````````````````
3701
3702
3703 ```````````````````````````````` example
3704 >>> foo
3705 > bar
3706 >>baz
3707 .
3708 <blockquote>
3709 <blockquote>
3710 <blockquote>
3711 <p>foo
3712 bar
3713 baz</p>
3714 </blockquote>
3715 </blockquote>
3716 </blockquote>
3717 ````````````````````````````````
3718
3719
3720 When including an indented code block in a block quote,
3721 remember that the [block quote marker] includes
3722 both the `>` and a following space. So *five spaces* are needed after
3723 the `>`:
3724
3725 ```````````````````````````````` example
3726 > code
3727
3728 > not code
3729 .
3730 <blockquote>
3731 <pre><code>code
3732 </code></pre>
3733 </blockquote>
3734 <blockquote>
3735 <p>not code</p>
3736 </blockquote>
3737 ````````````````````````````````
3738
3739
3740
3741 ## List items
3742
3743 A [list marker](@) is a
3744 [bullet list marker] or an [ordered list marker].
3745
3746 A [bullet list marker](@)
3747 is a `-`, `+`, or `*` character.
3748
3749 An [ordered list marker](@)
3750 is a sequence of 1--9 arabic digits (`0-9`), followed by either a
3751 `.` character or a `)` character. (The reason for the length
3752 limit is that with 10 digits we start seeing integer overflows
3753 in some browsers.)
3754
3755 The following rules define [list items]:
3756
3757 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
3758 blocks *Bs* starting with a [non-whitespace character], and *M* is a
3759 list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
3760 of prepending *M* and the following spaces to the first line of
3761 *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
3762 list item with *Bs* as its contents. The type of the list item
3763 (bullet or ordered) is determined by the type of its list marker.
3764 If the list item is ordered, then it is also assigned a start
3765 number, based on the ordered list marker.
3766
3767 Exceptions:
3768
3769 1. When the first list item in a [list] interrupts
3770 a paragraph---that is, when it starts on a line that would
3771 otherwise count as [paragraph continuation text]---then (a)
3772 the lines *Ls* must not begin with a blank line, and (b) if
3773 the list item is ordered, the start number must be 1.
3774 2. If any line is a [thematic break][thematic breaks] then
3775 that line is not a list item.
3776
3777 For example, let *Ls* be the lines
3778
3779 ```````````````````````````````` example
3780 A paragraph
3781 with two lines.
3782
3783 indented code
3784
3785 > A block quote.
3786 .
3787 <p>A paragraph
3788 with two lines.</p>
3789 <pre><code>indented code
3790 </code></pre>
3791 <blockquote>
3792 <p>A block quote.</p>
3793 </blockquote>
3794 ````````````````````````````````
3795
3796
3797 And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
3798 that the following is an ordered list item with start number 1,
3799 and the same contents as *Ls*:
3800
3801 ```````````````````````````````` example
3802 1. A paragraph
3803 with two lines.
3804
3805 indented code
3806
3807 > A block quote.
3808 .
3809 <ol>
3810 <li>
3811 <p>A paragraph
3812 with two lines.</p>
3813 <pre><code>indented code
3814 </code></pre>
3815 <blockquote>
3816 <p>A block quote.</p>
3817 </blockquote>
3818 </li>
3819 </ol>
3820 ````````````````````````````````
3821
3822
3823 The most important thing to notice is that the position of
3824 the text after the list marker determines how much indentation
3825 is needed in subsequent blocks in the list item. If the list
3826 marker takes up two spaces, and there are three spaces between
3827 the list marker and the next [non-whitespace character], then blocks
3828 must be indented five spaces in order to fall under the list
3829 item.
3830
3831 Here are some examples showing how far content must be indented to be
3832 put under the list item:
3833
3834 ```````````````````````````````` example
3835 - one
3836
3837 two
3838 .
3839 <ul>
3840 <li>one</li>
3841 </ul>
3842 <p>two</p>
3843 ````````````````````````````````
3844
3845
3846 ```````````````````````````````` example
3847 - one
3848
3849 two
3850 .
3851 <ul>
3852 <li>
3853 <p>one</p>
3854 <p>two</p>
3855 </li>
3856 </ul>
3857 ````````````````````````````````
3858
3859
3860 ```````````````````````````````` example
3861 - one
3862
3863 two
3864 .
3865 <ul>
3866 <li>one</li>
3867 </ul>
3868 <pre><code> two
3869 </code></pre>
3870 ````````````````````````````````
3871
3872
3873 ```````````````````````````````` example
3874 - one
3875
3876 two
3877 .
3878 <ul>
3879 <li>
3880 <p>one</p>
3881 <p>two</p>
3882 </li>
3883 </ul>
3884 ````````````````````````````````
3885
3886
3887 It is tempting to think of this in terms of columns: the continuation
3888 blocks must be indented at least to the column of the first
3889 [non-whitespace character] after the list marker. However, that is not quite right.
3890 The spaces after the list marker determine how much relative indentation
3891 is needed. Which column this indentation reaches will depend on
3892 how the list item is embedded in other constructions, as shown by
3893 this example:
3894
3895 ```````````````````````````````` example
3896 > > 1. one
3897 >>
3898 >> two
3899 .
3900 <blockquote>
3901 <blockquote>
3902 <ol>
3903 <li>
3904 <p>one</p>
3905 <p>two</p>
3906 </li>
3907 </ol>
3908 </blockquote>
3909 </blockquote>
3910 ````````````````````````````````
3911
3912
3913 Here `two` occurs in the same column as the list marker `1.`,
3914 but is actually contained in the list item, because there is
3915 sufficient indentation after the last containing blockquote marker.
3916
3917 The converse is also possible. In the following example, the word `two`
3918 occurs far to the right of the initial text of the list item, `one`, but
3919 it is not considered part of the list item, because it is not indented
3920 far enough past the blockquote marker:
3921
3922 ```````````````````````````````` example
3923 >>- one
3924 >>
3925 > > two
3926 .
3927 <blockquote>
3928 <blockquote>
3929 <ul>
3930 <li>one</li>
3931 </ul>
3932 <p>two</p>
3933 </blockquote>
3934 </blockquote>
3935 ````````````````````````````````
3936
3937
3938 Note that at least one space is needed between the list marker and
3939 any following content, so these are not list items:
3940
3941 ```````````````````````````````` example
3942 -one
3943
3944 2.two
3945 .
3946 <p>-one</p>
3947 <p>2.two</p>
3948 ````````````````````````````````
3949
3950
3951 A list item may contain blocks that are separated by more than
3952 one blank line.
3953
3954 ```````````````````````````````` example
3955 - foo
3956
3957
3958 bar
3959 .
3960 <ul>
3961 <li>
3962 <p>foo</p>
3963 <p>bar</p>
3964 </li>
3965 </ul>
3966 ````````````````````````````````
3967
3968
3969 A list item may contain any kind of block:
3970
3971 ```````````````````````````````` example
3972 1. foo
3973
3974 ```
3975 bar
3976 ```
3977
3978 baz
3979
3980 > bam
3981 .
3982 <ol>
3983 <li>
3984 <p>foo</p>
3985 <pre><code>bar
3986 </code></pre>
3987 <p>baz</p>
3988 <blockquote>
3989 <p>bam</p>
3990 </blockquote>
3991 </li>
3992 </ol>
3993 ````````````````````````````````
3994
3995
3996 A list item that contains an indented code block will preserve
3997 empty lines within the code block verbatim.
3998
3999 ```````````````````````````````` example
4000 - Foo
4001
4002 bar
4003
4004
4005 baz
4006 .
4007 <ul>
4008 <li>
4009 <p>Foo</p>
4010 <pre><code>bar
4011
4012
4013 baz
4014 </code></pre>
4015 </li>
4016 </ul>
4017 ````````````````````````````````
4018
4019 Note that ordered list start numbers must be nine digits or less:
4020
4021 ```````````````````````````````` example
4022 123456789. ok
4023 .
4024 <ol start="123456789">
4025 <li>ok</li>
4026 </ol>
4027 ````````````````````````````````
4028
4029
4030 ```````````````````````````````` example
4031 1234567890. not ok
4032 .
4033 <p>1234567890. not ok</p>
4034 ````````````````````````````````
4035
4036
4037 A start number may begin with 0s:
4038
4039 ```````````````````````````````` example
4040 0. ok
4041 .
4042 <ol start="0">
4043 <li>ok</li>
4044 </ol>
4045 ````````````````````````````````
4046
4047
4048 ```````````````````````````````` example
4049 003. ok
4050 .
4051 <ol start="3">
4052 <li>ok</li>
4053 </ol>
4054 ````````````````````````````````
4055
4056
4057 A start number may not be negative:
4058
4059 ```````````````````````````````` example
4060 -1. not ok
4061 .
4062 <p>-1. not ok</p>
4063 ````````````````````````````````
4064
4065
4066
4067 2. **Item starting with indented code.** If a sequence of lines *Ls*
4068 constitute a sequence of blocks *Bs* starting with an indented code
4069 block, and *M* is a list marker of width *W* followed by
4070 one space, then the result of prepending *M* and the following
4071 space to the first line of *Ls*, and indenting subsequent lines of
4072 *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
4073 If a line is empty, then it need not be indented. The type of the
4074 list item (bullet or ordered) is determined by the type of its list
4075 marker. If the list item is ordered, then it is also assigned a
4076 start number, based on the ordered list marker.
4077
4078 An indented code block will have to be indented four spaces beyond
4079 the edge of the region where text will be included in the list item.
4080 In the following case that is 6 spaces:
4081
4082 ```````````````````````````````` example
4083 - foo
4084
4085 bar
4086 .
4087 <ul>
4088 <li>
4089 <p>foo</p>
4090 <pre><code>bar
4091 </code></pre>
4092 </li>
4093 </ul>
4094 ````````````````````````````````
4095
4096
4097 And in this case it is 11 spaces:
4098
4099 ```````````````````````````````` example
4100 10. foo
4101
4102 bar
4103 .
4104 <ol start="10">
4105 <li>
4106 <p>foo</p>
4107 <pre><code>bar
4108 </code></pre>
4109 </li>
4110 </ol>
4111 ````````````````````````````````
4112
4113
4114 If the *first* block in the list item is an indented code block,
4115 then by rule #2, the contents must be indented *one* space after the
4116 list marker:
4117
4118 ```````````````````````````````` example
4119 indented code
4120
4121 paragraph
4122
4123 more code
4124 .
4125 <pre><code>indented code
4126 </code></pre>
4127 <p>paragraph</p>
4128 <pre><code>more code
4129 </code></pre>
4130 ````````````````````````````````
4131
4132
4133 ```````````````````````````````` example
4134 1. indented code
4135
4136 paragraph
4137
4138 more code
4139 .
4140 <ol>
4141 <li>
4142 <pre><code>indented code
4143 </code></pre>
4144 <p>paragraph</p>
4145 <pre><code>more code
4146 </code></pre>
4147 </li>
4148 </ol>
4149 ````````````````````````````````
4150
4151
4152 Note that an additional space indent is interpreted as space
4153 inside the code block:
4154
4155 ```````````````````````````````` example
4156 1. indented code
4157
4158 paragraph
4159
4160 more code
4161 .
4162 <ol>
4163 <li>
4164 <pre><code> indented code
4165 </code></pre>
4166 <p>paragraph</p>
4167 <pre><code>more code
4168 </code></pre>
4169 </li>
4170 </ol>
4171 ````````````````````````````````
4172
4173
4174 Note that rules #1 and #2 only apply to two cases: (a) cases
4175 in which the lines to be included in a list item begin with a
4176 [non-whitespace character], and (b) cases in which
4177 they begin with an indented code
4178 block. In a case like the following, where the first block begins with
4179 a three-space indent, the rules do not allow us to form a list item by
4180 indenting the whole thing and prepending a list marker:
4181
4182 ```````````````````````````````` example
4183 foo
4184
4185 bar
4186 .
4187 <p>foo</p>
4188 <p>bar</p>
4189 ````````````````````````````````
4190
4191
4192 ```````````````````````````````` example
4193 - foo
4194
4195 bar
4196 .
4197 <ul>
4198 <li>foo</li>
4199 </ul>
4200 <p>bar</p>
4201 ````````````````````````````````
4202
4203
4204 This is not a significant restriction, because when a block begins
4205 with 1-3 spaces indent, the indentation can always be removed without
4206 a change in interpretation, allowing rule #1 to be applied. So, in
4207 the above case:
4208
4209 ```````````````````````````````` example
4210 - foo
4211
4212 bar
4213 .
4214 <ul>
4215 <li>
4216 <p>foo</p>
4217 <p>bar</p>
4218 </li>
4219 </ul>
4220 ````````````````````````````````
4221
4222
4223 3. **Item starting with a blank line.** If a sequence of lines *Ls*
4224 starting with a single [blank line] constitute a (possibly empty)
4225 sequence of blocks *Bs*, not separated from each other by more than
4226 one blank line, and *M* is a list marker of width *W*,
4227 then the result of prepending *M* to the first line of *Ls*, and
4228 indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
4229 item with *Bs* as its contents.
4230 If a line is empty, then it need not be indented. The type of the
4231 list item (bullet or ordered) is determined by the type of its list
4232 marker. If the list item is ordered, then it is also assigned a
4233 start number, based on the ordered list marker.
4234
4235 Here are some list items that start with a blank line but are not empty:
4236
4237 ```````````````````````````````` example
4238 -
4239 foo
4240 -
4241 ```
4242 bar
4243 ```
4244 -
4245 baz
4246 .
4247 <ul>
4248 <li>foo</li>
4249 <li>
4250 <pre><code>bar
4251 </code></pre>
4252 </li>
4253 <li>
4254 <pre><code>baz
4255 </code></pre>
4256 </li>
4257 </ul>
4258 ````````````````````````````````
4259
4260 When the list item starts with a blank line, the number of spaces
4261 following the list marker doesn't change the required indentation:
4262
4263 ```````````````````````````````` example
4264 -
4265 foo
4266 .
4267 <ul>
4268 <li>foo</li>
4269 </ul>
4270 ````````````````````````````````
4271
4272
4273 A list item can begin with at most one blank line.
4274 In the following example, `foo` is not part of the list
4275 item:
4276
4277 ```````````````````````````````` example
4278 -
4279
4280 foo
4281 .
4282 <ul>
4283 <li></li>
4284 </ul>
4285 <p>foo</p>
4286 ````````````````````````````````
4287
4288
4289 Here is an empty bullet list item:
4290
4291 ```````````````````````````````` example
4292 - foo
4293 -
4294 - bar
4295 .
4296 <ul>
4297 <li>foo</li>
4298 <li></li>
4299 <li>bar</li>
4300 </ul>
4301 ````````````````````````````````
4302
4303
4304 It does not matter whether there are spaces following the [list marker]:
4305
4306 ```````````````````````````````` example
4307 - foo
4308 -
4309 - bar
4310 .
4311 <ul>
4312 <li>foo</li>
4313 <li></li>
4314 <li>bar</li>
4315 </ul>
4316 ````````````````````````````````
4317
4318
4319 Here is an empty ordered list item:
4320
4321 ```````````````````````````````` example
4322 1. foo
4323 2.
4324 3. bar
4325 .
4326 <ol>
4327 <li>foo</li>
4328 <li></li>
4329 <li>bar</li>
4330 </ol>
4331 ````````````````````````````````
4332
4333
4334 A list may start or end with an empty list item:
4335
4336 ```````````````````````````````` example
4337 *
4338 .
4339 <ul>
4340 <li></li>
4341 </ul>
4342 ````````````````````````````````
4343
4344 However, an empty list item cannot interrupt a paragraph:
4345
4346 ```````````````````````````````` example
4347 foo
4348 *
4349
4350 foo
4351 1.
4352 .
4353 <p>foo
4354 *</p>
4355 <p>foo
4356 1.</p>
4357 ````````````````````````````````
4358
4359
4360 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
4361 according to rule #1, #2, or #3, then the result of indenting each line
4362 of *Ls* by 1-3 spaces (the same for each line) also constitutes a
4363 list item with the same contents and attributes. If a line is
4364 empty, then it need not be indented.
4365
4366 Indented one space:
4367
4368 ```````````````````````````````` example
4369 1. A paragraph
4370 with two lines.
4371
4372 indented code
4373
4374 > A block quote.
4375 .
4376 <ol>
4377 <li>
4378 <p>A paragraph
4379 with two lines.</p>
4380 <pre><code>indented code
4381 </code></pre>
4382 <blockquote>
4383 <p>A block quote.</p>
4384 </blockquote>
4385 </li>
4386 </ol>
4387 ````````````````````````````````
4388
4389
4390 Indented two spaces:
4391
4392 ```````````````````````````````` example
4393 1. A paragraph
4394 with two lines.
4395
4396 indented code
4397
4398 > A block quote.
4399 .
4400 <ol>
4401 <li>
4402 <p>A paragraph
4403 with two lines.</p>
4404 <pre><code>indented code
4405 </code></pre>
4406 <blockquote>
4407 <p>A block quote.</p>
4408 </blockquote>
4409 </li>
4410 </ol>
4411 ````````````````````````````````
4412
4413
4414 Indented three spaces:
4415
4416 ```````````````````````````````` example
4417 1. A paragraph
4418 with two lines.
4419
4420 indented code
4421
4422 > A block quote.
4423 .
4424 <ol>
4425 <li>
4426 <p>A paragraph
4427 with two lines.</p>
4428 <pre><code>indented code
4429 </code></pre>
4430 <blockquote>
4431 <p>A block quote.</p>
4432 </blockquote>
4433 </li>
4434 </ol>
4435 ````````````````````````````````
4436
4437
4438 Four spaces indent gives a code block:
4439
4440 ```````````````````````````````` example
4441 1. A paragraph
4442 with two lines.
4443
4444 indented code
4445
4446 > A block quote.
4447 .
4448 <pre><code>1. A paragraph
4449 with two lines.
4450
4451 indented code
4452
4453 &gt; A block quote.
4454 </code></pre>
4455 ````````````````````````````````
4456
4457
4458
4459 5. **Laziness.** If a string of lines *Ls* constitute a [list
4460 item](#list-items) with contents *Bs*, then the result of deleting
4461 some or all of the indentation from one or more lines in which the
4462 next [non-whitespace character] after the indentation is
4463 [paragraph continuation text] is a
4464 list item with the same contents and attributes. The unindented
4465 lines are called
4466 [lazy continuation line](@)s.
4467
4468 Here is an example with [lazy continuation lines]:
4469
4470 ```````````````````````````````` example
4471 1. A paragraph
4472 with two lines.
4473
4474 indented code
4475
4476 > A block quote.
4477 .
4478 <ol>
4479 <li>
4480 <p>A paragraph
4481 with two lines.</p>
4482 <pre><code>indented code
4483 </code></pre>
4484 <blockquote>
4485 <p>A block quote.</p>
4486 </blockquote>
4487 </li>
4488 </ol>
4489 ````````````````````````````````
4490
4491
4492 Indentation can be partially deleted:
4493
4494 ```````````````````````````````` example
4495 1. A paragraph
4496 with two lines.
4497 .
4498 <ol>
4499 <li>A paragraph
4500 with two lines.</li>
4501 </ol>
4502 ````````````````````````````````
4503
4504
4505 These examples show how laziness can work in nested structures:
4506
4507 ```````````````````````````````` example
4508 > 1. > Blockquote
4509 continued here.
4510 .
4511 <blockquote>
4512 <ol>
4513 <li>
4514 <blockquote>
4515 <p>Blockquote
4516 continued here.</p>
4517 </blockquote>
4518 </li>
4519 </ol>
4520 </blockquote>
4521 ````````````````````````````````
4522
4523
4524 ```````````````````````````````` example
4525 > 1. > Blockquote
4526 > continued here.
4527 .
4528 <blockquote>
4529 <ol>
4530 <li>
4531 <blockquote>
4532 <p>Blockquote
4533 continued here.</p>
4534 </blockquote>
4535 </li>
4536 </ol>
4537 </blockquote>
4538 ````````````````````````````````
4539
4540
4541
4542 6. **That's all.** Nothing that is not counted as a list item by rules
4543 #1--5 counts as a [list item](#list-items).
4544
4545 The rules for sublists follow from the general rules
4546 [above][List items]. A sublist must be indented the same number
4547 of spaces a paragraph would need to be in order to be included
4548 in the list item.
4549
4550 So, in this case we need two spaces indent:
4551
4552 ```````````````````````````````` example
4553 - foo
4554 - bar
4555 - baz
4556 - boo
4557 .
4558 <ul>
4559 <li>foo
4560 <ul>
4561 <li>bar
4562 <ul>
4563 <li>baz
4564 <ul>
4565 <li>boo</li>
4566 </ul>
4567 </li>
4568 </ul>
4569 </li>
4570 </ul>
4571 </li>
4572 </ul>
4573 ````````````````````````````````
4574
4575
4576 One is not enough:
4577
4578 ```````````````````````````````` example
4579 - foo
4580 - bar
4581 - baz
4582 - boo
4583 .
4584 <ul>
4585 <li>foo</li>
4586 <li>bar</li>
4587 <li>baz</li>
4588 <li>boo</li>
4589 </ul>
4590 ````````````````````````````````
4591
4592
4593 Here we need four, because the list marker is wider:
4594
4595 ```````````````````````````````` example
4596 10) foo
4597 - bar
4598 .
4599 <ol start="10">
4600 <li>foo
4601 <ul>
4602 <li>bar</li>
4603 </ul>
4604 </li>
4605 </ol>
4606 ````````````````````````````````
4607
4608
4609 Three is not enough:
4610
4611 ```````````````````````````````` example
4612 10) foo
4613 - bar
4614 .
4615 <ol start="10">
4616 <li>foo</li>
4617 </ol>
4618 <ul>
4619 <li>bar</li>
4620 </ul>
4621 ````````````````````````````````
4622
4623
4624 A list may be the first block in a list item:
4625
4626 ```````````````````````````````` example
4627 - - foo
4628 .
4629 <ul>
4630 <li>
4631 <ul>
4632 <li>foo</li>
4633 </ul>
4634 </li>
4635 </ul>
4636 ````````````````````````````````
4637
4638
4639 ```````````````````````````````` example
4640 1. - 2. foo
4641 .
4642 <ol>
4643 <li>
4644 <ul>
4645 <li>
4646 <ol start="2">
4647 <li>foo</li>
4648 </ol>
4649 </li>
4650 </ul>
4651 </li>
4652 </ol>
4653 ````````````````````````````````
4654
4655
4656 A list item can contain a heading:
4657
4658 ```````````````````````````````` example
4659 - # Foo
4660 - Bar
4661 ---
4662 baz
4663 .
4664 <ul>
4665 <li>
4666 <h1>Foo</h1>
4667 </li>
4668 <li>
4669 <h2>Bar</h2>
4670 baz</li>
4671 </ul>
4672 ````````````````````````````````
4673
4674
4675 ### Motivation
4676
4677 John Gruber's Markdown spec says the following about list items:
4678
4679 1. "List markers typically start at the left margin, but may be indented
4680 by up to three spaces. List markers must be followed by one or more
4681 spaces or a tab."
4682
4683 2. "To make lists look nice, you can wrap items with hanging indents....
4684 But if you don't want to, you don't have to."
4685
4686 3. "List items may consist of multiple paragraphs. Each subsequent
4687 paragraph in a list item must be indented by either 4 spaces or one
4688 tab."
4689
4690 4. "It looks nice if you indent every line of the subsequent paragraphs,
4691 but here again, Markdown will allow you to be lazy."
4692
4693 5. "To put a blockquote within a list item, the blockquote's `>`
4694 delimiters need to be indented."
4695
4696 6. "To put a code block within a list item, the code block needs to be
4697 indented twice — 8 spaces or two tabs."
4698
4699 These rules specify that a paragraph under a list item must be indented
4700 four spaces (presumably, from the left margin, rather than the start of
4701 the list marker, but this is not said), and that code under a list item
4702 must be indented eight spaces instead of the usual four. They also say
4703 that a block quote must be indented, but not by how much; however, the
4704 example given has four spaces indentation. Although nothing is said
4705 about other kinds of block-level content, it is certainly reasonable to
4706 infer that *all* block elements under a list item, including other
4707 lists, must be indented four spaces. This principle has been called the
4708 *four-space rule*.
4709
4710 The four-space rule is clear and principled, and if the reference
4711 implementation `Markdown.pl` had followed it, it probably would have
4712 become the standard. However, `Markdown.pl` allowed paragraphs and
4713 sublists to start with only two spaces indentation, at least on the
4714 outer level. Worse, its behavior was inconsistent: a sublist of an
4715 outer-level list needed two spaces indentation, but a sublist of this
4716 sublist needed three spaces. It is not surprising, then, that different
4717 implementations of Markdown have developed very different rules for
4718 determining what comes under a list item. (Pandoc and python-Markdown,
4719 for example, stuck with Gruber's syntax description and the four-space
4720 rule, while discount, redcarpet, marked, PHP Markdown, and others
4721 followed `Markdown.pl`'s behavior more closely.)
4722
4723 Unfortunately, given the divergences between implementations, there
4724 is no way to give a spec for list items that will be guaranteed not
4725 to break any existing documents. However, the spec given here should
4726 correctly handle lists formatted with either the four-space rule or
4727 the more forgiving `Markdown.pl` behavior, provided they are laid out
4728 in a way that is natural for a human to read.
4729
4730 The strategy here is to let the width and indentation of the list marker
4731 determine the indentation necessary for blocks to fall under the list
4732 item, rather than having a fixed and arbitrary number. The writer can
4733 think of the body of the list item as a unit which gets indented to the
4734 right enough to fit the list marker (and any indentation on the list
4735 marker). (The laziness rule, #5, then allows continuation lines to be
4736 unindented if needed.)
4737
4738 This rule is superior, we claim, to any rule requiring a fixed level of
4739 indentation from the margin. The four-space rule is clear but
4740 unnatural. It is quite unintuitive that
4741
4742 ``` markdown
4743 - foo
4744
4745 bar
4746
4747 - baz
4748 ```
4749
4750 should be parsed as two lists with an intervening paragraph,
4751
4752 ``` html
4753 <ul>
4754 <li>foo</li>
4755 </ul>
4756 <p>bar</p>
4757 <ul>
4758 <li>baz</li>
4759 </ul>
4760 ```
4761
4762 as the four-space rule demands, rather than a single list,
4763
4764 ``` html
4765 <ul>
4766 <li>
4767 <p>foo</p>
4768 <p>bar</p>
4769 <ul>
4770 <li>baz</li>
4771 </ul>
4772 </li>
4773 </ul>
4774 ```
4775
4776 The choice of four spaces is arbitrary. It can be learned, but it is
4777 not likely to be guessed, and it trips up beginners regularly.
4778
4779 Would it help to adopt a two-space rule? The problem is that such
4780 a rule, together with the rule allowing 1--3 spaces indentation of the
4781 initial list marker, allows text that is indented *less than* the
4782 original list marker to be included in the list item. For example,
4783 `Markdown.pl` parses
4784
4785 ``` markdown
4786 - one
4787
4788 two
4789 ```
4790
4791 as a single list item, with `two` a continuation paragraph:
4792
4793 ``` html
4794 <ul>
4795 <li>
4796 <p>one</p>
4797 <p>two</p>
4798 </li>
4799 </ul>
4800 ```
4801
4802 and similarly
4803
4804 ``` markdown
4805 > - one
4806 >
4807 > two
4808 ```
4809
4810 as
4811
4812 ``` html
4813 <blockquote>
4814 <ul>
4815 <li>
4816 <p>one</p>
4817 <p>two</p>
4818 </li>
4819 </ul>
4820 </blockquote>
4821 ```
4822
4823 This is extremely unintuitive.
4824
4825 Rather than requiring a fixed indent from the margin, we could require
4826 a fixed indent (say, two spaces, or even one space) from the list marker (which
4827 may itself be indented). This proposal would remove the last anomaly
4828 discussed. Unlike the spec presented above, it would count the following
4829 as a list item with a subparagraph, even though the paragraph `bar`
4830 is not indented as far as the first paragraph `foo`:
4831
4832 ``` markdown
4833 10. foo
4834
4835 bar
4836 ```
4837
4838 Arguably this text does read like a list item with `bar` as a subparagraph,
4839 which may count in favor of the proposal. However, on this proposal indented
4840 code would have to be indented six spaces after the list marker. And this
4841 would break a lot of existing Markdown, which has the pattern:
4842
4843 ``` markdown
4844 1. foo
4845
4846 indented code
4847 ```
4848
4849 where the code is indented eight spaces. The spec above, by contrast, will
4850 parse this text as expected, since the code block's indentation is measured
4851 from the beginning of `foo`.
4852
4853 The one case that needs special treatment is a list item that *starts*
4854 with indented code. How much indentation is required in that case, since
4855 we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
4856 that in such cases, we require one space indentation from the list marker
4857 (and then the normal four spaces for the indented code). This will match the
4858 four-space rule in cases where the list marker plus its initial indentation
4859 takes four spaces (a common case), but diverge in other cases.
4860
4861 ## Lists
4862
4863 A [list](@) is a sequence of one or more
4864 list items [of the same type]. The list items
4865 may be separated by any number of blank lines.
4866
4867 Two list items are [of the same type](@)
4868 if they begin with a [list marker] of the same type.
4869 Two list markers are of the
4870 same type if (a) they are bullet list markers using the same character
4871 (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
4872 delimiter (either `.` or `)`).
4873
4874 A list is an [ordered list](@)
4875 if its constituent list items begin with
4876 [ordered list markers], and a
4877 [bullet list](@) if its constituent list
4878 items begin with [bullet list markers].
4879
4880 The [start number](@)
4881 of an [ordered list] is determined by the list number of
4882 its initial list item. The numbers of subsequent list items are
4883 disregarded.
4884
4885 A list is [loose](@) if any of its constituent
4886 list items are separated by blank lines, or if any of its constituent
4887 list items directly contain two block-level elements with a blank line
4888 between them. Otherwise a list is [tight](@).
4889 (The difference in HTML output is that paragraphs in a loose list are
4890 wrapped in `<p>` tags, while paragraphs in a tight list are not.)
4891
4892 Changing the bullet or ordered list delimiter starts a new list:
4893
4894 ```````````````````````````````` example
4895 - foo
4896 - bar
4897 + baz
4898 .
4899 <ul>
4900 <li>foo</li>
4901 <li>bar</li>
4902 </ul>
4903 <ul>
4904 <li>baz</li>
4905 </ul>
4906 ````````````````````````````````
4907
4908
4909 ```````````````````````````````` example
4910 1. foo
4911 2. bar
4912 3) baz
4913 .
4914 <ol>
4915 <li>foo</li>
4916 <li>bar</li>
4917 </ol>
4918 <ol start="3">
4919 <li>baz</li>
4920 </ol>
4921 ````````````````````````````````
4922
4923
4924 In CommonMark, a list can interrupt a paragraph. That is,
4925 no blank line is needed to separate a paragraph from a following
4926 list:
4927
4928 ```````````````````````````````` example
4929 Foo
4930 - bar
4931 - baz
4932 .
4933 <p>Foo</p>
4934 <ul>
4935 <li>bar</li>
4936 <li>baz</li>
4937 </ul>
4938 ````````````````````````````````
4939
4940 `Markdown.pl` does not allow this, through fear of triggering a list
4941 via a numeral in a hard-wrapped line:
4942
4943 ``` markdown
4944 The number of windows in my house is
4945 14. The number of doors is 6.
4946 ```
4947
4948 Oddly, though, `Markdown.pl` *does* allow a blockquote to
4949 interrupt a paragraph, even though the same considerations might
4950 apply.
4951
4952 In CommonMark, we do allow lists to interrupt paragraphs, for
4953 two reasons. First, it is natural and not uncommon for people
4954 to start lists without blank lines:
4955
4956 ``` markdown
4957 I need to buy
4958 - new shoes
4959 - a coat
4960 - a plane ticket
4961 ```
4962
4963 Second, we are attracted to a
4964
4965 > [principle of uniformity](@):
4966 > if a chunk of text has a certain
4967 > meaning, it will continue to have the same meaning when put into a
4968 > container block (such as a list item or blockquote).
4969
4970 (Indeed, the spec for [list items] and [block quotes] presupposes
4971 this principle.) This principle implies that if
4972
4973 ``` markdown
4974 * I need to buy
4975 - new shoes
4976 - a coat
4977 - a plane ticket
4978 ```
4979
4980 is a list item containing a paragraph followed by a nested sublist,
4981 as all Markdown implementations agree it is (though the paragraph
4982 may be rendered without `<p>` tags, since the list is "tight"),
4983 then
4984
4985 ``` markdown
4986 I need to buy
4987 - new shoes
4988 - a coat
4989 - a plane ticket
4990 ```
4991
4992 by itself should be a paragraph followed by a nested sublist.
4993
4994 Since it is well established Markdown practice to allow lists to
4995 interrupt paragraphs inside list items, the [principle of
4996 uniformity] requires us to allow this outside list items as
4997 well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
4998 takes a different approach, requiring blank lines before lists
4999 even inside other list items.)
5000
5001 In order to solve of unwanted lists in paragraphs with
5002 hard-wrapped numerals, we allow only lists starting with `1` to
5003 interrupt paragraphs. Thus,
5004
5005 ```````````````````````````````` example
5006 The number of windows in my house is
5007 14. The number of doors is 6.
5008 .
5009 <p>The number of windows in my house is
5010 14. The number of doors is 6.</p>
5011 ````````````````````````````````
5012
5013 We may still get an unintended result in cases like
5014
5015 ```````````````````````````````` example
5016 The number of windows in my house is
5017 1. The number of doors is 6.
5018 .
5019 <p>The number of windows in my house is</p>
5020 <ol>
5021 <li>The number of doors is 6.</li>
5022 </ol>
5023 ````````````````````````````````
5024
5025 but this rule should prevent most spurious list captures.
5026
5027 There can be any number of blank lines between items:
5028
5029 ```````````````````````````````` example
5030 - foo
5031
5032 - bar
5033
5034
5035 - baz
5036 .
5037 <ul>
5038 <li>
5039 <p>foo</p>
5040 </li>
5041 <li>
5042 <p>bar</p>
5043 </li>
5044 <li>
5045 <p>baz</p>
5046 </li>
5047 </ul>
5048 ````````````````````````````````
5049
5050 ```````````````````````````````` example
5051 - foo
5052 - bar
5053 - baz
5054
5055
5056 bim
5057 .
5058 <ul>
5059 <li>foo
5060 <ul>
5061 <li>bar
5062 <ul>
5063 <li>
5064 <p>baz</p>
5065 <p>bim</p>
5066 </li>
5067 </ul>
5068 </li>
5069 </ul>
5070 </li>
5071 </ul>
5072 ````````````````````````````````
5073
5074
5075 To separate consecutive lists of the same type, or to separate a
5076 list from an indented code block that would otherwise be parsed
5077 as a subparagraph of the final list item, you can insert a blank HTML
5078 comment:
5079
5080 ```````````````````````````````` example
5081 - foo
5082 - bar
5083
5084 <!-- -->
5085
5086 - baz
5087 - bim
5088 .
5089 <ul>
5090 <li>foo</li>
5091 <li>bar</li>
5092 </ul>
5093 <!-- -->
5094 <ul>
5095 <li>baz</li>
5096 <li>bim</li>
5097 </ul>
5098 ````````````````````````````````
5099
5100
5101 ```````````````````````````````` example
5102 - foo
5103
5104 notcode
5105
5106 - foo
5107
5108 <!-- -->
5109
5110 code
5111 .
5112 <ul>
5113 <li>
5114 <p>foo</p>
5115 <p>notcode</p>
5116 </li>
5117 <li>
5118 <p>foo</p>
5119 </li>
5120 </ul>
5121 <!-- -->
5122 <pre><code>code
5123 </code></pre>
5124 ````````````````````````````````
5125
5126
5127 List items need not be indented to the same level. The following
5128 list items will be treated as items at the same list level,
5129 since none is indented enough to belong to the previous list
5130 item:
5131
5132 ```````````````````````````````` example
5133 - a
5134 - b
5135 - c
5136 - d
5137 - e
5138 - f
5139 - g
5140 .
5141 <ul>
5142 <li>a</li>
5143 <li>b</li>
5144 <li>c</li>
5145 <li>d</li>
5146 <li>e</li>
5147 <li>f</li>
5148 <li>g</li>
5149 </ul>
5150 ````````````````````````````````
5151
5152
5153 ```````````````````````````````` example
5154 1. a
5155
5156 2. b
5157
5158 3. c
5159 .
5160 <ol>
5161 <li>
5162 <p>a</p>
5163 </li>
5164 <li>
5165 <p>b</p>
5166 </li>
5167 <li>
5168 <p>c</p>
5169 </li>
5170 </ol>
5171 ````````````````````````````````
5172
5173 Note, however, that list items may not be indented more than
5174 three spaces. Here `- e` is treated as a paragraph continuation
5175 line, because it is indented more than three spaces:
5176
5177 ```````````````````````````````` example
5178 - a
5179 - b
5180 - c
5181 - d
5182 - e
5183 .
5184 <ul>
5185 <li>a</li>
5186 <li>b</li>
5187 <li>c</li>
5188 <li>d
5189 - e</li>
5190 </ul>
5191 ````````````````````````````````
5192
5193 And here, `3. c` is treated as in indented code block,
5194 because it is indented four spaces and preceded by a
5195 blank line.
5196
5197 ```````````````````````````````` example
5198 1. a
5199
5200 2. b
5201
5202 3. c
5203 .
5204 <ol>
5205 <li>
5206 <p>a</p>
5207 </li>
5208 <li>
5209 <p>b</p>
5210 </li>
5211 </ol>
5212 <pre><code>3. c
5213 </code></pre>
5214 ````````````````````````````````
5215
5216
5217 This is a loose list, because there is a blank line between
5218 two of the list items:
5219
5220 ```````````````````````````````` example
5221 - a
5222 - b
5223
5224 - c
5225 .
5226 <ul>
5227 <li>
5228 <p>a</p>
5229 </li>
5230 <li>
5231 <p>b</p>
5232 </li>
5233 <li>
5234 <p>c</p>
5235 </li>
5236 </ul>
5237 ````````````````````````````````
5238
5239
5240 So is this, with a empty second item:
5241
5242 ```````````````````````````````` example
5243 * a
5244 *
5245
5246 * c
5247 .
5248 <ul>
5249 <li>
5250 <p>a</p>
5251 </li>
5252 <li></li>
5253 <li>
5254 <p>c</p>
5255 </li>
5256 </ul>
5257 ````````````````````````````````
5258
5259
5260 These are loose lists, even though there is no space between the items,
5261 because one of the items directly contains two block-level elements
5262 with a blank line between them:
5263
5264 ```````````````````````````````` example
5265 - a
5266 - b
5267
5268 c
5269 - d
5270 .
5271 <ul>
5272 <li>
5273 <p>a</p>
5274 </li>
5275 <li>
5276 <p>b</p>
5277 <p>c</p>
5278 </li>
5279 <li>
5280 <p>d</p>
5281 </li>
5282 </ul>
5283 ````````````````````````````````
5284
5285
5286 ```````````````````````````````` example
5287 - a
5288 - b
5289
5290 [ref]: /url
5291 - d
5292 .
5293 <ul>
5294 <li>
5295 <p>a</p>
5296 </li>
5297 <li>
5298 <p>b</p>
5299 </li>
5300 <li>
5301 <p>d</p>
5302 </li>
5303 </ul>
5304 ````````````````````````````````
5305
5306
5307 This is a tight list, because the blank lines are in a code block:
5308
5309 ```````````````````````````````` example
5310 - a
5311 - ```
5312 b
5313
5314
5315 ```
5316 - c
5317 .
5318 <ul>
5319 <li>a</li>
5320 <li>
5321 <pre><code>b
5322
5323
5324 </code></pre>
5325 </li>
5326 <li>c</li>
5327 </ul>
5328 ````````````````````````````````
5329
5330
5331 This is a tight list, because the blank line is between two
5332 paragraphs of a sublist. So the sublist is loose while
5333 the outer list is tight:
5334
5335 ```````````````````````````````` example
5336 - a
5337 - b
5338
5339 c
5340 - d
5341 .
5342 <ul>
5343 <li>a
5344 <ul>
5345 <li>
5346 <p>b</p>
5347 <p>c</p>
5348 </li>
5349 </ul>
5350 </li>
5351 <li>d</li>
5352 </ul>
5353 ````````````````````````````````
5354
5355
5356 This is a tight list, because the blank line is inside the
5357 block quote:
5358
5359 ```````````````````````````````` example
5360 * a
5361 > b
5362 >
5363 * c
5364 .
5365 <ul>
5366 <li>a
5367 <blockquote>
5368 <p>b</p>
5369 </blockquote>
5370 </li>
5371 <li>c</li>
5372 </ul>
5373 ````````````````````````````````
5374
5375
5376 This list is tight, because the consecutive block elements
5377 are not separated by blank lines:
5378
5379 ```````````````````````````````` example
5380 - a
5381 > b
5382 ```
5383 c
5384 ```
5385 - d
5386 .
5387 <ul>
5388 <li>a
5389 <blockquote>
5390 <p>b</p>
5391 </blockquote>
5392 <pre><code>c
5393 </code></pre>
5394 </li>
5395 <li>d</li>
5396 </ul>
5397 ````````````````````````````````
5398
5399
5400 A single-paragraph list is tight:
5401
5402 ```````````````````````````````` example
5403 - a
5404 .
5405 <ul>
5406 <li>a</li>
5407 </ul>
5408 ````````````````````````````````
5409
5410
5411 ```````````````````````````````` example
5412 - a
5413 - b
5414 .
5415 <ul>
5416 <li>a
5417 <ul>
5418 <li>b</li>
5419 </ul>
5420 </li>
5421 </ul>
5422 ````````````````````````````````
5423
5424
5425 This list is loose, because of the blank line between the
5426 two block elements in the list item:
5427
5428 ```````````````````````````````` example
5429 1. ```
5430 foo
5431 ```
5432
5433 bar
5434 .
5435 <ol>
5436 <li>
5437 <pre><code>foo
5438 </code></pre>
5439 <p>bar</p>
5440 </li>
5441 </ol>
5442 ````````````````````````````````
5443
5444
5445 Here the outer list is loose, the inner list tight:
5446
5447 ```````````````````````````````` example
5448 * foo
5449 * bar
5450
5451 baz
5452 .
5453 <ul>
5454 <li>
5455 <p>foo</p>
5456 <ul>
5457 <li>bar</li>
5458 </ul>
5459 <p>baz</p>
5460 </li>
5461 </ul>
5462 ````````````````````````````````
5463
5464
5465 ```````````````````````````````` example
5466 - a
5467 - b
5468 - c
5469
5470 - d
5471 - e
5472 - f
5473 .
5474 <ul>
5475 <li>
5476 <p>a</p>
5477 <ul>
5478 <li>b</li>
5479 <li>c</li>
5480 </ul>
5481 </li>
5482 <li>
5483 <p>d</p>
5484 <ul>
5485 <li>e</li>
5486 <li>f</li>
5487 </ul>
5488 </li>
5489 </ul>
5490 ````````````````````````````````
5491
5492
5493 # Inlines
5494
5495 Inlines are parsed sequentially from the beginning of the character
5496 stream to the end (left to right, in left-to-right languages).
5497 Thus, for example, in
5498
5499 ```````````````````````````````` example
5500 `hi`lo`
5501 .
5502 <p><code>hi</code>lo`</p>
5503 ````````````````````````````````
5504
5505 `hi` is parsed as code, leaving the backtick at the end as a literal
5506 backtick.
5507
5508
5509 ## Backslash escapes
5510
5511 Any ASCII punctuation character may be backslash-escaped:
5512
5513 ```````````````````````````````` example
5514 \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
5515 .
5516 <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
5517 ````````````````````````````````
5518
5519
5520 Backslashes before other characters are treated as literal
5521 backslashes:
5522
5523 ```````````````````````````````` example
5524 \→\A\a\ \3\φ\«
5525 .
5526 <p>\→\A\a\ \3\φ\«</p>
5527 ````````````````````````````````
5528
5529
5530 Escaped characters are treated as regular characters and do
5531 not have their usual Markdown meanings:
5532
5533 ```````````````````````````````` example
5534 \*not emphasized*
5535 \<br/> not a tag
5536 \[not a link](/foo)
5537 \`not code`
5538 1\. not a list
5539 \* not a list
5540 \# not a heading
5541 \[foo]: /url "not a reference"
5542 \&ouml; not a character entity
5543 .
5544 <p>*not emphasized*
5545 &lt;br/&gt; not a tag
5546 [not a link](/foo)
5547 `not code`
5548 1. not a list
5549 * not a list
5550 # not a heading
5551 [foo]: /url &quot;not a reference&quot;
5552 &amp;ouml; not a character entity</p>
5553 ````````````````````````````````
5554
5555
5556 If a backslash is itself escaped, the following character is not:
5557
5558 ```````````````````````````````` example
5559 \\*emphasis*
5560 .
5561 <p>\<em>emphasis</em></p>
5562 ````````````````````````````````
5563
5564
5565 A backslash at the end of the line is a [hard line break]:
5566
5567 ```````````````````````````````` example
5568 foo\
5569 bar
5570 .
5571 <p>foo<br />
5572 bar</p>
5573 ````````````````````````````````
5574
5575
5576 Backslash escapes do not work in code blocks, code spans, autolinks, or
5577 raw HTML:
5578
5579 ```````````````````````````````` example
5580 `` \[\` ``
5581 .
5582 <p><code>\[\`</code></p>
5583 ````````````````````````````````
5584
5585
5586 ```````````````````````````````` example
5587 \[\]
5588 .
5589 <pre><code>\[\]
5590 </code></pre>
5591 ````````````````````````````````
5592
5593
5594 ```````````````````````````````` example
5595 ~~~
5596 \[\]
5597 ~~~
5598 .
5599 <pre><code>\[\]
5600 </code></pre>
5601 ````````````````````````````````
5602
5603
5604 ```````````````````````````````` example
5605 <http://example.com?find=\*>
5606 .
5607 <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
5608 ````````````````````````````````
5609
5610
5611 ```````````````````````````````` example
5612 <a href="/bar\/)">
5613 .
5614 <a href="/bar\/)">
5615 ````````````````````````````````
5616
5617
5618 But they work in all other contexts, including URLs and link titles,
5619 link references, and [info strings] in [fenced code blocks]:
5620
5621 ```````````````````````````````` example
5622 [foo](/bar\* "ti\*tle")
5623 .
5624 <p><a href="/bar*" title="ti*tle">foo</a></p>
5625 ````````````````````````````````
5626
5627
5628 ```````````````````````````````` example
5629 [foo]
5630
5631 [foo]: /bar\* "ti\*tle"
5632 .
5633 <p><a href="/bar*" title="ti*tle">foo</a></p>
5634 ````````````````````````````````
5635
5636
5637 ```````````````````````````````` example
5638 ``` foo\+bar
5639 foo
5640 ```
5641 .
5642 <pre><code class="language-foo+bar">foo
5643 </code></pre>
5644 ````````````````````````````````
5645
5646
5647
5648 ## Entity and numeric character references
5649
5650 Valid HTML entity references and numeric character references
5651 can be used in place of the corresponding Unicode character,
5652 with the following exceptions:
5653
5654 - Entity and character references are not recognized in code
5655 blocks and code spans.
5656
5657 - Entity and character references cannot stand in place of
5658 special characters that define structural elements in
5659 CommonMark. For example, although `&#42;` can be used
5660 in place of a literal `*` character, `&#42;` cannot replace
5661 `*` in emphasis delimiters, bullet list markers, or thematic
5662 breaks.
5663
5664 Conforming CommonMark parsers need not store information about
5665 whether a particular character was represented in the source
5666 using a Unicode character or an entity reference.
5667
5668 [Entity references](@) consist of `&` + any of the valid
5669 HTML5 entity names + `;`. The
5670 document <https://html.spec.whatwg.org/multipage/entities.json>
5671 is used as an authoritative source for the valid entity
5672 references and their corresponding code points.
5673
5674 ```````````````````````````````` example
5675 &nbsp; &amp; &copy; &AElig; &Dcaron;
5676 &frac34; &HilbertSpace; &DifferentialD;
5677 &ClockwiseContourIntegral; &ngE;
5678 .
5679 <p>  &amp; © Æ Ď
5680 ¾ ℋ ⅆ
5681 ∲ ≧̸</p>
5682 ````````````````````````````````
5683
5684
5685 [Decimal numeric character
5686 references](@)
5687 consist of `&#` + a string of 1--7 arabic digits + `;`. A
5688 numeric character reference is parsed as the corresponding
5689 Unicode character. Invalid Unicode code points will be replaced by
5690 the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
5691 the code point `U+0000` will also be replaced by `U+FFFD`.
5692
5693 ```````````````````````````````` example
5694 &#35; &#1234; &#992; &#0;
5695 .
5696 <p># Ӓ Ϡ �</p>
5697 ````````````````````````````````
5698
5699
5700 [Hexadecimal numeric character
5701 references](@) consist of `&#` +
5702 either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
5703 They too are parsed as the corresponding Unicode character (this
5704 time specified with a hexadecimal numeral instead of decimal).
5705
5706 ```````````````````````````````` example
5707 &#X22; &#XD06; &#xcab;
5708 .
5709 <p>&quot; ആ ಫ</p>
5710 ````````````````````````````````
5711
5712
5713 Here are some nonentities:
5714
5715 ```````````````````````````````` example
5716 &nbsp &x; &#; &#x;
5717 &#987654321;
5718 &#abcdef0;
5719 &ThisIsNotDefined; &hi?;
5720 .
5721 <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
5722 &amp;#987654321;
5723 &amp;#abcdef0;
5724 &amp;ThisIsNotDefined; &amp;hi?;</p>
5725 ````````````````````````````````
5726
5727
5728 Although HTML5 does accept some entity references
5729 without a trailing semicolon (such as `&copy`), these are not
5730 recognized here, because it makes the grammar too ambiguous:
5731
5732 ```````````````````````````````` example
5733 &copy
5734 .
5735 <p>&amp;copy</p>
5736 ````````````````````````````````
5737
5738
5739 Strings that are not on the list of HTML5 named entities are not
5740 recognized as entity references either:
5741
5742 ```````````````````````````````` example
5743 &MadeUpEntity;
5744 .
5745 <p>&amp;MadeUpEntity;</p>
5746 ````````````````````````````````
5747
5748
5749 Entity and numeric character references are recognized in any
5750 context besides code spans or code blocks, including
5751 URLs, [link titles], and [fenced code block][] [info strings]:
5752
5753 ```````````````````````````````` example
5754 <a href="&ouml;&ouml;.html">
5755 .
5756 <a href="&ouml;&ouml;.html">
5757 ````````````````````````````````
5758
5759
5760 ```````````````````````````````` example
5761 [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
5762 .
5763 <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
5764 ````````````````````````````````
5765
5766
5767 ```````````````````````````````` example
5768 [foo]
5769
5770 [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
5771 .
5772 <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
5773 ````````````````````````````````
5774
5775
5776 ```````````````````````````````` example
5777 ``` f&ouml;&ouml;
5778 foo
5779 ```
5780 .
5781 <pre><code class="language-föö">foo
5782 </code></pre>
5783 ````````````````````````````````
5784
5785
5786 Entity and numeric character references are treated as literal
5787 text in code spans and code blocks:
5788
5789 ```````````````````````````````` example
5790 `f&ouml;&ouml;`
5791 .
5792 <p><code>f&amp;ouml;&amp;ouml;</code></p>
5793 ````````````````````````````````
5794
5795
5796 ```````````````````````````````` example
5797 f&ouml;f&ouml;
5798 .
5799 <pre><code>f&amp;ouml;f&amp;ouml;
5800 </code></pre>
5801 ````````````````````````````````
5802
5803
5804 Entity and numeric character references cannot be used
5805 in place of symbols indicating structure in CommonMark
5806 documents.
5807
5808 ```````````````````````````````` example
5809 &#42;foo&#42;
5810 *foo*
5811 .
5812 <p>*foo*
5813 <em>foo</em></p>
5814 ````````````````````````````````
5815
5816 ```````````````````````````````` example
5817 &#42; foo
5818
5819 * foo
5820 .
5821 <p>* foo</p>
5822 <ul>
5823 <li>foo</li>
5824 </ul>
5825 ````````````````````````````````
5826
5827 ```````````````````````````````` example
5828 foo&#10;&#10;bar
5829 .
5830 <p>foo
5831
5832 bar</p>
5833 ````````````````````````````````
5834
5835 ```````````````````````````````` example
5836 &#9;foo
5837 .
5838 <p>→foo</p>
5839 ````````````````````````````````
5840
5841
5842 ```````````````````````````````` example
5843 [a](url &quot;tit&quot;)
5844 .
5845 <p>[a](url &quot;tit&quot;)</p>
5846 ````````````````````````````````
5847
5848
5849 ## Code spans
5850
5851 A [backtick string](@)
5852 is a string of one or more backtick characters (`` ` ``) that is neither
5853 preceded nor followed by a backtick.
5854
5855 A [code span](@) begins with a backtick string and ends with
5856 a backtick string of equal length. The contents of the code span are
5857 the characters between the two backtick strings, normalized in the
5858 following ways:
5859
5860 - First, [line endings] are converted to [spaces].
5861 - If the resulting string both begins *and* ends with a [space]
5862 character, but does not consist entirely of [space]
5863 characters, a single [space] character is removed from the
5864 front and back. This allows you to include code that begins
5865 or ends with backtick characters, which must be separated by
5866 whitespace from the opening or closing backtick strings.
5867
5868 This is a simple code span:
5869
5870 ```````````````````````````````` example
5871 `foo`
5872 .
5873 <p><code>foo</code></p>
5874 ````````````````````````````````
5875
5876
5877 Here two backticks are used, because the code contains a backtick.
5878 This example also illustrates stripping of a single leading and
5879 trailing space:
5880
5881 ```````````````````````````````` example
5882 `` foo ` bar ``
5883 .
5884 <p><code>foo ` bar</code></p>
5885 ````````````````````````````````
5886
5887
5888 This example shows the motivation for stripping leading and trailing
5889 spaces:
5890
5891 ```````````````````````````````` example
5892 ` `` `
5893 .
5894 <p><code>``</code></p>
5895 ````````````````````````````````
5896
5897 Note that only *one* space is stripped:
5898
5899 ```````````````````````````````` example
5900 ` `` `
5901 .
5902 <p><code> `` </code></p>
5903 ````````````````````````````````
5904
5905 The stripping only happens if the space is on both
5906 sides of the string:
5907
5908 ```````````````````````````````` example
5909 ` a`
5910 .
5911 <p><code> a</code></p>
5912 ````````````````````````````````
5913
5914 Only [spaces], and not [unicode whitespace] in general, are
5915 stripped in this way:
5916
5917 ```````````````````````````````` example
5918 ` b `
5919 .
5920 <p><code> b </code></p>
5921 ````````````````````````````````
5922
5923 No stripping occurs if the code span contains only spaces:
5924
5925 ```````````````````````````````` example
5926 ` `
5927 ` `
5928 .
5929 <p><code> </code>
5930 <code> </code></p>
5931 ````````````````````````````````
5932
5933
5934 [Line endings] are treated like spaces:
5935
5936 ```````````````````````````````` example
5937 ``
5938 foo
5939 bar
5940 baz
5941 ``
5942 .
5943 <p><code>foo bar baz</code></p>
5944 ````````````````````````````````
5945
5946 ```````````````````````````````` example
5947 ``
5948 foo
5949 ``
5950 .
5951 <p><code>foo </code></p>
5952 ````````````````````````````````
5953
5954
5955 Interior spaces are not collapsed:
5956
5957 ```````````````````````````````` example
5958 `foo bar
5959 baz`
5960 .
5961 <p><code>foo bar baz</code></p>
5962 ````````````````````````````````
5963
5964 Note that browsers will typically collapse consecutive spaces
5965 when rendering `<code>` elements, so it is recommended that
5966 the following CSS be used:
5967
5968 code{white-space: pre-wrap;}
5969
5970
5971 Note that backslash escapes do not work in code spans. All backslashes
5972 are treated literally:
5973
5974 ```````````````````````````````` example
5975 `foo\`bar`
5976 .
5977 <p><code>foo\</code>bar`</p>
5978 ````````````````````````````````
5979
5980
5981 Backslash escapes are never needed, because one can always choose a
5982 string of *n* backtick characters as delimiters, where the code does
5983 not contain any strings of exactly *n* backtick characters.
5984
5985 ```````````````````````````````` example
5986 ``foo`bar``
5987 .
5988 <p><code>foo`bar</code></p>
5989 ````````````````````````````````
5990
5991 ```````````````````````````````` example
5992 ` foo `` bar `
5993 .
5994 <p><code>foo `` bar</code></p>
5995 ````````````````````````````````
5996
5997
5998 Code span backticks have higher precedence than any other inline
5999 constructs except HTML tags and autolinks. Thus, for example, this is
6000 not parsed as emphasized text, since the second `*` is part of a code
6001 span:
6002
6003 ```````````````````````````````` example
6004 *foo`*`
6005 .
6006 <p>*foo<code>*</code></p>
6007 ````````````````````````````````
6008
6009
6010 And this is not parsed as a link:
6011
6012 ```````````````````````````````` example
6013 [not a `link](/foo`)
6014 .
6015 <p>[not a <code>link](/foo</code>)</p>
6016 ````````````````````````````````
6017
6018
6019 Code spans, HTML tags, and autolinks have the same precedence.
6020 Thus, this is code:
6021
6022 ```````````````````````````````` example
6023 `<a href="`">`
6024 .
6025 <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
6026 ````````````````````````````````
6027
6028
6029 But this is an HTML tag:
6030
6031 ```````````````````````````````` example
6032 <a href="`">`
6033 .
6034 <p><a href="`">`</p>
6035 ````````````````````````````````
6036
6037
6038 And this is code:
6039
6040 ```````````````````````````````` example
6041 `<http://foo.bar.`baz>`
6042 .
6043 <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
6044 ````````````````````````````````
6045
6046
6047 But this is an autolink:
6048
6049 ```````````````````````````````` example
6050 <http://foo.bar.`baz>`
6051 .
6052 <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
6053 ````````````````````````````````
6054
6055
6056 When a backtick string is not closed by a matching backtick string,
6057 we just have literal backticks:
6058
6059 ```````````````````````````````` example
6060 ```foo``
6061 .
6062 <p>```foo``</p>
6063 ````````````````````````````````
6064
6065
6066 ```````````````````````````````` example
6067 `foo
6068 .
6069 <p>`foo</p>
6070 ````````````````````````````````
6071
6072 The following case also illustrates the need for opening and
6073 closing backtick strings to be equal in length:
6074
6075 ```````````````````````````````` example
6076 `foo``bar``
6077 .
6078 <p>`foo<code>bar</code></p>
6079 ````````````````````````````````
6080
6081
6082 ## Emphasis and strong emphasis
6083
6084 John Gruber's original [Markdown syntax
6085 description](http://daringfireball.net/projects/markdown/syntax#em) says:
6086
6087 > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
6088 > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
6089 > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
6090 > tag.
6091
6092 This is enough for most users, but these rules leave much undecided,
6093 especially when it comes to nested emphasis. The original
6094 `Markdown.pl` test suite makes it clear that triple `***` and
6095 `___` delimiters can be used for strong emphasis, and most
6096 implementations have also allowed the following patterns:
6097
6098 ``` markdown
6099 ***strong emph***
6100 ***strong** in emph*
6101 ***emph* in strong**
6102 **in strong *emph***
6103 *in emph **strong***
6104 ```
6105
6106 The following patterns are less widely supported, but the intent
6107 is clear and they are useful (especially in contexts like bibliography
6108 entries):
6109
6110 ``` markdown
6111 *emph *with emph* in it*
6112 **strong **with strong** in it**
6113 ```
6114
6115 Many implementations have also restricted intraword emphasis to
6116 the `*` forms, to avoid unwanted emphasis in words containing
6117 internal underscores. (It is best practice to put these in code
6118 spans, but users often do not.)
6119
6120 ``` markdown
6121 internal emphasis: foo*bar*baz
6122 no emphasis: foo_bar_baz
6123 ```
6124
6125 The rules given below capture all of these patterns, while allowing
6126 for efficient parsing strategies that do not backtrack.
6127
6128 First, some definitions. A [delimiter run](@) is either
6129 a sequence of one or more `*` characters that is not preceded or
6130 followed by a non-backslash-escaped `*` character, or a sequence
6131 of one or more `_` characters that is not preceded or followed by
6132 a non-backslash-escaped `_` character.
6133
6134 A [left-flanking delimiter run](@) is
6135 a [delimiter run] that is (1) not followed by [Unicode whitespace],
6136 and either (2a) not followed by a [punctuation character], or
6137 (2b) followed by a [punctuation character] and
6138 preceded by [Unicode whitespace] or a [punctuation character].
6139 For purposes of this definition, the beginning and the end of
6140 the line count as Unicode whitespace.
6141
6142 A [right-flanking delimiter run](@) is
6143 a [delimiter run] that is (1) not preceded by [Unicode whitespace],
6144 and either (2a) not preceded by a [punctuation character], or
6145 (2b) preceded by a [punctuation character] and
6146 followed by [Unicode whitespace] or a [punctuation character].
6147 For purposes of this definition, the beginning and the end of
6148 the line count as Unicode whitespace.
6149
6150 Here are some examples of delimiter runs.
6151
6152 - left-flanking but not right-flanking:
6153
6154 ```
6155 ***abc
6156 _abc
6157 **"abc"
6158 _"abc"
6159 ```
6160
6161 - right-flanking but not left-flanking:
6162
6163 ```
6164 abc***
6165 abc_
6166 "abc"**
6167 "abc"_
6168 ```
6169
6170 - Both left and right-flanking:
6171
6172 ```
6173 abc***def
6174 "abc"_"def"
6175 ```
6176
6177 - Neither left nor right-flanking:
6178
6179 ```
6180 abc *** def
6181 a _ b
6182 ```
6183
6184 (The idea of distinguishing left-flanking and right-flanking
6185 delimiter runs based on the character before and the character
6186 after comes from Roopesh Chander's
6187 [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
6188 vfmd uses the terminology "emphasis indicator string" instead of "delimiter
6189 run," and its rules for distinguishing left- and right-flanking runs
6190 are a bit more complex than the ones given here.)
6191
6192 The following rules define emphasis and strong emphasis:
6193
6194 1. A single `*` character [can open emphasis](@)
6195 iff (if and only if) it is part of a [left-flanking delimiter run].
6196
6197 2. A single `_` character [can open emphasis] iff
6198 it is part of a [left-flanking delimiter run]
6199 and either (a) not part of a [right-flanking delimiter run]
6200 or (b) part of a [right-flanking delimiter run]
6201 preceded by punctuation.
6202
6203 3. A single `*` character [can close emphasis](@)
6204 iff it is part of a [right-flanking delimiter run].
6205
6206 4. A single `_` character [can close emphasis] iff
6207 it is part of a [right-flanking delimiter run]
6208 and either (a) not part of a [left-flanking delimiter run]
6209 or (b) part of a [left-flanking delimiter run]
6210 followed by punctuation.
6211
6212 5. A double `**` [can open strong emphasis](@)
6213 iff it is part of a [left-flanking delimiter run].
6214
6215 6. A double `__` [can open strong emphasis] iff
6216 it is part of a [left-flanking delimiter run]
6217 and either (a) not part of a [right-flanking delimiter run]
6218 or (b) part of a [right-flanking delimiter run]
6219 preceded by punctuation.
6220
6221 7. A double `**` [can close strong emphasis](@)
6222 iff it is part of a [right-flanking delimiter run].
6223
6224 8. A double `__` [can close strong emphasis] iff
6225 it is part of a [right-flanking delimiter run]
6226 and either (a) not part of a [left-flanking delimiter run]
6227 or (b) part of a [left-flanking delimiter run]
6228 followed by punctuation.
6229
6230 9. Emphasis begins with a delimiter that [can open emphasis] and ends
6231 with a delimiter that [can close emphasis], and that uses the same
6232 character (`_` or `*`) as the opening delimiter. The
6233 opening and closing delimiters must belong to separate
6234 [delimiter runs]. If one of the delimiters can both
6235 open and close emphasis, then the sum of the lengths of the
6236 delimiter runs containing the opening and closing delimiters
6237 must not be a multiple of 3 unless both lengths are
6238 multiples of 3.
6239
6240 10. Strong emphasis begins with a delimiter that
6241 [can open strong emphasis] and ends with a delimiter that
6242 [can close strong emphasis], and that uses the same character
6243 (`_` or `*`) as the opening delimiter. The
6244 opening and closing delimiters must belong to separate
6245 [delimiter runs]. If one of the delimiters can both open
6246 and close strong emphasis, then the sum of the lengths of
6247 the delimiter runs containing the opening and closing
6248 delimiters must not be a multiple of 3 unless both lengths
6249 are multiples of 3.
6250
6251 11. A literal `*` character cannot occur at the beginning or end of
6252 `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
6253 is backslash-escaped.
6254
6255 12. A literal `_` character cannot occur at the beginning or end of
6256 `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
6257 is backslash-escaped.
6258
6259 Where rules 1--12 above are compatible with multiple parsings,
6260 the following principles resolve ambiguity:
6261
6262 13. The number of nestings should be minimized. Thus, for example,
6263 an interpretation `<strong>...</strong>` is always preferred to
6264 `<em><em>...</em></em>`.
6265
6266 14. An interpretation `<em><strong>...</strong></em>` is always
6267 preferred to `<strong><em>...</em></strong>`.
6268
6269 15. When two potential emphasis or strong emphasis spans overlap,
6270 so that the second begins before the first ends and ends after
6271 the first ends, the first takes precedence. Thus, for example,
6272 `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
6273 than `*foo <em>bar* baz</em>`.
6274
6275 16. When there are two potential emphasis or strong emphasis spans
6276 with the same closing delimiter, the shorter one (the one that
6277 opens later) takes precedence. Thus, for example,
6278 `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
6279 rather than `<strong>foo **bar baz</strong>`.
6280
6281 17. Inline code spans, links, images, and HTML tags group more tightly
6282 than emphasis. So, when there is a choice between an interpretation
6283 that contains one of these elements and one that does not, the
6284 former always wins. Thus, for example, `*[foo*](bar)` is
6285 parsed as `*<a href="bar">foo*</a>` rather than as
6286 `<em>[foo</em>](bar)`.
6287
6288 These rules can be illustrated through a series of examples.
6289
6290 Rule 1:
6291
6292 ```````````````````````````````` example
6293 *foo bar*
6294 .
6295 <p><em>foo bar</em></p>
6296 ````````````````````````````````
6297
6298
6299 This is not emphasis, because the opening `*` is followed by
6300 whitespace, and hence not part of a [left-flanking delimiter run]:
6301
6302 ```````````````````````````````` example
6303 a * foo bar*
6304 .
6305 <p>a * foo bar*</p>
6306 ````````````````````````````````
6307
6308
6309 This is not emphasis, because the opening `*` is preceded
6310 by an alphanumeric and followed by punctuation, and hence
6311 not part of a [left-flanking delimiter run]:
6312
6313 ```````````````````````````````` example
6314 a*"foo"*
6315 .
6316 <p>a*&quot;foo&quot;*</p>
6317 ````````````````````````````````
6318
6319
6320 Unicode nonbreaking spaces count as whitespace, too:
6321
6322 ```````````````````````````````` example
6323 * a *
6324 .
6325 <p>* a *</p>
6326 ````````````````````````````````
6327
6328
6329 Intraword emphasis with `*` is permitted:
6330
6331 ```````````````````````````````` example
6332 foo*bar*
6333 .
6334 <p>foo<em>bar</em></p>
6335 ````````````````````````````````
6336
6337
6338 ```````````````````````````````` example
6339 5*6*78
6340 .
6341 <p>5<em>6</em>78</p>
6342 ````````````````````````````````
6343
6344
6345 Rule 2:
6346
6347 ```````````````````````````````` example
6348 _foo bar_
6349 .
6350 <p><em>foo bar</em></p>
6351 ````````````````````````````````
6352
6353
6354 This is not emphasis, because the opening `_` is followed by
6355 whitespace:
6356
6357 ```````````````````````````````` example
6358 _ foo bar_
6359 .
6360 <p>_ foo bar_</p>
6361 ````````````````````````````````
6362
6363
6364 This is not emphasis, because the opening `_` is preceded
6365 by an alphanumeric and followed by punctuation:
6366
6367 ```````````````````````````````` example
6368 a_"foo"_
6369 .
6370 <p>a_&quot;foo&quot;_</p>
6371 ````````````````````````````````
6372
6373
6374 Emphasis with `_` is not allowed inside words:
6375
6376 ```````````````````````````````` example
6377 foo_bar_
6378 .
6379 <p>foo_bar_</p>
6380 ````````````````````````````````
6381
6382
6383 ```````````````````````````````` example
6384 5_6_78
6385 .
6386 <p>5_6_78</p>
6387 ````````````````````````````````
6388
6389
6390 ```````````````````````````````` example
6391 пристаням_стремятся_
6392 .
6393 <p>пристаням_стремятся_</p>
6394 ````````````````````````````````
6395
6396
6397 Here `_` does not generate emphasis, because the first delimiter run
6398 is right-flanking and the second left-flanking:
6399
6400 ```````````````````````````````` example
6401 aa_"bb"_cc
6402 .
6403 <p>aa_&quot;bb&quot;_cc</p>
6404 ````````````````````````````````
6405
6406
6407 This is emphasis, even though the opening delimiter is
6408 both left- and right-flanking, because it is preceded by
6409 punctuation:
6410
6411 ```````````````````````````````` example
6412 foo-_(bar)_
6413 .
6414 <p>foo-<em>(bar)</em></p>
6415 ````````````````````````````````
6416
6417
6418 Rule 3:
6419
6420 This is not emphasis, because the closing delimiter does
6421 not match the opening delimiter:
6422
6423 ```````````````````````````````` example
6424 _foo*
6425 .
6426 <p>_foo*</p>
6427 ````````````````````````````````
6428
6429
6430 This is not emphasis, because the closing `*` is preceded by
6431 whitespace:
6432
6433 ```````````````````````````````` example
6434 *foo bar *
6435 .
6436 <p>*foo bar *</p>
6437 ````````````````````````````````
6438
6439
6440 A newline also counts as whitespace:
6441
6442 ```````````````````````````````` example
6443 *foo bar
6444 *
6445 .
6446 <p>*foo bar
6447 *</p>
6448 ````````````````````````````````
6449
6450
6451 This is not emphasis, because the second `*` is
6452 preceded by punctuation and followed by an alphanumeric
6453 (hence it is not part of a [right-flanking delimiter run]:
6454
6455 ```````````````````````````````` example
6456 *(*foo)
6457 .
6458 <p>*(*foo)</p>
6459 ````````````````````````````````
6460
6461
6462 The point of this restriction is more easily appreciated
6463 with this example:
6464
6465 ```````````````````````````````` example
6466 *(*foo*)*
6467 .
6468 <p><em>(<em>foo</em>)</em></p>
6469 ````````````````````````````````
6470
6471
6472 Intraword emphasis with `*` is allowed:
6473
6474 ```````````````````````````````` example
6475 *foo*bar
6476 .
6477 <p><em>foo</em>bar</p>
6478 ````````````````````````````````
6479
6480
6481
6482 Rule 4:
6483
6484 This is not emphasis, because the closing `_` is preceded by
6485 whitespace:
6486
6487 ```````````````````````````````` example
6488 _foo bar _
6489 .
6490 <p>_foo bar _</p>
6491 ````````````````````````````````
6492
6493
6494 This is not emphasis, because the second `_` is
6495 preceded by punctuation and followed by an alphanumeric:
6496
6497 ```````````````````````````````` example
6498 _(_foo)
6499 .
6500 <p>_(_foo)</p>
6501 ````````````````````````````````
6502
6503
6504 This is emphasis within emphasis:
6505
6506 ```````````````````````````````` example
6507 _(_foo_)_
6508 .
6509 <p><em>(<em>foo</em>)</em></p>
6510 ````````````````````````````````
6511
6512
6513 Intraword emphasis is disallowed for `_`:
6514
6515 ```````````````````````````````` example
6516 _foo_bar
6517 .
6518 <p>_foo_bar</p>
6519 ````````````````````````````````
6520
6521
6522 ```````````````````````````````` example
6523 _пристаням_стремятся
6524 .
6525 <p>_пристаням_стремятся</p>
6526 ````````````````````````````````
6527
6528
6529 ```````````````````````````````` example
6530 _foo_bar_baz_
6531 .
6532 <p><em>foo_bar_baz</em></p>
6533 ````````````````````````````````
6534
6535
6536 This is emphasis, even though the closing delimiter is
6537 both left- and right-flanking, because it is followed by
6538 punctuation:
6539
6540 ```````````````````````````````` example
6541 _(bar)_.
6542 .
6543 <p><em>(bar)</em>.</p>
6544 ````````````````````````````````
6545
6546
6547 Rule 5:
6548
6549 ```````````````````````````````` example
6550 **foo bar**
6551 .
6552 <p><strong>foo bar</strong></p>
6553 ````````````````````````````````
6554
6555
6556 This is not strong emphasis, because the opening delimiter is
6557 followed by whitespace:
6558
6559 ```````````````````````````````` example
6560 ** foo bar**
6561 .
6562 <p>** foo bar**</p>
6563 ````````````````````````````````
6564
6565
6566 This is not strong emphasis, because the opening `**` is preceded
6567 by an alphanumeric and followed by punctuation, and hence
6568 not part of a [left-flanking delimiter run]:
6569
6570 ```````````````````````````````` example
6571 a**"foo"**
6572 .
6573 <p>a**&quot;foo&quot;**</p>
6574 ````````````````````````````````
6575
6576
6577 Intraword strong emphasis with `**` is permitted:
6578
6579 ```````````````````````````````` example
6580 foo**bar**
6581 .
6582 <p>foo<strong>bar</strong></p>
6583 ````````````````````````````````
6584
6585
6586 Rule 6:
6587
6588 ```````````````````````````````` example
6589 __foo bar__
6590 .
6591 <p><strong>foo bar</strong></p>
6592 ````````````````````````````````
6593
6594
6595 This is not strong emphasis, because the opening delimiter is
6596 followed by whitespace:
6597
6598 ```````````````````````````````` example
6599 __ foo bar__
6600 .
6601 <p>__ foo bar__</p>
6602 ````````````````````````````````
6603
6604
6605 A newline counts as whitespace:
6606 ```````````````````````````````` example
6607 __
6608 foo bar__
6609 .
6610 <p>__
6611 foo bar__</p>
6612 ````````````````````````````````
6613
6614
6615 This is not strong emphasis, because the opening `__` is preceded
6616 by an alphanumeric and followed by punctuation:
6617
6618 ```````````````````````````````` example
6619 a__"foo"__
6620 .
6621 <p>a__&quot;foo&quot;__</p>
6622 ````````````````````````````````
6623
6624
6625 Intraword strong emphasis is forbidden with `__`:
6626
6627 ```````````````````````````````` example
6628 foo__bar__
6629 .
6630 <p>foo__bar__</p>
6631 ````````````````````````````````
6632
6633
6634 ```````````````````````````````` example
6635 5__6__78
6636 .
6637 <p>5__6__78</p>
6638 ````````````````````````````````
6639
6640
6641 ```````````````````````````````` example
6642 пристаням__стремятся__
6643 .
6644 <p>пристаням__стремятся__</p>
6645 ````````````````````````````````
6646
6647
6648 ```````````````````````````````` example
6649 __foo, __bar__, baz__
6650 .
6651 <p><strong>foo, <strong>bar</strong>, baz</strong></p>
6652 ````````````````````````````````
6653
6654
6655 This is strong emphasis, even though the opening delimiter is
6656 both left- and right-flanking, because it is preceded by
6657 punctuation:
6658
6659 ```````````````````````````````` example
6660 foo-__(bar)__
6661 .
6662 <p>foo-<strong>(bar)</strong></p>
6663 ````````````````````````````````
6664
6665
6666
6667 Rule 7:
6668
6669 This is not strong emphasis, because the closing delimiter is preceded
6670 by whitespace:
6671
6672 ```````````````````````````````` example
6673 **foo bar **
6674 .
6675 <p>**foo bar **</p>
6676 ````````````````````````````````
6677
6678
6679 (Nor can it be interpreted as an emphasized `*foo bar *`, because of
6680 Rule 11.)
6681
6682 This is not strong emphasis, because the second `**` is
6683 preceded by punctuation and followed by an alphanumeric:
6684
6685 ```````````````````````````````` example
6686 **(**foo)
6687 .
6688 <p>**(**foo)</p>
6689 ````````````````````````````````
6690
6691
6692 The point of this restriction is more easily appreciated
6693 with these examples:
6694
6695 ```````````````````````````````` example
6696 *(**foo**)*
6697 .
6698 <p><em>(<strong>foo</strong>)</em></p>
6699 ````````````````````````````````
6700
6701
6702 ```````````````````````````````` example
6703 **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
6704 *Asclepias physocarpa*)**
6705 .
6706 <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
6707 <em>Asclepias physocarpa</em>)</strong></p>
6708 ````````````````````````````````
6709
6710
6711 ```````````````````````````````` example
6712 **foo "*bar*" foo**
6713 .
6714 <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
6715 ````````````````````````````````
6716
6717
6718 Intraword emphasis:
6719
6720 ```````````````````````````````` example
6721 **foo**bar
6722 .
6723 <p><strong>foo</strong>bar</p>
6724 ````````````````````````````````
6725
6726
6727 Rule 8:
6728
6729 This is not strong emphasis, because the closing delimiter is
6730 preceded by whitespace:
6731
6732 ```````````````````````````````` example
6733 __foo bar __
6734 .
6735 <p>__foo bar __</p>
6736 ````````````````````````````````
6737
6738
6739 This is not strong emphasis, because the second `__` is
6740 preceded by punctuation and followed by an alphanumeric:
6741
6742 ```````````````````````````````` example
6743 __(__foo)
6744 .
6745 <p>__(__foo)</p>
6746 ````````````````````````````````
6747
6748
6749 The point of this restriction is more easily appreciated
6750 with this example:
6751
6752 ```````````````````````````````` example
6753 _(__foo__)_
6754 .
6755 <p><em>(<strong>foo</strong>)</em></p>
6756 ````````````````````````````````
6757
6758
6759 Intraword strong emphasis is forbidden with `__`:
6760
6761 ```````````````````````````````` example
6762 __foo__bar
6763 .
6764 <p>__foo__bar</p>
6765 ````````````````````````````````
6766
6767
6768 ```````````````````````````````` example
6769 __пристаням__стремятся
6770 .
6771 <p>__пристаням__стремятся</p>
6772 ````````````````````````````````
6773
6774
6775 ```````````````````````````````` example
6776 __foo__bar__baz__
6777 .
6778 <p><strong>foo__bar__baz</strong></p>
6779 ````````````````````````````````
6780
6781
6782 This is strong emphasis, even though the closing delimiter is
6783 both left- and right-flanking, because it is followed by
6784 punctuation:
6785
6786 ```````````````````````````````` example
6787 __(bar)__.
6788 .
6789 <p><strong>(bar)</strong>.</p>
6790 ````````````````````````````````
6791
6792
6793 Rule 9:
6794
6795 Any nonempty sequence of inline elements can be the contents of an
6796 emphasized span.
6797
6798 ```````````````````````````````` example
6799 *foo [bar](/url)*
6800 .
6801 <p><em>foo <a href="/url">bar</a></em></p>
6802 ````````````````````````````````
6803
6804
6805 ```````````````````````````````` example
6806 *foo
6807 bar*
6808 .
6809 <p><em>foo
6810 bar</em></p>
6811 ````````````````````````````````
6812
6813
6814 In particular, emphasis and strong emphasis can be nested
6815 inside emphasis:
6816
6817 ```````````````````````````````` example
6818 _foo __bar__ baz_
6819 .
6820 <p><em>foo <strong>bar</strong> baz</em></p>
6821 ````````````````````````````````
6822
6823
6824 ```````````````````````````````` example
6825 _foo _bar_ baz_
6826 .
6827 <p><em>foo <em>bar</em> baz</em></p>
6828 ````````````````````````````````
6829
6830
6831 ```````````````````````````````` example
6832 __foo_ bar_
6833 .
6834 <p><em><em>foo</em> bar</em></p>
6835 ````````````````````````````````
6836
6837
6838 ```````````````````````````````` example
6839 *foo *bar**
6840 .
6841 <p><em>foo <em>bar</em></em></p>
6842 ````````````````````````````````
6843
6844
6845 ```````````````````````````````` example
6846 *foo **bar** baz*
6847 .
6848 <p><em>foo <strong>bar</strong> baz</em></p>
6849 ````````````````````````````````
6850
6851 ```````````````````````````````` example
6852 *foo**bar**baz*
6853 .
6854 <p><em>foo<strong>bar</strong>baz</em></p>
6855 ````````````````````````````````
6856
6857 Note that in the preceding case, the interpretation
6858
6859 ``` markdown
6860 <p><em>foo</em><em>bar<em></em>baz</em></p>
6861 ```
6862
6863
6864 is precluded by the condition that a delimiter that
6865 can both open and close (like the `*` after `foo`)
6866 cannot form emphasis if the sum of the lengths of
6867 the delimiter runs containing the opening and
6868 closing delimiters is a multiple of 3 unless
6869 both lengths are multiples of 3.
6870
6871
6872 For the same reason, we don't get two consecutive
6873 emphasis sections in this example:
6874
6875 ```````````````````````````````` example
6876 *foo**bar*
6877 .
6878 <p><em>foo**bar</em></p>
6879 ````````````````````````````````
6880
6881
6882 The same condition ensures that the following
6883 cases are all strong emphasis nested inside
6884 emphasis, even when the interior spaces are
6885 omitted:
6886
6887
6888 ```````````````````````````````` example
6889 ***foo** bar*
6890 .
6891 <p><em><strong>foo</strong> bar</em></p>
6892 ````````````````````````````````
6893
6894
6895 ```````````````````````````````` example
6896 *foo **bar***
6897 .
6898 <p><em>foo <strong>bar</strong></em></p>
6899 ````````````````````````````````
6900
6901
6902 ```````````````````````````````` example
6903 *foo**bar***
6904 .
6905 <p><em>foo<strong>bar</strong></em></p>
6906 ````````````````````````````````
6907
6908
6909 When the lengths of the interior closing and opening
6910 delimiter runs are *both* multiples of 3, though,
6911 they can match to create emphasis:
6912
6913 ```````````````````````````````` example
6914 foo***bar***baz
6915 .
6916 <p>foo<em><strong>bar</strong></em>baz</p>
6917 ````````````````````````````````
6918
6919 ```````````````````````````````` example
6920 foo******bar*********baz
6921 .
6922 <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
6923 ````````````````````````````````
6924
6925
6926 Indefinite levels of nesting are possible:
6927
6928 ```````````````````````````````` example
6929 *foo **bar *baz* bim** bop*
6930 .
6931 <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
6932 ````````````````````````````````
6933
6934
6935 ```````````````````````````````` example
6936 *foo [*bar*](/url)*
6937 .
6938 <p><em>foo <a href="/url"><em>bar</em></a></em></p>
6939 ````````````````````````````````
6940
6941
6942 There can be no empty emphasis or strong emphasis:
6943
6944 ```````````````````````````````` example
6945 ** is not an empty emphasis
6946 .
6947 <p>** is not an empty emphasis</p>
6948 ````````````````````````````````
6949
6950
6951 ```````````````````````````````` example
6952 **** is not an empty strong emphasis
6953 .
6954 <p>**** is not an empty strong emphasis</p>
6955 ````````````````````````````````
6956
6957
6958
6959 Rule 10:
6960
6961 Any nonempty sequence of inline elements can be the contents of an
6962 strongly emphasized span.
6963
6964 ```````````````````````````````` example
6965 **foo [bar](/url)**
6966 .
6967 <p><strong>foo <a href="/url">bar</a></strong></p>
6968 ````````````````````````````````
6969
6970
6971 ```````````````````````````````` example
6972 **foo
6973 bar**
6974 .
6975 <p><strong>foo
6976 bar</strong></p>
6977 ````````````````````````````````
6978
6979
6980 In particular, emphasis and strong emphasis can be nested
6981 inside strong emphasis:
6982
6983 ```````````````````````````````` example
6984 __foo _bar_ baz__
6985 .
6986 <p><strong>foo <em>bar</em> baz</strong></p>
6987 ````````````````````````````````
6988
6989
6990 ```````````````````````````````` example
6991 __foo __bar__ baz__
6992 .
6993 <p><strong>foo <strong>bar</strong> baz</strong></p>
6994 ````````````````````````````````
6995
6996
6997 ```````````````````````````````` example
6998 ____foo__ bar__
6999 .
7000 <p><strong><strong>foo</strong> bar</strong></p>
7001 ````````````````````````````````
7002
7003
7004 ```````````````````````````````` example
7005 **foo **bar****
7006 .
7007 <p><strong>foo <strong>bar</strong></strong></p>
7008 ````````````````````````````````
7009
7010
7011 ```````````````````````````````` example
7012 **foo *bar* baz**
7013 .
7014 <p><strong>foo <em>bar</em> baz</strong></p>
7015 ````````````````````````````````
7016
7017
7018 ```````````````````````````````` example
7019 **foo*bar*baz**
7020 .
7021 <p><strong>foo<em>bar</em>baz</strong></p>
7022 ````````````````````````````````
7023
7024
7025 ```````````````````````````````` example
7026 ***foo* bar**
7027 .
7028 <p><strong><em>foo</em> bar</strong></p>
7029 ````````````````````````````````
7030
7031
7032 ```````````````````````````````` example
7033 **foo *bar***
7034 .
7035 <p><strong>foo <em>bar</em></strong></p>
7036 ````````````````````````````````
7037
7038
7039 Indefinite levels of nesting are possible:
7040
7041 ```````````````````````````````` example
7042 **foo *bar **baz**
7043 bim* bop**
7044 .
7045 <p><strong>foo <em>bar <strong>baz</strong>
7046 bim</em> bop</strong></p>
7047 ````````````````````````````````
7048
7049
7050 ```````````````````````````````` example
7051 **foo [*bar*](/url)**
7052 .
7053 <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
7054 ````````````````````````````````
7055
7056
7057 There can be no empty emphasis or strong emphasis:
7058
7059 ```````````````````````````````` example
7060 __ is not an empty emphasis
7061 .
7062 <p>__ is not an empty emphasis</p>
7063 ````````````````````````````````
7064
7065
7066 ```````````````````````````````` example
7067 ____ is not an empty strong emphasis
7068 .
7069 <p>____ is not an empty strong emphasis</p>
7070 ````````````````````````````````
7071
7072
7073
7074 Rule 11:
7075
7076 ```````````````````````````````` example
7077 foo ***
7078 .
7079 <p>foo ***</p>
7080 ````````````````````````````````
7081
7082
7083 ```````````````````````````````` example
7084 foo *\**
7085 .
7086 <p>foo <em>*</em></p>
7087 ````````````````````````````````
7088
7089
7090 ```````````````````````````````` example
7091 foo *_*
7092 .
7093 <p>foo <em>_</em></p>
7094 ````````````````````````````````
7095
7096
7097 ```````````````````````````````` example
7098 foo *****
7099 .
7100 <p>foo *****</p>
7101 ````````````````````````````````
7102
7103
7104 ```````````````````````````````` example
7105 foo **\***
7106 .
7107 <p>foo <strong>*</strong></p>
7108 ````````````````````````````````
7109
7110
7111 ```````````````````````````````` example
7112 foo **_**
7113 .
7114 <p>foo <strong>_</strong></p>
7115 ````````````````````````````````
7116
7117
7118 Note that when delimiters do not match evenly, Rule 11 determines
7119 that the excess literal `*` characters will appear outside of the
7120 emphasis, rather than inside it:
7121
7122 ```````````````````````````````` example
7123 **foo*
7124 .
7125 <p>*<em>foo</em></p>
7126 ````````````````````````````````
7127
7128
7129 ```````````````````````````````` example
7130 *foo**
7131 .
7132 <p><em>foo</em>*</p>
7133 ````````````````````````````````
7134
7135
7136 ```````````````````````````````` example
7137 ***foo**
7138 .
7139 <p>*<strong>foo</strong></p>
7140 ````````````````````````````````
7141
7142
7143 ```````````````````````````````` example
7144 ****foo*
7145 .
7146 <p>***<em>foo</em></p>
7147 ````````````````````````````````
7148
7149
7150 ```````````````````````````````` example
7151 **foo***
7152 .
7153 <p><strong>foo</strong>*</p>
7154 ````````````````````````````````
7155
7156
7157 ```````````````````````````````` example
7158 *foo****
7159 .
7160 <p><em>foo</em>***</p>
7161 ````````````````````````````````
7162
7163
7164
7165 Rule 12:
7166
7167 ```````````````````````````````` example
7168 foo ___
7169 .
7170 <p>foo ___</p>
7171 ````````````````````````````````
7172
7173
7174 ```````````````````````````````` example
7175 foo _\__
7176 .
7177 <p>foo <em>_</em></p>
7178 ````````````````````````````````
7179
7180
7181 ```````````````````````````````` example
7182 foo _*_
7183 .
7184 <p>foo <em>*</em></p>
7185 ````````````````````````````````
7186
7187
7188 ```````````````````````````````` example
7189 foo _____
7190 .
7191 <p>foo _____</p>
7192 ````````````````````````````````
7193
7194
7195 ```````````````````````````````` example
7196 foo __\___
7197 .
7198 <p>foo <strong>_</strong></p>
7199 ````````````````````````````````
7200
7201
7202 ```````````````````````````````` example
7203 foo __*__
7204 .
7205 <p>foo <strong>*</strong></p>
7206 ````````````````````````````````
7207
7208
7209 ```````````````````````````````` example
7210 __foo_
7211 .
7212 <p>_<em>foo</em></p>
7213 ````````````````````````````````
7214
7215
7216 Note that when delimiters do not match evenly, Rule 12 determines
7217 that the excess literal `_` characters will appear outside of the
7218 emphasis, rather than inside it:
7219
7220 ```````````````````````````````` example
7221 _foo__
7222 .
7223 <p><em>foo</em>_</p>
7224 ````````````````````````````````
7225
7226
7227 ```````````````````````````````` example
7228 ___foo__
7229 .
7230 <p>_<strong>foo</strong></p>
7231 ````````````````````````````````
7232
7233
7234 ```````````````````````````````` example
7235 ____foo_
7236 .
7237 <p>___<em>foo</em></p>
7238 ````````````````````````````````
7239
7240
7241 ```````````````````````````````` example
7242 __foo___
7243 .
7244 <p><strong>foo</strong>_</p>
7245 ````````````````````````````````
7246
7247
7248 ```````````````````````````````` example
7249 _foo____
7250 .
7251 <p><em>foo</em>___</p>
7252 ````````````````````````````````
7253
7254
7255 Rule 13 implies that if you want emphasis nested directly inside
7256 emphasis, you must use different delimiters:
7257
7258 ```````````````````````````````` example
7259 **foo**
7260 .
7261 <p><strong>foo</strong></p>
7262 ````````````````````````````````
7263
7264
7265 ```````````````````````````````` example
7266 *_foo_*
7267 .
7268 <p><em><em>foo</em></em></p>
7269 ````````````````````````````````
7270
7271
7272 ```````````````````````````````` example
7273 __foo__
7274 .
7275 <p><strong>foo</strong></p>
7276 ````````````````````````````````
7277
7278
7279 ```````````````````````````````` example
7280 _*foo*_
7281 .
7282 <p><em><em>foo</em></em></p>
7283 ````````````````````````````````
7284
7285
7286 However, strong emphasis within strong emphasis is possible without
7287 switching delimiters:
7288
7289 ```````````````````````````````` example
7290 ****foo****
7291 .
7292 <p><strong><strong>foo</strong></strong></p>
7293 ````````````````````````````````
7294
7295
7296 ```````````````````````````````` example
7297 ____foo____
7298 .
7299 <p><strong><strong>foo</strong></strong></p>
7300 ````````````````````````````````
7301
7302
7303
7304 Rule 13 can be applied to arbitrarily long sequences of
7305 delimiters:
7306
7307 ```````````````````````````````` example
7308 ******foo******
7309 .
7310 <p><strong><strong><strong>foo</strong></strong></strong></p>
7311 ````````````````````````````````
7312
7313
7314 Rule 14:
7315
7316 ```````````````````````````````` example
7317 ***foo***
7318 .
7319 <p><em><strong>foo</strong></em></p>
7320 ````````````````````````````````
7321
7322
7323 ```````````````````````````````` example
7324 _____foo_____
7325 .
7326 <p><em><strong><strong>foo</strong></strong></em></p>
7327 ````````````````````````````````
7328
7329
7330 Rule 15:
7331
7332 ```````````````````````````````` example
7333 *foo _bar* baz_
7334 .
7335 <p><em>foo _bar</em> baz_</p>
7336 ````````````````````````````````
7337
7338
7339 ```````````````````````````````` example
7340 *foo __bar *baz bim__ bam*
7341 .
7342 <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
7343 ````````````````````````````````
7344
7345
7346 Rule 16:
7347
7348 ```````````````````````````````` example
7349 **foo **bar baz**
7350 .
7351 <p>**foo <strong>bar baz</strong></p>
7352 ````````````````````````````````
7353
7354
7355 ```````````````````````````````` example
7356 *foo *bar baz*
7357 .
7358 <p>*foo <em>bar baz</em></p>
7359 ````````````````````````````````
7360
7361
7362 Rule 17:
7363
7364 ```````````````````````````````` example
7365 *[bar*](/url)
7366 .
7367 <p>*<a href="/url">bar*</a></p>
7368 ````````````````````````````````
7369
7370
7371 ```````````````````````````````` example
7372 _foo [bar_](/url)
7373 .
7374 <p>_foo <a href="/url">bar_</a></p>
7375 ````````````````````````````````
7376
7377
7378 ```````````````````````````````` example
7379 *<img src="foo" title="*"/>
7380 .
7381 <p>*<img src="foo" title="*"/></p>
7382 ````````````````````````````````
7383
7384
7385 ```````````````````````````````` example
7386 **<a href="**">
7387 .
7388 <p>**<a href="**"></p>
7389 ````````````````````````````````
7390
7391
7392 ```````````````````````````````` example
7393 __<a href="__">
7394 .
7395 <p>__<a href="__"></p>
7396 ````````````````````````````````
7397
7398
7399 ```````````````````````````````` example
7400 *a `*`*
7401 .
7402 <p><em>a <code>*</code></em></p>
7403 ````````````````````````````````
7404
7405
7406 ```````````````````````````````` example
7407 _a `_`_
7408 .
7409 <p><em>a <code>_</code></em></p>
7410 ````````````````````````````````
7411
7412
7413 ```````````````````````````````` example
7414 **a<http://foo.bar/?q=**>
7415 .
7416 <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
7417 ````````````````````````````````
7418
7419
7420 ```````````````````````````````` example
7421 __a<http://foo.bar/?q=__>
7422 .
7423 <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
7424 ````````````````````````````````
7425
7426
7427
7428 ## Links
7429
7430 A link contains [link text] (the visible text), a [link destination]
7431 (the URI that is the link destination), and optionally a [link title].
7432 There are two basic kinds of links in Markdown. In [inline links] the
7433 destination and title are given immediately after the link text. In
7434 [reference links] the destination and title are defined elsewhere in
7435 the document.
7436
7437 A [link text](@) consists of a sequence of zero or more
7438 inline elements enclosed by square brackets (`[` and `]`). The
7439 following rules apply:
7440
7441 - Links may not contain other links, at any level of nesting. If
7442 multiple otherwise valid link definitions appear nested inside each
7443 other, the inner-most definition is used.
7444
7445 - Brackets are allowed in the [link text] only if (a) they
7446 are backslash-escaped or (b) they appear as a matched pair of brackets,
7447 with an open bracket `[`, a sequence of zero or more inlines, and
7448 a close bracket `]`.
7449
7450 - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
7451 than the brackets in link text. Thus, for example,
7452 `` [foo`]` `` could not be a link text, since the second `]`
7453 is part of a code span.
7454
7455 - The brackets in link text bind more tightly than markers for
7456 [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
7457
7458 A [link destination](@) consists of either
7459
7460 - a sequence of zero or more characters between an opening `<` and a
7461 closing `>` that contains no line breaks or unescaped
7462 `<` or `>` characters, or
7463
7464 - a nonempty sequence of characters that does not start with
7465 `<`, does not include ASCII space or control characters, and
7466 includes parentheses only if (a) they are backslash-escaped or
7467 (b) they are part of a balanced pair of unescaped parentheses.
7468 (Implementations may impose limits on parentheses nesting to
7469 avoid performance issues, but at least three levels of nesting
7470 should be supported.)
7471
7472 A [link title](@) consists of either
7473
7474 - a sequence of zero or more characters between straight double-quote
7475 characters (`"`), including a `"` character only if it is
7476 backslash-escaped, or
7477
7478 - a sequence of zero or more characters between straight single-quote
7479 characters (`'`), including a `'` character only if it is
7480 backslash-escaped, or
7481
7482 - a sequence of zero or more characters between matching parentheses
7483 (`(...)`), including a `(` or `)` character only if it is
7484 backslash-escaped.
7485
7486 Although [link titles] may span multiple lines, they may not contain
7487 a [blank line].
7488
7489 An [inline link](@) consists of a [link text] followed immediately
7490 by a left parenthesis `(`, optional [whitespace], an optional
7491 [link destination], an optional [link title] separated from the link
7492 destination by [whitespace], optional [whitespace], and a right
7493 parenthesis `)`. The link's text consists of the inlines contained
7494 in the [link text] (excluding the enclosing square brackets).
7495 The link's URI consists of the link destination, excluding enclosing
7496 `<...>` if present, with backslash-escapes in effect as described
7497 above. The link's title consists of the link title, excluding its
7498 enclosing delimiters, with backslash-escapes in effect as described
7499 above.
7500
7501 Here is a simple inline link:
7502
7503 ```````````````````````````````` example
7504 [link](/uri "title")
7505 .
7506 <p><a href="/uri" title="title">link</a></p>
7507 ````````````````````````````````
7508
7509
7510 The title may be omitted:
7511
7512 ```````````````````````````````` example
7513 [link](/uri)
7514 .
7515 <p><a href="/uri">link</a></p>
7516 ````````````````````````````````
7517
7518
7519 Both the title and the destination may be omitted:
7520
7521 ```````````````````````````````` example
7522 [link]()
7523 .
7524 <p><a href="">link</a></p>
7525 ````````````````````````````````
7526
7527
7528 ```````````````````````````````` example
7529 [link](<>)
7530 .
7531 <p><a href="">link</a></p>
7532 ````````````````````````````````
7533
7534 The destination can only contain spaces if it is
7535 enclosed in pointy brackets:
7536
7537 ```````````````````````````````` example
7538 [link](/my uri)
7539 .
7540 <p>[link](/my uri)</p>
7541 ````````````````````````````````
7542
7543 ```````````````````````````````` example
7544 [link](</my uri>)
7545 .
7546 <p><a href="/my%20uri">link</a></p>
7547 ````````````````````````````````
7548
7549 The destination cannot contain line breaks,
7550 even if enclosed in pointy brackets:
7551
7552 ```````````````````````````````` example
7553 [link](foo
7554 bar)
7555 .
7556 <p>[link](foo
7557 bar)</p>
7558 ````````````````````````````````
7559
7560 ```````````````````````````````` example
7561 [link](<foo
7562 bar>)
7563 .
7564 <p>[link](<foo
7565 bar>)</p>
7566 ````````````````````````````````
7567
7568 The destination can contain `)` if it is enclosed
7569 in pointy brackets:
7570
7571 ```````````````````````````````` example
7572 [a](<b)c>)
7573 .
7574 <p><a href="b)c">a</a></p>
7575 ````````````````````````````````
7576
7577 Pointy brackets that enclose links must be unescaped:
7578
7579 ```````````````````````````````` example
7580 [link](<foo\>)
7581 .
7582 <p>[link](&lt;foo&gt;)</p>
7583 ````````````````````````````````
7584
7585 These are not links, because the opening pointy bracket
7586 is not matched properly:
7587
7588 ```````````````````````````````` example
7589 [a](<b)c
7590 [a](<b)c>
7591 [a](<b>c)
7592 .
7593 <p>[a](&lt;b)c
7594 [a](&lt;b)c&gt;
7595 [a](<b>c)</p>
7596 ````````````````````````````````
7597
7598 Parentheses inside the link destination may be escaped:
7599
7600 ```````````````````````````````` example
7601 [link](\(foo\))
7602 .
7603 <p><a href="(foo)">link</a></p>
7604 ````````````````````````````````
7605
7606 Any number of parentheses are allowed without escaping, as long as they are
7607 balanced:
7608
7609 ```````````````````````````````` example
7610 [link](foo(and(bar)))
7611 .
7612 <p><a href="foo(and(bar))">link</a></p>
7613 ````````````````````````````````
7614
7615 However, if you have unbalanced parentheses, you need to escape or use the
7616 `<...>` form:
7617
7618 ```````````````````````````````` example
7619 [link](foo\(and\(bar\))
7620 .
7621 <p><a href="foo(and(bar)">link</a></p>
7622 ````````````````````````````````
7623
7624
7625 ```````````````````````````````` example
7626 [link](<foo(and(bar)>)
7627 .
7628 <p><a href="foo(and(bar)">link</a></p>
7629 ````````````````````````````````
7630
7631
7632 Parentheses and other symbols can also be escaped, as usual
7633 in Markdown:
7634
7635 ```````````````````````````````` example
7636 [link](foo\)\:)
7637 .
7638 <p><a href="foo):">link</a></p>
7639 ````````````````````````````````
7640
7641
7642 A link can contain fragment identifiers and queries:
7643
7644 ```````````````````````````````` example
7645 [link](#fragment)
7646
7647 [link](http://example.com#fragment)
7648
7649 [link](http://example.com?foo=3#frag)
7650 .
7651 <p><a href="#fragment">link</a></p>
7652 <p><a href="http://example.com#fragment">link</a></p>
7653 <p><a href="http://example.com?foo=3#frag">link</a></p>
7654 ````````````````````````````````
7655
7656
7657 Note that a backslash before a non-escapable character is
7658 just a backslash:
7659
7660 ```````````````````````````````` example
7661 [link](foo\bar)
7662 .
7663 <p><a href="foo%5Cbar">link</a></p>
7664 ````````````````````````````````
7665
7666
7667 URL-escaping should be left alone inside the destination, as all
7668 URL-escaped characters are also valid URL characters. Entity and
7669 numerical character references in the destination will be parsed
7670 into the corresponding Unicode code points, as usual. These may
7671 be optionally URL-escaped when written as HTML, but this spec
7672 does not enforce any particular policy for rendering URLs in
7673 HTML or other formats. Renderers may make different decisions
7674 about how to escape or normalize URLs in the output.
7675
7676 ```````````````````````````````` example
7677 [link](foo%20b&auml;)
7678 .
7679 <p><a href="foo%20b%C3%A4">link</a></p>
7680 ````````````````````````````````
7681
7682
7683 Note that, because titles can often be parsed as destinations,
7684 if you try to omit the destination and keep the title, you'll
7685 get unexpected results:
7686
7687 ```````````````````````````````` example
7688 [link]("title")
7689 .
7690 <p><a href="%22title%22">link</a></p>
7691 ````````````````````````````````
7692
7693
7694 Titles may be in single quotes, double quotes, or parentheses:
7695
7696 ```````````````````````````````` example
7697 [link](/url "title")
7698 [link](/url 'title')
7699 [link](/url (title))
7700 .
7701 <p><a href="/url" title="title">link</a>
7702 <a href="/url" title="title">link</a>
7703 <a href="/url" title="title">link</a></p>
7704 ````````````````````````````````
7705
7706
7707 Backslash escapes and entity and numeric character references
7708 may be used in titles:
7709
7710 ```````````````````````````````` example
7711 [link](/url "title \"&quot;")
7712 .
7713 <p><a href="/url" title="title &quot;&quot;">link</a></p>
7714 ````````````````````````````````
7715
7716
7717 Titles must be separated from the link using a [whitespace].
7718 Other [Unicode whitespace] like non-breaking space doesn't work.
7719
7720 ```````````````````````````````` example
7721 [link](/url "title")
7722 .
7723 <p><a href="/url%C2%A0%22title%22">link</a></p>
7724 ````````````````````````````````
7725
7726
7727 Nested balanced quotes are not allowed without escaping:
7728
7729 ```````````````````````````````` example
7730 [link](/url "title "and" title")
7731 .
7732 <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
7733 ````````````````````````````````
7734
7735
7736 But it is easy to work around this by using a different quote type:
7737
7738 ```````````````````````````````` example
7739 [link](/url 'title "and" title')
7740 .
7741 <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
7742 ````````````````````````````````
7743
7744
7745 (Note: `Markdown.pl` did allow double quotes inside a double-quoted
7746 title, and its test suite included a test demonstrating this.
7747 But it is hard to see a good rationale for the extra complexity this
7748 brings, since there are already many ways---backslash escaping,
7749 entity and numeric character references, or using a different
7750 quote type for the enclosing title---to write titles containing
7751 double quotes. `Markdown.pl`'s handling of titles has a number
7752 of other strange features. For example, it allows single-quoted
7753 titles in inline links, but not reference links. And, in
7754 reference links but not inline links, it allows a title to begin
7755 with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
7756 titles with no closing quotation mark, though 1.0.2b8 does not.
7757 It seems preferable to adopt a simple, rational rule that works
7758 the same way in inline links and link reference definitions.)
7759
7760 [Whitespace] is allowed around the destination and title:
7761
7762 ```````````````````````````````` example
7763 [link]( /uri
7764 "title" )
7765 .
7766 <p><a href="/uri" title="title">link</a></p>
7767 ````````````````````````````````
7768
7769
7770 But it is not allowed between the link text and the
7771 following parenthesis:
7772
7773 ```````````````````````````````` example
7774 [link] (/uri)
7775 .
7776 <p>[link] (/uri)</p>
7777 ````````````````````````````````
7778
7779
7780 The link text may contain balanced brackets, but not unbalanced ones,
7781 unless they are escaped:
7782
7783 ```````````````````````````````` example
7784 [link [foo [bar]]](/uri)
7785 .
7786 <p><a href="/uri">link [foo [bar]]</a></p>
7787 ````````````````````````````````
7788
7789
7790 ```````````````````````````````` example
7791 [link] bar](/uri)
7792 .
7793 <p>[link] bar](/uri)</p>
7794 ````````````````````````````````
7795
7796
7797 ```````````````````````````````` example
7798 [link [bar](/uri)
7799 .
7800 <p>[link <a href="/uri">bar</a></p>
7801 ````````````````````````````````
7802
7803
7804 ```````````````````````````````` example
7805 [link \[bar](/uri)
7806 .
7807 <p><a href="/uri">link [bar</a></p>
7808 ````````````````````````````````
7809
7810
7811 The link text may contain inline content:
7812
7813 ```````````````````````````````` example
7814 [link *foo **bar** `#`*](/uri)
7815 .
7816 <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
7817 ````````````````````````````````
7818
7819
7820 ```````````````````````````````` example
7821 [![moon](moon.jpg)](/uri)
7822 .
7823 <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
7824 ````````````````````````````````
7825
7826
7827 However, links may not contain other links, at any level of nesting.
7828
7829 ```````````````````````````````` example
7830 [foo [bar](/uri)](/uri)
7831 .
7832 <p>[foo <a href="/uri">bar</a>](/uri)</p>
7833 ````````````````````````````````
7834
7835
7836 ```````````````````````````````` example
7837 [foo *[bar [baz](/uri)](/uri)*](/uri)
7838 .
7839 <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
7840 ````````````````````````````````
7841
7842
7843 ```````````````````````````````` example
7844 ![[[foo](uri1)](uri2)](uri3)
7845 .
7846 <p><img src="uri3" alt="[foo](uri2)" /></p>
7847 ````````````````````````````````
7848
7849
7850 These cases illustrate the precedence of link text grouping over
7851 emphasis grouping:
7852
7853 ```````````````````````````````` example
7854 *[foo*](/uri)
7855 .
7856 <p>*<a href="/uri">foo*</a></p>
7857 ````````````````````````````````
7858
7859
7860 ```````````````````````````````` example
7861 [foo *bar](baz*)
7862 .
7863 <p><a href="baz*">foo *bar</a></p>
7864 ````````````````````````````````
7865
7866
7867 Note that brackets that *aren't* part of links do not take
7868 precedence:
7869
7870 ```````````````````````````````` example
7871 *foo [bar* baz]
7872 .
7873 <p><em>foo [bar</em> baz]</p>
7874 ````````````````````````````````
7875
7876
7877 These cases illustrate the precedence of HTML tags, code spans,
7878 and autolinks over link grouping:
7879
7880 ```````````````````````````````` example
7881 [foo <bar attr="](baz)">
7882 .
7883 <p>[foo <bar attr="](baz)"></p>
7884 ````````````````````````````````
7885
7886
7887 ```````````````````````````````` example
7888 [foo`](/uri)`
7889 .
7890 <p>[foo<code>](/uri)</code></p>
7891 ````````````````````````````````
7892
7893
7894 ```````````````````````````````` example
7895 [foo<http://example.com/?search=](uri)>
7896 .
7897 <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
7898 ````````````````````````````````
7899
7900
7901 There are three kinds of [reference link](@)s:
7902 [full](#full-reference-link), [collapsed](#collapsed-reference-link),
7903 and [shortcut](#shortcut-reference-link).
7904
7905 A [full reference link](@)
7906 consists of a [link text] immediately followed by a [link label]
7907 that [matches] a [link reference definition] elsewhere in the document.
7908
7909 A [link label](@) begins with a left bracket (`[`) and ends
7910 with the first right bracket (`]`) that is not backslash-escaped.
7911 Between these brackets there must be at least one [non-whitespace character].
7912 Unescaped square bracket characters are not allowed inside the
7913 opening and closing square brackets of [link labels]. A link
7914 label can have at most 999 characters inside the square
7915 brackets.
7916
7917 One label [matches](@)
7918 another just in case their normalized forms are equal. To normalize a
7919 label, strip off the opening and closing brackets,
7920 perform the *Unicode case fold*, strip leading and trailing
7921 [whitespace] and collapse consecutive internal
7922 [whitespace] to a single space. If there are multiple
7923 matching reference link definitions, the one that comes first in the
7924 document is used. (It is desirable in such cases to emit a warning.)
7925
7926 The contents of the first link label are parsed as inlines, which are
7927 used as the link's text. The link's URI and title are provided by the
7928 matching [link reference definition].
7929
7930 Here is a simple example:
7931
7932 ```````````````````````````````` example
7933 [foo][bar]
7934
7935 [bar]: /url "title"
7936 .
7937 <p><a href="/url" title="title">foo</a></p>
7938 ````````````````````````````````
7939
7940
7941 The rules for the [link text] are the same as with
7942 [inline links]. Thus:
7943
7944 The link text may contain balanced brackets, but not unbalanced ones,
7945 unless they are escaped:
7946
7947 ```````````````````````````````` example
7948 [link [foo [bar]]][ref]
7949
7950 [ref]: /uri
7951 .
7952 <p><a href="/uri">link [foo [bar]]</a></p>
7953 ````````````````````````````````
7954
7955
7956 ```````````````````````````````` example
7957 [link \[bar][ref]
7958
7959 [ref]: /uri
7960 .
7961 <p><a href="/uri">link [bar</a></p>
7962 ````````````````````````````````
7963
7964
7965 The link text may contain inline content:
7966
7967 ```````````````````````````````` example
7968 [link *foo **bar** `#`*][ref]
7969
7970 [ref]: /uri
7971 .
7972 <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
7973 ````````````````````````````````
7974
7975
7976 ```````````````````````````````` example
7977 [![moon](moon.jpg)][ref]
7978
7979 [ref]: /uri
7980 .
7981 <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
7982 ````````````````````````````````
7983
7984
7985 However, links may not contain other links, at any level of nesting.
7986
7987 ```````````````````````````````` example
7988 [foo [bar](/uri)][ref]
7989
7990 [ref]: /uri
7991 .
7992 <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
7993 ````````````````````````````````
7994
7995
7996 ```````````````````````````````` example
7997 [foo *bar [baz][ref]*][ref]
7998
7999 [ref]: /uri
8000 .
8001 <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
8002 ````````````````````````````````
8003
8004
8005 (In the examples above, we have two [shortcut reference links]
8006 instead of one [full reference link].)
8007
8008 The following cases illustrate the precedence of link text grouping over
8009 emphasis grouping:
8010
8011 ```````````````````````````````` example
8012 *[foo*][ref]
8013
8014 [ref]: /uri
8015 .
8016 <p>*<a href="/uri">foo*</a></p>
8017 ````````````````````````````````
8018
8019
8020 ```````````````````````````````` example
8021 [foo *bar][ref]
8022
8023 [ref]: /uri
8024 .
8025 <p><a href="/uri">foo *bar</a></p>
8026 ````````````````````````````````
8027
8028
8029 These cases illustrate the precedence of HTML tags, code spans,
8030 and autolinks over link grouping:
8031
8032 ```````````````````````````````` example
8033 [foo <bar attr="][ref]">
8034
8035 [ref]: /uri
8036 .
8037 <p>[foo <bar attr="][ref]"></p>
8038 ````````````````````````````````
8039
8040
8041 ```````````````````````````````` example
8042 [foo`][ref]`
8043
8044 [ref]: /uri
8045 .
8046 <p>[foo<code>][ref]</code></p>
8047 ````````````````````````````````
8048
8049
8050 ```````````````````````````````` example
8051 [foo<http://example.com/?search=][ref]>
8052
8053 [ref]: /uri
8054 .
8055 <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
8056 ````````````````````````````````
8057
8058
8059 Matching is case-insensitive:
8060
8061 ```````````````````````````````` example
8062 [foo][BaR]
8063
8064 [bar]: /url "title"
8065 .
8066 <p><a href="/url" title="title">foo</a></p>
8067 ````````````````````````````````
8068
8069
8070 Unicode case fold is used:
8071
8072 ```````````````````````````````` example
8073 [Толпой][Толпой] is a Russian word.
8074
8075 [ТОЛПОЙ]: /url
8076 .
8077 <p><a href="/url">Толпой</a> is a Russian word.</p>
8078 ````````````````````````````````
8079
8080
8081 Consecutive internal [whitespace] is treated as one space for
8082 purposes of determining matching:
8083
8084 ```````````````````````````````` example
8085 [Foo
8086 bar]: /url
8087
8088 [Baz][Foo bar]
8089 .
8090 <p><a href="/url">Baz</a></p>
8091 ````````````````````````````````
8092
8093
8094 No [whitespace] is allowed between the [link text] and the
8095 [link label]:
8096
8097 ```````````````````````````````` example
8098 [foo] [bar]
8099
8100 [bar]: /url "title"
8101 .
8102 <p>[foo] <a href="/url" title="title">bar</a></p>
8103 ````````````````````````````````
8104
8105
8106 ```````````````````````````````` example
8107 [foo]
8108 [bar]
8109
8110 [bar]: /url "title"
8111 .
8112 <p>[foo]
8113 <a href="/url" title="title">bar</a></p>
8114 ````````````````````````````````
8115
8116
8117 This is a departure from John Gruber's original Markdown syntax
8118 description, which explicitly allows whitespace between the link
8119 text and the link label. It brings reference links in line with
8120 [inline links], which (according to both original Markdown and
8121 this spec) cannot have whitespace after the link text. More
8122 importantly, it prevents inadvertent capture of consecutive
8123 [shortcut reference links]. If whitespace is allowed between the
8124 link text and the link label, then in the following we will have
8125 a single reference link, not two shortcut reference links, as
8126 intended:
8127
8128 ``` markdown
8129 [foo]
8130 [bar]
8131
8132 [foo]: /url1
8133 [bar]: /url2
8134 ```
8135
8136 (Note that [shortcut reference links] were introduced by Gruber
8137 himself in a beta version of `Markdown.pl`, but never included
8138 in the official syntax description. Without shortcut reference
8139 links, it is harmless to allow space between the link text and
8140 link label; but once shortcut references are introduced, it is
8141 too dangerous to allow this, as it frequently leads to
8142 unintended results.)
8143
8144 When there are multiple matching [link reference definitions],
8145 the first is used:
8146
8147 ```````````````````````````````` example
8148 [foo]: /url1
8149
8150 [foo]: /url2
8151
8152 [bar][foo]
8153 .
8154 <p><a href="/url1">bar</a></p>
8155 ````````````````````````````````
8156
8157
8158 Note that matching is performed on normalized strings, not parsed
8159 inline content. So the following does not match, even though the
8160 labels define equivalent inline content:
8161
8162 ```````````````````````````````` example
8163 [bar][foo\!]
8164
8165 [foo!]: /url
8166 .
8167 <p>[bar][foo!]</p>
8168 ````````````````````````````````
8169
8170
8171 [Link labels] cannot contain brackets, unless they are
8172 backslash-escaped:
8173
8174 ```````````````````````````````` example
8175 [foo][ref[]
8176
8177 [ref[]: /uri
8178 .
8179 <p>[foo][ref[]</p>
8180 <p>[ref[]: /uri</p>
8181 ````````````````````````````````
8182
8183
8184 ```````````````````````````````` example
8185 [foo][ref[bar]]
8186
8187 [ref[bar]]: /uri
8188 .
8189 <p>[foo][ref[bar]]</p>
8190 <p>[ref[bar]]: /uri</p>
8191 ````````````````````````````````
8192
8193
8194 ```````````````````````````````` example
8195 [[[foo]]]
8196
8197 [[[foo]]]: /url
8198 .
8199 <p>[[[foo]]]</p>
8200 <p>[[[foo]]]: /url</p>
8201 ````````````````````````````````
8202
8203
8204 ```````````````````````````````` example
8205 [foo][ref\[]
8206
8207 [ref\[]: /uri
8208 .
8209 <p><a href="/uri">foo</a></p>
8210 ````````````````````````````````
8211
8212
8213 Note that in this example `]` is not backslash-escaped:
8214
8215 ```````````````````````````````` example
8216 [bar\\]: /uri
8217
8218 [bar\\]
8219 .
8220 <p><a href="/uri">bar\</a></p>
8221 ````````````````````````````````
8222
8223
8224 A [link label] must contain at least one [non-whitespace character]:
8225
8226 ```````````````````````````````` example
8227 []
8228
8229 []: /uri
8230 .
8231 <p>[]</p>
8232 <p>[]: /uri</p>
8233 ````````````````````````````````
8234
8235
8236 ```````````````````````````````` example
8237 [
8238 ]
8239
8240 [
8241 ]: /uri
8242 .
8243 <p>[
8244 ]</p>
8245 <p>[
8246 ]: /uri</p>
8247 ````````````````````````````````
8248
8249
8250 A [collapsed reference link](@)
8251 consists of a [link label] that [matches] a
8252 [link reference definition] elsewhere in the
8253 document, followed by the string `[]`.
8254 The contents of the first link label are parsed as inlines,
8255 which are used as the link's text. The link's URI and title are
8256 provided by the matching reference link definition. Thus,
8257 `[foo][]` is equivalent to `[foo][foo]`.
8258
8259 ```````````````````````````````` example
8260 [foo][]
8261
8262 [foo]: /url "title"
8263 .
8264 <p><a href="/url" title="title">foo</a></p>
8265 ````````````````````````````````
8266
8267
8268 ```````````````````````````````` example
8269 [*foo* bar][]
8270
8271 [*foo* bar]: /url "title"
8272 .
8273 <p><a href="/url" title="title"><em>foo</em> bar</a></p>
8274 ````````````````````````````````
8275
8276
8277 The link labels are case-insensitive:
8278
8279 ```````````````````````````````` example
8280 [Foo][]
8281
8282 [foo]: /url "title"
8283 .
8284 <p><a href="/url" title="title">Foo</a></p>
8285 ````````````````````````````````
8286
8287
8288
8289 As with full reference links, [whitespace] is not
8290 allowed between the two sets of brackets:
8291
8292 ```````````````````````````````` example
8293 [foo]
8294 []
8295
8296 [foo]: /url "title"
8297 .
8298 <p><a href="/url" title="title">foo</a>
8299 []</p>
8300 ````````````````````````````````
8301
8302
8303 A [shortcut reference link](@)
8304 consists of a [link label] that [matches] a
8305 [link reference definition] elsewhere in the
8306 document and is not followed by `[]` or a link label.
8307 The contents of the first link label are parsed as inlines,
8308 which are used as the link's text. The link's URI and title
8309 are provided by the matching link reference definition.
8310 Thus, `[foo]` is equivalent to `[foo][]`.
8311
8312 ```````````````````````````````` example
8313 [foo]
8314
8315 [foo]: /url "title"
8316 .
8317 <p><a href="/url" title="title">foo</a></p>
8318 ````````````````````````````````
8319
8320
8321 ```````````````````````````````` example
8322 [*foo* bar]
8323
8324 [*foo* bar]: /url "title"
8325 .
8326 <p><a href="/url" title="title"><em>foo</em> bar</a></p>
8327 ````````````````````````````````
8328
8329
8330 ```````````````````````````````` example
8331 [[*foo* bar]]
8332
8333 [*foo* bar]: /url "title"
8334 .
8335 <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
8336 ````````````````````````````````
8337
8338
8339 ```````````````````````````````` example
8340 [[bar [foo]
8341
8342 [foo]: /url
8343 .
8344 <p>[[bar <a href="/url">foo</a></p>
8345 ````````````````````````````````
8346
8347
8348 The link labels are case-insensitive:
8349
8350 ```````````````````````````````` example
8351 [Foo]
8352
8353 [foo]: /url "title"
8354 .
8355 <p><a href="/url" title="title">Foo</a></p>
8356 ````````````````````````````````
8357
8358
8359 A space after the link text should be preserved:
8360
8361 ```````````````````````````````` example
8362 [foo] bar
8363
8364 [foo]: /url
8365 .
8366 <p><a href="/url">foo</a> bar</p>
8367 ````````````````````````````````
8368
8369
8370 If you just want bracketed text, you can backslash-escape the
8371 opening bracket to avoid links:
8372
8373 ```````````````````````````````` example
8374 \[foo]
8375
8376 [foo]: /url "title"
8377 .
8378 <p>[foo]</p>
8379 ````````````````````````````````
8380
8381
8382 Note that this is a link, because a link label ends with the first
8383 following closing bracket:
8384
8385 ```````````````````````````````` example
8386 [foo*]: /url
8387
8388 *[foo*]
8389 .
8390 <p>*<a href="/url">foo*</a></p>
8391 ````````````````````````````````
8392
8393
8394 Full and compact references take precedence over shortcut
8395 references:
8396
8397 ```````````````````````````````` example
8398 [foo][bar]
8399
8400 [foo]: /url1
8401 [bar]: /url2
8402 .
8403 <p><a href="/url2">foo</a></p>
8404 ````````````````````````````````
8405
8406 ```````````````````````````````` example
8407 [foo][]
8408
8409 [foo]: /url1
8410 .
8411 <p><a href="/url1">foo</a></p>
8412 ````````````````````````````````
8413
8414 Inline links also take precedence:
8415
8416 ```````````````````````````````` example
8417 [foo]()
8418
8419 [foo]: /url1
8420 .
8421 <p><a href="">foo</a></p>
8422 ````````````````````````````````
8423
8424 ```````````````````````````````` example
8425 [foo](not a link)
8426
8427 [foo]: /url1
8428 .
8429 <p><a href="/url1">foo</a>(not a link)</p>
8430 ````````````````````````````````
8431
8432 In the following case `[bar][baz]` is parsed as a reference,
8433 `[foo]` as normal text:
8434
8435 ```````````````````````````````` example
8436 [foo][bar][baz]
8437
8438 [baz]: /url
8439 .
8440 <p>[foo]<a href="/url">bar</a></p>
8441 ````````````````````````````````
8442
8443
8444 Here, though, `[foo][bar]` is parsed as a reference, since
8445 `[bar]` is defined:
8446
8447 ```````````````````````````````` example
8448 [foo][bar][baz]
8449
8450 [baz]: /url1
8451 [bar]: /url2
8452 .
8453 <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
8454 ````````````````````````````````
8455
8456
8457 Here `[foo]` is not parsed as a shortcut reference, because it
8458 is followed by a link label (even though `[bar]` is not defined):
8459
8460 ```````````````````````````````` example
8461 [foo][bar][baz]
8462
8463 [baz]: /url1
8464 [foo]: /url2
8465 .
8466 <p>[foo]<a href="/url1">bar</a></p>
8467 ````````````````````````````````
8468
8469
8470
8471 ## Images
8472
8473 Syntax for images is like the syntax for links, with one
8474 difference. Instead of [link text], we have an
8475 [image description](@). The rules for this are the
8476 same as for [link text], except that (a) an
8477 image description starts with `![` rather than `[`, and
8478 (b) an image description may contain links.
8479 An image description has inline elements
8480 as its contents. When an image is rendered to HTML,
8481 this is standardly used as the image's `alt` attribute.
8482
8483 ```````````````````````````````` example
8484 ![foo](/url "title")
8485 .
8486 <p><img src="/url" alt="foo" title="title" /></p>
8487 ````````````````````````````````
8488
8489
8490 ```````````````````````````````` example
8491 ![foo *bar*]
8492
8493 [foo *bar*]: train.jpg "train & tracks"
8494 .
8495 <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
8496 ````````````````````````````````
8497
8498
8499 ```````````````````````````````` example
8500 ![foo ![bar](/url)](/url2)
8501 .
8502 <p><img src="/url2" alt="foo bar" /></p>
8503 ````````````````````````````````
8504
8505
8506 ```````````````````````````````` example
8507 ![foo [bar](/url)](/url2)
8508 .
8509 <p><img src="/url2" alt="foo bar" /></p>
8510 ````````````````````````````````
8511
8512
8513 Though this spec is concerned with parsing, not rendering, it is
8514 recommended that in rendering to HTML, only the plain string content
8515 of the [image description] be used. Note that in
8516 the above example, the alt attribute's value is `foo bar`, not `foo
8517 [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
8518 content is rendered, without formatting.
8519
8520 ```````````````````````````````` example
8521 ![foo *bar*][]
8522
8523 [foo *bar*]: train.jpg "train & tracks"
8524 .
8525 <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
8526 ````````````````````````````````
8527
8528
8529 ```````````````````````````````` example
8530 ![foo *bar*][foobar]
8531
8532 [FOOBAR]: train.jpg "train & tracks"
8533 .
8534 <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
8535 ````````````````````````````````
8536
8537
8538 ```````````````````````````````` example
8539 ![foo](train.jpg)
8540 .
8541 <p><img src="train.jpg" alt="foo" /></p>
8542 ````````````````````````````````
8543
8544
8545 ```````````````````````````````` example
8546 My ![foo bar](/path/to/train.jpg "title" )
8547 .
8548 <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
8549 ````````````````````````````````
8550
8551
8552 ```````````````````````````````` example
8553 ![foo](<url>)
8554 .
8555 <p><img src="url" alt="foo" /></p>
8556 ````````````````````````````````
8557
8558
8559 ```````````````````````````````` example
8560 ![](/url)
8561 .
8562 <p><img src="/url" alt="" /></p>
8563 ````````````````````````````````
8564
8565
8566 Reference-style:
8567
8568 ```````````````````````````````` example
8569 ![foo][bar]
8570
8571 [bar]: /url
8572 .
8573 <p><img src="/url" alt="foo" /></p>
8574 ````````````````````````````````
8575
8576
8577 ```````````````````````````````` example
8578 ![foo][bar]
8579
8580 [BAR]: /url
8581 .
8582 <p><img src="/url" alt="foo" /></p>
8583 ````````````````````````````````
8584
8585
8586 Collapsed:
8587
8588 ```````````````````````````````` example
8589 ![foo][]
8590
8591 [foo]: /url "title"
8592 .
8593 <p><img src="/url" alt="foo" title="title" /></p>
8594 ````````````````````````````````
8595
8596
8597 ```````````````````````````````` example
8598 ![*foo* bar][]
8599
8600 [*foo* bar]: /url "title"
8601 .
8602 <p><img src="/url" alt="foo bar" title="title" /></p>
8603 ````````````````````````````````
8604
8605
8606 The labels are case-insensitive:
8607
8608 ```````````````````````````````` example
8609 ![Foo][]
8610
8611 [foo]: /url "title"
8612 .
8613 <p><img src="/url" alt="Foo" title="title" /></p>
8614 ````````````````````````````````
8615
8616
8617 As with reference links, [whitespace] is not allowed
8618 between the two sets of brackets:
8619
8620 ```````````````````````````````` example
8621 ![foo]
8622 []
8623
8624 [foo]: /url "title"
8625 .
8626 <p><img src="/url" alt="foo" title="title" />
8627 []</p>
8628 ````````````````````````````````
8629
8630
8631 Shortcut:
8632
8633 ```````````````````````````````` example
8634 ![foo]
8635
8636 [foo]: /url "title"
8637 .
8638 <p><img src="/url" alt="foo" title="title" /></p>
8639 ````````````````````````````````
8640
8641
8642 ```````````````````````````````` example
8643 ![*foo* bar]
8644
8645 [*foo* bar]: /url "title"
8646 .
8647 <p><img src="/url" alt="foo bar" title="title" /></p>
8648 ````````````````````````````````
8649
8650
8651 Note that link labels cannot contain unescaped brackets:
8652
8653 ```````````````````````````````` example
8654 ![[foo]]
8655
8656 [[foo]]: /url "title"
8657 .
8658 <p>![[foo]]</p>
8659 <p>[[foo]]: /url &quot;title&quot;</p>
8660 ````````````````````````````````
8661
8662
8663 The link labels are case-insensitive:
8664
8665 ```````````````````````````````` example
8666 ![Foo]
8667
8668 [foo]: /url "title"
8669 .
8670 <p><img src="/url" alt="Foo" title="title" /></p>
8671 ````````````````````````````````
8672
8673
8674 If you just want a literal `!` followed by bracketed text, you can
8675 backslash-escape the opening `[`:
8676
8677 ```````````````````````````````` example
8678 !\[foo]
8679
8680 [foo]: /url "title"
8681 .
8682 <p>![foo]</p>
8683 ````````````````````````````````
8684
8685
8686 If you want a link after a literal `!`, backslash-escape the
8687 `!`:
8688
8689 ```````````````````````````````` example
8690 \![foo]
8691
8692 [foo]: /url "title"
8693 .
8694 <p>!<a href="/url" title="title">foo</a></p>
8695 ````````````````````````````````
8696
8697
8698 ## Autolinks
8699
8700 [Autolink](@)s are absolute URIs and email addresses inside
8701 `<` and `>`. They are parsed as links, with the URL or email address
8702 as the link label.
8703
8704 A [URI autolink](@) consists of `<`, followed by an
8705 [absolute URI] followed by `>`. It is parsed as
8706 a link to the URI, with the URI as the link's label.
8707
8708 An [absolute URI](@),
8709 for these purposes, consists of a [scheme] followed by a colon (`:`)
8710 followed by zero or more characters other than ASCII
8711 [whitespace] and control characters, `<`, and `>`. If
8712 the URI includes these characters, they must be percent-encoded
8713 (e.g. `%20` for a space).
8714
8715 For purposes of this spec, a [scheme](@) is any sequence
8716 of 2--32 characters beginning with an ASCII letter and followed
8717 by any combination of ASCII letters, digits, or the symbols plus
8718 ("+"), period ("."), or hyphen ("-").
8719
8720 Here are some valid autolinks:
8721
8722 ```````````````````````````````` example
8723 <http://foo.bar.baz>
8724 .
8725 <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
8726 ````````````````````````````````
8727
8728
8729 ```````````````````````````````` example
8730 <http://foo.bar.baz/test?q=hello&id=22&boolean>
8731 .
8732 <p><a href="http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean">http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean</a></p>
8733 ````````````````````````````````
8734
8735
8736 ```````````````````````````````` example
8737 <irc://foo.bar:2233/baz>
8738 .
8739 <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
8740 ````````````````````````````````
8741
8742
8743 Uppercase is also fine:
8744
8745 ```````````````````````````````` example
8746 <MAILTO:[email protected]>
8747 .
8748 <p><a href="MAILTO:[email protected]">MAILTO:[email protected]</a></p>
8749 ````````````````````````````````
8750
8751
8752 Note that many strings that count as [absolute URIs] for
8753 purposes of this spec are not valid URIs, because their
8754 schemes are not registered or because of other problems
8755 with their syntax:
8756
8757 ```````````````````````````````` example
8758 <a+b+c:d>
8759 .
8760 <p><a href="a+b+c:d">a+b+c:d</a></p>
8761 ````````````````````````````````
8762
8763
8764 ```````````````````````````````` example
8765 <made-up-scheme://foo,bar>
8766 .
8767 <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
8768 ````````````````````````````````
8769
8770
8771 ```````````````````````````````` example
8772 <http://../>
8773 .
8774 <p><a href="http://../">http://../</a></p>
8775 ````````````````````````````````
8776
8777
8778 ```````````````````````````````` example
8779 <localhost:5001/foo>
8780 .
8781 <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
8782 ````````````````````````````````
8783
8784
8785 Spaces are not allowed in autolinks:
8786
8787 ```````````````````````````````` example
8788 <http://foo.bar/baz bim>
8789 .
8790 <p>&lt;http://foo.bar/baz bim&gt;</p>
8791 ````````````````````````````````
8792
8793
8794 Backslash-escapes do not work inside autolinks:
8795
8796 ```````````````````````````````` example
8797 <http://example.com/\[\>
8798 .
8799 <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
8800 ````````````````````````````````
8801
8802
8803 An [email autolink](@)
8804 consists of `<`, followed by an [email address],
8805 followed by `>`. The link's label is the email address,
8806 and the URL is `mailto:` followed by the email address.
8807
8808 An [email address](@),
8809 for these purposes, is anything that matches
8810 the [non-normative regex from the HTML5
8811 spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
8812
8813 /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
8814 (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
8815
8816 Examples of email autolinks:
8817
8818 ```````````````````````````````` example
8819 <[email protected]>
8820 .
8821 <p><a href="mailto:[email protected]">[email protected]</a></p>
8822 ````````````````````````````````
8823
8824
8825 ```````````````````````````````` example
8826 <[email protected]>
8827 .
8828 <p><a href="mailto:[email protected]">[email protected]</a></p>
8829 ````````````````````````````````
8830
8831
8832 Backslash-escapes do not work inside email autolinks:
8833
8834 ```````````````````````````````` example
8835 <foo\[email protected]>
8836 .
8837 <p>&lt;[email protected]&gt;</p>
8838 ````````````````````````````````
8839
8840
8841 These are not autolinks:
8842
8843 ```````````````````````````````` example
8844 <>
8845 .
8846 <p>&lt;&gt;</p>
8847 ````````````````````````````````
8848
8849
8850 ```````````````````````````````` example
8851 < http://foo.bar >
8852 .
8853 <p>&lt; http://foo.bar &gt;</p>
8854 ````````````````````````````````
8855
8856
8857 ```````````````````````````````` example
8858 <m:abc>
8859 .
8860 <p>&lt;m:abc&gt;</p>
8861 ````````````````````````````````
8862
8863
8864 ```````````````````````````````` example
8865 <foo.bar.baz>
8866 .
8867 <p>&lt;foo.bar.baz&gt;</p>
8868 ````````````````````````````````
8869
8870
8871 ```````````````````````````````` example
8872 http://example.com
8873 .
8874 <p>http://example.com</p>
8875 ````````````````````````````````
8876
8877
8878 ```````````````````````````````` example
8879 [email protected]
8880 .
8881 <p>[email protected]</p>
8882 ````````````````````````````````
8883
8884
8885 ## Raw HTML
8886
8887 Text between `<` and `>` that looks like an HTML tag is parsed as a
8888 raw HTML tag and will be rendered in HTML without escaping.
8889 Tag and attribute names are not limited to current HTML tags,
8890 so custom tags (and even, say, DocBook tags) may be used.
8891
8892 Here is the grammar for tags:
8893
8894 A [tag name](@) consists of an ASCII letter
8895 followed by zero or more ASCII letters, digits, or
8896 hyphens (`-`).
8897
8898 An [attribute](@) consists of [whitespace],
8899 an [attribute name], and an optional
8900 [attribute value specification].
8901
8902 An [attribute name](@)
8903 consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
8904 letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
8905 specification restricted to ASCII. HTML5 is laxer.)
8906
8907 An [attribute value specification](@)
8908 consists of optional [whitespace],
8909 a `=` character, optional [whitespace], and an [attribute
8910 value].
8911
8912 An [attribute value](@)
8913 consists of an [unquoted attribute value],
8914 a [single-quoted attribute value], or a [double-quoted attribute value].
8915
8916 An [unquoted attribute value](@)
8917 is a nonempty string of characters not
8918 including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
8919
8920 A [single-quoted attribute value](@)
8921 consists of `'`, zero or more
8922 characters not including `'`, and a final `'`.
8923
8924 A [double-quoted attribute value](@)
8925 consists of `"`, zero or more
8926 characters not including `"`, and a final `"`.
8927
8928 An [open tag](@) consists of a `<` character, a [tag name],
8929 zero or more [attributes], optional [whitespace], an optional `/`
8930 character, and a `>` character.
8931
8932 A [closing tag](@) consists of the string `</`, a
8933 [tag name], optional [whitespace], and the character `>`.
8934
8935 An [HTML comment](@) consists of `<!--` + *text* + `-->`,
8936 where *text* does not start with `>` or `->`, does not end with `-`,
8937 and does not contain `--`. (See the
8938 [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
8939
8940 A [processing instruction](@)
8941 consists of the string `<?`, a string
8942 of characters not including the string `?>`, and the string
8943 `?>`.
8944
8945 A [declaration](@) consists of the
8946 string `<!`, a name consisting of one or more uppercase ASCII letters,
8947 [whitespace], a string of characters not including the
8948 character `>`, and the character `>`.
8949
8950 A [CDATA section](@) consists of
8951 the string `<![CDATA[`, a string of characters not including the string
8952 `]]>`, and the string `]]>`.
8953
8954 An [HTML tag](@) consists of an [open tag], a [closing tag],
8955 an [HTML comment], a [processing instruction], a [declaration],
8956 or a [CDATA section].
8957
8958 Here are some simple open tags:
8959
8960 ```````````````````````````````` example
8961 <a><bab><c2c>
8962 .
8963 <p><a><bab><c2c></p>
8964 ````````````````````````````````
8965
8966
8967 Empty elements:
8968
8969 ```````````````````````````````` example
8970 <a/><b2/>
8971 .
8972 <p><a/><b2/></p>
8973 ````````````````````````````````
8974
8975
8976 [Whitespace] is allowed:
8977
8978 ```````````````````````````````` example
8979 <a /><b2
8980 data="foo" >
8981 .
8982 <p><a /><b2
8983 data="foo" ></p>
8984 ````````````````````````````````
8985
8986
8987 With attributes:
8988
8989 ```````````````````````````````` example
8990 <a foo="bar" bam = 'baz <em>"</em>'
8991 _boolean zoop:33=zoop:33 />
8992 .
8993 <p><a foo="bar" bam = 'baz <em>"</em>'
8994 _boolean zoop:33=zoop:33 /></p>
8995 ````````````````````````````````
8996
8997
8998 Custom tag names can be used:
8999
9000 ```````````````````````````````` example
9001 Foo <responsive-image src="foo.jpg" />
9002 .
9003 <p>Foo <responsive-image src="foo.jpg" /></p>
9004 ````````````````````````````````
9005
9006
9007 Illegal tag names, not parsed as HTML:
9008
9009 ```````````````````````````````` example
9010 <33> <__>
9011 .
9012 <p>&lt;33&gt; &lt;__&gt;</p>
9013 ````````````````````````````````
9014
9015
9016 Illegal attribute names:
9017
9018 ```````````````````````````````` example
9019 <a h*#ref="hi">
9020 .
9021 <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
9022 ````````````````````````````````
9023
9024
9025 Illegal attribute values:
9026
9027 ```````````````````````````````` example
9028 <a href="hi'> <a href=hi'>
9029 .
9030 <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
9031 ````````````````````````````````
9032
9033
9034 Illegal [whitespace]:
9035
9036 ```````````````````````````````` example
9037 < a><
9038 foo><bar/ >
9039 <foo bar=baz
9040 bim!bop />
9041 .
9042 <p>&lt; a&gt;&lt;
9043 foo&gt;&lt;bar/ &gt;
9044 &lt;foo bar=baz
9045 bim!bop /&gt;</p>
9046 ````````````````````````````````
9047
9048
9049 Missing [whitespace]:
9050
9051 ```````````````````````````````` example
9052 <a href='bar'title=title>
9053 .
9054 <p>&lt;a href='bar'title=title&gt;</p>
9055 ````````````````````````````````
9056
9057
9058 Closing tags:
9059
9060 ```````````````````````````````` example
9061 </a></foo >
9062 .
9063 <p></a></foo ></p>
9064 ````````````````````````````````
9065
9066
9067 Illegal attributes in closing tag:
9068
9069 ```````````````````````````````` example
9070 </a href="foo">
9071 .
9072 <p>&lt;/a href=&quot;foo&quot;&gt;</p>
9073 ````````````````````````````````
9074
9075
9076 Comments:
9077
9078 ```````````````````````````````` example
9079 foo <!-- this is a
9080 comment - with hyphen -->
9081 .
9082 <p>foo <!-- this is a
9083 comment - with hyphen --></p>
9084 ````````````````````````````````
9085
9086
9087 ```````````````````````````````` example
9088 foo <!-- not a comment -- two hyphens -->
9089 .
9090 <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
9091 ````````````````````````````````
9092
9093
9094 Not comments:
9095
9096 ```````````````````````````````` example
9097 foo <!--> foo -->
9098
9099 foo <!-- foo--->
9100 .
9101 <p>foo &lt;!--&gt; foo --&gt;</p>
9102 <p>foo &lt;!-- foo---&gt;</p>
9103 ````````````````````````````````
9104
9105
9106 Processing instructions:
9107
9108 ```````````````````````````````` example
9109 foo <?php echo $a; ?>
9110 .
9111 <p>foo <?php echo $a; ?></p>
9112 ````````````````````````````````
9113
9114
9115 Declarations:
9116
9117 ```````````````````````````````` example
9118 foo <!ELEMENT br EMPTY>
9119 .
9120 <p>foo <!ELEMENT br EMPTY></p>
9121 ````````````````````````````````
9122
9123
9124 CDATA sections:
9125
9126 ```````````````````````````````` example
9127 foo <![CDATA[>&<]]>
9128 .
9129 <p>foo <![CDATA[>&<]]></p>
9130 ````````````````````````````````
9131
9132
9133 Entity and numeric character references are preserved in HTML
9134 attributes:
9135
9136 ```````````````````````````````` example
9137 foo <a href="&ouml;">
9138 .
9139 <p>foo <a href="&ouml;"></p>
9140 ````````````````````````````````
9141
9142
9143 Backslash escapes do not work in HTML attributes:
9144
9145 ```````````````````````````````` example
9146 foo <a href="\*">
9147 .
9148 <p>foo <a href="\*"></p>
9149 ````````````````````````````````
9150
9151
9152 ```````````````````````````````` example
9153 <a href="\"">
9154 .
9155 <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
9156 ````````````````````````````````
9157
9158
9159 ## Hard line breaks
9160
9161 A line break (not in a code span or HTML tag) that is preceded
9162 by two or more spaces and does not occur at the end of a block
9163 is parsed as a [hard line break](@) (rendered
9164 in HTML as a `<br />` tag):
9165
9166 ```````````````````````````````` example
9167 foo
9168 baz
9169 .
9170 <p>foo<br />
9171 baz</p>
9172 ````````````````````````````````
9173
9174
9175 For a more visible alternative, a backslash before the
9176 [line ending] may be used instead of two spaces:
9177
9178 ```````````````````````````````` example
9179 foo\
9180 baz
9181 .
9182 <p>foo<br />
9183 baz</p>
9184 ````````````````````````````````
9185
9186
9187 More than two spaces can be used:
9188
9189 ```````````````````````````````` example
9190 foo
9191 baz
9192 .
9193 <p>foo<br />
9194 baz</p>
9195 ````````````````````````````````
9196
9197
9198 Leading spaces at the beginning of the next line are ignored:
9199
9200 ```````````````````````````````` example
9201 foo
9202 bar
9203 .
9204 <p>foo<br />
9205 bar</p>
9206 ````````````````````````````````
9207
9208
9209 ```````````````````````````````` example
9210 foo\
9211 bar
9212 .
9213 <p>foo<br />
9214 bar</p>
9215 ````````````````````````````````
9216
9217
9218 Line breaks can occur inside emphasis, links, and other constructs
9219 that allow inline content:
9220
9221 ```````````````````````````````` example
9222 *foo
9223 bar*
9224 .
9225 <p><em>foo<br />
9226 bar</em></p>
9227 ````````````````````````````````
9228
9229
9230 ```````````````````````````````` example
9231 *foo\
9232 bar*
9233 .
9234 <p><em>foo<br />
9235 bar</em></p>
9236 ````````````````````````````````
9237
9238
9239 Line breaks do not occur inside code spans
9240
9241 ```````````````````````````````` example
9242 `code
9243 span`
9244 .
9245 <p><code>code span</code></p>
9246 ````````````````````````````````
9247
9248
9249 ```````````````````````````````` example
9250 `code\
9251 span`
9252 .
9253 <p><code>code\ span</code></p>
9254 ````````````````````````````````
9255
9256
9257 or HTML tags:
9258
9259 ```````````````````````````````` example
9260 <a href="foo
9261 bar">
9262 .
9263 <p><a href="foo
9264 bar"></p>
9265 ````````````````````````````````
9266
9267
9268 ```````````````````````````````` example
9269 <a href="foo\
9270 bar">
9271 .
9272 <p><a href="foo\
9273 bar"></p>
9274 ````````````````````````````````
9275
9276
9277 Hard line breaks are for separating inline content within a block.
9278 Neither syntax for hard line breaks works at the end of a paragraph or
9279 other block element:
9280
9281 ```````````````````````````````` example
9282 foo\
9283 .
9284 <p>foo\</p>
9285 ````````````````````````````````
9286
9287
9288 ```````````````````````````````` example
9289 foo
9290 .
9291 <p>foo</p>
9292 ````````````````````````````````
9293
9294
9295 ```````````````````````````````` example
9296 ### foo\
9297 .
9298 <h3>foo\</h3>
9299 ````````````````````````````````
9300
9301
9302 ```````````````````````````````` example
9303 ### foo
9304 .
9305 <h3>foo</h3>
9306 ````````````````````````````````
9307
9308
9309 ## Soft line breaks
9310
9311 A regular line break (not in a code span or HTML tag) that is not
9312 preceded by two or more spaces or a backslash is parsed as a
9313 [softbreak](@). (A softbreak may be rendered in HTML either as a
9314 [line ending] or as a space. The result will be the same in
9315 browsers. In the examples here, a [line ending] will be used.)
9316
9317 ```````````````````````````````` example
9318 foo
9319 baz
9320 .
9321 <p>foo
9322 baz</p>
9323 ````````````````````````````````
9324
9325
9326 Spaces at the end of the line and beginning of the next line are
9327 removed:
9328
9329 ```````````````````````````````` example
9330 foo
9331 baz
9332 .
9333 <p>foo
9334 baz</p>
9335 ````````````````````````````````
9336
9337
9338 A conforming parser may render a soft line break in HTML either as a
9339 line break or as a space.
9340
9341 A renderer may also provide an option to render soft line breaks
9342 as hard line breaks.
9343
9344 ## Textual content
9345
9346 Any characters not given an interpretation by the above rules will
9347 be parsed as plain textual content.
9348
9349 ```````````````````````````````` example
9350 hello $.;'there
9351 .
9352 <p>hello $.;'there</p>
9353 ````````````````````````````````
9354
9355
9356 ```````````````````````````````` example
9357 Foo χρῆν
9358 .
9359 <p>Foo χρῆν</p>
9360 ````````````````````````````````
9361
9362
9363 Internal spaces are preserved verbatim:
9364
9365 ```````````````````````````````` example
9366 Multiple spaces
9367 .
9368 <p>Multiple spaces</p>
9369 ````````````````````````````````
9370
9371
9372 <!-- END TESTS -->
9373
9374 # Appendix: A parsing strategy
9375
9376 In this appendix we describe some features of the parsing strategy
9377 used in the CommonMark reference implementations.
9378
9379 ## Overview
9380
9381 Parsing has two phases:
9382
9383 1. In the first phase, lines of input are consumed and the block
9384 structure of the document---its division into paragraphs, block quotes,
9385 list items, and so on---is constructed. Text is assigned to these
9386 blocks but not parsed. Link reference definitions are parsed and a
9387 map of links is constructed.
9388
9389 2. In the second phase, the raw text contents of paragraphs and headings
9390 are parsed into sequences of Markdown inline elements (strings,
9391 code spans, links, emphasis, and so on), using the map of link
9392 references constructed in phase 1.
9393
9394 At each point in processing, the document is represented as a tree of
9395 **blocks**. The root of the tree is a `document` block. The `document`
9396 may have any number of other blocks as **children**. These children
9397 may, in turn, have other blocks as children. The last child of a block
9398 is normally considered **open**, meaning that subsequent lines of input
9399 can alter its contents. (Blocks that are not open are **closed**.)
9400 Here, for example, is a possible document tree, with the open blocks
9401 marked by arrows:
9402
9403 ``` tree
9404 -> document
9405 -> block_quote
9406 paragraph
9407 "Lorem ipsum dolor\nsit amet."
9408 -> list (type=bullet tight=true bullet_char=-)
9409 list_item
9410 paragraph
9411 "Qui *quodsi iracundia*"
9412 -> list_item
9413 -> paragraph
9414 "aliquando id"
9415 ```
9416
9417 ## Phase 1: block structure
9418
9419 Each line that is processed has an effect on this tree. The line is
9420 analyzed and, depending on its contents, the document may be altered
9421 in one or more of the following ways:
9422
9423 1. One or more open blocks may be closed.
9424 2. One or more new blocks may be created as children of the
9425 last open block.
9426 3. Text may be added to the last (deepest) open block remaining
9427 on the tree.
9428
9429 Once a line has been incorporated into the tree in this way,
9430 it can be discarded, so input can be read in a stream.
9431
9432 For each line, we follow this procedure:
9433
9434 1. First we iterate through the open blocks, starting with the
9435 root document, and descending through last children down to the last
9436 open block. Each block imposes a condition that the line must satisfy
9437 if the block is to remain open. For example, a block quote requires a
9438 `>` character. A paragraph requires a non-blank line.
9439 In this phase we may match all or just some of the open
9440 blocks. But we cannot close unmatched blocks yet, because we may have a
9441 [lazy continuation line].
9442
9443 2. Next, after consuming the continuation markers for existing
9444 blocks, we look for new block starts (e.g. `>` for a block quote).
9445 If we encounter a new block start, we close any blocks unmatched
9446 in step 1 before creating the new block as a child of the last
9447 matched block.
9448
9449 3. Finally, we look at the remainder of the line (after block
9450 markers like `>`, list markers, and indentation have been consumed).
9451 This is text that can be incorporated into the last open
9452 block (a paragraph, code block, heading, or raw HTML).
9453
9454 Setext headings are formed when we see a line of a paragraph
9455 that is a [setext heading underline].
9456
9457 Reference link definitions are detected when a paragraph is closed;
9458 the accumulated text lines are parsed to see if they begin with
9459 one or more reference link definitions. Any remainder becomes a
9460 normal paragraph.
9461
9462 We can see how this works by considering how the tree above is
9463 generated by four lines of Markdown:
9464
9465 ``` markdown
9466 > Lorem ipsum dolor
9467 sit amet.
9468 > - Qui *quodsi iracundia*
9469 > - aliquando id
9470 ```
9471
9472 At the outset, our document model is just
9473
9474 ``` tree
9475 -> document
9476 ```
9477
9478 The first line of our text,
9479
9480 ``` markdown
9481 > Lorem ipsum dolor
9482 ```
9483
9484 causes a `block_quote` block to be created as a child of our
9485 open `document` block, and a `paragraph` block as a child of
9486 the `block_quote`. Then the text is added to the last open
9487 block, the `paragraph`:
9488
9489 ``` tree
9490 -> document
9491 -> block_quote
9492 -> paragraph
9493 "Lorem ipsum dolor"
9494 ```
9495
9496 The next line,
9497
9498 ``` markdown
9499 sit amet.
9500 ```
9501
9502 is a "lazy continuation" of the open `paragraph`, so it gets added
9503 to the paragraph's text:
9504
9505 ``` tree
9506 -> document
9507 -> block_quote
9508 -> paragraph
9509 "Lorem ipsum dolor\nsit amet."
9510 ```
9511
9512 The third line,
9513
9514 ``` markdown
9515 > - Qui *quodsi iracundia*
9516 ```
9517
9518 causes the `paragraph` block to be closed, and a new `list` block
9519 opened as a child of the `block_quote`. A `list_item` is also
9520 added as a child of the `list`, and a `paragraph` as a child of
9521 the `list_item`. The text is then added to the new `paragraph`:
9522
9523 ``` tree
9524 -> document
9525 -> block_quote
9526 paragraph
9527 "Lorem ipsum dolor\nsit amet."
9528 -> list (type=bullet tight=true bullet_char=-)
9529 -> list_item
9530 -> paragraph
9531 "Qui *quodsi iracundia*"
9532 ```
9533
9534 The fourth line,
9535
9536 ``` markdown
9537 > - aliquando id
9538 ```
9539
9540 causes the `list_item` (and its child the `paragraph`) to be closed,
9541 and a new `list_item` opened up as child of the `list`. A `paragraph`
9542 is added as a child of the new `list_item`, to contain the text.
9543 We thus obtain the final tree:
9544
9545 ``` tree
9546 -> document
9547 -> block_quote
9548 paragraph
9549 "Lorem ipsum dolor\nsit amet."
9550 -> list (type=bullet tight=true bullet_char=-)
9551 list_item
9552 paragraph
9553 "Qui *quodsi iracundia*"
9554 -> list_item
9555 -> paragraph
9556 "aliquando id"
9557 ```
9558
9559 ## Phase 2: inline structure
9560
9561 Once all of the input has been parsed, all open blocks are closed.
9562
9563 We then "walk the tree," visiting every node, and parse raw
9564 string contents of paragraphs and headings as inlines. At this
9565 point we have seen all the link reference definitions, so we can
9566 resolve reference links as we go.
9567
9568 ``` tree
9569 document
9570 block_quote
9571 paragraph
9572 str "Lorem ipsum dolor"
9573 softbreak
9574 str "sit amet."
9575 list (type=bullet tight=true bullet_char=-)
9576 list_item
9577 paragraph
9578 str "Qui "
9579 emph
9580 str "quodsi iracundia"
9581 list_item
9582 paragraph
9583 str "aliquando id"
9584 ```
9585
9586 Notice how the [line ending] in the first paragraph has
9587 been parsed as a `softbreak`, and the asterisks in the first list item
9588 have become an `emph`.
9589
9590 ### An algorithm for parsing nested emphasis and links
9591
9592 By far the trickiest part of inline parsing is handling emphasis,
9593 strong emphasis, links, and images. This is done using the following
9594 algorithm.
9595
9596 When we're parsing inlines and we hit either
9597
9598 - a run of `*` or `_` characters, or
9599 - a `[` or `![`
9600
9601 we insert a text node with these symbols as its literal content, and we
9602 add a pointer to this text node to the [delimiter stack](@).
9603
9604 The [delimiter stack] is a doubly linked list. Each
9605 element contains a pointer to a text node, plus information about
9606
9607 - the type of delimiter (`[`, `![`, `*`, `_`)
9608 - the number of delimiters,
9609 - whether the delimiter is "active" (all are active to start), and
9610 - whether the delimiter is a potential opener, a potential closer,
9611 or both (which depends on what sort of characters precede
9612 and follow the delimiters).
9613
9614 When we hit a `]` character, we call the *look for link or image*
9615 procedure (see below).
9616
9617 When we hit the end of the input, we call the *process emphasis*
9618 procedure (see below), with `stack_bottom` = NULL.
9619
9620 #### *look for link or image*
9621
9622 Starting at the top of the delimiter stack, we look backwards
9623 through the stack for an opening `[` or `![` delimiter.
9624
9625 - If we don't find one, we return a literal text node `]`.
9626
9627 - If we do find one, but it's not *active*, we remove the inactive
9628 delimiter from the stack, and return a literal text node `]`.
9629
9630 - If we find one and it's active, then we parse ahead to see if
9631 we have an inline link/image, reference link/image, compact reference
9632 link/image, or shortcut reference link/image.
9633
9634 + If we don't, then we remove the opening delimiter from the
9635 delimiter stack and return a literal text node `]`.
9636
9637 + If we do, then
9638
9639 * We return a link or image node whose children are the inlines
9640 after the text node pointed to by the opening delimiter.
9641
9642 * We run *process emphasis* on these inlines, with the `[` opener
9643 as `stack_bottom`.
9644
9645 * We remove the opening delimiter.
9646
9647 * If we have a link (and not an image), we also set all
9648 `[` delimiters before the opening delimiter to *inactive*. (This
9649 will prevent us from getting links within links.)
9650
9651 #### *process emphasis*
9652
9653 Parameter `stack_bottom` sets a lower bound to how far we
9654 descend in the [delimiter stack]. If it is NULL, we can
9655 go all the way to the bottom. Otherwise, we stop before
9656 visiting `stack_bottom`.
9657
9658 Let `current_position` point to the element on the [delimiter stack]
9659 just above `stack_bottom` (or the first element if `stack_bottom`
9660 is NULL).
9661
9662 We keep track of the `openers_bottom` for each delimiter
9663 type (`*`, `_`) and each length of the closing delimiter run
9664 (modulo 3). Initialize this to `stack_bottom`.
9665
9666 Then we repeat the following until we run out of potential
9667 closers:
9668
9669 - Move `current_position` forward in the delimiter stack (if needed)
9670 until we find the first potential closer with delimiter `*` or `_`.
9671 (This will be the potential closer closest
9672 to the beginning of the input -- the first one in parse order.)
9673
9674 - Now, look back in the stack (staying above `stack_bottom` and
9675 the `openers_bottom` for this delimiter type) for the
9676 first matching potential opener ("matching" means same delimiter).
9677
9678 - If one is found:
9679
9680 + Figure out whether we have emphasis or strong emphasis:
9681 if both closer and opener spans have length >= 2, we have
9682 strong, otherwise regular.
9683
9684 + Insert an emph or strong emph node accordingly, after
9685 the text node corresponding to the opener.
9686
9687 + Remove any delimiters between the opener and closer from
9688 the delimiter stack.
9689
9690 + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
9691 from the opening and closing text nodes. If they become empty
9692 as a result, remove them and remove the corresponding element
9693 of the delimiter stack. If the closing node is removed, reset
9694 `current_position` to the next element in the stack.
9695
9696 - If none is found:
9697
9698 + Set `openers_bottom` to the element before `current_position`.
9699 (We know that there are no openers for this kind of closer up to and
9700 including this point, so this puts a lower bound on future searches.)
9701
9702 + If the closer at `current_position` is not a potential opener,
9703 remove it from the delimiter stack (since we know it can't
9704 be a closer either).
9705
9706 + Advance `current_position` to the next element in the stack.
9707
9708 After we're done, we remove all delimiters above `stack_bottom` from the
9709 delimiter stack.
9710