5f3d2e916bb9fecde21f5fbca986e403ca1d4f59

Author
TheEdgeOfRage <git@theedgeofrage.com>
Committer
TheEdgeOfRage <git@theedgeofrage.com>
Date

Message

Add markdown rendering for all md files

Diff

This diff is truncated to protect this page.

 1diff --git a/pkg/web/pages/browser.go b/pkg/web/pages/browser.go
 2index 64aaeb8c3f2af63ec7a8941b8bbfe9e7dfd912d2..64d8c6666227c88675959a9a5df293def42cfbfd 100644
 3--- a/pkg/web/pages/browser.go
 4+++ b/pkg/web/pages/browser.go
 5@@ -62,6 +62,7 @@ type treePageFile struct {
 6 	Name        string
 7 	Path        string
 8 	Size        int64
 9+	Markdown    template.HTML
10 	Source      template.HTML
11 	DownloadURL string
12 	Message     string
13@@ -236,10 +237,19 @@ func treeFilePage(entry *git.TreeEntry, repository, refName, treePath string) (*
14 		page.Message = "This binary file cannot be displayed. Download it instead."
15 		return page, nil
16 	}
17+	if isMarkdownFile(entry.Name()) {
18+		page.Markdown, err = RenderRepositoryMarkdown(repository, refName, treePath, content.Bytes)
19+		return page, err
20+	}
21 	page.Source, err = RenderSource(entry.Name(), content.Bytes)
22 	return page, err
23 }
24 
25+func isMarkdownFile(name string) bool {
26+	extension := strings.ToLower(path.Ext(name))
27+	return extension == ".md" || extension == ".markdown"
28+}
29+
30 func rawFile(w http.ResponseWriter, r *http.Request) {
31 	_, gitRepo, ok := openPublicRepository(w, r)
32 	if !ok {
33diff --git a/pkg/web/pages/templates/repository.gohtml b/pkg/web/pages/templates/repository.gohtml
34index 2b0711be67dac4db3962a8872b370014dc112e66..97c4bbbac34d12ae49421c6ff0724399fb0538ed 100644
35--- a/pkg/web/pages/templates/repository.gohtml
36+++ b/pkg/web/pages/templates/repository.gohtml
37@@ -52,7 +52,7 @@
38       <article>
39         <h1>{{ .File.Name }}</h1>
40         <p>{{ .File.Size }} bytes</p>
41-        {{ if .File.Message }}<p>{{ .File.Message }}{{ if .File.DownloadURL }} <a href="{{ .File.DownloadURL }}">Download {{ .File.Name }}</a>{{ end }}</p>{{ else }}<pre class="source"><code>{{ .File.Source }}</code></pre>{{ end }}
42diff --git a/pkg/web/repository_browser_test.go b/pkg/web/repository_browser_test.go
43index a5d512e1171d3d40991cdd2532ea664272b5d8f1..fa62121211f54e00b24a64b59d8d4b34addaf5da 100644
44--- a/pkg/web/repository_browser_test.go
45+++ b/pkg/web/repository_browser_test.go
46@@ -65,6 +65,7 @@ func TestRepositoryBrowserOverviewAndTreeRoutes(t *testing.T) {
47 	commitHomepageFile(t, cfg.DataPath, "team/nested", "dir/a.txt", "a")
48 	commitHomepageFile(t, cfg.DataPath, "team/nested", "dir/nested/b.txt", "b")
49 	commitHomepageFile(t, cfg.DataPath, "team/nested", "source.go", "<script>alert(1)</script>")
50+	commitHomepageFile(t, cfg.DataPath, "team/nested", "docs/guide.md", "# Guide\n\n[Source](../source.go)\n\n![Icon](../images/icon.png)\n\n<script>alert(1)</script>\n")
51 	commitHomepageFile(t, cfg.DataPath, "team/nested", "images/icon.png", string([]byte{'\x89', 'P', 'N', 'G'}))
52 	commitHomepageFile(t, cfg.DataPath, "team/nested", "unsafe.svg", "<svg></svg>")
53 	commitHomepageFile(t, cfg.DataPath, "team/nested", "unsafe.html", "<script></script>")
54@@ -118,6 +119,10 @@ func TestRepositoryBrowserOverviewAndTreeRoutes(t *testing.T) {
55 	if source.Code != http.StatusOK || strings.Contains(source.Body.String(), "<script>alert") || !strings.Contains(source.Body.String(), "&lt;") {
56 		t.Errorf("source response = %d: %s", source.Code, source.Body.String())
57 	}
58+	markdown := request("/team/nested/@/tree?ref=refs%2Fheads%2Fmain&path=docs%2Fguide.md")