summaryrefslogtreecommitdiffstats
path: root/vendor/gitlab.com/golang-commonmark/markdown/backticks.go
blob: 07f268dcc4fad7a2eb693fc5a77b464b7a3df209 (plain) (blame)
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
// Copyright 2015 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package markdown

import "strings"

func ruleBackticks(s *StateInline, silent bool) bool {
	pos := s.Pos
	src := s.Src

	if src[pos] != '`' {
		return false
	}

	start := pos
	pos++
	max := s.PosMax

	for pos < max && src[pos] == '`' {
		pos++
	}

	marker := src[start:pos]

	matchStart := pos
	matchEnd := pos
	for {
		matchStart = strings.IndexByte(src[matchEnd:], '`')
		if matchStart == -1 {
			break
		}
		matchStart += matchEnd

		matchEnd = matchStart + 1

		for matchEnd < max && src[matchEnd] == '`' {
			matchEnd++
		}

		if matchEnd-matchStart == len(marker) {
			if !silent {
				s.PushToken(&CodeInline{
					Content: normalizeInlineCode(src[pos:matchStart]),
				})
			}
			s.Pos = matchEnd
			return true
		}
	}

	if !silent {
		s.Pending.WriteString(marker)
	}

	s.Pos += len(marker)

	return true
}