Apply external markup rendering to non-text files #2366

Closed
opened 2025-11-02 04:33:54 -06:00 by GiteaMirror · 2 comments
Owner

Originally created by @causal-agent on GitHub (Sep 29, 2018).

  • Gitea version (or commit ref): 1.4.3
  • Git version: 2.18.0
  • Operating system: FreeBSD 11.2-RELEASE
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
    • Not relevant
  • Log gist:

Description

Currently, files are only checked against external markup extensions when the file is a text file: fc0001caa1/routers/repo/view.go (L201-L214)

I would like to be able to use external markup renderers to render previews of some binary formats, similar to how PDFs can be displayed inline. Instead, I always get the "view raw" page for binary files.

Originally created by @causal-agent on GitHub (Sep 29, 2018). <!-- 1. Please speak English, this is the language all of us can speak and write. 2. Please ask questions or configuration/deploy problems on our Discord server (https://discord.gg/NsatcWJ) or forum (https://discourse.gitea.io). 3. Please take a moment to check that your issue doesn't already exist. 4. Please give all relevant information below for bug reports, because incomplete details will be handled as an invalid report. --> - Gitea version (or commit ref): 1.4.3 - Git version: 2.18.0 - Operating system: FreeBSD 11.2-RELEASE - Database (use `[x]`): - [ ] PostgreSQL - [ ] MySQL - [ ] MSSQL - [x] SQLite - Can you reproduce the bug at https://try.gitea.io: - [ ] Yes (provide example URL) - [ ] No - [x] Not relevant - Log gist: ## Description Currently, files are only checked against external markup extensions when the file is a text file: https://github.com/go-gitea/gitea/blob/fc0001caa1e9873a86990f7815a75d8f75fbc309/routers/repo/view.go#L201-L214 I would like to be able to use external markup renderers to render previews of some binary formats, similar to how PDFs can be displayed inline. Instead, I always get the "view raw" page for binary files.
GiteaMirror added the type/proposaltype/enhancement labels 2025-11-02 04:33:54 -06:00
Author
Owner

@HarvsG commented on GitHub (Jul 25, 2019):

I have the same issue trying to render docx files using pandoc. I created an issue #7614.

@HarvsG commented on GitHub (Jul 25, 2019): I have the same issue trying to render docx files using pandoc. I created an issue #7614.
Author
Owner

@HarvsG commented on GitHub (Jul 25, 2019):

As you identified the type of rendering is determined by this switch
fc0001caa1/routers/repo/view.go (L200-L269)

But interestingly I cant see a case base.IsBinaryFile(buf): or a default: case that would manage binary files. So they must either be handled before the switch or after it.

Edit:

In the case of the render type not being known then this is run which provides the 'view raw' link.
bcbc9f33d7/templates/repo/view_file.tmpl (L67-L68)

So a change must be made that makes either of .IsMarkup or .IsRenderedHTML options true:
bcbc9f33d7/templates/repo/view_file.tmpl (L49-L52)

Edit2, Solution:

Perhaps a solution would be to add

	default:
		if blob.Size() >= setting.UI.MaxDisplayFileSize {
			ctx.Data["IsFileTooLarge"] = true
			break
		}

		d, _ := ioutil.ReadAll(dataRc)
		buf = templates.ToUTF8WithFallback(append(buf, d...))
		if markup.Type(blob.Name()) != "" {
			ctx.Data["IsMarkup"] = true // or ctx.Data["IsRenderedHTML"] = true
			ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
		}

at line fc0001caa1/routers/repo/view.go (L278)

@HarvsG commented on GitHub (Jul 25, 2019): As you identified the type of rendering is determined by this switch https://github.com/go-gitea/gitea/blob/fc0001caa1e9873a86990f7815a75d8f75fbc309/routers/repo/view.go#L200-L269 But interestingly I cant see a `case base.IsBinaryFile(buf):` or a `default:` case that would manage binary files. So they must ~either~ be handled ~before the switch or~ after it. ## Edit: In the case of the render type not being known then this is run which provides the 'view raw' link. https://github.com/go-gitea/gitea/blob/bcbc9f33d73393c47b27793ac91b8f9faf98d349/templates/repo/view_file.tmpl#L67-L68 So a change must be made that makes either of `.IsMarkup` or `.IsRenderedHTML` options true: https://github.com/go-gitea/gitea/blob/bcbc9f33d73393c47b27793ac91b8f9faf98d349/templates/repo/view_file.tmpl#L49-L52 Edit2, Solution: Perhaps a solution would be to add ``` default: if blob.Size() >= setting.UI.MaxDisplayFileSize { ctx.Data["IsFileTooLarge"] = true break } d, _ := ioutil.ReadAll(dataRc) buf = templates.ToUTF8WithFallback(append(buf, d...)) if markup.Type(blob.Name()) != "" { ctx.Data["IsMarkup"] = true // or ctx.Data["IsRenderedHTML"] = true ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas())) } ``` at line https://github.com/go-gitea/gitea/blob/fc0001caa1e9873a86990f7815a75d8f75fbc309/routers/repo/view.go#L278
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#2366