forked from mmarkdown/mmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callouts.go
41 lines (36 loc) · 888 Bytes
/
callouts.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
package xml
import (
"bytes"
"fmt"
"io"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
// EscapeHTMLCallouts writes html-escaped d to w. It escapes &, <, > and " characters, *but*
// expands callouts <<N>> with the callout HTML, i.e. by calling rendering it as <N>.
func EscapeHTMLCallouts(w io.Writer, d []byte, comments [][]byte) {
ld := len(d)
Parse:
for i := 0; i < ld; i++ {
for _, comment := range comments {
if !bytes.HasPrefix(d[i:], comment) {
break
}
lc := len(comment)
if i+lc < ld {
if id, consumed := parser.IsCallout(d[i+lc:]); consumed > 0 {
// We have seen a callout
io.WriteString(w, fmt.Sprintf("<%s>", id))
i += consumed + lc - 1
continue Parse
}
}
}
escSeq := html.Escaper[d[i]]
if escSeq != nil {
w.Write(escSeq)
} else {
w.Write([]byte{d[i]})
}
}
}