all repos — elgit @ 95dec0f0564c158d2884faa24cac650043e8dc37

fork of legit: web frontend for git, written in go

routes: render markdown readmes in repo index (#11)

James Mills
commit

95dec0f0564c158d2884faa24cac650043e8dc37

parent

0547b04260fde4b3c31c41bb11489a2063a81faa

1 file changed, 17 insertions(+), 3 deletions(-)

changed files
M routes/routes.goroutes/routes.go
@@ -1,6 +1,7 @@
package routes import ( + "fmt" "html/template" "log" "net/http"
@@ -13,6 +14,8 @@ "git.icyphox.sh/legit/config"
"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 @@ log.Println(err)
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 } }