-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.go
49 lines (42 loc) · 1.3 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package formatter
import (
"io"
attributes "github.com/mdigger/goldmark-attributes"
lineblocks "github.com/mdigger/goldmark-lineblocks"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
)
// Format write reformatted markdown source.
//
// Use internal markdown parser with extensions GFM, DefinitionList,
// Footnote, LineBlocks, BlockAttributes and other.
func Format(source []byte, w io.Writer, opts ...parser.ParseOption) error {
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.DefinitionList,
extension.Footnote,
lineblocks.Extension,
attributes.Extension,
),
goldmark.WithParserOptions(
parser.WithAttribute(),
),
)
doc := md.Parser().Parse(
text.NewReader(source), opts...)
return Render(w, source, doc)
}
// Markdown is a markdown format renderer.
var Markdown renderer.Renderer = new(markdownRenderer)
type markdownRenderer struct{}
// AddOptions adds given option to this renderer.
func (*markdownRenderer) AddOptions(opts ...renderer.Option) {}
// Write render node as Markdown.
func (*markdownRenderer) Render(w io.Writer, source []byte, node ast.Node) (err error) {
return Render(w, source, node)
}