forked from rsc/markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heading.go
161 lines (150 loc) · 3.62 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2021 The Go 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 (
"bytes"
"fmt"
"strings"
)
type Heading struct {
Position
Level int
Text *Text
// The HTML id attribute. The parser populates this field if
// [Parser.HeadingIDs] is true and the heading ends with text like "{#id}".
ID string
}
func (b *Heading) PrintHTML(buf *bytes.Buffer) {
fmt.Fprintf(buf, "<h%d", b.Level)
if b.ID != "" {
fmt.Fprintf(buf, ` id="%s"`, htmlQuoteEscaper.Replace(b.ID))
}
buf.WriteByte('>')
b.Text.PrintHTML(buf)
fmt.Fprintf(buf, "</h%d>\n", b.Level)
}
func (b *Heading) printMarkdown(buf *bytes.Buffer, s mdState) {
// TODO: handle setext headings properly.
buf.WriteString(s.prefix)
for i := 0; i < b.Level; i++ {
buf.WriteByte('#')
}
buf.WriteByte(' ')
// The prefix has already been printed for this line of text.
s.prefix = ""
b.Text.printMarkdown(buf, s)
if b.ID != "" {
// A heading text is a block, so it ends in a newline. Move the newline
// after the ID.
buf.Truncate(buf.Len() - 1)
fmt.Fprintf(buf, " {#%s}\n", b.ID)
}
}
func newATXHeading(p *parseState, s line) (line, bool) {
peek := s
var n int
if peek.trimHeading(&n) {
s := peek.string()
s = trimRightSpaceTab(s)
// Remove trailing '#'s.
if t := strings.TrimRight(s, "#"); t != trimRightSpaceTab(t) || t == "" {
s = t
}
var id string
if p.HeadingIDs {
// Parse and remove ID attribute.
// It must come before trailing '#'s to more closely follow the spec:
// The optional closing sequence of #s must be preceded by spaces or tabs
// and may be followed by spaces or tabs only.
// But Goldmark allows it to come after.
id, s = extractID(p, s)
// Goldmark is strict about the id syntax.
for _, c := range id {
if c >= 0x80 || !isLetterDigit(byte(c)) {
p.corner = true
}
}
}
pos := Position{p.lineno, p.lineno}
p.doneBlock(&Heading{pos, n, p.newText(pos, s), id})
return line{}, true
}
return s, false
}
// extractID removes an ID attribute from s if one is present.
// It returns the attribute value and the resulting string.
// The attribute has the form "{#...}", where the "..." can contain
// any character other than '}'.
// The attribute must be followed only by whitespace.
func extractID(p *parseState, s string) (id, s2 string) {
i := strings.LastIndexByte(s, '{')
if i < 0 {
return "", s
}
if i+1 >= len(s) || s[i+1] != '#' {
p.corner = true // goldmark accepts {}
return "", s
}
j := i + strings.IndexByte(s[i:], '}')
if j < 0 || trimRightSpaceTab(s[j+1:]) != "" {
return "", s
}
id = strings.TrimSpace(s[i+2 : j])
if id == "" {
p.corner = true // goldmark accepts {#}
return "", s
}
return s[i+2 : j], s[:i]
}
func newSetextHeading(p *parseState, s line) (line, bool) {
var n int
peek := s
if p.nextB() == p.para() && peek.trimSetext(&n) {
p.closeBlock()
para, ok := p.last().(*Paragraph)
if !ok {
return s, false
}
p.deleteLast()
p.doneBlock(&Heading{Position{para.StartLine, p.lineno}, n, para.Text, ""})
return line{}, true
}
return s, false
}
func (s *line) trimHeading(width *int) bool {
t := *s
t.trimSpace(0, 3, false)
if !t.trim('#') {
return false
}
n := 1
for n < 6 && t.trim('#') {
n++
}
if !t.trimSpace(1, 1, true) {
return false
}
*width = n
*s = t
return true
}
func (s *line) trimSetext(n *int) bool {
t := *s
t.trimSpace(0, 3, false)
c := t.peek()
if c == '-' || c == '=' {
for t.trim(c) {
}
t.skipSpace()
if t.eof() {
if c == '=' {
*n = 1
} else {
*n = 2
}
return true
}
}
return false
}