markdown: fall back to internal renderer if gitea's is unavailable

This commit is contained in:
mappu 2024-03-19 18:52:16 +13:00
parent 1adacc0d49
commit fda3899a23
3 changed files with 25 additions and 2 deletions

2
go.mod
View File

@ -6,3 +6,5 @@ require (
github.com/BurntSushi/toml v0.3.1
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
)
require github.com/yuin/goldmark v1.7.0 // indirect

2
go.sum
View File

@ -1,4 +1,6 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

View File

@ -8,6 +8,8 @@ import (
"net/http"
"net/url"
"strings"
"github.com/yuin/goldmark"
)
func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoName string) {
@ -58,8 +60,25 @@ func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoNa
readmeHtml, err := this.gitea.RenderMarkdown(ctx, repoName, strings.Join(lines, "\n"))
if err != nil {
log.Printf("%s %s: %s", r.Method, r.URL.Path, fmt.Errorf("rendering markdown: %w", err))
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
return
// Failed to use Gitea's markdown renderer
// HTTP 401 (Unauthorized) started happening with this API sometime
// between Gitea 1.18 -> 1.21, and no authorization token seems
// sufficient to make it work again
// Use our own one instead as a fallback
buff := bytes.Buffer{}
err = goldmark.Convert(readme, &buff)
if err != nil {
// Built-in markdown renderer didn't work either
log.Printf("%s %s: %s", r.Method, r.URL.Path, fmt.Errorf("rendering markdown: %w", err))
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
return
}
// OK
readmeHtml = buff.Bytes()
err = nil
}
readmeHtml = []byte(strings.Replace(string(readmeHtml), `%%REPLACEME__BADGE%%`, `<img src="/static/build_success_brightgreen.svg" style="width:90px;height:20px;">`, 1))