diff --git a/markup/goldmark/convert.go b/markup/goldmark/convert.go
index 50e7bcb8a71..629e2b15a18 100644
--- a/markup/goldmark/convert.go
+++ b/markup/goldmark/convert.go
@@ -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"
@@ -137,10 +139,14 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
parserOptions = append(parserOptions, parser.WithAutoHeadingID())
}
- if cfg.Parser.Attribute {
+ if cfg.Parser.Attribute.Title {
parserOptions = append(parserOptions, parser.WithAttribute())
}
+ if cfg.Parser.Attribute.Block {
+ extensions = append(extensions, attributes.New())
+ }
+
md := goldmark.New(
goldmark.WithExtensions(
extensions...,
diff --git a/markup/goldmark/convert_test.go b/markup/goldmark/convert_test.go
index f105afdc424..76c34c0bc3f 100644
--- a/markup/goldmark/convert_test.go
+++ b/markup/goldmark/convert_test.go
@@ -193,6 +193,85 @@ func TestConvertAutoIDBlackfriday(t *testing.T) {
c.Assert(got, qt.Contains, "
")
}
+func TestConvertAttributes(t *testing.T) {
+ c := qt.New(t)
+
+ withBlockAttributes := func(conf *markup_config.Config) {
+ conf.Goldmark.Parser.Attribute.Block = true
+ conf.Goldmark.Parser.Attribute.Title = false
+ }
+
+ withTitleAndBlockAttributes := func(conf *markup_config.Config) {
+ conf.Goldmark.Parser.Attribute.Block = true
+ conf.Goldmark.Parser.Attribute.Title = true
+ }
+
+ 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\"}",
+ "heading
\n",
+ },
+ {
+ "Blockquote",
+ withBlockAttributes,
+ "{#id .className attrName=attrValue class=\"class1 class2\"}\n> foo\n> bar\n",
+ "foo\nbar
\n
\n",
+ },
+ {
+ "Paragraph",
+ withBlockAttributes,
+ "{.myclass }\nHi there.",
+ "
Hi there.
\n",
+ },
+ {
+ "Ordered list",
+ withBlockAttributes,
+ "{.myclass }\n1. First\n2. Second\n",
+ "\n- First
\n- Second
\n
\n",
+ },
+ {
+ "Unordered list",
+ withBlockAttributes,
+ "{.myclass }\n* First\n* Second\n",
+ "\n",
+ },
+ {
+ "Table",
+ withBlockAttributes,
+ `{.myclass }
+| A | B |
+| ------------- |:-------------:| -----:|
+| AV | BV |`,
+ "\n",
+ },
+ {
+ "Title and Blockquote",
+ withTitleAndBlockAttributes,
+ "## heading {#id .className attrName=attrValue class=\"class1 class2\"}\n{.myclass}\n> foo\n> bar\n",
+ "heading
\nfoo\nbar
\n
\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)
diff --git a/markup/goldmark/goldmark_config/config.go b/markup/goldmark/goldmark_config/config.go
index af33e03dc4b..82b8d96301c 100644
--- a/markup/goldmark/goldmark_config/config.go
+++ b/markup/goldmark/goldmark_config/config.go
@@ -37,7 +37,10 @@ var Default = Config{
Parser: Parser{
AutoHeadingID: true,
AutoHeadingIDType: AutoHeadingIDTypeGitHub,
- Attribute: true,
+ Attribute: ParserAttribute{
+ Title: true,
+ Block: false,
+ },
},
}
@@ -82,5 +85,12 @@ type Parser struct {
AutoHeadingIDType string
// Enables custom attributes.
- Attribute bool
+ Attribute ParserAttribute
+}
+
+type ParserAttribute struct {
+ // Enables custom attributes for titles.
+ Title bool
+ // Enables custom attributeds for blocks.
+ Block bool
}
diff --git a/markup/goldmark/internal/extensions/attributes/attributes.go b/markup/goldmark/internal/extensions/attributes/attributes.go
new file mode 100644
index 00000000000..640847a07c6
--- /dev/null
+++ b/markup/goldmark/internal/extensions/attributes/attributes.go
@@ -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 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 {
+ 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)
+ }
+}
diff --git a/markup/markup_config/config.go b/markup/markup_config/config.go
index 376350c95a2..b477c73c64a 100644
--- a/markup/markup_config/config.go
+++ b/markup/markup_config/config.go
@@ -44,6 +44,10 @@ type Config struct {
func Decode(cfg config.Provider) (conf Config, err error) {
conf = Default
+ if err = applyLegacyConfig(cfg, &conf); err != nil {
+ return
+ }
+
m := cfg.GetStringMap("markup")
if m == nil {
return
@@ -54,10 +58,6 @@ func Decode(cfg config.Provider) (conf Config, err error) {
return
}
- if err = applyLegacyConfig(cfg, &conf); err != nil {
- return
- }
-
if err = highlight.ApplyLegacyConfig(cfg, &conf.Highlight); err != nil {
return
}
@@ -82,6 +82,15 @@ func applyLegacyConfig(cfg config.Provider, conf *Config) error {
conf.BlackFriday.FootnoteReturnLinkContents = cfg.GetString("footnoteReturnLinkContents")
}
+ // Changed from a bool in 0.81.0
+ const attrKey = "markup.goldmark.parser.attribute"
+ av := cfg.Get(attrKey)
+ if avb, ok := av.(bool); ok {
+ cfg.Set(attrKey, goldmark_config.ParserAttribute{
+ Title: avb,
+ })
+ }
+
return nil
}
diff --git a/markup/markup_config/config_test.go b/markup/markup_config/config_test.go
index 89da62bab99..4a1f1232b7d 100644
--- a/markup/markup_config/config_test.go
+++ b/markup/markup_config/config_test.go
@@ -46,6 +46,8 @@ func TestConfig(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(conf.Goldmark.Renderer.Unsafe, qt.Equals, true)
c.Assert(conf.BlackFriday.Fractions, qt.Equals, true)
+ c.Assert(conf.Goldmark.Parser.Attribute.Title, qt.Equals, true)
+ c.Assert(conf.Goldmark.Parser.Attribute.Block, qt.Equals, false)
c.Assert(conf.AsciidocExt.WorkingFolderCurrent, qt.Equals, true)
c.Assert(conf.AsciidocExt.Extensions[0], qt.Equals, "asciidoctor-html5s")
@@ -63,6 +65,14 @@ func TestConfig(t *testing.T) {
v.Set("footnoteReturnLinkContents", "myreturn")
v.Set("pygmentsStyle", "hugo")
v.Set("pygmentsCodefencesGuessSyntax", true)
+
+ v.Set("markup", map[string]interface{}{
+ "goldmark": map[string]interface{}{
+ "parser": map[string]interface{}{
+ "attribute": false, // Was changed to a struct in 0.81.0
+ },
+ },
+ })
conf, err := Decode(v)
c.Assert(err, qt.IsNil)
@@ -72,5 +82,8 @@ func TestConfig(t *testing.T) {
c.Assert(conf.Highlight.Style, qt.Equals, "hugo")
c.Assert(conf.Highlight.CodeFences, qt.Equals, true)
c.Assert(conf.Highlight.GuessSyntax, qt.Equals, true)
+ c.Assert(conf.Goldmark.Parser.Attribute.Title, qt.Equals, false)
+ c.Assert(conf.Goldmark.Parser.Attribute.Block, qt.Equals, false)
+
})
}