emphasis.go (1247B)
1 package parser 2 3 import ( 4 "github.com/yuin/goldmark/ast" 5 "github.com/yuin/goldmark/text" 6 ) 7 8 type emphasisDelimiterProcessor struct { 9 } 10 11 func (p *emphasisDelimiterProcessor) IsDelimiter(b byte) bool { 12 return b == '*' || b == '_' 13 } 14 15 func (p *emphasisDelimiterProcessor) CanOpenCloser(opener, closer *Delimiter) bool { 16 return opener.Char == closer.Char 17 } 18 19 func (p *emphasisDelimiterProcessor) OnMatch(consumes int) ast.Node { 20 return ast.NewEmphasis(consumes) 21 } 22 23 var defaultEmphasisDelimiterProcessor = &emphasisDelimiterProcessor{} 24 25 type emphasisParser struct { 26 } 27 28 var defaultEmphasisParser = &emphasisParser{} 29 30 // NewEmphasisParser return a new InlineParser that parses emphasises. 31 func NewEmphasisParser() InlineParser { 32 return defaultEmphasisParser 33 } 34 35 func (s *emphasisParser) Trigger() []byte { 36 return []byte{'*', '_'} 37 } 38 39 func (s *emphasisParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.Node { 40 before := block.PrecendingCharacter() 41 line, segment := block.PeekLine() 42 node := ScanDelimiter(line, before, 1, defaultEmphasisDelimiterProcessor) 43 if node == nil { 44 return nil 45 } 46 node.Segment = segment.WithStop(segment.Start + node.OriginalLength) 47 block.Advance(node.OriginalLength) 48 pc.PushDelimiter(node) 49 return node 50 }