diff --git a/go.mod b/go.mod index 79cc8b2..a50291f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 98d7877..c3981f9 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/page_repository.go b/page_repository.go index a62d98d..aad8417 100644 --- a/page_repository.go +++ b/page_repository.go @@ -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%%`, ``, 1))