diff --git a/spec.txt b/spec.txt
index 9fd58413..dbb5355f 100644
--- a/spec.txt
+++ b/spec.txt
@@ -102,10 +102,10 @@ source, not just in the processed document.
## Why is a spec needed?
-John Gruber's [canonical description of Markdown's
-syntax](http://daringfireball.net/projects/markdown/syntax)
-does not specify the syntax unambiguously. Here are some examples of
-questions it does not answer:
+John Gruber's [canonical description of Markdown's
+syntax](http://daringfireball.net/projects/markdown/syntax)
+does not specify the syntax unambiguously.
+Here are some examples of questions it does not answer:
1. How much indentation is needed for a sublist? The spec says that
continuation paragraphs need to be indented four spaces, but is
@@ -898,15 +898,18 @@ Spaces are allowed after the closing sequence:
A sequence of `#` characters with anything but [spaces] following it
-is not a closing sequence, but counts as part of the contents of the
-heading:
+is only a closing sequence, if the number of its `#` characters
+exactly matches the respective number in the opening sequence.
+Whatever is following the first matching closing sequence
+is treated as an [info string].
```````````````````````````````` example
### foo ### b
.
-
foo ### b
+foo
````````````````````````````````
+Otherwise it counts as part of the contents of the heading.
The closing sequence must be preceded by a space:
@@ -983,9 +986,9 @@ interpretable as a [code fence], [ATX heading][ATX headings],
A [setext heading underline](@) is a sequence of
`=` characters or a sequence of `-` characters, with no more than 3
-spaces indentation and any number of trailing spaces. If a line
+spaces indentation and any number of trailing spaces. If a line
containing a single `-` can be interpreted as an
-empty [list items], it should be interpreted this way
+empty [list item][list items], it *should* be interpreted this way
and not as a [setext heading underline].
The heading is a level 1 heading if `=` characters are used in
@@ -1863,7 +1866,6 @@ aaa
````````````````````````````````
-
Code fences (opening and closing) cannot contain internal spaces:
```````````````````````````````` example
@@ -7168,7 +7170,8 @@ __a
## Links
A link contains [link text] (the visible text), a [link destination]
-(the URI that is the link destination), and optionally a [link title].
+(the URL that is the link destination), and optionally an [info string]
+that usually starts with or only consists of a [link title].
There are two basic kinds of links in Markdown. In [inline links] the
destination and title are given immediately after the link text. In
[reference links] the destination and title are defined elsewhere in
@@ -7208,7 +7211,14 @@ A [link destination](@) consists of either
may impose limits on parentheses nesting to avoid performance
issues, but at least three levels of nesting should be supported.)
-A [link title](@) consists of either
+A [link info string](@) works like any other [info string],
+but it should contain a [link title], which may be empty
+and should come first. It starts after the first run of whitespace
+that terminates the [link destination].
+All kinds of brackets should occur either in properly nested pairs
+or backslash-escaped inside a link info string.
+
+A [link title](@) consists of either
- a sequence of zero or more characters between straight double-quote
characters (`"`), including a `"` character only if it is
@@ -7239,22 +7249,30 @@ above.
Here is a simple inline link:
```````````````````````````````` example
-[link](/uri "title")
+[link](/url info string)
.
-link
+link
````````````````````````````````
+The info string is usually just a title:
-The title may be omitted:
+```````````````````````````````` example
+[link](/url "title")
+.
+link
+````````````````````````````````
+
+
+The info string may be omitted:
```````````````````````````````` example
-[link](/uri)
+[link](/url)
.
-link
+link
````````````````````````````````
-Both the title and the destination may be omitted:
+Both the info string and the destination may be omitted:
```````````````````````````````` example
[link]()
@@ -7269,32 +7287,60 @@ Both the title and the destination may be omitted:
link
````````````````````````````````
+The link text may be empty, but this will
+usually render the link inaccessible in the output.
+
+```````````````````````````````` example
+[](/url "title")
+.
+
+````````````````````````````````
The destination cannot contain spaces or line breaks,
-even if enclosed in pointy brackets:
+i.e. whitespace.
```````````````````````````````` example
-[link](/my uri)
+[link](/foo bar)
.
-[link](/my uri)
+link
````````````````````````````````
+Except if enclosed in pointy brackets:
```````````````````````````````` example
-[link]()
+[link]()
.
-[link](</my uri>)
+link
````````````````````````````````
+There can be a line break before or after an info string:
```````````````````````````````` example
[link](foo
bar)
.
-[link](foo
-bar)
+link
````````````````````````````````
+```````````````````````````````` example
+[link](foo bar
+)
+.
+link
+````````````````````````````````
+
+But no empty line:
+
+```````````````````````````````` example
+[link](foo
+
+bar)
+.
+[link](foo
+bar)
+````````````````````````````````
+
+There can also be no line break inside the target with angular brackets:
```````````````````````````````` example
[link](link
````````````````````````````````
-Any number of parentheses are allowed without escaping, as long as they are
+Any number of parentheses are allowed without escaping, as long as they are
balanced:
```````````````````````````````` example
@@ -7321,8 +7367,7 @@ balanced:
link
````````````````````````````````
-However, if you have unbalanced parentheses, you need to escape or use the
-`<...>` form:
+However, if you have unbalanced parentheses, you need to escape them:
```````````````````````````````` example
[link](foo\(and\(bar\))
@@ -7330,6 +7375,7 @@ However, if you have unbalanced parentheses, you need to escape or use the
link
````````````````````````````````
+Or use the `<...>` form:
```````````````````````````````` example
[link]()
@@ -7355,15 +7401,18 @@ A link can contain fragment identifiers and queries:
[link](http://example.com#fragment)
+[link](?foo=3)
+
[link](http://example.com?foo=3#frag)
.
link
link
+link
link
````````````````````````````````
-Note that a backslash before a non-escapable character is
+As usual, a backslash before a non-escapable character is
just a backslash:
```````````````````````````````` example
@@ -7388,10 +7437,11 @@ about how to escape or normalize URLs in the output.
link
````````````````````````````````
+(`auml;` = _รค_ = U+00E4 = 0xC3A4 in UTF-8.)
Note that, because titles can often be parsed as destinations,
-if you try to omit the destination and keep the title, you'll
-get unexpected results:
+trying to omit the destination and keep the title,
+will lead to unexpected results:
```````````````````````````````` example
[link]("title")
@@ -7399,17 +7449,30 @@ get unexpected results:
link
````````````````````````````````
+This can be avoided by usually harmless dummy destinations
+that refer to the document itself:
+
+```````````````````````````````` example
+[link](# "title")
+
+[link](? "title")
+.
+link
+link
+````````````````````````````````
Titles may be in single quotes, double quotes, or parentheses:
```````````````````````````````` example
[link](/url "title")
+
[link](/url 'title')
+
[link](/url (title))
.
-link
-link
-link
+link
+link
+link
````````````````````````````````
@@ -7417,13 +7480,15 @@ Backslash escapes and entity and numeric character references
may be used in titles:
```````````````````````````````` example
-[link](/url "title \""")
+[link](/url "title \""""")
.
-link
+link
````````````````````````````````
+(Whether or not character references are normalized to a canonic form
+should probably be left to the implementation and output format.)
-Titles must be separated from the link using a [whitespace].
+Info strings must be separated from the destination using a [whitespace].
Other [Unicode whitespace] like non-breaking space doesn't work.
```````````````````````````````` example
@@ -7466,13 +7531,13 @@ titles with no closing quotation mark, though 1.0.2b8 does not.
It seems preferable to adopt a simple, rational rule that works
the same way in inline links and link reference definitions.)
-[Whitespace] is allowed around the destination and title:
+[Whitespace] is allowed around the destination and info string:
```````````````````````````````` example
-[link]( /uri
+[link]( /url
"title" )
.
-link
+link
````````````````````````````````
@@ -7480,79 +7545,87 @@ But it is not allowed between the link text and the
following parenthesis:
```````````````````````````````` example
-[link] (/uri)
+[link] (/url)
.
-[link] (/uri)
+[link] (/url)
````````````````````````````````
-The link text may contain balanced brackets, but not unbalanced ones,
-unless they are escaped:
+The link text may contain balanced brackets:
```````````````````````````````` example
-[link [foo [bar]]](/uri)
+[link [foo [bar]]](/url)
.
-link [foo [bar]]
+link [foo [bar]]
````````````````````````````````
+But not unbalanced ones:
```````````````````````````````` example
-[link] bar](/uri)
+[link] bar](/url)
.
-[link] bar](/uri)
+[link] bar](/url)
````````````````````````````````
-
```````````````````````````````` example
-[link [bar](/uri)
+[link [bar](/url)
.
-[link bar
+[link bar
````````````````````````````````
+Unless they are escaped:
+
```````````````````````````````` example
-[link \[bar](/uri)
+[link \[bar](/url)
.
-link [bar
+link [bar
````````````````````````````````
The link text may contain inline content:
```````````````````````````````` example
-[link *foo **bar** `#`*](/uri)
+[link *foo **bar** `#`*](/url)
.
-link foo bar #
+link foo bar #
````````````````````````````````
```````````````````````````````` example
-[](/uri)
+[](/url)
.
-
+
````````````````````````````````
However, links may not contain other links, at any level of nesting.
```````````````````````````````` example
-[foo [bar](/uri)](/uri)
+[foo [bar](/url)](/url)
+.
+[foo bar](/url)
+````````````````````````````````
+
+
+```````````````````````````````` example
+[foo *[bar [baz](/url)](/url)*](/url)
.
-[foo bar](/uri)
+[foo [bar baz](/url)](/url)
````````````````````````````````
```````````````````````````````` example
-[foo *[bar [baz](/uri)](/uri)*](/uri)
+](url2)](url3)
.
-[foo [bar baz](/uri)](/uri)
+](url3)
````````````````````````````````
```````````````````````````````` example
-](uri2)](uri3)
+[](url2)](url3)
.
-](uri3)
+](url2)
````````````````````````````````
@@ -7560,9 +7633,9 @@ These cases illustrate the precedence of link text grouping over
emphasis grouping:
```````````````````````````````` example
-*[foo*](/uri)
+*[foo*](/url)
.
-*foo*
+*foo*
````````````````````````````````
@@ -7594,9 +7667,9 @@ and autolinks over link grouping:
```````````````````````````````` example
-[foo`](/uri)`
+[foo`](/url)`
.
-[foo](/uri)
+[foo](/url)
````````````````````````````````
@@ -7646,7 +7719,6 @@ Here is a simple example:
foo
````````````````````````````````
-
The rules for the [link text] are the same as with
[inline links]. Thus:
@@ -7656,18 +7728,18 @@ unless they are escaped:
```````````````````````````````` example
[link [foo [bar]]][ref]
-[ref]: /uri
+[ref]: /url
.
-link [foo [bar]]
+link [foo [bar]]
````````````````````````````````
```````````````````````````````` example
[link \[bar][ref]
-[ref]: /uri
+[ref]: /url
.
-link [bar
+link [bar
````````````````````````````````
@@ -7676,38 +7748,38 @@ The link text may contain inline content:
```````````````````````````````` example
[link *foo **bar** `#`*][ref]
-[ref]: /uri
+[ref]: /url
.
-link foo bar #
+link foo bar #
````````````````````````````````
```````````````````````````````` example
[][ref]
-[ref]: /uri
+[ref]: /url
.
-
+
````````````````````````````````
However, links may not contain other links, at any level of nesting.
```````````````````````````````` example
-[foo [bar](/uri)][ref]
+[foo [bar](/url)][ref]
-[ref]: /uri
+[ref]: /url
.
-[foo bar]ref
+[foo bar]ref
````````````````````````````````
```````````````````````````````` example
[foo *bar [baz][ref]*][ref]
-[ref]: /uri
+[ref]: /url
.
-[foo bar baz]ref
+[foo bar baz]ref
````````````````````````````````
@@ -7720,18 +7792,18 @@ emphasis grouping:
```````````````````````````````` example
*[foo*][ref]
-[ref]: /uri
+[ref]: /url
.
-*foo*
+*foo*
````````````````````````````````
```````````````````````````````` example
[foo *bar][ref]
-[ref]: /uri
+[ref]: /url
.
-foo *bar
+foo *bar
````````````````````````````````
@@ -7741,7 +7813,7 @@ and autolinks over link grouping:
```````````````````````````````` example
[foo
-[ref]: /uri
+[ref]: /url
.
[foo
````````````````````````````````
@@ -7750,7 +7822,7 @@ and autolinks over link grouping:
```````````````````````````````` example
[foo`][ref]`
-[ref]: /uri
+[ref]: /url
.
[foo][ref]
````````````````````````````````
@@ -7759,7 +7831,7 @@ and autolinks over link grouping:
```````````````````````````````` example
[foo
-[ref]: /uri
+[ref]: /url
.
[foohttp://example.com/?search=][ref]
````````````````````````````````
@@ -7834,13 +7906,16 @@ link text and the link label, then in the following we will have
a single reference link, not two shortcut reference links, as
intended:
-``` markdown
+```````````````````````````````` example
[foo]
[bar]
[foo]: /url1
[bar]: /url2
-```
+.
+foo
+bar
+````````````````````````````````
(Note that [shortcut reference links] were introduced by Gruber
himself in a beta version of `Markdown.pl`, but never included
@@ -7883,20 +7958,20 @@ backslash-escaped:
```````````````````````````````` example
[foo][ref[]
-[ref[]: /uri
+[ref[]: /url
.
[foo][ref[]
-[ref[]: /uri
+[ref[]: /url
````````````````````````````````
```````````````````````````````` example
[foo][ref[bar]]
-[ref[bar]]: /uri
+[ref[bar]]: /url
.
[foo][ref[bar]]
-[ref[bar]]: /uri
+[ref[bar]]: /url
````````````````````````````````
@@ -7913,20 +7988,20 @@ backslash-escaped:
```````````````````````````````` example
[foo][ref\[]
-[ref\[]: /uri
+[ref\[]: /url
.
-foo
+foo
````````````````````````````````
Note that in this example `]` is not backslash-escaped:
```````````````````````````````` example
-[bar\\]: /uri
+[bar\\]: /url
[bar\\]
.
-bar\
+bar\
````````````````````````````````
@@ -7935,10 +8010,10 @@ A [link label] must contain at least one [non-whitespace character]:
```````````````````````````````` example
[]
-[]: /uri
+[]: /url
.
[]
-[]: /uri
+[]: /url
````````````````````````````````
@@ -7947,12 +8022,12 @@ A [link label] must contain at least one [non-whitespace character]:
]
[
- ]: /uri
+ ]: /url
.
[
]
[
-]: /uri
+]: /url
````````````````````````````````
@@ -7961,7 +8036,7 @@ consists of a [link label] that [matches] a
[link reference definition] elsewhere in the
document, followed by the string `[]`.
The contents of the first link label are parsed as inlines,
-which are used as the link's text. The link's URI and title are
+which are used as the link's text. The link's destination and title are
provided by the matching reference link definition. Thus,
`[foo][]` is equivalent to `[foo][foo]`.
@@ -7994,9 +8069,9 @@ The link labels are case-insensitive:
````````````````````````````````
-
As with full reference links, [whitespace] is not
-allowed between the two sets of brackets:
+allowed between the two sets of brackets,
+so the first pair is treated as a [shortcut reference link]:
```````````````````````````````` example
[foo]
@@ -8014,9 +8089,9 @@ consists of a [link label] that [matches] a
[link reference definition] elsewhere in the
document and is not followed by `[]` or a link label.
The contents of the first link label are parsed as inlines,
-which are used as the link's text. The link's URI and title
+which are used as the link's text. The link's destination and title
are provided by the matching link reference definition.
-Thus, `[foo]` is equivalent to `[foo][]`.
+Thus, `[foo]` is equivalent to `[foo][]` and `[foo][foo]`.
```````````````````````````````` example
[foo]
@@ -8130,13 +8205,6 @@ Inline links also take precedence:
foo
````````````````````````````````
-```````````````````````````````` example
-[foo](not a link)
-
-[foo]: /url1
-.
-foo(not a link)
-````````````````````````````````
In the following case `[bar][baz]` is parsed as a reference,
`[foo]` as normal text:
@@ -8163,8 +8231,9 @@ Here, though, `[foo][bar]` is parsed as a reference, since
````````````````````````````````
-Here `[foo]` is not parsed as a shortcut reference, because it
-is followed by a link label (even though `[bar]` is not defined):
+Here, `[foo]` is not parsed as a shortcut reference, because it
+is followed by a link label (even though `[bar]` is not defined
+and is then found to be a link text instead):
```````````````````````````````` example
[foo][bar][baz]
@@ -8221,11 +8290,14 @@ this is standardly used as the image's `alt` attribute.
Though this spec is concerned with parsing, not rendering, it is
recommended that in rendering to HTML, only the plain string content
-of the [image description] be used. Note that in
+of the [image description] be used.
+Note that in
the above example, the alt attribute's value is `foo bar`, not `foo
[bar](/url)` or `foo bar`. Only the plain string
content is rendered, without formatting.
+The rules for inline formatting other than links is the same as for links:
+
```````````````````````````````` example
![foo *bar*][]
@@ -8244,6 +8316,8 @@ content is rendered, without formatting.
````````````````````````````````
+Inline image:
+
```````````````````````````````` example

.
@@ -8406,7 +8480,7 @@ If you want a link after a literal `!`, backslash-escape the
## Autolinks
-[Autolink](@)s are absolute URIs and email addresses inside
+[Autolink](@)s are absolute URLs and email addresses inside
`<` and `>`. They are parsed as links, with the URL or email address
as the link label.
@@ -8491,7 +8565,7 @@ with their syntax:
````````````````````````````````
-Spaces are not allowed in autolinks:
+Spaces are not allowed in autolink URLs.
```````````````````````````````` example
@@ -8519,8 +8593,11 @@ for these purposes, is anything that matches
the [non-normative regex from the HTML5
spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
- /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
- (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
+``` regex
+/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
+(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
+```
+
Examples of email autolinks: