FencedCodeBlock attributes support

This commit is contained in:
Dmitry Sedykh 2019-12-26 00:11:39 +03:00
parent bcd7fd5cb3
commit 2a31867397
2 changed files with 19 additions and 1 deletions

View file

@ -63,6 +63,20 @@ func (b *fencedCodeBlockParser) Open(parent ast.Node, reader text.Reader, pc Con
}
}
node := ast.NewFencedCodeBlock(info)
// add attributes
if info != nil {
infoText := info.Text(reader.Source())
pos := bytes.IndexByte(infoText, '{')
if pos > -1 {
attrs, ok := ParseAttributes(text.NewReader(infoText[pos:]))
if ok {
for _, attr := range attrs {
node.SetAttribute(attr.Name, attr.Value)
}
}
}
}
pc.Set(fencedCodeBlockInfoKey, &fenceData{fenceChar, findent, oFenceLength, node})
return node, NoChildren

View file

@ -274,7 +274,11 @@ func (r *Renderer) renderCodeBlock(w util.BufWriter, source []byte, n ast.Node,
func (r *Renderer) renderFencedCodeBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.FencedCodeBlock)
if entering {
_, _ = w.WriteString("<pre><code")
_, _ = w.WriteString("<pre")
if n.Attributes() != nil {
RenderAttributes(w, n, GlobalAttributeFilter)
}
_, _ = w.WriteString("><code")
language := n.Language(source)
if language != nil {
_, _ = w.WriteString(" class=\"language-")