-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend.go
60 lines (54 loc) · 1.29 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
package anchor
import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
// Extender adds support for anchors to a Goldmark Markdown parser.
//
// Use it by installing it into the [goldmark.Markdown] object upon creation.
// For example:
//
// goldmark.New(
// // ...
// goldmark.WithExtensions(
// // ...
// &anchor.Extender{},
// ),
// )
type Extender struct {
// Texter determines the anchor text.
//
// Defaults to '¶' if unspecified.
Texter Texter
// Position specifies where the anchor will be placed in a header.
//
// Defaults to After.
Position Position
// Attributer determines the attributes
// that will be associated with the anchor link.
//
// Defaults to adding a 'class="anchor"' attribute.
Attributer Attributer
}
var _ goldmark.Extender = (*Extender)(nil)
// Extend extends the provided Goldmark Markdown.
func (e *Extender) Extend(md goldmark.Markdown) {
md.Parser().AddOptions(
parser.WithASTTransformers(
util.Prioritized(&Transformer{
Texter: e.Texter,
Position: e.Position,
Attributer: e.Attributer,
}, 100),
),
)
md.Renderer().AddOptions(
renderer.WithNodeRenderers(
util.Prioritized(&Renderer{
Position: e.Position,
}, 100),
),
)
}