Skip to content

Commit 6924d43

Browse files
committed
add "none standard"/optional headlines to bold
1 parent 64b00e8 commit 6924d43

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

slack.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@ import (
66
"strings"
77
)
88

9-
// Slack returns github markdown converted to slack
10-
func Slack(markdown string) string {
9+
// SlackConvertOptions contains options to fine-toon GitHub to Slack markdown convert
10+
type SlackConvertOptions struct {
11+
// Headlines will define if GitHub headlines will be updated to be bold text in slack
12+
// there is no headlines as sucks in Slack
13+
Headlines bool
14+
}
15+
16+
// Slack returns github markdown converted to Slack
17+
func Slack(markdown string, options ...SlackConvertOptions) string {
1118
var re *regexp.Regexp
1219

20+
opt := SlackConvertOptions{}
21+
if len(options) > 0 {
22+
opt = options[0]
23+
}
24+
25+
// TODO: write proper regex
1326
linkRegex := ".*"
1427

1528
// bold **TEXT** -> *TEXT*
@@ -55,5 +68,10 @@ func Slack(markdown string) string {
5568
re = regexp.MustCompile(`(?m)((\n[ ]{0,})(\*))`)
5669
markdown = re.ReplaceAllString(markdown, "$2•")
5770

71+
if opt.Headlines {
72+
re = regexp.MustCompile(`(?m)((^\t?[ ]{0,15}#{1,4}[ ]{1,})(.+))`)
73+
markdown = re.ReplaceAllString(markdown, "*$3*")
74+
}
75+
5876
return markdown
5977
}

slack_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,23 @@ func TestSlack(t *testing.T) {
4747
• remove DELETE /v1/message (<https://github.com/foo/boo/issues/121|#121>) (<https://github.com/foo/boo/commit/3523r42|3523r42>)`
4848

4949
assert.Equal(msgSlack, Slack(msgGithub))
50+
51+
// test headlines parse
52+
optWithHeadlines := SlackConvertOptions{
53+
Headlines: true,
54+
}
55+
assert.Equal("*fooo*", Slack("### fooo", optWithHeadlines))
56+
assert.Equal("*Boo foo 123*", Slack(" # Boo foo 123", optWithHeadlines))
57+
assert.Equal(`
58+
*Features*
59+
`, Slack(`
60+
### Features
61+
`, optWithHeadlines))
62+
63+
msgSlackHeadlinesBold := `*<https://github.com/foo/boo/compare/v1.49.3...v1.50.0|1.50.0> (2015-02-12)*
64+
*Features*
65+
• add GET /v1/events (<https://github.com/foo/boo/issues/134|#134>) (<https://github.com/foo/boo/commit/1726806|1726806>)
66+
• remove DELETE /v1/message (<https://github.com/foo/boo/issues/121|#121>) (<https://github.com/foo/boo/commit/3523r42|3523r42>)`
67+
68+
assert.Equal(msgSlackHeadlinesBold, Slack(msgGithub, optWithHeadlines))
5069
}

0 commit comments

Comments
 (0)