summaryrefslogtreecommitdiffstats
path: root/routes/routes.go
diff options
context:
space:
mode:
authorLibravatar James Mills <1290234+prologic@users.noreply.github.com>2022-12-26 16:20:08 +1000
committerLibravatar GitHub <noreply@github.com>2022-12-26 11:50:08 +0530
commit95dec0f0564c158d2884faa24cac650043e8dc37 (patch)
treea3c6aed24a921142265090513b9bb9fd9b776b02 /routes/routes.go
parent0547b04260fde4b3c31c41bb11489a2063a81faa (diff)
downloadlegit-95dec0f0564c158d2884faa24cac650043e8dc37.tar.gz
legit-95dec0f0564c158d2884faa24cac650043e8dc37.tar.bz2
legit-95dec0f0564c158d2884faa24cac650043e8dc37.zip
routes: render markdown readmes in repo index (#11)
Diffstat (limited to 'routes/routes.go')
-rw-r--r--routes/routes.go20
1 files changed, 17 insertions, 3 deletions
diff --git a/routes/routes.go b/routes/routes.go
index 30f101a..1034f2f 100644
--- a/routes/routes.go
+++ b/routes/routes.go
@@ -1,6 +1,7 @@
package routes
import (
+ "fmt"
"html/template"
"log"
"net/http"
@@ -13,6 +14,8 @@ import (
"git.icyphox.sh/legit/git"
"github.com/alexedwards/flow"
"github.com/dustin/go-humanize"
+ "github.com/microcosm-cc/bluemonday"
+ "github.com/russross/blackfriday/v2"
)
type deps struct {
@@ -101,10 +104,21 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
return
}
- var readmeContent string
+ var readmeContent template.HTML
for _, readme := range d.c.Repo.Readme {
- readmeContent, _ = gr.FileContent(readme)
- if readmeContent != "" {
+ ext := filepath.Ext(readme)
+ content, _ := gr.FileContent(readme)
+ if len(content) > 0 {
+ switch ext {
+ case ".md":
+ unsafe := blackfriday.Run([]byte(content), blackfriday.WithExtensions(blackfriday.CommonExtensions))
+ html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
+ readmeContent = template.HTML(html)
+ default:
+ readmeContent = template.HTML(
+ fmt.Sprintf(`<pre>%s</pre>`, content),
+ )
+ }
break
}
}