diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-02-07 18:08:46 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-02-08 19:52:55 +0100 |
commit | 2681633db8d340d2dc59cf801419874d572fc704 (patch) | |
tree | 74451c9bc4249a387aacf8071127d880cfea07db /markup/goldmark | |
parent | 1b2472825664763c0b88807b0d193e73553423ec (diff) |
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 #7548
Diffstat (limited to 'markup/goldmark')
-rw-r--r-- | markup/goldmark/convert.go | 8 | ||||
-rw-r--r-- | markup/goldmark/convert_test.go | 99 | ||||
-rw-r--r-- | markup/goldmark/goldmark_config/config.go | 14 | ||||
-rw-r--r-- | markup/goldmark/internal/extensions/attributes/attributes.go | 119 |
4 files changed, 237 insertions, 3 deletions
diff --git a/markup/goldmark/convert.go b/markup/goldmark/convert.go index 50e7bcb8a..629e2b15a 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 f105afdc4..d35d4d1fd 100644 --- a/markup/goldmark/convert_test.go +++ b/markup/goldmark/convert_test.go @@ -17,6 +17,8 @@ import ( "strings" "testing" + "github.com/spf13/cast" + "github.com/gohugoio/hugo/markup/goldmark/goldmark_config" "github.com/gohugoio/hugo/markup/highlight" @@ -193,6 +195,103 @@ 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) + + 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 interface{} + }{ + { + "Title", + nil, + "## heading {#id .className attrName=attrValue class=\"class1 class2\"}", + "<h2 id=\"id\" class=\"className class1 class2\" attrName=\"attrValue\">heading</h2>\n", + }, + { + "Blockquote", + withBlockAttributes, + "> foo\n> bar\n{#id .className attrName=attrValue class=\"class1 class2\"}\n", + "<blockquote id=\"id\" class=\"className class1 class2\"><p>foo\nbar</p>\n</blockquote>\n", + }, + { + "Paragraph", + withBlockAttributes, + "\nHi there.\n{.myclass }", + "<p class=\"myclass\">Hi there.</p>\n", + }, + { + "Ordered list", + withBlockAttributes, + "\n1. First\n2. Second\n{.myclass }", + "<ol class=\"myclass\">\n<li>First</li>\n<li>Second</li>\n</ol>\n", + }, + { + "Unordered list", + withBlockAttributes, + "\n* First\n* Second\n{.myclass }", + "<ul class=\"myclass\">\n<li>First</li>\n<li>Second</li>\n</ul>\n", + }, + { + "Unordered list, indented", + withBlockAttributes, + `* Fruit + * Apple + * Orange + * Banana + {.fruits} +* Dairy + * Milk + * Cheese + {.dairies} +{.list}`, + []string{"<ul class=\"list\">\n<li>Fruit\n<ul class=\"fruits\">", "<li>Dairy\n<ul class=\"dairies\">"}, + }, + { + "Table", + withBlockAttributes, + `| A | B | +| ------------- |:-------------:| -----:| +| AV | BV | +{.myclass }`, + "<table class=\"myclass\">\n<thead>", + }, + { + "Title and Blockquote", + withTitleAndBlockAttributes, + "## heading {#id .className attrName=attrValue class=\"class1 class2\"}\n> foo\n> bar\n{.myclass}", + "<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()) + + for _, s := range cast.ToStringSlice(test.expect) { + c.Assert(got, qt.Contains, s) + } + + }) + } + +} + 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 af33e03dc..82b8d9630 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 000000000..ce745295c --- /dev/null +++ b/markup/goldmark/internal/extensions/attributes/attributes.go @@ -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) + } +} |