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