-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdoc.go
54 lines (46 loc) · 931 Bytes
/
doc.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
// Copyright 2024 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
type Document struct {
Position
Blocks []Block
Links map[string]*Link
}
func (*Document) Block() {}
func (b *Document) printHTML(p *printer) {
for _, c := range b.Blocks {
c.printHTML(p)
}
}
func (b *Document) printMarkdown(p *printer) {
printMarkdownBlocks(b.Blocks, p)
// Terminate with a single newline.
text := p.buf.Bytes()
w := len(text)
for w > 0 && text[w-1] == '\n' {
w--
}
p.buf.Truncate(w)
if w > 0 {
p.nl()
}
// Add link reference definitions.
if len(b.Links) > 0 {
if p.buf.Len() > 0 {
p.nl()
}
printLinks(p, b.Links)
}
}
func printMarkdownBlocks(bs []Block, p *printer) {
for bn, b := range bs {
if bn > 0 {
p.nl() // end block
if p.loose > 0 {
p.nl()
}
}
b.printMarkdown(p)
}
}