-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (98 loc) · 4.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"regexp"
"strings"
)
var (
linkReplacement = regexp.MustCompile(`(.?)<([^|>]+)\|([^>]+)>(.?)`) // refactor links
appendLeadingEscape = regexp.MustCompile(`(?m)^(#+\s|-\s)`) // add escape for leading # or -
backSlashReplacementFromGfm = regexp.MustCompile("\\\\([-*_`~#[]\\\\])")
boldReplacement = regexp.MustCompile("([\\s~_`]|^*)\\\\\\*([^\\s\\\\]+|[^*\\s\n]+[^*\n]*[^*\\s\n]+)\\\\\\*([\\s~_`\n]|$)") // find actual bold styles
underscoreReplacement = regexp.MustCompile("([*\\s~`]|^*)\\\\(_)([^\\s\n_]+|[^\\s\n_]+[^\n_]*[^\\s\n_]+)\\\\\\_([*\\s~`\n]|$)") // find actual italic blocks
tildeReplacement = regexp.MustCompile("([*\\s_`]|^*)\\\\(~)([^\\s\n~]+|[^\\s\n~]+[^\n~]*[^\\s\n~]+)\\\\\\~([*\\s_`\n]|$)") // find actual strike through blocks
backtickReplacement = regexp.MustCompile("\\\\`([^\n\\`]+)\\\\`") // find code
parenthesisReplace = regexp.MustCompile(`(?m)^\s*(\d+)\)`) // find ordered list with format 1), 2) as UI render these as 1., 2.
backslaskMultipler = regexp.MustCompile(`(\\+)`) // make \ double to make it slash symbol
tildeToDoubleReplacement = regexp.MustCompile(`([^\\]|^)~`)
codeBlockReplacement = regexp.MustCompile("(```)(.*)(```)")
blockquoteReplacement = regexp.MustCompile("(> )([^\n]*)($)")
)
const tempStr string = "REPLACERSTR"
func SlackMarkdownToGeneral(slackMarkdown string) string {
markdown := linkReplacement.ReplaceAllStringFunc(slackMarkdown, func(match string) string {
submatches := linkReplacement.FindStringSubmatch(match)
if len(submatches) > 4 {
if submatches[1] == "`" && submatches[4] == "`" {
return fmt.Sprintf("[`%s`](%s)", submatches[3], submatches[2])
}
return fmt.Sprintf("%s[%s](%s)%s", submatches[1], submatches[3], submatches[2], submatches[4])
}
return match
})
markdown = backslaskMultipler.ReplaceAllString(markdown, "$1$1")
markdown = backSlashReplacementFromGfm.ReplaceAllString(markdown, "$1")
markdown = strings.ReplaceAll(markdown, "*", "\\*")
markdown = boldReplacement.ReplaceAllString(markdown, "$1**$2**$3")
markdown = strings.ReplaceAll(markdown, "_", "\\_")
markdown = underscoreReplacement.ReplaceAllString(markdown, "$1$2$3$2$4")
markdown = strings.ReplaceAll(markdown, "~", "\\~")
markdown = tildeReplacement.ReplaceAllString(markdown, "$1$2$3$2$4")
markdown = codeBlockReplacement.ReplaceAllStringFunc(markdown, func(match string) string {
submatches := codeBlockReplacement.FindStringSubmatch(match)
return fmt.Sprintf("%s\n%s\n%s", tempStr, submatches[2], tempStr)
})
markdown = strings.ReplaceAll(markdown, "`", "\\`")
markdown = blockquoteReplacement.ReplaceAllString(markdown, "$1$2$3\n")
markdown = backtickReplacement.ReplaceAllString(markdown, "`$1`")
markdown = strings.ReplaceAll(markdown, tempStr, "```")
markdown = appendLeadingEscape.ReplaceAllString(markdown, "\\$1")
markdown = parenthesisReplace.ReplaceAllString(markdown, "$1 )")
return markdown
}
func MarkdownToHtmlMark(generalMarkdown string) string {
markdown := tildeToDoubleReplacement.ReplaceAllString(generalMarkdown, "$1~~")
markdown = strings.ReplaceAll(markdown, "\n", "\n\n") // double enter will render as <p> in html
return markdown
}
func splitSentence(input string) []string {
pattern := regexp.MustCompile(`(\*.*?\*|_.*?_|~.*?~|` + "`" + `.*?` + "`" + `|<.*?>|\s+|\b)`)
matches := pattern.FindAllString(input, -1)
var result []string
var lastIndex int
for _, match := range matches {
index := pattern.FindStringIndex(input[lastIndex:])
fmt.Println("")
if beforeMatch := input[lastIndex : lastIndex+index[0]]; beforeMatch != "" {
result = append(result, beforeMatch)
}
result = append(result, match)
lastIndex += index[1] + index[0]
}
if lastIndex < len(input) {
result = append(result, input[lastIndex:])
}
return result
}
func splitInput(input string) []string {
var splitedText []string
catchIndex := 0
for i, s := range input {
if strings.ContainsRune("*_~`<", s) {
if catchIndex < i {
splitedText = append(splitedText, input[catchIndex:i])
}
splitedText = append(splitedText, string(s))
catchIndex = i + 1
}
}
if catchIndex < len(input) {
splitedText = append(splitedText, input[catchIndex:])
}
return splitedText
}
func main() {
input := "This is *complex* <https://github.com|style> for _~*my slack*~_ `message`"
split := splitSentence(input)
fmt.Printf("%q\n", split)
}