Core components of the web application. https://write.as
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

38 lignes
1.1 KiB

  1. package posts
  2. import (
  3. "github.com/microcosm-cc/bluemonday"
  4. "github.com/writeas/saturday"
  5. "regexp"
  6. )
  7. var (
  8. blockReg = regexp.MustCompile("<(ul|ol|blockquote)>\n")
  9. endBlockReg = regexp.MustCompile("</([a-z]+)>\n</(ul|ol|blockquote)>")
  10. )
  11. func ApplyMarkdown(data []byte) string {
  12. mdExtensions := 0 |
  13. blackfriday.EXTENSION_TABLES |
  14. blackfriday.EXTENSION_FENCED_CODE |
  15. blackfriday.EXTENSION_AUTOLINK |
  16. blackfriday.EXTENSION_STRIKETHROUGH |
  17. blackfriday.EXTENSION_SPACE_HEADERS |
  18. blackfriday.EXTENSION_HEADER_IDS
  19. htmlFlags := 0 |
  20. blackfriday.HTML_USE_SMARTYPANTS |
  21. blackfriday.HTML_SMARTYPANTS_DASHES
  22. // Generate Markdown
  23. md := blackfriday.Markdown([]byte(data), blackfriday.HtmlRenderer(htmlFlags, "", ""), mdExtensions)
  24. // Strip out bad HTML
  25. policy := bluemonday.UGCPolicy()
  26. policy.AllowAttrs("class", "id").Globally()
  27. outHTML := string(policy.SanitizeBytes(md))
  28. // Strip newlines on certain block elements that render with them
  29. outHTML = blockReg.ReplaceAllString(outHTML, "<$1>")
  30. outHTML = endBlockReg.ReplaceAllString(outHTML, "</$1></$2>")
  31. return outHTML
  32. }