Skip to content

Commit

Permalink
Merge pull request #15 from ikura-hamu/fix/code_block
Browse files Browse the repository at this point in the history
🐛 もとのメッセージにコードブロックがあっても表示が壊れないよう修正
  • Loading branch information
ikura-hamu authored Aug 23, 2024
2 parents 93c8adc + 11b3fec commit 55e0b4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
15 changes: 14 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 55e0b4f

Please sign in to comment.