-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextend.go
70 lines (61 loc) · 1.66 KB
/
extend.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package frontmatter
import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/util"
)
// Mode specifies the mode in which the Extender operates.
// By default, the extender extracts the front matter from the document,
// but does not render it or do anything else with it.
//
// Change the mode by setting the Mode field of the Extender object.
type Mode int
//go:generate stringer -type Mode
const (
// SetMetadata instructs the extender to convert the front matter
// into a map[string]interface{} and set it as the metadata
// of the document.
//
// This may be accessed by calling the Document.Meta() method.
SetMetadata Mode = 1 << iota
)
// Extender adds support for front matter to a Goldmark Markdown parser.
//
// Use it by installing it into the [goldmark.Markdown] object upon creation.
// For example:
//
// goldmark.New(
// // ...
// goldmark.WithExtensions(
// // ...
// &frontmatter.Extender{},
// ),
// )
type Extender struct {
// Formats lists the front matter formats
// that are supported by the extender.
//
// If empty, DefaultFormats is used.
Formats []Format
// Mode specifies the mode in which the extender operates.
// See documentation of the Mode type for more information.
Mode Mode
}
var _ goldmark.Extender = (*Extender)(nil)
// Extend extends the provided Goldmark Markdown.
func (e *Extender) Extend(md goldmark.Markdown) {
md.Parser().AddOptions(
parser.WithBlockParsers(
util.Prioritized(&Parser{
Formats: e.Formats,
}, 0),
),
)
if e.Mode&SetMetadata != 0 {
md.Parser().AddOptions(
parser.WithASTTransformers(
util.Prioritized(&MetaTransformer{}, 0),
),
)
}
}