-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.go
46 lines (41 loc) · 864 Bytes
/
markdown.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
package main
import (
"fmt"
"strings"
)
var (
// is thread safe/goroutine safe
markdownReplacer = strings.NewReplacer(
"\\", "\\\\",
"`", "\\`",
"*", "\\*",
"_", "\\_",
"{", "\\{",
"}", "\\}",
"[", "\\[",
"]", "\\]",
"(", "\\(",
")", "\\)",
"#", "\\#",
"+", "\\+",
"-", "\\-",
".", "\\.",
"!", "\\!",
)
)
// Escape user input outside of inline code blocks
func Escape(userInput string) string {
return markdownReplacer.Replace(userInput)
}
// WrapInInlineCodeBlock puts the user input into a inline codeblock that is properly escaped.
func WrapInInlineCodeBlock(userInput string) (userOutput string) {
if userInput == "" {
return ""
}
numberBackticks := strings.Count(userInput, "`") + 1
userOutput = userInput
for idx := 0; idx < numberBackticks; idx++ {
userOutput = fmt.Sprintf("`%s`", userOutput)
}
return
}