From 3f868fccdd7e183a013043a742d90d7b16987893 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 2 Aug 2022 22:18:28 +0100 Subject: [PATCH] Restructure RenderConfig to allow the markdown header to define if math should be enabled. Signed-off-by: Andrew Thornton --- .../markdown/{ => config}/convertyaml.go | 9 +- .../markup/markdown/config/renderconfig.go | 197 ++++++++++++ .../markdown/config/renderconfig_test.go | 288 ++++++++++++++++++ .../markup/markdown/{ => extension}/ast.go | 2 +- .../markup/markdown/{ => extension}/toc.go | 5 +- modules/markup/markdown/goldmark.go | 34 +-- modules/markup/markdown/markdown.go | 17 +- modules/markup/markdown/math/block_parser.go | 20 +- modules/markup/markdown/math/inline_parser.go | 8 + modules/markup/markdown/math/math.go | 64 +--- modules/markup/markdown/renderconfig.go | 118 ------- modules/markup/markdown/renderconfig_test.go | 162 ---------- 12 files changed, 546 insertions(+), 378 deletions(-) rename modules/markup/markdown/{ => config}/convertyaml.go (90%) create mode 100644 modules/markup/markdown/config/renderconfig.go create mode 100644 modules/markup/markdown/config/renderconfig_test.go rename modules/markup/markdown/{ => extension}/ast.go (99%) rename modules/markup/markdown/{ => extension}/toc.go (86%) delete mode 100644 modules/markup/markdown/renderconfig.go delete mode 100644 modules/markup/markdown/renderconfig_test.go diff --git a/modules/markup/markdown/convertyaml.go b/modules/markup/markdown/config/convertyaml.go similarity index 90% rename from modules/markup/markdown/convertyaml.go rename to modules/markup/markdown/config/convertyaml.go index 3f5ebec90899e..1ddf0b7d9012f 100644 --- a/modules/markup/markdown/convertyaml.go +++ b/modules/markup/markdown/config/convertyaml.go @@ -2,9 +2,10 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package markdown +package config import ( + "code.gitea.io/gitea/modules/markup/markdown/extension" "github.com/yuin/goldmark/ast" east "github.com/yuin/goldmark/extension/ast" "gopkg.in/yaml.v3" @@ -74,9 +75,9 @@ func sequenceNodeToTable(meta *yaml.Node) ast.Node { } func nodeToDetails(meta *yaml.Node, icon string) ast.Node { - details := NewDetails() - summary := NewSummary() - summary.AppendChild(summary, NewIcon(icon)) + details := extension.NewDetails() + summary := extension.NewSummary() + summary.AppendChild(summary, extension.NewIcon(icon)) details.AppendChild(details, summary) details.AppendChild(details, nodeToTable(meta)) diff --git a/modules/markup/markdown/config/renderconfig.go b/modules/markup/markdown/config/renderconfig.go new file mode 100644 index 0000000000000..a6d7e45624b27 --- /dev/null +++ b/modules/markup/markdown/config/renderconfig.go @@ -0,0 +1,197 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package config + +import ( + "fmt" + "strings" + + "github.com/yuin/goldmark/ast" + "github.com/yuin/goldmark/parser" + "gopkg.in/yaml.v3" +) + +var renderConfigKey = parser.NewContextKey() + +func GetRenderConfig(pc parser.Context) *RenderConfig { + return pc.Get(renderConfigKey).(*RenderConfig) +} + +func SetRenderConfig(pc parser.Context, rc *RenderConfig) { + pc.Set(renderConfigKey, rc) +} + +// RenderConfig represents rendering configuration for this file +type RenderConfig struct { + Meta string + Icon string + TOC bool + Lang string + Math *MathConfig + yamlNode *yaml.Node +} + +type MathConfig struct { + InlineDollar bool `yaml:"inline_dollar"` + InlineLatex bool `yaml:"inline_latex"` + DisplayDollar bool `yaml:"display_dollar"` + DisplayLatex bool `yaml:"display_latex"` +} + +// UnmarshalYAML implement yaml.v3 UnmarshalYAML +func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error { + rc.yamlNode = value + + basic := &yamlRenderConfig{} + err := value.Decode(basic) + if err != nil { + return fmt.Errorf("failed to decode basic: %w", err) + } + + if basic.Lang != "" { + rc.Lang = basic.Lang + } + + rc.TOC = basic.TOC + + if basic.Math != nil { + rc.Math = basic.Math + } + + if basic.Gitea != nil { + if basic.Gitea.Meta != nil { + rc.Meta = *basic.Gitea.Meta + } + if basic.Gitea.Icon != nil { + rc.Icon = *basic.Gitea.Icon + } + if basic.Gitea.Lang != nil { + rc.Lang = *basic.Gitea.Lang + } + if basic.Gitea.TOC != nil { + rc.TOC = *basic.Gitea.TOC + } + if basic.Gitea.Math != nil { + rc.Math = basic.Gitea.Math + } + } + + return nil +} + +type yamlRenderConfig struct { + TOC bool `yaml:"include_toc"` + Lang string `yaml:"lang"` + Math *MathConfig `yaml:"math"` + Gitea *yamlGitea `yaml:"gitea"` +} + +type yamlGitea struct { + Meta *string + Icon *string `yaml:"details_icon"` + TOC *bool `yaml:"include_toc"` + Lang *string + Math *MathConfig +} + +func (y *yamlGitea) UnmarshalYAML(node *yaml.Node) error { + var controlString string + if err := node.Decode(&controlString); err == nil { + var meta string + switch strings.TrimSpace(strings.ToLower(controlString)) { + case "none": + meta = "none" + case "table": + meta = "table" + default: // "details" + meta = "details" + } + y.Meta = &meta + return nil + } + + type yExactType yamlGitea + yExact := (*yExactType)(y) + if err := node.Decode(yExact); err != nil { + return fmt.Errorf("unable to parse yamlGitea: %w", err) + } + y = (*yamlGitea)(yExact) + + return nil +} + +func (m *MathConfig) UnmarshalYAML(node *yaml.Node) error { + var controlBool bool + if err := node.Decode(&controlBool); err == nil { + m.InlineLatex = controlBool + m.DisplayLatex = controlBool + m.DisplayDollar = controlBool + // Not InlineDollar + m.InlineDollar = false + return nil + } + + var enableMathStrs []string + if err := node.Decode(&enableMathStrs); err != nil { + var enableMathStr string + if err := node.Decode(&enableMathStr); err == nil { + m.InlineLatex = false + m.DisplayLatex = false + m.DisplayDollar = false + m.InlineDollar = false + if enableMathStr == "" { + enableMathStr = "true" + } + enableMathStrs = strings.Split(enableMathStr, ",") + } + } + if enableMathStrs != nil { + for _, value := range enableMathStrs { + switch strings.TrimSpace(strings.ToLower(value)) { + case "all": + m.InlineLatex = true + m.DisplayLatex = true + m.DisplayDollar = true + m.InlineDollar = true + break + case "inline_dollar": + m.InlineDollar = true + case "inline_latex": + m.InlineLatex = true + case "display_dollar": + m.DisplayDollar = true + case "display_latex": + m.DisplayLatex = true + case "true": + m.InlineLatex = true + m.DisplayLatex = true + m.DisplayDollar = true + } + } + return nil + } + + type mExactType MathConfig + mExact := (*mExactType)(m) + if err := node.Decode(mExact); err != nil { + return fmt.Errorf("unable to parse MathConfig: %w", err) + } + m = (*MathConfig)(mExact) + return nil +} + +func (rc *RenderConfig) ToMetaNode() ast.Node { + if rc.yamlNode == nil { + return nil + } + switch rc.Meta { + case "table": + return nodeToTable(rc.yamlNode) + case "details": + return nodeToDetails(rc.yamlNode, rc.Icon) + default: + return nil + } +} diff --git a/modules/markup/markdown/config/renderconfig_test.go b/modules/markup/markdown/config/renderconfig_test.go new file mode 100644 index 0000000000000..d3a8ac23569a5 --- /dev/null +++ b/modules/markup/markdown/config/renderconfig_test.go @@ -0,0 +1,288 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package config + +import ( + "testing" + + "gopkg.in/yaml.v3" +) + +func TestRenderConfig_UnmarshalYAML(t *testing.T) { + tests := []struct { + name string + expected *RenderConfig + args string + }{ + { + "empty", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "", + }, "", + }, + { + "lang", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "test", + }, "lang: test", + }, + { + "metatable", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "", + }, "gitea: table", + }, + { + "metanone", &RenderConfig{ + Meta: "none", + Icon: "table", + Lang: "", + }, "gitea: none", + }, + { + "metadetails", &RenderConfig{ + Meta: "details", + Icon: "table", + Lang: "", + }, "gitea: details", + }, + { + "metawrong", &RenderConfig{ + Meta: "details", + Icon: "table", + Lang: "", + }, "gitea: wrong", + }, + { + "toc", &RenderConfig{ + TOC: true, + Meta: "table", + Icon: "table", + Lang: "", + }, "include_toc: true", + }, + { + "tocfalse", &RenderConfig{ + TOC: false, + Meta: "table", + Icon: "table", + Lang: "", + }, "include_toc: false", + }, + { + "toclang", &RenderConfig{ + Meta: "table", + Icon: "table", + TOC: true, + Lang: "testlang", + }, ` include_toc: true + lang: testlang`, + }, + { + "complexlang", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "testlang", + }, ` + gitea: + lang: testlang +`, + }, + { + "complexlang2", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "testlang", + }, ` + lang: notright + gitea: + lang: testlang +`, + }, + { + "complex2", &RenderConfig{ + Lang: "two", + Meta: "table", + TOC: true, + Icon: "smiley", + }, ` + lang: one + include_toc: false + gitea: + details_icon: smiley + meta: table + include_toc: true + lang: two +`, + }, + { + "complex3", &RenderConfig{ + Lang: "two", + Meta: "table", + TOC: false, + Icon: "smiley", + }, ` + lang: one + include_toc: true + gitea: + details_icon: smiley + meta: table + include_toc: false + lang: two +`, + }, + { + "mathall", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "testlang", + Math: &MathConfig{ + InlineDollar: true, + InlineLatex: true, + DisplayDollar: true, + DisplayLatex: true, + }, + }, ` + math: all + gitea: + lang: testlang +`, + }, + { + "mathtrue", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "testlang", + Math: &MathConfig{ + InlineDollar: false, + InlineLatex: true, + DisplayDollar: true, + DisplayLatex: true, + }, + }, ` + math: true + gitea: + lang: testlang +`, + }, + { + "mathstrings", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "testlang", + Math: &MathConfig{ + InlineDollar: true, + InlineLatex: false, + DisplayDollar: true, + DisplayLatex: false, + }, + }, ` + math: "display_dollar,inline_dollar" + gitea: + lang: testlang +`, + }, + { + "mathstringarray", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "testlang", + Math: &MathConfig{ + InlineDollar: true, + InlineLatex: false, + DisplayDollar: true, + DisplayLatex: false, + }, + }, ` + math: [display_dollar,inline_dollar] + gitea: + lang: testlang +`, + }, + { + "mathstringarrayalone", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "", + Math: &MathConfig{ + InlineDollar: true, + InlineLatex: false, + DisplayDollar: true, + DisplayLatex: false, + }, + }, `math: [display_dollar,inline_dollar]`, + }, + { + "mathstruct", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "testlang", + Math: &MathConfig{ + InlineDollar: false, + InlineLatex: true, + DisplayDollar: true, + DisplayLatex: false, + }, + }, ` + math: + display_dollar: true + inline_latex: true + gitea: + lang: testlang +`, + }, + { + "mathoverride", &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "testlang", + Math: &MathConfig{ + InlineDollar: false, + InlineLatex: true, + DisplayDollar: true, + DisplayLatex: false, + }, + }, ` + math: + inline_dollar: true + display_latex: true + gitea: + lang: testlang + math: + display_dollar: true + inline_latex: true +`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := &RenderConfig{ + Meta: "table", + Icon: "table", + Lang: "", + } + if err := yaml.Unmarshal([]byte(tt.args), got); err != nil { + t.Errorf("RenderConfig.UnmarshalYAML() error = %v", err) + return + } + + if got.Meta != tt.expected.Meta { + t.Errorf("Meta Expected %s Got %s", tt.expected.Meta, got.Meta) + } + if got.Icon != tt.expected.Icon { + t.Errorf("Icon Expected %s Got %s", tt.expected.Icon, got.Icon) + } + if got.Lang != tt.expected.Lang { + t.Errorf("Lang Expected %s Got %s", tt.expected.Lang, got.Lang) + } + if got.TOC != tt.expected.TOC { + t.Errorf("TOC Expected %t Got %t", tt.expected.TOC, got.TOC) + } + }) + } +} diff --git a/modules/markup/markdown/ast.go b/modules/markup/markdown/extension/ast.go similarity index 99% rename from modules/markup/markdown/ast.go rename to modules/markup/markdown/extension/ast.go index 5191d94cdd85a..eb5901c87510c 100644 --- a/modules/markup/markdown/ast.go +++ b/modules/markup/markdown/extension/ast.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package markdown +package extension import ( "strconv" diff --git a/modules/markup/markdown/toc.go b/modules/markup/markdown/extension/toc.go similarity index 86% rename from modules/markup/markdown/toc.go rename to modules/markup/markdown/extension/toc.go index 103894d1abfc1..7198c49392660 100644 --- a/modules/markup/markdown/toc.go +++ b/modules/markup/markdown/extension/toc.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package markdown +package extension import ( "fmt" @@ -14,7 +14,8 @@ import ( "github.com/yuin/goldmark/ast" ) -func createTOCNode(toc []markup.Header, lang string) ast.Node { +// CreateTOCNode creates a Table of Contents node for a provided slices of Headers corresponding to the TOC +func CreateTOCNode(toc []markup.Header, lang string) ast.Node { details := NewDetails() summary := NewSummary() diff --git a/modules/markup/markdown/goldmark.go b/modules/markup/markdown/goldmark.go index 24f1ab7a01674..edb34a793f8b4 100644 --- a/modules/markup/markdown/goldmark.go +++ b/modules/markup/markdown/goldmark.go @@ -12,6 +12,8 @@ import ( "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/common" + "code.gitea.io/gitea/modules/markup/markdown/config" + "code.gitea.io/gitea/modules/markup/markdown/extension" "code.gitea.io/gitea/modules/setting" giteautil "code.gitea.io/gitea/modules/util" @@ -32,17 +34,13 @@ type ASTTransformer struct{} // Transform transforms the given AST tree. func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) { firstChild := node.FirstChild() - createTOC := false ctx := pc.Get(renderContextKey).(*markup.RenderContext) - rc := pc.Get(renderConfigKey).(*RenderConfig) - if rc.yamlNode != nil { - metaNode := rc.toMetaNode() - if metaNode != nil { - node.InsertBefore(node, firstChild, metaNode) - } - createTOC = rc.TOC - ctx.TableOfContents = make([]markup.Header, 0, 100) + rc := config.GetRenderConfig(pc) + metaNode := rc.ToMetaNode() + if metaNode != nil { + node.InsertBefore(node, firstChild, metaNode) } + ctx.TableOfContents = make([]markup.Header, 0, 100) _ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) { if !entering { @@ -161,7 +159,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa v.AppendChild(v, child) continue } - newChild := NewTaskCheckBoxListItem(listItem) + newChild := extension.NewTaskCheckBoxListItem(listItem) newChild.IsChecked = taskCheckBox.IsChecked newChild.SetAttributeString("class", []byte("task-list-item")) v.AppendChild(v, newChild) @@ -181,12 +179,12 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa return ast.WalkContinue, nil }) - if createTOC && len(ctx.TableOfContents) > 0 { + if rc.TOC && len(ctx.TableOfContents) > 0 { lang := rc.Lang if len(lang) == 0 { lang = setting.Langs[0] } - tocNode := createTOCNode(ctx.TableOfContents, lang) + tocNode := extension.CreateTOCNode(ctx.TableOfContents, lang) if tocNode != nil { node.InsertBefore(node, firstChild, tocNode) } @@ -264,10 +262,10 @@ type HTMLRenderer struct { // RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs. func (r *HTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { reg.Register(ast.KindDocument, r.renderDocument) - reg.Register(KindDetails, r.renderDetails) - reg.Register(KindSummary, r.renderSummary) - reg.Register(KindIcon, r.renderIcon) - reg.Register(KindTaskCheckBoxListItem, r.renderTaskCheckBoxListItem) + reg.Register(extension.KindDetails, r.renderDetails) + reg.Register(extension.KindSummary, r.renderSummary) + reg.Register(extension.KindIcon, r.renderIcon) + reg.Register(extension.KindTaskCheckBoxListItem, r.renderTaskCheckBoxListItem) reg.Register(east.KindTaskCheckBox, r.renderTaskCheckBox) } @@ -333,7 +331,7 @@ func (r *HTMLRenderer) renderIcon(w util.BufWriter, source []byte, node ast.Node return ast.WalkContinue, nil } - n := node.(*Icon) + n := node.(*extension.Icon) name := strings.TrimSpace(strings.ToLower(string(n.Name))) @@ -358,7 +356,7 @@ func (r *HTMLRenderer) renderIcon(w util.BufWriter, source []byte, node ast.Node } func (r *HTMLRenderer) renderTaskCheckBoxListItem(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { - n := node.(*TaskCheckBoxListItem) + n := node.(*extension.TaskCheckBoxListItem) if entering { if n.Attributes() != nil { _, _ = w.WriteString(" 0 { - value = enable[0] - } - return extensionFunc(func(e *Extension) { - e.enabled = value - }) -} - -// WithInlineDollarParser enables or disables the parsing of $...$ -func WithInlineDollarParser(enable ...bool) Option { - value := true - if len(enable) > 0 { - value = enable[0] - } - return extensionFunc(func(e *Extension) { - e.parseDollarInline = value - }) -} - -// WithBlockDollarParser enables or disables the parsing of $$...$$ -func WithBlockDollarParser(enable ...bool) Option { - value := true - if len(enable) > 0 { - value = enable[0] - } - return extensionFunc(func(e *Extension) { - e.parseDollarBlock = value - }) -} - // Math represents a math extension with default rendered delimiters -var Math = &Extension{ - enabled: true, - parseDollarBlock: true, -} +var Math = &Extension{} // NewExtension creates a new math extension with the provided options func NewExtension(opts ...Option) *Extension { - r := &Extension{ - enabled: true, - parseDollarBlock: true, - } + r := &Extension{} for _, o := range opts { o.SetOption(r) @@ -83,21 +40,14 @@ func NewExtension(opts ...Option) *Extension { // Extend extends goldmark with our parsers and renderers func (e *Extension) Extend(m goldmark.Markdown) { - if !e.enabled { - return - } - m.Parser().AddOptions(parser.WithBlockParsers( - util.Prioritized(NewBlockParser(e.parseDollarBlock), 701), + util.Prioritized(NewBlockParser(), 701), )) - inlines := []util.PrioritizedValue{ + m.Parser().AddOptions(parser.WithInlineParsers( util.Prioritized(NewInlineBracketParser(), 501), - } - if e.parseDollarInline { - inlines = append(inlines, util.Prioritized(NewInlineDollarParser(), 501)) - } - m.Parser().AddOptions(parser.WithInlineParsers(inlines...)) + util.Prioritized(NewInlineDollarParser(), 501), + )) m.Renderer().AddOptions(renderer.WithNodeRenderers( util.Prioritized(NewBlockRenderer(), 501), diff --git a/modules/markup/markdown/renderconfig.go b/modules/markup/markdown/renderconfig.go deleted file mode 100644 index 6a3b3a1bde8b4..0000000000000 --- a/modules/markup/markdown/renderconfig.go +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package markdown - -import ( - "strings" - - "code.gitea.io/gitea/modules/log" - "github.com/yuin/goldmark/ast" - "gopkg.in/yaml.v3" -) - -// RenderConfig represents rendering configuration for this file -type RenderConfig struct { - Meta string - Icon string - TOC bool - Lang string - yamlNode *yaml.Node -} - -// UnmarshalYAML implement yaml.v3 UnmarshalYAML -func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error { - if rc == nil { - rc = &RenderConfig{ - Meta: "table", - Icon: "table", - Lang: "", - } - } - rc.yamlNode = value - - type basicRenderConfig struct { - Gitea *yaml.Node `yaml:"gitea"` - TOC bool `yaml:"include_toc"` - Lang string `yaml:"lang"` - } - - var basic basicRenderConfig - - err := value.Decode(&basic) - if err != nil { - return err - } - - if basic.Lang != "" { - rc.Lang = basic.Lang - } - - rc.TOC = basic.TOC - if basic.Gitea == nil { - return nil - } - - var control *string - if err := basic.Gitea.Decode(&control); err == nil && control != nil { - log.Info("control %v", control) - switch strings.TrimSpace(strings.ToLower(*control)) { - case "none": - rc.Meta = "none" - case "table": - rc.Meta = "table" - default: // "details" - rc.Meta = "details" - } - return nil - } - - type giteaControl struct { - Meta string `yaml:"meta"` - Icon string `yaml:"details_icon"` - TOC *yaml.Node `yaml:"include_toc"` - Lang string `yaml:"lang"` - } - - var controlStruct *giteaControl - if err := basic.Gitea.Decode(controlStruct); err != nil || controlStruct == nil { - return err - } - - switch strings.TrimSpace(strings.ToLower(controlStruct.Meta)) { - case "none": - rc.Meta = "none" - case "table": - rc.Meta = "table" - default: // "details" - rc.Meta = "details" - } - - rc.Icon = strings.TrimSpace(strings.ToLower(controlStruct.Icon)) - - if controlStruct.Lang != "" { - rc.Lang = controlStruct.Lang - } - - var toc bool - if err := controlStruct.TOC.Decode(&toc); err == nil { - rc.TOC = toc - } - - return nil -} - -func (rc *RenderConfig) toMetaNode() ast.Node { - if rc.yamlNode == nil { - return nil - } - switch rc.Meta { - case "table": - return nodeToTable(rc.yamlNode) - case "details": - return nodeToDetails(rc.yamlNode, rc.Icon) - default: - return nil - } -} diff --git a/modules/markup/markdown/renderconfig_test.go b/modules/markup/markdown/renderconfig_test.go deleted file mode 100644 index 1027035cda5a6..0000000000000 --- a/modules/markup/markdown/renderconfig_test.go +++ /dev/null @@ -1,162 +0,0 @@ -// Copyright 2022 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package markdown - -import ( - "testing" - - "gopkg.in/yaml.v3" -) - -func TestRenderConfig_UnmarshalYAML(t *testing.T) { - tests := []struct { - name string - expected *RenderConfig - args string - }{ - { - "empty", &RenderConfig{ - Meta: "table", - Icon: "table", - Lang: "", - }, "", - }, - { - "lang", &RenderConfig{ - Meta: "table", - Icon: "table", - Lang: "test", - }, "lang: test", - }, - { - "metatable", &RenderConfig{ - Meta: "table", - Icon: "table", - Lang: "", - }, "gitea: table", - }, - { - "metanone", &RenderConfig{ - Meta: "none", - Icon: "table", - Lang: "", - }, "gitea: none", - }, - { - "metadetails", &RenderConfig{ - Meta: "details", - Icon: "table", - Lang: "", - }, "gitea: details", - }, - { - "metawrong", &RenderConfig{ - Meta: "details", - Icon: "table", - Lang: "", - }, "gitea: wrong", - }, - { - "toc", &RenderConfig{ - TOC: true, - Meta: "table", - Icon: "table", - Lang: "", - }, "include_toc: true", - }, - { - "tocfalse", &RenderConfig{ - TOC: false, - Meta: "table", - Icon: "table", - Lang: "", - }, "include_toc: false", - }, - { - "toclang", &RenderConfig{ - Meta: "table", - Icon: "table", - TOC: true, - Lang: "testlang", - }, ` - include_toc: true - lang: testlang -`, - }, - { - "complexlang", &RenderConfig{ - Meta: "table", - Icon: "table", - Lang: "testlang", - }, ` - gitea: - lang: testlang -`, - }, - { - "complexlang2", &RenderConfig{ - Meta: "table", - Icon: "table", - Lang: "testlang", - }, ` - lang: notright - gitea: - lang: testlang -`, - }, - { - "complexlang", &RenderConfig{ - Meta: "table", - Icon: "table", - Lang: "testlang", - }, ` - gitea: - lang: testlang -`, - }, - { - "complex2", &RenderConfig{ - Lang: "two", - Meta: "table", - TOC: true, - Icon: "smiley", - }, ` - lang: one - include_toc: true - gitea: - details_icon: smiley - meta: table - include_toc: true - lang: two -`, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := &RenderConfig{ - Meta: "table", - Icon: "table", - Lang: "", - } - if err := yaml.Unmarshal([]byte(tt.args), got); err != nil { - t.Errorf("RenderConfig.UnmarshalYAML() error = %v", err) - return - } - - if got.Meta != tt.expected.Meta { - t.Errorf("Meta Expected %s Got %s", tt.expected.Meta, got.Meta) - } - if got.Icon != tt.expected.Icon { - t.Errorf("Icon Expected %s Got %s", tt.expected.Icon, got.Icon) - } - if got.Lang != tt.expected.Lang { - t.Errorf("Lang Expected %s Got %s", tt.expected.Lang, got.Lang) - } - if got.TOC != tt.expected.TOC { - t.Errorf("TOC Expected %t Got %t", tt.expected.TOC, got.TOC) - } - }) - } -}