@@ -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.
188167func 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"
225210name="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"
228213sandbox="%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
322289func Type (filename string ) string {
323290 if parser := GetRendererByFileName (filename ); parser != nil {
0 commit comments