@@ -36,13 +36,8 @@ var urlPrefixKey = parser.NewContextKey()
36
36
var isWikiKey = parser .NewContextKey ()
37
37
var renderMetasKey = parser .NewContextKey ()
38
38
39
- type closesWithError interface {
40
- io.WriteCloser
41
- CloseWithError (err error ) error
42
- }
43
-
44
39
type limitWriter struct {
45
- w closesWithError
40
+ w io. Writer
46
41
sum int64
47
42
limit int64
48
43
}
@@ -56,24 +51,13 @@ func (l *limitWriter) Write(data []byte) (int, error) {
56
51
if err != nil {
57
52
return n , err
58
53
}
59
- _ = l .w .Close ()
60
54
return n , fmt .Errorf ("Rendered content too large - truncating render" )
61
55
}
62
56
n , err := l .w .Write (data )
63
57
l .sum += int64 (n )
64
58
return n , err
65
59
}
66
60
67
- // Close closes the writer
68
- func (l * limitWriter ) Close () error {
69
- return l .w .Close ()
70
- }
71
-
72
- // CloseWithError closes the writer
73
- func (l * limitWriter ) CloseWithError (err error ) error {
74
- return l .w .CloseWithError (err )
75
- }
76
-
77
61
// newParserContext creates a parser.Context with the render context set
78
62
func newParserContext (ctx * markup.RenderContext ) parser.Context {
79
63
pc := parser .NewContext (parser .WithIDs (newPrefixedIDs ()))
@@ -159,49 +143,37 @@ func actualRender(ctx *markup.RenderContext, input io.Reader, output io.Writer)
159
143
160
144
})
161
145
162
- rd , wr := io .Pipe ()
163
- defer func () {
164
- _ = rd .Close ()
165
- _ = wr .Close ()
166
- }()
167
-
168
146
lw := & limitWriter {
169
- w : wr ,
147
+ w : output ,
170
148
limit : setting .UI .MaxDisplayFileSize * 3 ,
171
149
}
172
150
173
- // FIXME: should we include a timeout that closes the pipe to abort the renderer and sanitizer if it takes too long?
174
- go func () {
175
- defer func () {
176
- err := recover ()
177
- if err == nil {
178
- return
179
- }
180
-
181
- log .Warn ("Unable to render markdown due to panic in goldmark: %v" , err )
182
- if log .IsDebug () {
183
- log .Debug ("Panic in markdown: %v\n %s" , err , string (log .Stack (2 )))
184
- }
185
- _ = lw .CloseWithError (fmt .Errorf ("%v" , err ))
186
- }()
187
-
188
- // FIXME: Don't read all to memory, but goldmark doesn't support
189
- pc := newParserContext (ctx )
190
- buf , err := ioutil .ReadAll (input )
191
- if err != nil {
192
- log .Error ("Unable to ReadAll: %v" , err )
151
+ // FIXME: should we include a timeout to abort the renderer if it takes too long?
152
+ defer func () {
153
+ err := recover ()
154
+ if err == nil {
193
155
return
194
156
}
195
- if err := converter . Convert ( giteautil . NormalizeEOL ( buf ), lw , parser . WithContext ( pc )); err != nil {
196
- log .Error ("Unable to render: %v" , err )
197
- _ = lw . CloseWithError ( err )
198
- return
157
+
158
+ log .Warn ("Unable to render markdown due to panic in goldmark : %v" , err )
159
+ if log . IsDebug () {
160
+ log . Debug ( "Panic in markdown: %v \n %s" , err , string ( log . Stack ( 2 )))
199
161
}
200
- _ = lw .Close ()
201
162
}()
202
- buf := markup .SanitizeReader (rd , "" )
203
- _ , err := io .Copy (output , buf )
204
- return err
163
+
164
+ // FIXME: Don't read all to memory, but goldmark doesn't support
165
+ pc := newParserContext (ctx )
166
+ buf , err := ioutil .ReadAll (input )
167
+ if err != nil {
168
+ log .Error ("Unable to ReadAll: %v" , err )
169
+ return err
170
+ }
171
+ if err := converter .Convert (giteautil .NormalizeEOL (buf ), lw , parser .WithContext (pc )); err != nil {
172
+ log .Error ("Unable to render: %v" , err )
173
+ return err
174
+ }
175
+
176
+ return nil
205
177
}
206
178
207
179
func render (ctx * markup.RenderContext , input io.Reader , output io.Writer ) error {
@@ -211,14 +183,13 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
211
183
return
212
184
}
213
185
214
- log .Warn ("Unable to render markdown due to panic in goldmark - will return sanitized raw bytes" )
186
+ log .Warn ("Unable to render markdown due to panic in goldmark - will return raw bytes" )
215
187
if log .IsDebug () {
216
188
log .Debug ("Panic in markdown: %v\n %s" , err , string (log .Stack (2 )))
217
189
}
218
- ret := markup .SanitizeReader (input , "" )
219
- _ , err = io .Copy (output , ret )
190
+ _ , err = io .Copy (output , input )
220
191
if err != nil {
221
- log .Error ("SanitizeReader failed: %v" , err )
192
+ log .Error ("io.Copy failed: %v" , err )
222
193
}
223
194
}()
224
195
return actualRender (ctx , input , output )
@@ -261,8 +232,8 @@ func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Wri
261
232
262
233
// Render renders Markdown to HTML with all specific handling stuff.
263
234
func Render (ctx * markup.RenderContext , input io.Reader , output io.Writer ) error {
264
- if ctx .Filename == "" {
265
- ctx .Filename = "a.md"
235
+ if ctx .Type == "" {
236
+ ctx .Type = MarkupName
266
237
}
267
238
return markup .Render (ctx , input , output )
268
239
}
@@ -278,7 +249,24 @@ func RenderString(ctx *markup.RenderContext, content string) (string, error) {
278
249
279
250
// RenderRaw renders Markdown to HTML without handling special links.
280
251
func RenderRaw (ctx * markup.RenderContext , input io.Reader , output io.Writer ) error {
281
- return render (ctx , input , output )
252
+ rd , wr := io .Pipe ()
253
+ defer func () {
254
+ _ = rd .Close ()
255
+ _ = wr .Close ()
256
+ }()
257
+
258
+ go func () {
259
+ if err := render (ctx , input , wr ); err != nil {
260
+ wr .CloseWithError (err )
261
+ return
262
+ }
263
+ _ = wr .Close ()
264
+ }()
265
+
266
+ buf := markup .SanitizeReader (rd , "" )
267
+ _ , err := io .Copy (output , buf )
268
+
269
+ return err
282
270
}
283
271
284
272
// RenderRawString renders Markdown to HTML without handling special links and return string
0 commit comments