add link & image atributes parser

This commit is contained in:
Dmitry Sedykh 2019-12-25 23:07:59 +03:00
parent afb6db3f1a
commit 93f691a086
2 changed files with 25 additions and 0 deletions

View file

@ -438,6 +438,10 @@ func NewImage(link *Link) *Image {
c.AppendChild(c, n) c.AppendChild(c, n)
n = next n = next
} }
// copy attributes
for _, attr := range link.Attributes() {
c.SetAttribute(attr.Name, attr.Value)
}
return c return c
} }

View file

@ -99,7 +99,20 @@ func removeLinkLabelState(pc Context, d *linkLabelState) {
d.Last = nil d.Last = nil
} }
// A AttributeConfig struct is a data structure that holds configuration of the renderers attributes.
type AttributeConfig struct {
Attribute bool
}
// SetOption implements SetOptioner.
func (a *AttributeConfig) SetOption(name OptionName, value interface{}) {
if name == optAttribute {
a.Attribute = true
}
}
type linkParser struct { type linkParser struct {
AttributeConfig
} }
var defaultLinkParser = &linkParser{} var defaultLinkParser = &linkParser{}
@ -182,6 +195,14 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
link.Title = ref.Title() link.Title = ref.Title()
link.Destination = ref.Destination() link.Destination = ref.Destination()
} }
// parse attributes
if s.Attribute {
if attrs, ok := ParseAttributes(block); ok {
for _, attr := range attrs {
link.SetAttribute(attr.Name, attr.Value)
}
}
}
if last.IsImage { if last.IsImage {
last.Parent().RemoveChild(last.Parent(), last) last.Parent().RemoveChild(last.Parent(), last)
return ast.NewImage(link) return ast.NewImage(link)