forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
markup/goldmark: Add attributes support for blocks (tables etc.)
E.g.: ``` > foo > bar {.myclass} ``` There are some current limitations: For tables you can currently only apply it to the full table, and for lists the ul/ol-nodes only, e.g.: ``` * Fruit * Apple * Orange * Banana {.fruits} * Dairy * Milk * Cheese {.dairies} {.list} ``` Fixes gohugoio#7548
- Loading branch information
Showing
8 changed files
with
303 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
markup/goldmark/internal/extensions/attributes/attributes.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package attributes | ||
|
||
import ( | ||
"github.com/yuin/goldmark" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
// This extenion is based on/inspired by https://github.com/mdigger/goldmark-attributes | ||
// MIT License | ||
// Copyright (c) 2019 Dmitry Sedykh | ||
|
||
var ( | ||
kindAttributesBlock = ast.NewNodeKind("AttributesBlock") | ||
|
||
defaultParser = new(attrParser) | ||
defaultTransformer = new(transformer) | ||
attributes goldmark.Extender = new(attrExtension) | ||
) | ||
|
||
func New() goldmark.Extender { | ||
return attributes | ||
} | ||
|
||
type attrExtension struct{} | ||
|
||
func (a *attrExtension) Extend(m goldmark.Markdown) { | ||
m.Parser().AddOptions( | ||
parser.WithBlockParsers( | ||
util.Prioritized(defaultParser, 100)), | ||
parser.WithASTTransformers( | ||
util.Prioritized(defaultTransformer, 100), | ||
), | ||
) | ||
} | ||
|
||
type attrParser struct{} | ||
|
||
func (a *attrParser) CanAcceptIndentedLine() bool { | ||
return false | ||
} | ||
|
||
func (a *attrParser) CanInterruptParagraph() bool { | ||
return true | ||
} | ||
|
||
func (a *attrParser) Close(node ast.Node, reader text.Reader, pc parser.Context) { | ||
} | ||
|
||
func (a *attrParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State { | ||
return parser.Close | ||
} | ||
|
||
func (a *attrParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) { | ||
if attrs, ok := parser.ParseAttributes(reader); ok { | ||
// add attributes | ||
var node = &attributesBlock{ | ||
BaseBlock: ast.BaseBlock{}, | ||
} | ||
for _, attr := range attrs { | ||
node.SetAttribute(attr.Name, attr.Value) | ||
} | ||
return node, parser.NoChildren | ||
} | ||
return nil, parser.RequireParagraph | ||
} | ||
|
||
func (a *attrParser) Trigger() []byte { | ||
return []byte{'{'} | ||
} | ||
|
||
type attributesBlock struct { | ||
ast.BaseBlock | ||
} | ||
|
||
func (a *attributesBlock) Dump(source []byte, level int) { | ||
attrs := a.Attributes() | ||
list := make(map[string]string, len(attrs)) | ||
for _, attr := range attrs { | ||
var ( | ||
name = util.BytesToReadOnlyString(attr.Name) | ||
value = util.BytesToReadOnlyString(util.EscapeHTML(attr.Value.([]byte))) | ||
) | ||
list[name] = value | ||
} | ||
ast.DumpHelper(a, source, level, list, nil) | ||
} | ||
|
||
func (a *attributesBlock) Kind() ast.NodeKind { | ||
return kindAttributesBlock | ||
} | ||
|
||
type transformer struct{} | ||
|
||
func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) { | ||
var attributes = make([]ast.Node, 0, 500) | ||
ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) { | ||
if entering && node.Kind() == kindAttributesBlock && !node.HasBlankPreviousLines() { | ||
attributes = append(attributes, node) | ||
return ast.WalkSkipChildren, nil | ||
} | ||
return ast.WalkContinue, nil | ||
}) | ||
|
||
for _, attr := range attributes { | ||
if prev := attr.PreviousSibling(); prev != nil && | ||
prev.Type() == ast.TypeBlock { | ||
for _, attr := range attr.Attributes() { | ||
if _, found := prev.Attribute(attr.Name); !found { | ||
prev.SetAttribute(attr.Name, attr.Value) | ||
} | ||
} | ||
} | ||
// remove attributes node | ||
attr.Parent().RemoveChild(attr.Parent(), attr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.