Skip to content

Commit

Permalink
add "none standard"/optional headlines to bold
Browse files Browse the repository at this point in the history
  • Loading branch information
eritikass committed Feb 6, 2020
1 parent 64b00e8 commit 6924d43
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
22 changes: 20 additions & 2 deletions slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ import (
"strings"
)

// Slack returns github markdown converted to slack
func Slack(markdown string) string {
// SlackConvertOptions contains options to fine-toon GitHub to Slack markdown convert
type SlackConvertOptions struct {
// Headlines will define if GitHub headlines will be updated to be bold text in slack
// there is no headlines as sucks in Slack
Headlines bool
}

// Slack returns github markdown converted to Slack
func Slack(markdown string, options ...SlackConvertOptions) string {
var re *regexp.Regexp

opt := SlackConvertOptions{}
if len(options) > 0 {
opt = options[0]
}

// TODO: write proper regex
linkRegex := ".*"

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

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

return markdown
}
19 changes: 19 additions & 0 deletions slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,23 @@ func TestSlack(t *testing.T) {
• remove DELETE /v1/message (<https://github.com/foo/boo/issues/121|#121>) (<https://github.com/foo/boo/commit/3523r42|3523r42>)`

assert.Equal(msgSlack, Slack(msgGithub))

// test headlines parse
optWithHeadlines := SlackConvertOptions{
Headlines: true,
}
assert.Equal("*fooo*", Slack("### fooo", optWithHeadlines))
assert.Equal("*Boo foo 123*", Slack(" # Boo foo 123", optWithHeadlines))
assert.Equal(`
*Features*
`, Slack(`
### Features
`, optWithHeadlines))

msgSlackHeadlinesBold := `*<https://github.com/foo/boo/compare/v1.49.3...v1.50.0|1.50.0> (2015-02-12)*
*Features*
• add GET /v1/events (<https://github.com/foo/boo/issues/134|#134>) (<https://github.com/foo/boo/commit/1726806|1726806>)
• remove DELETE /v1/message (<https://github.com/foo/boo/issues/121|#121>) (<https://github.com/foo/boo/commit/3523r42|3523r42>)`

assert.Equal(msgSlackHeadlinesBold, Slack(msgGithub, optWithHeadlines))
}

0 comments on commit 6924d43

Please sign in to comment.