From 1924d4a6195f018c3bc140bebec04061a011ac02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20BABAL?= Date: Fri, 17 Nov 2023 14:02:59 +0300 Subject: [PATCH] md formatter change (#1311) --- pkg/bot/interactive/markdown.go | 46 ++++++++++++++-------------- pkg/bot/interactive/markdown_test.go | 16 +++++----- pkg/bot/interactive/plaintext.go | 8 ++--- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/pkg/bot/interactive/markdown.go b/pkg/bot/interactive/markdown.go index 8a433a5c1..4f5dbf107 100644 --- a/pkg/bot/interactive/markdown.go +++ b/pkg/bot/interactive/markdown.go @@ -11,19 +11,19 @@ import ( // MDFormatter represents the capability of Markdown Formatter type MDFormatter struct { - newlineFormatter func(msg string) string - headerFormatter func(msg string) string - codeBlockFormatter func(msg string) string - adaptiveCodeBlockFormatter func(msg string) string + NewlineFormatter func(msg string) string + HeaderFormatter func(msg string) string + CodeBlockFormatter func(msg string) string + AdaptiveCodeBlockFormatter func(msg string) string } // NewMDFormatter is for initializing custom Markdown formatter func NewMDFormatter(newlineFormatter, headerFormatter func(msg string) string) MDFormatter { return MDFormatter{ - newlineFormatter: newlineFormatter, - headerFormatter: headerFormatter, - codeBlockFormatter: formatx.CodeBlock, - adaptiveCodeBlockFormatter: formatx.AdaptiveCodeBlock, + NewlineFormatter: newlineFormatter, + HeaderFormatter: headerFormatter, + CodeBlockFormatter: formatx.CodeBlock, + AdaptiveCodeBlockFormatter: formatx.AdaptiveCodeBlock, } } @@ -36,11 +36,11 @@ func DefaultMDFormatter() MDFormatter { func RenderMessage(mdFormatter MDFormatter, msg CoreMessage) string { var out strings.Builder addLine := func(in string) { - out.WriteString(mdFormatter.newlineFormatter(in)) + out.WriteString(mdFormatter.NewlineFormatter(in)) } if msg.Header != "" { - addLine(mdFormatter.headerFormatter(msg.Header)) + addLine(mdFormatter.HeaderFormatter(msg.Header)) } if msg.Description != "" { @@ -52,7 +52,7 @@ func RenderMessage(mdFormatter MDFormatter, msg CoreMessage) string { } if msg.BaseBody.CodeBlock != "" { - addLine(mdFormatter.codeBlockFormatter(msg.BaseBody.CodeBlock)) + addLine(mdFormatter.CodeBlockFormatter(msg.BaseBody.CodeBlock)) } for i, section := range msg.Sections { @@ -63,13 +63,13 @@ func RenderMessage(mdFormatter MDFormatter, msg CoreMessage) string { } if section.Header != "" { - addLine(mdFormatter.headerFormatter(section.Header)) + addLine(mdFormatter.HeaderFormatter(section.Header)) } if len(section.TextFields) > 0 { - addLine(mdFormatter.headerFormatter("Fields")) + addLine(mdFormatter.HeaderFormatter("Fields")) for _, field := range section.TextFields { - addLine(fmt.Sprintf(" • %s: %s", mdFormatter.headerFormatter(field.Key), field.Value)) + addLine(fmt.Sprintf(" • %s: %s", mdFormatter.HeaderFormatter(field.Key), field.Value)) } addLine("") // new line to separate other } @@ -85,13 +85,13 @@ func RenderMessage(mdFormatter MDFormatter, msg CoreMessage) string { if section.Body.CodeBlock != "" { // not using the adaptive code block is on purpose, we always want to have // a multiline code block to improve readability - addLine(mdFormatter.codeBlockFormatter(section.Body.CodeBlock)) + addLine(mdFormatter.CodeBlockFormatter(section.Body.CodeBlock)) } if section.BulletLists.AreItemsDefined() { for _, item := range section.BulletLists { addLine("") // new line - addLine(mdFormatter.headerFormatter(item.Title)) + addLine(mdFormatter.HeaderFormatter(item.Title)) for _, opt := range item.Items { addLine(fmt.Sprintf(" • %s", opt)) @@ -107,26 +107,26 @@ func RenderMessage(mdFormatter MDFormatter, msg CoreMessage) string { } if ms.Description.CodeBlock != "" { - addLine(mdFormatter.adaptiveCodeBlockFormatter(ms.Description.CodeBlock)) + addLine(mdFormatter.AdaptiveCodeBlockFormatter(ms.Description.CodeBlock)) } addLine("") // new line - addLine(mdFormatter.headerFormatter("Available options")) + addLine(mdFormatter.HeaderFormatter("Available options")) for _, opt := range ms.Options { - addLine(fmt.Sprintf(" • %s", mdFormatter.adaptiveCodeBlockFormatter(opt.Value))) + addLine(fmt.Sprintf(" • %s", mdFormatter.AdaptiveCodeBlockFormatter(opt.Value))) } } if section.Selects.AreOptionsDefined() { addLine("") // new line - addLine(mdFormatter.headerFormatter("Available options")) + addLine(mdFormatter.HeaderFormatter("Available options")) for _, item := range section.Selects.Items { for _, group := range item.OptionGroups { addLine(fmt.Sprintf(" • %s", group.Name)) for _, opt := range group.Options { - addLine(fmt.Sprintf(" • %s", mdFormatter.adaptiveCodeBlockFormatter(opt.Value))) + addLine(fmt.Sprintf(" • %s", mdFormatter.AdaptiveCodeBlockFormatter(opt.Value))) } } } @@ -134,7 +134,7 @@ func RenderMessage(mdFormatter MDFormatter, msg CoreMessage) string { for _, btn := range section.Buttons { if btn.DescriptionStyle == api.ButtonDescriptionStyleBold && btn.Description != "" { - addLine(mdFormatter.headerFormatter(btn.Description)) + addLine(mdFormatter.HeaderFormatter(btn.Description)) } if btn.URL != "" { @@ -142,7 +142,7 @@ func RenderMessage(mdFormatter MDFormatter, msg CoreMessage) string { continue } if btn.Command != "" { - addLine(fmt.Sprintf(" • %s", mdFormatter.adaptiveCodeBlockFormatter(btn.Command))) + addLine(fmt.Sprintf(" • %s", mdFormatter.AdaptiveCodeBlockFormatter(btn.Command))) continue } } diff --git a/pkg/bot/interactive/markdown_test.go b/pkg/bot/interactive/markdown_test.go index 0336cda9a..fdc0b94f2 100644 --- a/pkg/bot/interactive/markdown_test.go +++ b/pkg/bot/interactive/markdown_test.go @@ -131,21 +131,21 @@ func TestInteractiveMessageToMarkdownMultiSelect(t *testing.T) { // go test -run=TestInteractiveMessageToMarkdown ./pkg/bot/interactive/... -test.update-golden func TestInteractiveMessageToMarkdown(t *testing.T) { formatterForCustomNewLines := MDFormatter{ - newlineFormatter: func(msg string) string { + NewlineFormatter: func(msg string) string { return fmt.Sprintf("%s
", msg) }, - headerFormatter: MdHeaderFormatter, - codeBlockFormatter: formatx.CodeBlock, - adaptiveCodeBlockFormatter: formatx.AdaptiveCodeBlock, + HeaderFormatter: MdHeaderFormatter, + CodeBlockFormatter: formatx.CodeBlock, + AdaptiveCodeBlockFormatter: formatx.AdaptiveCodeBlock, } formatterForCustomHeaders := MDFormatter{ - newlineFormatter: NewlineFormatter, - headerFormatter: func(msg string) string { + NewlineFormatter: NewlineFormatter, + HeaderFormatter: func(msg string) string { return fmt.Sprintf("*%s*", msg) }, - codeBlockFormatter: formatx.CodeBlock, - adaptiveCodeBlockFormatter: formatx.AdaptiveCodeBlock, + CodeBlockFormatter: formatx.CodeBlock, + AdaptiveCodeBlockFormatter: formatx.AdaptiveCodeBlock, } tests := []struct { name string diff --git a/pkg/bot/interactive/plaintext.go b/pkg/bot/interactive/plaintext.go index 6b42243d4..4c036a139 100644 --- a/pkg/bot/interactive/plaintext.go +++ b/pkg/bot/interactive/plaintext.go @@ -5,10 +5,10 @@ func MessageToPlaintext(msg CoreMessage, newlineFormatter func(in string) string msg.Description = "" fmt := MDFormatter{ - newlineFormatter: newlineFormatter, - headerFormatter: NoFormatting, - codeBlockFormatter: NoFormatting, - adaptiveCodeBlockFormatter: NoFormatting, + NewlineFormatter: newlineFormatter, + HeaderFormatter: NoFormatting, + CodeBlockFormatter: NoFormatting, + AdaptiveCodeBlockFormatter: NoFormatting, } return RenderMessage(fmt, msg) }