Skip to content

Commit

Permalink
markup/goldmark: Add attributes support for blocks (tables etc.)
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 8, 2021
1 parent 441b11b commit fe372e4
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
3 changes: 3 additions & 0 deletions markup/goldmark/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"path/filepath"
"runtime/debug"

"github.com/gohugoio/hugo/markup/goldmark/internal/extensions/attributes"

"github.com/gohugoio/hugo/identity"

"github.com/pkg/errors"
Expand Down Expand Up @@ -139,6 +141,7 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {

if cfg.Parser.Attribute {
parserOptions = append(parserOptions, parser.WithAttribute())
extensions = append(extensions, attributes.Configure())
}

md := goldmark.New(
Expand Down
69 changes: 69 additions & 0 deletions markup/goldmark/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,75 @@ func TestConvertAutoIDBlackfriday(t *testing.T) {
c.Assert(got, qt.Contains, "<h2 id=\"let-s-try-this-shall-we\">")
}

func TestConvertAttributes(t *testing.T) {
c := qt.New(t)

for _, test := range []struct {
name string
withConfig func(conf *markup_config.Config)
input string
expect string
}{
{
"Title",
nil,
"## heading {#id .className attrName=attrValue class=\"class1 class2\"}",
"<h2 id=\"id\" class=\"className class1 class2\" attrName=\"attrValue\">heading</h2>\n",
},
{
"Blockquote",
nil,
"{#id .className attrName=attrValue class=\"class1 class2\"}\n> foo\n> bar\n",
"<blockquote id=\"id\" class=\"className class1 class2\"><p>foo\nbar</p>\n</blockquote>\n",
},
{
"Paragraph",
nil,
"{.myclass }\nHi there.",
"<p class=\"myclass\">Hi there.</p>\n",
},
{
"Ordered list",
nil,
"{.myclass }\n1. First\n2. Second\n",
"<ol class=\"myclass\">\n<li>First</li>\n<li>Second</li>\n</ol>\n",
},
{
"Unordered list",
nil,
"{.myclass }\n* First\n* Second\n",
"<ul class=\"myclass\">\n<li>First</li>\n<li>Second</li>\n</ul>\n",
},
{
"Table",
nil,
`{.myclass }
| A | B |
| ------------- |:-------------:| -----:|
| AV | BV |`,
"<table class=\"myclass\">\n<thead>",
},
{
"Title and Blockquote",
nil,
"## heading {#id .className attrName=attrValue class=\"class1 class2\"}\n{.myclass}\n> foo\n> bar\n",
"<h2 id=\"id\" class=\"className class1 class2\" attrName=\"attrValue\">heading</h2>\n<blockquote class=\"myclass\"><p>foo\nbar</p>\n</blockquote>\n",
},
} {
c.Run(test.name, func(c *qt.C) {
mconf := markup_config.Default
if test.withConfig != nil {
test.withConfig(&mconf)
}
b := convert(c, mconf, test.input)
got := string(b.Bytes())

c.Assert(got, qt.Contains, test.expect)
})
}

}

func TestConvertIssues(t *testing.T) {
c := qt.New(t)

Expand Down
120 changes: 120 additions & 0 deletions markup/goldmark/internal/extensions/attributes/attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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 Configure() 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 {
attributes = append(attributes, node)
return ast.WalkSkipChildren, nil
}
return ast.WalkContinue, nil
})

for _, attr := range attributes {
if next := attr.NextSibling(); next != nil &&
next.Type() == ast.TypeBlock &&
!next.HasBlankPreviousLines() {
for _, attr := range attr.Attributes() {
if _, found := next.Attribute(attr.Name); !found {
next.SetAttribute(attr.Name, attr.Value)
}
}
}
// remove attributes node
attr.Parent().RemoveChild(attr.Parent(), attr)
}
}

0 comments on commit fe372e4

Please sign in to comment.