mirror of
https://github.com/yuin/goldmark
synced 2025-03-04 23:04:52 +00:00
FencedCodeBlock attributes support
This commit is contained in:
parent
bcd7fd5cb3
commit
2a31867397
2 changed files with 19 additions and 1 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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-")
|
||||
|
|
|
|||
Loading…
Reference in a new issue