discordmd.go 753 B

12345678910111213141516171819202122
  1. package discordmd
  2. import (
  3. "regexp"
  4. )
  5. var (
  6. boldRegex = regexp.MustCompile(`(?ms)\*\*(.*?)\*\*`)
  7. italicRegex = regexp.MustCompile(`(?ms)\*(.*?)\*`)
  8. underlineRegex = regexp.MustCompile(`(?ms)__(.*?)__`)
  9. strikeThroughRegex = regexp.MustCompile(`(?ms)~~(.*?)~~`)
  10. inlineCodeBlockRegex = regexp.MustCompile("(?ms)`" + `([^` + "`" + `\n]+)` + "`")
  11. )
  12. func Parse(input string) string {
  13. input = boldRegex.ReplaceAllString(input, "[::b]$1[::-]")
  14. input = italicRegex.ReplaceAllString(input, "[::i]$1[::-]")
  15. input = underlineRegex.ReplaceAllString(input, "[::u]$1[::-]")
  16. input = strikeThroughRegex.ReplaceAllString(input, "[::s]$1[::-]")
  17. input = inlineCodeBlockRegex.ReplaceAllString(input, "[::r]$1[::-]")
  18. return input
  19. }