auto_link.go (979B)
1 package parser 2 3 import ( 4 "github.com/yuin/goldmark/ast" 5 "github.com/yuin/goldmark/text" 6 "github.com/yuin/goldmark/util" 7 ) 8 9 type autoLinkParser struct { 10 } 11 12 var defaultAutoLinkParser = &autoLinkParser{} 13 14 // NewAutoLinkParser returns a new InlineParser that parses autolinks 15 // surrounded by '<' and '>' . 16 func NewAutoLinkParser() InlineParser { 17 return defaultAutoLinkParser 18 } 19 20 func (s *autoLinkParser) Trigger() []byte { 21 return []byte{'<'} 22 } 23 24 func (s *autoLinkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.Node { 25 line, segment := block.PeekLine() 26 stop := util.FindEmailIndex(line[1:]) 27 typ := ast.AutoLinkType(ast.AutoLinkEmail) 28 if stop < 0 { 29 stop = util.FindURLIndex(line[1:]) 30 typ = ast.AutoLinkURL 31 } 32 if stop < 0 { 33 return nil 34 } 35 stop++ 36 if stop >= len(line) || line[stop] != '>' { 37 return nil 38 } 39 value := ast.NewTextSegment(text.NewSegment(segment.Start+1, segment.Start+stop)) 40 block.Advance(stop + 1) 41 return ast.NewAutoLink(typ, value) 42 }