Skip to content

Commit f326e27

Browse files
committed
fix render
1 parent d1ed7a6 commit f326e27

File tree

3 files changed

+15
-48
lines changed

3 files changed

+15
-48
lines changed

modules/markup/renderer.go

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -163,32 +163,16 @@ func DetectRendererType(filename string, input io.Reader) string {
163163
return ""
164164
}
165165

166-
// GetRenderer returned the renderer according type or relative path
167-
func GetRenderer(renderType, relativePath string) (Renderer, error) {
168-
if renderType != "" {
169-
if renderer, ok := renderers[renderType]; ok {
170-
return renderer, nil
171-
}
172-
// FIXME: is it correct? if it returns here, then relativePath won't take effect
173-
return nil, ErrUnsupportedRenderType{renderType}
174-
}
175-
176-
if relativePath != "" {
177-
extension := strings.ToLower(filepath.Ext(relativePath))
178-
if renderer, ok := extRenderers[extension]; ok {
179-
return renderer, nil
180-
}
181-
return nil, ErrUnsupportedRenderExtension{extension}
182-
}
183-
184-
return nil, errors.New("render options both filename and type missing")
185-
}
186-
187166
// Render renders markup file to HTML with all specific handling stuff.
188167
func Render(ctx *RenderContext, input io.Reader, output io.Writer) error {
189-
renderer, err := GetRenderer(ctx.Type, ctx.RelativePath)
190-
if err != nil {
191-
return err
168+
var renderer Renderer
169+
if ctx.Type != "" {
170+
renderer = GetRendererByType(ctx.Type)
171+
} else {
172+
renderer = GetRendererByFileName(ctx.RelativePath)
173+
}
174+
if renderer == nil {
175+
return fmt.Errorf("no renderer for type=%q, filename=%q", ctx.Type, ctx.RelativePath)
192176
}
193177

194178
if r, ok := renderer.(ExternalRenderer); ok && r.DisplayInIFrame() {
@@ -219,12 +203,13 @@ func renderIFrame(ctx *RenderContext, output io.Writer, iframeSandbox string) er
219203
// set height="0" ahead, otherwise the scrollHeight would be max(150, realHeight)
220204
// at the moment, only "allow-scripts" is allowed for sandbox mode.
221205
// "allow-same-origin" should never be used, it leads to XSS attack, and it makes the JS in iframe can access parent window's config and CSRF token
206+
// when there is a strict CORS policy, the "onload" script can not read the loaded height at the moment.
222207
// TODO: when using dark theme, if the rendered content doesn't have proper style, the default text color is black, which is not easy to read
223208
_, err := io.WriteString(output, fmt.Sprintf(`
224209
<iframe src="%s/%s/%s/render/%s/%s"
225210
name="giteaExternalRender"
226-
onload="this.height=giteaExternalRender.document.documentElement.scrollHeight"
227-
width="100%%" height="0" scrolling="no" frameborder="0" style="overflow: hidden"
211+
onload="try { this.height=giteaExternalRender.document.documentElement.scrollHeight; } catch(e) { this.style.height='80vh'; }"
212+
width="100%%" height="0" scrolling="auto" frameborder="0" style="overflow: hidden"
228213
sandbox="%s"
229214
></iframe>`,
230215
setting.AppSubURL,
@@ -300,24 +285,6 @@ func RenderDirect(ctx *RenderContext, renderer Renderer, input io.Reader, output
300285
return err
301286
}
302287

303-
// ErrUnsupportedRenderType represents
304-
type ErrUnsupportedRenderType struct {
305-
Type string
306-
}
307-
308-
func (err ErrUnsupportedRenderType) Error() string {
309-
return fmt.Sprintf("Unsupported render type: %s", err.Type)
310-
}
311-
312-
// ErrUnsupportedRenderExtension represents the error when extension doesn't supported to render
313-
type ErrUnsupportedRenderExtension struct {
314-
Extension string
315-
}
316-
317-
func (err ErrUnsupportedRenderExtension) Error() string {
318-
return fmt.Sprintf("Unsupported render extension: %s", err.Extension)
319-
}
320-
321288
// Type returns if markup format via the filename
322289
func Type(filename string) string {
323290
if parser := GetRendererByFileName(filename); parser != nil {

modules/setting/markup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,6 @@ func newMarkupRenderer(name string, sec *ini.Section) {
177177
NeedPostProcess: sec.Key("NEED_POSTPROCESS").MustBool(true),
178178
RenderContentMode: renderContentMode,
179179
RenderContentIframeSandbox: sec.Key("RENDER_CONTENT_IFRAME_SANDBOX").MustString("allow-scripts"),
180-
RenderContentExternalCSP: sec.Key("RENDER_CONTENT_EXTERNAL_CSP").MustString("iframe-src 'self'; sandbox allow-scripts"),
180+
RenderContentExternalCSP: sec.Key("RENDER_CONTENT_EXTERNAL_CSP").MustString("frame-src 'self'; sandbox allow-scripts"),
181181
})
182182
}

routers/web/repo/render.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ func RenderFile(ctx *context.Context) {
3838
treeLink += "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
3939
}
4040

41-
renderer, err := markup.GetRenderer("", ctx.Repo.TreePath)
42-
if err != nil {
43-
ctx.ServerError("GetRenderer", err)
41+
renderer := markup.GetRendererByFileName(ctx.Repo.TreePath)
42+
if renderer == nil {
43+
ctx.Error(http.StatusBadRequest, "No renderer")
4444
return
4545
}
4646

0 commit comments

Comments
 (0)