discordmd.go 702 B

123456789101112131415161718192021222324
  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. )
  11. // Parse parses Discord-flavored markdown to tview's [Color Tags].
  12. //
  13. // [Color Tags]: https://pkg.go.dev/github.com/rivo/tview#hdr-Colors
  14. func Parse(md string) string {
  15. md = boldRegex.ReplaceAllString(md, "[::b]$1[::-]")
  16. md = italicRegex.ReplaceAllString(md, "[::i]$1[::-]")
  17. md = underlineRegex.ReplaceAllString(md, "[::u]$1[::-]")
  18. md = strikeThroughRegex.ReplaceAllString(md, "[::s]$1[::-]")
  19. return md
  20. }