Skip to content

Commit eed462c

Browse files
author
Gusted
committed
Add button to switch between source and rendered blob
- Adds a button to switch between the source blob and the rendered blob. Currently these buttons are only shown for markdown files, it is the only blob that can be shown rendered and shown source without having to show raw binary code. - Resolves go-gitea#11259
1 parent 1dfa26e commit eed462c

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,8 @@ file_view_source = View Source
10271027
file_view_rendered = View Rendered
10281028
file_view_raw = View Raw
10291029
file_permalink = Permalink
1030+
file_view_source_blob = View Source Blob
1031+
file_view_rendered_blob = View Rendered Blob
10301032
file_too_large = The file is too large to be shown.
10311033
bidi_bad_header = `This file contains unexpected Bidirectional Unicode characters!`
10321034
bidi_bad_description = `This file contains unexpected Bidirectional Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.`

routers/web/repo/view.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,14 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
489489
}
490490

491491
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
492-
readmeExist := markup.IsReadmeFile(blob.Name())
492+
shouldRenderMarkdown := !ctx.FormBool("plain")
493+
readmeExist := markup.IsReadmeFile(blob.Name()) && shouldRenderMarkdown
493494
ctx.Data["ReadmeExist"] = readmeExist
494-
if markupType := markup.Type(blob.Name()); markupType != "" {
495+
496+
markupType := markup.Type(blob.Name())
497+
ctx.Data["IsMarkdownBlob"] = markupType != ""
498+
499+
if markupType != "" && shouldRenderMarkdown {
495500
ctx.Data["IsMarkup"] = true
496501
ctx.Data["MarkupType"] = markupType
497502
var result strings.Builder

templates/repo/view_file.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
{{end}}
4040
{{if not .ReadmeInList}}
4141
<div class="ui buttons mr-2">
42+
{{if and .IsMarkdownBlob .IsMarkup}}
43+
<a class="ui mini basic button" href="{{.RepoLink}}/src/{{.BranchNameSubURL}}/{{PathEscapeSegments .TreePath}}?plain=1">{{.i18n.Tr "repo.file_view_source_blob"}}</a>
44+
{{end}}
45+
{{if and .IsMarkdownBlob (not .IsMarkup)}}
46+
<a class="ui mini basic button" href="{{.RepoLink}}/src/{{.BranchNameSubURL}}/{{PathEscapeSegments .TreePath}}">{{.i18n.Tr "repo.file_view_rendered_blob"}}</a>
47+
{{end}}
4248
<a class="ui mini basic button" href="{{$.RawFileLink}}">{{.i18n.Tr "repo.file_raw"}}</a>
4349
{{if not .IsViewCommit}}
4450
<a class="ui mini basic button" href="{{.RepoLink}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{.i18n.Tr "repo.file_permalink"}}</a>

0 commit comments

Comments
 (0)