fix: visible Preview/Source buttons for HTML files, default to preview

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-20 15:02:20 +01:00
parent 87848482a1
commit 33703222f8
3 changed files with 24 additions and 3 deletions
+2 -1
View File
@@ -1197,7 +1197,8 @@
"repo.file.title": "%s at %s", "repo.file.title": "%s at %s",
"repo.file_raw": "Raw", "repo.file_raw": "Raw",
"repo.file_history": "History", "repo.file_history": "History",
"repo.file_view_source": "View Source", "repo.file_view_source": "Source",
"repo.file_view_preview": "Preview",
"repo.file_view_rendered": "View Rendered", "repo.file_view_rendered": "View Rendered",
"repo.file_view_raw": "View Raw", "repo.file_view_raw": "View Raw",
"repo.file_permalink": "Permalink", "repo.file_permalink": "Permalink",
+16 -2
View File
@@ -57,7 +57,15 @@ func prepareFileViewLfsAttrs(ctx *context.Context) (*attribute.Attributes, bool)
return attrs, true return attrs, true
} }
func isHTMLTreePath(treePath string) bool {
lower := strings.ToLower(treePath)
return strings.HasSuffix(lower, ".html") || strings.HasSuffix(lower, ".htm")
}
func handleFileViewRenderMarkup(ctx *context.Context, prefetchBuf []byte, utf8Reader io.Reader) bool { func handleFileViewRenderMarkup(ctx *context.Context, prefetchBuf []byte, utf8Reader io.Reader) bool {
if isHTMLTreePath(ctx.Repo.TreePath) {
return false // HTML uses dedicated preview handler, not the markup engine
}
rctx := renderhelper.NewRenderContextRepoFile(ctx, ctx.Repo.Repository, renderhelper.RepoFileOptions{ rctx := renderhelper.NewRenderContextRepoFile(ctx, ctx.Repo.Repository, renderhelper.RepoFileOptions{
CurrentRefPath: ctx.Repo.RefTypeNameSubURL(), CurrentRefPath: ctx.Repo.RefTypeNameSubURL(),
CurrentTreePath: path.Dir(ctx.Repo.TreePath), CurrentTreePath: path.Dir(ctx.Repo.TreePath),
@@ -92,6 +100,9 @@ func handleFileViewRenderMarkup(ctx *context.Context, prefetchBuf []byte, utf8Re
} }
func handleFileViewRenderSource(ctx *context.Context, attrs *attribute.Attributes, fInfo *fileInfo, utf8Reader io.Reader) bool { func handleFileViewRenderSource(ctx *context.Context, attrs *attribute.Attributes, fInfo *fileInfo, utf8Reader io.Reader) bool {
if isHTMLTreePath(ctx.Repo.TreePath) && ctx.FormString("display") != "source" {
return false // default HTML files to iframe preview, not line-numbered source
}
filename := ctx.Repo.TreePath filename := ctx.Repo.TreePath
if ctx.FormString("display") == "rendered" || !fInfo.st.IsRepresentableAsText() { if ctx.FormString("display") == "rendered" || !fInfo.st.IsRepresentableAsText() {
return false return false
@@ -154,8 +165,7 @@ func handleFileViewRenderImage(ctx *context.Context, fInfo *fileInfo, prefetchBu
} }
func handleFileViewRenderHTML(ctx *context.Context, fInfo *fileInfo) bool { func handleFileViewRenderHTML(ctx *context.Context, fInfo *fileInfo) bool {
lower := strings.ToLower(ctx.Repo.TreePath) if !isHTMLTreePath(ctx.Repo.TreePath) {
if !strings.HasSuffix(lower, ".html") && !strings.HasSuffix(lower, ".htm") {
return false return false
} }
// Allow toggling to source view // Allow toggling to source view
@@ -181,6 +191,10 @@ func prepareFileView(ctx *context.Context, entry *git.TreeEntry) {
ctx.Data["FileIsSymlink"] = entry.IsLink() ctx.Data["FileIsSymlink"] = entry.IsLink()
ctx.Data["FileTreePath"] = ctx.Repo.TreePath ctx.Data["FileTreePath"] = ctx.Repo.TreePath
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
if isHTMLTreePath(ctx.Repo.TreePath) {
ctx.Data["IsHTMLFile"] = true
ctx.Data["HasSourceRenderedToggle"] = true
}
if ctx.Repo.TreePath == ".editorconfig" { if ctx.Repo.TreePath == ".editorconfig" {
_, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit) _, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
+6
View File
@@ -35,6 +35,12 @@
{{end}} {{end}}
</div> </div>
<div class="file-header-right file-actions flex-text-block tw-flex-wrap"> <div class="file-header-right file-actions flex-text-block tw-flex-wrap">
{{if .IsHTMLFile}}
<div class="ui mini buttons tw-mr-1">
<a href="?display=rendered" class="ui mini basic button {{if not .IsDisplayingSource}}active{{end}}">{{ctx.Locale.Tr "repo.file_view_preview"}}</a>
<a href="?display=source" class="ui mini basic button {{if .IsDisplayingSource}}active{{end}}">{{ctx.Locale.Tr "repo.file_view_source"}}</a>
</div>
{{end}}
{{/* this componment is also controlled by frontend plugin renders */}} {{/* this componment is also controlled by frontend plugin renders */}}
<div class="ui compact icon buttons file-view-toggle-buttons {{Iif .HasSourceRenderedToggle "" "tw-hidden"}}"> <div class="ui compact icon buttons file-view-toggle-buttons {{Iif .HasSourceRenderedToggle "" "tw-hidden"}}">
{{if .IsRepresentableAsText}} {{if .IsRepresentableAsText}}