| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package ultraviolet
- import (
- "io"
- "github.com/gomarkdown/markdown"
- "github.com/gomarkdown/markdown/ast"
- "github.com/gomarkdown/markdown/html"
- "github.com/gomarkdown/markdown/parser"
- )
- func MarkdownConvert(content string, paragraph bool) string {
- return string(
- markdown.Render(
- parser.NewWithExtensions(
- parser.CommonExtensions|parser.AutoHeadingIDs|parser.NoEmptyLineBeforeBlock,
- ).Parse(
- []byte(content),
- ),
- html.NewRenderer(
- html.RendererOptions{
- Flags: html.CommonFlags | html.HrefTargetBlank,
- RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
- if paragraph {
- _, ok := node.(*ast.Paragraph)
- if ok {
- if entering {
- io.WriteString(w, "<div>")
- } else {
- io.WriteString(w, "</div>")
- }
- return ast.GoToNext, true
- }
- }
- codeBlock, ok := node.(*ast.CodeBlock)
- if !ok {
- return ast.GoToNext, false
- }
- if entering {
- w.Write(codeBlock.Literal)
- }
- return ast.GoToNext, true
- },
- },
- ),
- ),
- )
- }
|