Browse Source

Fix plain readme didn't render correctly on repo home page (#2705)

* fix plain readme didn't render correctly on repo home page

* fix missing render

* remove unused template variables
Lunny Xiao 2 years ago
parent
commit
a75d5c72bb
2 changed files with 9 additions and 14 deletions
  1. 7 12
      routers/repo/view.go
  2. 2 2
      templates/repo/view_file.tmpl

+ 7 - 12
routers/repo/view.go

@@ -93,16 +93,12 @@ func renderDirectory(ctx *context.Context, treeLink string) {
93 93
 		if isTextFile {
94 94
 			d, _ := ioutil.ReadAll(dataRc)
95 95
 			buf = append(buf, d...)
96
-			newbuf := markup.Render(readmeFile.Name(), buf, treeLink, ctx.Repo.Repository.ComposeMetas())
97
-			if newbuf != nil {
98
-				ctx.Data["IsMarkup"] = true
96
+			ctx.Data["IsRenderedHTML"] = true
97
+			if markup.Type(readmeFile.Name()) != "" {
98
+				ctx.Data["FileContent"] = string(markup.Render(readmeFile.Name(), buf, treeLink, ctx.Repo.Repository.ComposeMetas()))
99 99
 			} else {
100
-				// FIXME This is the only way to show non-markdown files
101
-				// instead of a broken "View Raw" link
102
-				ctx.Data["IsMarkup"] = false
103
-				newbuf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
100
+				ctx.Data["FileContent"] = string(bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1))
104 101
 			}
105
-			ctx.Data["FileContent"] = string(newbuf)
106 102
 		}
107 103
 	}
108 104
 
@@ -195,14 +191,13 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
195 191
 		d, _ := ioutil.ReadAll(dataRc)
196 192
 		buf = append(buf, d...)
197 193
 
198
-		tp := markup.Type(blob.Name())
199
-		isSupportedMarkup := tp != ""
200
-		ctx.Data["IsMarkup"] = isSupportedMarkup
201 194
 		readmeExist := markup.IsReadmeFile(blob.Name())
202 195
 		ctx.Data["ReadmeExist"] = readmeExist
203
-		if isSupportedMarkup {
196
+		if markup.Type(blob.Name()) != "" {
197
+			ctx.Data["IsRenderedHTML"] = true
204 198
 			ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
205 199
 		} else if readmeExist {
200
+			ctx.Data["IsRenderedHTML"] = true
206 201
 			ctx.Data["FileContent"] = string(bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1))
207 202
 		} else {
208 203
 			// Building code view blocks with line number on server side.

+ 2 - 2
templates/repo/view_file.tmpl

@@ -36,8 +36,8 @@
36 36
 		{{end}}
37 37
 	</h4>
38 38
 	<div class="ui attached table segment">
39
-		<div class="file-view {{if .IsMarkup}}markdown{{else if .IsTextFile}}code-view{{end}} has-emoji">
40
-			{{if .IsMarkup}}
39
+		<div class="file-view {{if .IsRenderedHTML}}markdown{{else if .IsTextFile}}code-view{{end}} has-emoji">
40
+			{{if .IsRenderedHTML}}
41 41
 				{{if .FileContent}}{{.FileContent | Str2html}}{{end}}
42 42
 			{{else if not .IsTextFile}}
43 43
 				<div class="view-raw ui center">