Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Parser and Renderer options
### Attributes
The `parser.WithAttribute` option allows you to define attributes on some elements.

Currently only headings support attributes.
Currently only headings,images support attributes.

**Attributes are being discussed in the
[CommonMark forum](https://talk.commonmark.org/t/consistent-attribute-syntax/272).
Expand Down
10 changes: 10 additions & 0 deletions _test/options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@
//- - - - - - - - -//
<h1 id="id-foo_bar:baz.qux" class="foobar">Test</h1>
//= = = = = = = = = = = = = = = = = = = = = = = =//

8: image with attributes
//- - - - - - - - -//
![img](https://url.test){ width="400" }
![img](https://url.test) { width="400" height="400" }
//- - - - - - - - -//
<p><img src="https://url.test" alt="img" width="400">
<img src="https://url.test" alt="img" width="400" height="400"></p>
//= = = = = = = = = = = = = = = = = = = = = = = =//

1 change: 1 addition & 0 deletions ast/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ func NewImage(link *Link) *Image {
}
c.Destination = link.Destination
c.Title = link.Title
c.attributes = link.attributes
for n := link.FirstChild(); n != nil; {
next := n.NextSibling()
link.RemoveChild(link, n)
Expand Down
9 changes: 9 additions & 0 deletions parser/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
var hasValue bool
if c == '(' { // normal link
link = s.parseLink(parent, last, block, pc)
if last.IsImage {
attrs, ok := ParseAttributes(block)
if ok {
for _, attr := range attrs {
link.SetAttribute(attr.Name, attr.Value)
}
}
}
} else if c == '[' { // reference link
link, hasValue = s.parseReferenceLink(parent, last, block, pc)
if link == nil && hasValue {
Expand Down Expand Up @@ -324,6 +332,7 @@ func (s *linkParser) parseLink(parent ast.Node, last *linkLabelState, block text
}

link := ast.NewLink()

s.processLinkLabel(parent, link, last, pc)
link.Destination = destination
link.Title = title
Expand Down