markdown.go 866 B

12345678910111213141516171819202122232425
  1. package markdown
  2. import (
  3. "regexp"
  4. )
  5. var (
  6. boldRe = regexp.MustCompile(`(?ms)\*\*(.*?)\*\*`)
  7. italicRe = regexp.MustCompile(`(?ms)\*(.*?)\*`)
  8. underlineRe = regexp.MustCompile(`(?ms)__(.*?)__`)
  9. strikethroughRe = regexp.MustCompile(`(?ms)~~(.*?)~~`)
  10. codeblockRe = regexp.MustCompile("(?ms)`" + `([^` + "`" + `\n]+)` + "`")
  11. emoteRe = regexp.MustCompile(`<(:[a-zA-Z0-9]+:)[0-9]+>`)
  12. )
  13. func Parse(input string, emoteColor string) string {
  14. input = boldRe.ReplaceAllString(input, "[::b]$1[::B]")
  15. input = italicRe.ReplaceAllString(input, "[::i]$1[::I]")
  16. input = underlineRe.ReplaceAllString(input, "[::u]$1[::U]")
  17. input = strikethroughRe.ReplaceAllString(input, "[::s]$1[::S]")
  18. input = codeblockRe.ReplaceAllString(input, "[::r]$1[::R]")
  19. input = emoteRe.ReplaceAllString(input, "[" + emoteColor + "]$1[-:-:-]")
  20. return input
  21. }