-
Notifications
You must be signed in to change notification settings - Fork 14
/
markdown.go
160 lines (148 loc) · 3.64 KB
/
markdown.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
package renderers
import (
"bytes"
"io"
"regexp"
blackfriday "github.com/danog/blackfriday/v2"
"github.com/osteele/gojekyll/utils"
"golang.org/x/net/html"
)
const blackfridayFlags = 0 |
blackfriday.UseXHTML |
blackfriday.Smartypants |
blackfriday.SmartypantsFractions |
blackfriday.SmartypantsDashes |
blackfriday.SmartypantsLatexDashes |
blackfriday.FootnoteReturnLinks
const blackfridayExtensions = 0 |
blackfriday.NoIntraEmphasis |
blackfriday.Tables |
blackfriday.FencedCode |
blackfriday.Autolink |
blackfriday.Strikethrough |
blackfriday.SpaceHeadings |
blackfriday.HeadingIDs |
blackfriday.BackslashLineBreak |
blackfriday.DefinitionLists |
blackfriday.NoEmptyLineBeforeBlock |
// added relative to commonExtensions
blackfriday.AutoHeadingIDs |
blackfriday.Footnotes
func renderMarkdown(md []byte) ([]byte, error) {
params := blackfriday.HTMLRendererParameters{
Flags: blackfridayFlags,
}
renderer := blackfriday.NewHTMLRenderer(params)
html := blackfriday.Run(
md,
blackfriday.WithRenderer(renderer),
blackfriday.WithExtensions(blackfridayExtensions),
)
html, err := renderInnerMarkdown(html)
if err != nil {
return nil, utils.WrapError(err, "markdown")
}
return html, nil
}
func _renderMarkdown(md []byte) ([]byte, error) {
params := blackfriday.HTMLRendererParameters{
Flags: blackfridayFlags,
}
renderer := blackfriday.NewHTMLRenderer(params)
html := blackfriday.Run(
md,
blackfriday.WithRenderer(renderer),
blackfriday.WithExtensions(blackfridayExtensions),
)
return html, nil
}
// search HTML for markdown=1, and process if found
func renderInnerMarkdown(b []byte) ([]byte, error) {
z := html.NewTokenizer(bytes.NewReader(b))
buf := new(bytes.Buffer)
outer:
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
if z.Err() == io.EOF {
break outer
}
return nil, z.Err()
case html.StartTagToken:
if hasMarkdownAttr(z) {
_, err := buf.Write(stripMarkdownAttr(z.Raw()))
if err != nil {
return nil, err
}
if err := processInnerMarkdown(buf, z); err != nil {
return nil, err
}
// the above leaves z set to the end token
// fall through to render it
}
}
_, err := buf.Write(z.Raw())
if err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}
func hasMarkdownAttr(z *html.Tokenizer) bool {
for {
k, v, more := z.TagAttr()
switch {
case string(k) == "markdown" && string(v) == "1":
return true
case !more:
return false
}
}
}
var markdownAttrRE = regexp.MustCompile(`\s*markdown\s*=[^\s>]*\s*`)
// return the text of a start tag, w/out the markdown attribute
func stripMarkdownAttr(tag []byte) []byte {
tag = markdownAttrRE.ReplaceAll(tag, []byte(" "))
tag = bytes.Replace(tag, []byte(" >"), []byte(">"), 1)
return tag
}
// Used inside markdown=1.
// TODO Instead of this approach, only count tags that match the start
// tag. For example, if <div markdown="1"> kicked off the inner markdown,
// count the div depth.
var notATagRE = regexp.MustCompile(`@|(https?|ftp):`)
// called once markdown="1" attribute is detected.
// Collects the HTML tokens into a string, applies markdown to them,
// and writes the result
func processInnerMarkdown(w io.Writer, z *html.Tokenizer) error {
buf := new(bytes.Buffer)
depth := 1
loop:
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
return z.Err()
case html.StartTagToken:
if !notATagRE.Match(z.Raw()) {
depth++
}
case html.EndTagToken:
depth--
if depth == 0 {
break loop
}
}
_, err := buf.Write(z.Raw())
if err != nil {
return err
}
}
html, err := _renderMarkdown(buf.Bytes())
if err != nil {
return err
}
_, err = w.Write(html)
return err
}