-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.go
More file actions
73 lines (58 loc) · 2.41 KB
/
markdown.go
File metadata and controls
73 lines (58 loc) · 2.41 KB
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
package main
import (
"regexp"
"strings"
)
// convertMarkdownToMrkdwn converts Markdown format to Slack's Mrkdwn format
// See: https://api.slack.com/reference/surfaces/formatting
func convertMarkdownToMrkdwn(markdown string) string {
text := markdown
// Placeholder to protect already-converted bold text
const boldPlaceholder = "\x00BOLD\x00"
// Store bold conversions with placeholders
boldMatches := []string{}
// Convert bold: **text** or __text__ -> placeholder
boldPattern := regexp.MustCompile(`\*\*(.+?)\*\*`)
text = boldPattern.ReplaceAllStringFunc(text, func(match string) string {
content := boldPattern.FindStringSubmatch(match)
if len(content) > 1 {
replacement := "*" + content[1] + "*"
boldMatches = append(boldMatches, replacement)
return boldPlaceholder
}
return match
})
boldUnderscorePattern := regexp.MustCompile(`__(.+?)__`)
text = boldUnderscorePattern.ReplaceAllStringFunc(text, func(match string) string {
content := boldUnderscorePattern.FindStringSubmatch(match)
if len(content) > 1 {
replacement := "*" + content[1] + "*"
boldMatches = append(boldMatches, replacement)
return boldPlaceholder
}
return match
})
// Convert italic: single *text* -> _text_ (only single asterisks)
italicAsteriskPattern := regexp.MustCompile(`\*([^*\n]+?)\*`)
text = italicAsteriskPattern.ReplaceAllString(text, `_${1}_`)
// Convert italic: _text_ -> _text_ (already in Mrkdwn format, no change needed)
// Restore bold text from placeholders
for _, boldText := range boldMatches {
text = strings.Replace(text, boldPlaceholder, boldText, 1)
}
// Convert strikethrough: ~~text~~ -> ~text~
strikethroughPattern := regexp.MustCompile(`~~(.+?)~~`)
text = strikethroughPattern.ReplaceAllString(text, `~${1}~`)
// Convert links: [text](url) -> <url|text>
linkPattern := regexp.MustCompile(`\[([^\]]+)\]\(([^\)]+)\)`)
text = linkPattern.ReplaceAllString(text, `<${2}|${1}>`)
// Convert code blocks: ```lang\ncode\n``` -> ```code```
// Remove language specifier from code blocks (supports alphanumeric, hyphens, plus, etc.)
codeBlockPattern := regexp.MustCompile("```[a-zA-Z0-9+#\\-]*\n")
text = codeBlockPattern.ReplaceAllString(text, "```\n")
// Convert unordered lists: * item or - item -> • item
listPattern := regexp.MustCompile(`(?m)^[\*\-]\s+`)
text = listPattern.ReplaceAllString(text, "• ")
// Convert ordered lists: 1. item -> 1. item (no change needed)
return text
}