Attributes have the following syntax:
attributes <- '{' whitespace* attribute (whitespace attribute)*
whitespace* '}'
attribute <- id_attribute | class_attribute | kv_attribute
id_attribute <- '#' letter (alphanum | '-' | '_' | ':' | '.')*
class_attribute <- '.' letter (alphanum | '-' | '_')*
kv_attribute <- attrname '=' attrvalue
attrname <- (asciiletter | '_' | ':')
(asciialphanum | '_' | '.' | '-' | ':')*
attrvalue <- unquotedvalue | quotedvalue
unquotedvalue <- [^"-=<>`:whitespace:]+
quotedvalue <- '"' ([^"] | '\' '"')* '"'
**Attributes that occur at the end of the text of
a Setext or ATX heading (separated by whitespace
from the text) affect the heading element.**
```````````````````````````````` example
# Heading {#ident .class key="value value" key2=value2}
.
Heading
````````````````````````````````
```````````````````````````````` example
Heading {#ident .class key="value"}
=====
.
Heading
````````````````````````````````
Whitespace is tolerated around the delimiters:
```````````````````````````````` example
# Heading { #ident .class key="value" }
.
Heading
````````````````````````````````
Multiple class attributes are combined:
```````````````````````````````` example
# Heading {.class1 .class2 class="class3"}
.
Heading
````````````````````````````````
Only the last id attribute is used:
```````````````````````````````` example
# Heading {#id1 #id2 id="id3"}
.
Heading
````````````````````````````````
Heading attributes can be followed by whitespace, but
otherwise must come at the end of the line.
```````````````````````````````` example
# Foo {#bar}
.
Foo
````````````````````````````````
Headings should still work without attributes:
```````````````````````````````` example
# ATX
Setext
------
.
ATX
Setext
````````````````````````````````
**Attributes that occur after the opening fence
in a fenced code block affect the code block element.**
```````````````````````````````` example
``` {#ident .class key="value value" key2=value2}
xyz
```
.
xyz
````````````````````````````````
```````````````````````````````` example
~~~~{#mycode .ruby .number-lines}
xyz
~~~~
.
xyz
````````````````````````````````
If any non-space content comes after the attribute spec, the
whole thing is treated as a raw info string.
```````````````````````````````` example
``` {#foo} bar
xyz
```
.
xyz
````````````````````````````````
Here the attribute spec is at the end, so the
first word provides the info string and the rest is
treated as an attribute.
```````````````````````````````` example
``` bar {#foo}
xyz
```
.
xyz
````````````````````````````````
**Attributes on inline elements must immediately follow the element to
which they belong.** If they follow a space, then they belong
to the space.
```````````````````````````````` example
`hi`{#ident .class key=value}
.
hi
````````````````````````````````
```````````````````````````````` example
`hi` {#ident .class key=value}
.
hi
````````````````````````````````
The attributes can wrap:
```````````````````````````````` example
`hi`{#ident .class
key=value}
.
hi
````````````````````````````````
**Attributes that occur immediately before a block
element, on a line by themselves, affect that
element.**
```````````````````````````````` example
{.special}
* * * *
.
````````````````````````````````
```````````````````````````````` example
{#foo .special}
bar
.
bar
````````````````````````````````
```````````````````````````````` example
{#foo .special}
# Hi
.
Hi
````````````````````````````````
When conflicting specifications of the same attribute are given,
the last one takes precedence, except for classes, which are
cumulative.
```````````````````````````````` example
{#id1}
{#id2}
# Heading {#id3}
.
Heading
````````````````````````````````
```````````````````````````````` example
{#id1}
{#id3}
# Heading
.
Heading
````````````````````````````````
```````````````````````````````` example
{.class1 k=1}
{.class2 .class3 k=2}
# Heading {.class4}
.
Heading
````````````````````````````````
**Attributes that occur at the end of a reference
link definition affect links that refer to that
definition.**
```````````````````````````````` example
[foo]
[foo]: bar "title" {#ident .centered .big}
.
foo
````````````````````````````````
**Attributes that occur immediately after an inline
element affect that element.**
```````````````````````````````` example
[foo](bar){#ident .class key="value value" key2=value2}
.
foo
````````````````````````````````
```````````````````````````````` example
![foo](bar){#ident .centered .big}
.
````````````````````````````````
```````````````````````````````` example
[foo]{#ident .centered .big}
[foo]: bar
.
foo
````````````````````````````````
```````````````````````````````` example
*hi*{.underline}
.
hi
````````````````````````````````
**Consecutive attribute specifiers may be used,
either for blocks or for inlines.**
```````````````````````````````` example
*hi*{.underline}{#foo}
.
hi
````````````````````````````````
```````````````````````````````` example
{.special}
{#foo}
* * * *
.
````````````````````````````````
**Entities can be used in attribute values.**
```````````````````````````````` example
# Heading {key="value" }
.
Heading
````````````````````````````````