forked from ego008/go-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heading.go
62 lines (48 loc) · 1.1 KB
/
heading.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
// 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 ruleHeading(s *StateBlock, startLine, _ int, silent bool) (_ bool) {
shift := s.TShift[startLine]
if shift < 0 {
return
}
pos := s.BMarks[startLine] + shift
max := s.EMarks[startLine]
src := s.Src
if pos >= max || src[pos] != '#' {
return
}
pos++
level := 1
for pos < max && src[pos] == '#' && level <= 6 {
level++
pos++
}
if level > 6 || (pos < max && src[pos] != ' ') {
return
}
if silent {
return true
}
max = s.SkipBytesBack(max, ' ', pos)
tmp := s.SkipBytesBack(max, '#', pos)
if tmp > pos && src[tmp-1] == ' ' {
max = tmp
}
s.Line = startLine + 1
s.PushOpeningToken(&HeadingOpen{
HLevel: level,
Map: [2]int{startLine, s.Line},
ID: strings.TrimSpace(src[pos:max]),
})
if pos < max {
s.PushToken(&Inline{
Content: strings.TrimSpace(src[pos:max]),
Map: [2]int{startLine, s.Line},
})
}
s.PushClosingToken(&HeadingClose{HLevel: level})
return true
}