diff --git a/options/locale/locale_en-US.json b/options/locale/locale_en-US.json index d6c733b50b..b06cd7e715 100644 --- a/options/locale/locale_en-US.json +++ b/options/locale/locale_en-US.json @@ -1197,7 +1197,8 @@ "repo.file.title": "%s at %s", "repo.file_raw": "Raw", "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_raw": "View Raw", "repo.file_permalink": "Permalink", diff --git a/routers/web/repo/view_file.go b/routers/web/repo/view_file.go index a066df4067..029d74de9a 100644 --- a/routers/web/repo/view_file.go +++ b/routers/web/repo/view_file.go @@ -57,7 +57,15 @@ func prepareFileViewLfsAttrs(ctx *context.Context) (*attribute.Attributes, bool) 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 { + 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{ CurrentRefPath: ctx.Repo.RefTypeNameSubURL(), 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 { + 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 if ctx.FormString("display") == "rendered" || !fInfo.st.IsRepresentableAsText() { return false @@ -154,8 +165,7 @@ func handleFileViewRenderImage(ctx *context.Context, fInfo *fileInfo, prefetchBu } func handleFileViewRenderHTML(ctx *context.Context, fInfo *fileInfo) bool { - lower := strings.ToLower(ctx.Repo.TreePath) - if !strings.HasSuffix(lower, ".html") && !strings.HasSuffix(lower, ".htm") { + if !isHTMLTreePath(ctx.Repo.TreePath) { return false } // Allow toggling to source view @@ -181,6 +191,10 @@ func prepareFileView(ctx *context.Context, entry *git.TreeEntry) { ctx.Data["FileIsSymlink"] = entry.IsLink() ctx.Data["FileTreePath"] = 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" { _, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 73f1c761e2..287983ae96 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -35,6 +35,12 @@ {{end}}
+ {{if .IsHTMLFile}} +
+ {{ctx.Locale.Tr "repo.file_view_preview"}} + {{ctx.Locale.Tr "repo.file_view_source"}} +
+ {{end}} {{/* this componment is also controlled by frontend plugin renders */}}
{{if .IsRepresentableAsText}}