-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextend.go
64 lines (58 loc) · 1.47 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
package hashtag
import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
// Extender extends a goldmark Markdown object with support for parsing and
// rendering hashtags.
//
// Install it on your Markdown object upon creation.
//
// goldmark.New(
// goldmark.WithExtensions(
// // ...
// &hashtag.Extender{...},
// ),
// // ...
// )
//
// Provide a Resolver to render tags as links that point to a specific
// destination.
type Extender struct {
// Resolver specifies destination links for hashtags, if any.
//
// Defaults to no links.
Resolver Resolver
// Variant is the flavor of the hashtag syntax to support.
//
// Defaults to DefaultVariant. See the documentation of individual
// variants for more information.
Variant Variant
// Attributes are added to the <a> tag.
//
// Attributes will only be applied if the tag can be resolved by the Resolver.
// Defaults to no attributes.
Attributes []Attribute
}
var _ goldmark.Extender = (*Extender)(nil)
// Extend extends the provided goldmark Markdown object with support for
// hashtags.
func (e *Extender) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
parser.WithInlineParsers(
util.Prioritized(&Parser{
Variant: e.Variant,
}, 999),
),
)
m.Renderer().AddOptions(
renderer.WithNodeRenderers(
util.Prioritized(&Renderer{
Resolver: e.Resolver,
Attributes: e.Attributes,
}, 999),
),
)
}