From 11b3fec94175b554b74d2af1168e512a3512afb8 Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Fri, 23 Aug 2024 17:56:33 +0900 Subject: [PATCH] =?UTF-8?q?:bug:=20=E3=82=82=E3=81=A8=E3=81=AE=E3=83=A1?= =?UTF-8?q?=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8=E3=81=AB=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=83=96=E3=83=AD=E3=83=83=E3=82=AF=E3=81=8C=E3=81=82?= =?UTF-8?q?=E3=81=A3=E3=81=A6=E3=82=82=E8=A1=A8=E7=A4=BA=E3=81=8C=E5=A3=8A?= =?UTF-8?q?=E3=82=8C=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/root.go | 15 ++++++++++++++- cmd/root_test.go | 11 ++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index f1737f8..3a0dade 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -78,7 +78,20 @@ It reads the configuration file and sends the message to the specified webhook.` } if withCodeBlock { - message = fmt.Sprintf("```%s\n%s\n```", codeBlockLang, message) + leadingBackQuoteCountMax := 0 + + for _, line := range strings.Split(message, "\n") { + if !strings.HasPrefix(line, "```") { + continue + } + noLeadingBackQuoteLine := strings.TrimLeft(line, "`") + leadingBackQuoteCount := len(line) - len(noLeadingBackQuoteLine) + leadingBackQuoteCountMax = max(leadingBackQuoteCountMax, leadingBackQuoteCount) + } + + codeBlockBackQuote := strings.Repeat("`", max(leadingBackQuoteCountMax+1, 3)) + + message = fmt.Sprintf("%s%s\n%s\n%s", codeBlockBackQuote, codeBlockLang, message, codeBlockBackQuote) } if cl != nil { diff --git a/cmd/root_test.go b/cmd/root_test.go index e2de575..f3ba0e3 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -36,11 +36,12 @@ func TestRoot(t *testing.T) { isError bool }{ "ok": {defaultWebhookConfig, input{false, "", "", []string{"test"}}, nil, "test", false}, - "コードブロックがあっても問題なし": {defaultWebhookConfig, input{true, "", "", []string{"print('Hello, World!')"}}, nil, "```\nprint('Hello, World!')\n```", false}, - "コードブロックと言語指定があっても問題なし": {defaultWebhookConfig, input{true, "python", "", []string{"print('Hello, World!')"}}, nil, "```python\nprint('Hello, World!')\n```", false}, - "メッセージがない場合は標準入力から": {defaultWebhookConfig, input{false, "", "stdin test", nil}, nil, "stdin test", false}, - "メッセージがあったら標準入力は無視": {defaultWebhookConfig, input{false, "", "stdin test", []string{"test"}}, nil, "test", false}, - "SendMessageがErrEmptyMessageを返す": {defaultWebhookConfig, input{false, "", "", nil}, client.ErrEmptyMessage, "", true}, + "コードブロックがあっても問題なし": {defaultWebhookConfig, input{true, "", "", []string{"print('Hello, World!')"}}, nil, "```\nprint('Hello, World!')\n```", false}, + "コードブロックと言語指定があっても問題なし": {defaultWebhookConfig, input{true, "python", "", []string{"print('Hello, World!')"}}, nil, "```python\nprint('Hello, World!')\n```", false}, + "メッセージがない場合は標準入力から": {defaultWebhookConfig, input{false, "", "stdin test", nil}, nil, "stdin test", false}, + "メッセージがあったら標準入力は無視": {defaultWebhookConfig, input{false, "", "stdin test", []string{"test"}}, nil, "test", false}, + "SendMessageがErrEmptyMessageを返す": {defaultWebhookConfig, input{false, "", "", nil}, client.ErrEmptyMessage, "", true}, + "メッセージにコードブロックが含まれていて、そこにコードブロックを付けても問題なし": {defaultWebhookConfig, input{true, "", "```python\nprint('Hello, World!')\n```", nil}, nil, "````\n```python\nprint('Hello, World!')\n```\n````", false}, } for description, tt := range test {