markdown.go 706 B

12345678910111213141516171819202122
  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. )
  12. func Parse(input string) string {
  13. input = boldRe.ReplaceAllString(input, "[::b]$1[::-]")
  14. input = italicRe.ReplaceAllString(input, "[::i]$1[::-]")
  15. input = underlineRe.ReplaceAllString(input, "[::u]$1[::-]")
  16. input = strikethroughRe.ReplaceAllString(input, "[::s]$1[::-]")
  17. input = codeblockRe.ReplaceAllString(input, "[::r]$1[::-]")
  18. return input
  19. }