markdown.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package ultraviolet
  2. import (
  3. "io"
  4. "github.com/gomarkdown/markdown"
  5. "github.com/gomarkdown/markdown/ast"
  6. "github.com/gomarkdown/markdown/html"
  7. "github.com/gomarkdown/markdown/parser"
  8. )
  9. func MarkdownConvert(content string, paragraph bool) string {
  10. return string(
  11. markdown.Render(
  12. parser.NewWithExtensions(
  13. parser.CommonExtensions|parser.AutoHeadingIDs|parser.NoEmptyLineBeforeBlock,
  14. ).Parse(
  15. []byte(content),
  16. ),
  17. html.NewRenderer(
  18. html.RendererOptions{
  19. Flags: html.CommonFlags | html.HrefTargetBlank,
  20. RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
  21. if paragraph {
  22. _, ok := node.(*ast.Paragraph)
  23. if ok {
  24. if entering {
  25. io.WriteString(w, "<div>")
  26. } else {
  27. io.WriteString(w, "</div>")
  28. }
  29. return ast.GoToNext, true
  30. }
  31. }
  32. codeBlock, ok := node.(*ast.CodeBlock)
  33. if !ok {
  34. return ast.GoToNext, false
  35. }
  36. if entering {
  37. w.Write(codeBlock.Literal)
  38. }
  39. return ast.GoToNext, true
  40. },
  41. },
  42. ),
  43. ),
  44. )
  45. }