-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.go
117 lines (99 loc) · 2.92 KB
/
markdown.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package presenter
import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
var markdown = goldmark.New(
goldmark.WithExtensions(
extension.Strikethrough,
extension.TaskList,
NewTwitterLinkExtension(),
NewAnchorTargetBlankExtension(),
),
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
)
type anchorTargetBlankTransformer struct{}
func NewAnchorTargetBlankTransformer() *anchorTargetBlankTransformer {
return &anchorTargetBlankTransformer{}
}
func (t *anchorTargetBlankTransformer) Transform(n *ast.Document, reader text.Reader, pc parser.Context) {
ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
switch n.Kind() {
case ast.KindLink, ast.KindAutoLink:
n.SetAttributeString("target", []byte("_blank"))
// https://developers.google.com/web/tools/lighthouse/audits/noopener
n.SetAttributeString("rel", []byte("noopener"))
}
}
return ast.WalkContinue, nil
})
}
type anchorTargetBlankExtension struct{}
func NewAnchorTargetBlankExtension() *anchorTargetBlankExtension {
return &anchorTargetBlankExtension{}
}
func (e *anchorTargetBlankExtension) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithASTTransformers(util.Prioritized(NewAnchorTargetBlankTransformer(), 10)))
}
type twitterLinkExtension struct{}
// NewTwitterLinkExtension returns a new extension.
func NewTwitterLinkExtension() goldmark.Extender {
return &twitterLinkExtension{}
}
func (e *twitterLinkExtension) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
parser.WithInlineParsers(
util.Prioritized(NewTwitterLinkParser(), 999),
),
)
}
var twitterDestinationPrefix = []byte("https://twitter.com/")
type twitterLinkParser struct {
}
var defaultTwitterLinkParser = &twitterLinkParser{}
// NewTwitterLinkParser return a new InlineParser that parses links.
func NewTwitterLinkParser() parser.InlineParser {
return defaultTwitterLinkParser
}
func (s *twitterLinkParser) Trigger() []byte {
return []byte{'<'}
}
func (s *twitterLinkParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
line, segment := block.PeekLine()
stop := FindTwitterIndex(line[1:])
if stop < 0 {
return nil
}
stop++
if stop >= len(line) || line[stop] != '>' {
return nil
}
value := ast.NewTextSegment(text.NewSegment(segment.Start+1, segment.Start+stop))
block.Advance(stop + 1)
link := ast.NewLink()
link.AppendChild(link, value)
link.Destination = append(twitterDestinationPrefix, line[1:stop]...)
return link
}
// FindTwitterIndex returns a stop index value if the given bytes seem an email address.
func FindTwitterIndex(b []byte) int {
i := 0
if b[i] != '@' {
return -1
}
i++
for ; i < len(b); i++ {
if !util.IsAlphaNumeric(b[i]) && b[i] != '_' {
break
}
}
return i
}