markdown/markdown.go

67 lines
2.3 KiB
Go
Raw Permalink Normal View History

2018-01-25 21:01:19 +00:00
package markdown
2011-05-24 22:14:35 +00:00
import (
2018-01-28 02:50:27 +00:00
"bytes"
2016-07-05 04:32:16 +00:00
"io"
2018-01-28 02:00:30 +00:00
"github.com/status-im/markdown/ast"
"github.com/status-im/markdown/parser"
2011-05-24 22:14:35 +00:00
)
// Renderer is an interface for implementing custom renderers.
2011-06-29 17:13:17 +00:00
type Renderer interface {
2018-03-15 20:11:31 +00:00
// RenderNode renders markdown node to w.
// It's called once for a leaf node.
// It's called twice for non-leaf nodes:
// * first with entering=true
// * then with entering=false
//
// Return value is a way to tell the calling walker to adjust its walk
// pattern: e.g. it can terminate the traversal by returning Terminate. Or it
// can ask the walker to skip a subtree of this node by returning SkipChildren.
// The typical behavior is to return GoToNext, which asks for the usual
// traversal to the next node.
2018-01-28 08:50:21 +00:00
RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus
// RenderHeader is a method that allows the renderer to produce some
// content preceding the main body of the output document. The header is
// understood in the broad sense here. For example, the default HTML
// renderer will write not only the HTML document preamble, but also the
// table of contents if it was requested.
//
// The method will be passed an entire document tree, in case a particular
// implementation needs to inspect it to produce output.
//
// The output should be written to the supplied writer w. If your
// implementation has no header to write, supply an empty implementation.
2018-01-28 08:50:21 +00:00
RenderHeader(w io.Writer, ast ast.Node)
// RenderFooter is a symmetric counterpart of RenderHeader.
2018-01-28 08:50:21 +00:00
RenderFooter(w io.Writer, ast ast.Node)
2011-05-24 22:14:35 +00:00
}
2018-01-29 02:38:27 +00:00
// Parse parsers a markdown document using provided parser. If parser is nil,
// we use parser configured with parser.CommonExtensions.
2018-01-28 21:13:03 +00:00
//
2018-01-29 02:38:27 +00:00
// It returns AST (abstract syntax tree) that can be converted to another
// format using Render function.
2018-01-28 21:13:03 +00:00
func Parse(markdown []byte, p *parser.Parser) ast.Node {
if p == nil {
p = parser.New()
}
return p.Parse(markdown)
}
2018-01-28 04:44:39 +00:00
// Render uses renderer to convert parsed markdown document into a different format.
2018-01-28 21:13:03 +00:00
//
2018-01-29 02:38:27 +00:00
// To convert to HTML, pass html.Renderer
2018-01-28 08:50:21 +00:00
func Render(doc ast.Node, renderer Renderer) []byte {
2018-01-28 02:50:27 +00:00
var buf bytes.Buffer
2018-01-28 04:44:39 +00:00
renderer.RenderHeader(&buf, doc)
2018-01-28 08:50:21 +00:00
ast.WalkFunc(doc, func(node ast.Node, entering bool) ast.WalkStatus {
2018-01-28 02:50:27 +00:00
return renderer.RenderNode(&buf, node, entering)
})
2018-01-28 04:44:39 +00:00
renderer.RenderFooter(&buf, doc)
2018-01-28 02:50:27 +00:00
return buf.Bytes()
}