-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
332 lines (296 loc) · 9.46 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
Package github_flavored_markdown provides a GitHub Flavored Markdown renderer
with fenced code block highlighting, clickable heading anchor links.
The functionality should be equivalent to the GitHub Markdown API endpoint specified at
https://developer.github.com/v3/markdown/#render-a-markdown-document-in-raw-mode, except
the rendering is performed locally.
See examples for how to generate a complete HTML page, including CSS styles.
*/
package github_flavored_markdown
import (
"bytes"
"fmt"
"regexp"
"sort"
"strings"
"text/template"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"github.com/shurcooL/highlight_diff"
"github.com/shurcooL/highlight_go"
"github.com/shurcooL/octicon"
"github.com/shurcooL/sanitized_anchor_name"
"github.com/sourcegraph/annotate"
"github.com/sourcegraph/syntaxhighlight"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// Markdown renders GitHub Flavored Markdown text.
func Markdown(text []byte) []byte {
const htmlFlags = 0
renderer := &renderer{Html: blackfriday.HtmlRenderer(htmlFlags, "", "").(*blackfriday.Html)}
unsanitized := blackfriday.Markdown(text, renderer, extensions)
sanitized := policy.SanitizeBytes(unsanitized)
return sanitized
}
// Heading returns a heading HTML node with title text.
// The heading comes with an anchor based on the title.
//
// heading can be one of atom.H1, atom.H2, atom.H3, atom.H4, atom.H5, atom.H6.
func Heading(heading atom.Atom, title string) *html.Node {
aName := sanitized_anchor_name.Create(title)
a := &html.Node{
Type: html.ElementNode, Data: atom.A.String(),
Attr: []html.Attribute{
{Key: atom.Name.String(), Val: aName},
{Key: atom.Class.String(), Val: "anchor"},
{Key: atom.Href.String(), Val: "#" + aName},
{Key: atom.Rel.String(), Val: "nofollow"},
{Key: "aria-hidden", Val: "true"},
},
}
span := &html.Node{
Type: html.ElementNode, Data: atom.Span.String(),
Attr: []html.Attribute{{Key: atom.Class.String(), Val: "octicon-link"}}, // TODO: Factor out the CSS for just headings.
FirstChild: octicon.Link(),
}
a.AppendChild(span)
h := &html.Node{Type: html.ElementNode, Data: heading.String()}
h.AppendChild(a)
h.AppendChild(&html.Node{Type: html.TextNode, Data: title})
return h
}
// extensions for GitHub Flavored Markdown-like parsing.
const extensions = blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
// policy for GitHub Flavored Markdown-like sanitization.
var policy = func() *bluemonday.Policy {
p := bluemonday.UGCPolicy()
p.AllowAttrs("class").Matching(bluemonday.SpaceSeparatedTokens).OnElements("div", "span")
p.AllowAttrs("class", "name").Matching(bluemonday.SpaceSeparatedTokens).OnElements("a")
p.AllowAttrs("rel").Matching(regexp.MustCompile(`^nofollow$`)).OnElements("a")
p.AllowAttrs("aria-hidden").Matching(regexp.MustCompile(`^true$`)).OnElements("a")
p.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input")
p.AllowAttrs("checked", "disabled").Matching(regexp.MustCompile(`^$`)).OnElements("input")
p.AllowDataURIImages()
return p
}()
type renderer struct {
*blackfriday.Html
}
// GitHub Flavored Markdown heading with clickable and hidden anchor.
func (*renderer) Header(out *bytes.Buffer, text func() bool, level int, _ string) {
marker := out.Len()
doubleSpace(out)
if !text() {
out.Truncate(marker)
return
}
textHTML := out.String()[marker:]
out.Truncate(marker)
// Extract text content of the heading.
var textContent string
if node, err := html.Parse(strings.NewReader(textHTML)); err == nil {
textContent = extractText(node)
} else {
// Failed to parse HTML (probably can never happen), so just use the whole thing.
textContent = html.UnescapeString(textHTML)
}
anchorName := sanitized_anchor_name.Create(textContent)
out.WriteString(fmt.Sprintf(`<h%d><a name="%s" class="anchor" href="#%s" rel="nofollow" aria-hidden="true"><span class="octicon octicon-link"></span></a>`, level, anchorName, anchorName))
out.WriteString(textHTML)
out.WriteString(fmt.Sprintf("</h%d>\n", level))
}
// extractText returns the recursive concatenation of the text content of an html node.
func extractText(n *html.Node) string {
var out string
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
out += c.Data
} else {
out += extractText(c)
}
}
return out
}
// TODO: Clean up and improve this code.
// GitHub Flavored Markdown fenced code block with highlighting.
func (*renderer) BlockCode(out *bytes.Buffer, text []byte, lang string) {
doubleSpace(out)
// parse out the language name
count := 0
for _, elt := range strings.Fields(lang) {
if elt[0] == '.' {
elt = elt[1:]
}
if len(elt) == 0 {
continue
}
out.WriteString(`<div class="highlight highlight-`)
attrEscape(out, []byte(elt))
lang = elt
out.WriteString(`"><pre>`)
count++
break
}
if count == 0 {
out.WriteString("<pre><code>")
}
if highlightedCode, ok := highlightCode(text, lang); ok {
out.Write(highlightedCode)
} else {
attrEscape(out, text)
}
if count == 0 {
out.WriteString("</code></pre>\n")
} else {
out.WriteString("</pre></div>\n")
}
}
// Task List support.
func (r *renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
switch {
case bytes.HasPrefix(text, []byte("[ ] ")):
text = append([]byte(`<input type="checkbox" disabled="">`), text[3:]...)
case bytes.HasPrefix(text, []byte("[x] ")) || bytes.HasPrefix(text, []byte("[X] ")):
text = append([]byte(`<input type="checkbox" checked="" disabled="">`), text[3:]...)
}
r.Html.ListItem(out, text, flags)
}
var gfmHTMLConfig = syntaxhighlight.HTMLConfig{
String: "s",
Keyword: "k",
Comment: "c",
Type: "n",
Literal: "o",
Punctuation: "p",
Plaintext: "n",
Tag: "tag",
HTMLTag: "htm",
HTMLAttrName: "atn",
HTMLAttrValue: "atv",
Decimal: "m",
}
func highlightCode(src []byte, lang string) (highlightedCode []byte, ok bool) {
switch lang {
case "Go", "Go-unformatted":
var buf bytes.Buffer
err := highlight_go.Print(src, &buf, syntaxhighlight.HTMLPrinter(gfmHTMLConfig))
if err != nil {
return nil, false
}
return buf.Bytes(), true
case "diff":
anns, err := highlight_diff.Annotate(src)
if err != nil {
return nil, false
}
lines := bytes.Split(src, []byte("\n"))
lineStarts := make([]int, len(lines))
var offset int
for lineIndex := 0; lineIndex < len(lines); lineIndex++ {
lineStarts[lineIndex] = offset
offset += len(lines[lineIndex]) + 1
}
lastDel, lastIns := -1, -1
for lineIndex := 0; lineIndex < len(lines); lineIndex++ {
var lineFirstChar byte
if len(lines[lineIndex]) > 0 {
lineFirstChar = lines[lineIndex][0]
}
switch lineFirstChar {
case '+':
if lastIns == -1 {
lastIns = lineIndex
}
case '-':
if lastDel == -1 {
lastDel = lineIndex
}
default:
if lastDel != -1 || lastIns != -1 {
if lastDel == -1 {
lastDel = lastIns
} else if lastIns == -1 {
lastIns = lineIndex
}
beginOffsetLeft := lineStarts[lastDel]
endOffsetLeft := lineStarts[lastIns]
beginOffsetRight := lineStarts[lastIns]
endOffsetRight := lineStarts[lineIndex]
anns = append(anns, &annotate.Annotation{Start: beginOffsetLeft, End: endOffsetLeft, Left: []byte(`<span class="gd input-block">`), Right: []byte(`</span>`), WantInner: 0})
anns = append(anns, &annotate.Annotation{Start: beginOffsetRight, End: endOffsetRight, Left: []byte(`<span class="gi input-block">`), Right: []byte(`</span>`), WantInner: 0})
if '@' != lineFirstChar {
//leftContent := string(src[beginOffsetLeft:endOffsetLeft])
//rightContent := string(src[beginOffsetRight:endOffsetRight])
// This is needed to filter out the "-" and "+" at the beginning of each line from being highlighted.
// TODO: Still not completely filtered out.
leftContent := ""
for line := lastDel; line < lastIns; line++ {
leftContent += "\x00" + string(lines[line][1:]) + "\n"
}
rightContent := ""
for line := lastIns; line < lineIndex; line++ {
rightContent += "\x00" + string(lines[line][1:]) + "\n"
}
var sectionSegments [2][]*annotate.Annotation
highlight_diff.HighlightedDiffFunc(leftContent, rightContent, §ionSegments, [2]int{beginOffsetLeft, beginOffsetRight})
anns = append(anns, sectionSegments[0]...)
anns = append(anns, sectionSegments[1]...)
}
}
lastDel, lastIns = -1, -1
}
}
sort.Sort(anns)
out, err := annotate.Annotate(src, anns, template.HTMLEscape)
if err != nil {
return nil, false
}
return out, true
default:
return nil, false
}
}
// Unexported blackfriday helpers.
func doubleSpace(out *bytes.Buffer) {
if out.Len() > 0 {
out.WriteByte('\n')
}
}
func escapeSingleChar(char byte) (string, bool) {
if char == '"' {
return """, true
}
if char == '&' {
return "&", true
}
if char == '<' {
return "<", true
}
if char == '>' {
return ">", true
}
return "", false
}
func attrEscape(out *bytes.Buffer, src []byte) {
org := 0
for i, ch := range src {
if entity, ok := escapeSingleChar(ch); ok {
if i > org {
// copy all the normal characters since the last escape
out.Write(src[org:i])
}
org = i + 1
out.WriteString(entity)
}
}
if org < len(src) {
out.Write(src[org:])
}
}